Skip to content

Commit 76fc1bc

Browse files
authored
refactor(cast): deduplicate browser wallet receipt handling in cast send (#13747)
Extract `CastTxSender::print_tx_result()` to share the "print hash or wait for receipt" logic between the normal send flow and the browser wallet flow. Flatten the browser case into its own `else if` branch so `--browser` takes priority over `--unlocked` instead of being silently ignored.
1 parent ce69566 commit 76fc1bc

2 files changed

Lines changed: 32 additions & 34 deletions

File tree

crates/cast/src/cmd/send.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -202,34 +202,18 @@ impl SendTxArgs {
202202
)
203203
.await
204204
// Case 2:
205+
// Browser wallet signs and sends the transaction in one step.
206+
} else if let Some(browser) = browser {
207+
let (tx_request, _) = builder.build(browser.address()).await?;
208+
let tx_hash = browser.send_transaction_via_browser(tx_request).await?;
209+
210+
let cast = CastTxSender::new(&provider);
211+
cast.print_tx_result(tx_hash, send_tx.cast_async, send_tx.confirmations, timeout).await
212+
// Case 3:
205213
// An option to use a local signer was provided.
206214
// If we cannot successfully instantiate a local signer, then we will assume we don't have
207215
// enough information to sign and we must bail.
208216
} else {
209-
// Browser wallet work differently as it sign and send the transaction in one step.
210-
if let Some(browser) = browser {
211-
let (tx_request, _) = builder.build(browser.address()).await?;
212-
let tx_hash = browser.send_transaction_via_browser(tx_request).await?;
213-
214-
if send_tx.cast_async {
215-
sh_println!("{tx_hash:#x}")?;
216-
} else {
217-
let receipt = CastTxSender::new(&provider)
218-
.receipt(
219-
format!("{tx_hash:#x}"),
220-
None,
221-
send_tx.confirmations,
222-
Some(timeout),
223-
false,
224-
)
225-
.await?;
226-
sh_println!("{receipt}")?;
227-
}
228-
229-
return Ok(());
230-
}
231-
232-
// Retrieve the signer, and bail if it can't be constructed.
233217
let signer = send_tx.eth.wallet.signer().await?;
234218
let from = signer.address();
235219

@@ -275,15 +259,8 @@ where
275259
sh_println!("{receipt}")?;
276260
} else {
277261
let pending_tx = cast.send(tx).await?;
278-
let tx_hash = pending_tx.inner().tx_hash();
279-
280-
if cast_async {
281-
sh_println!("{tx_hash:#x}")?;
282-
} else {
283-
let receipt =
284-
cast.receipt(format!("{tx_hash:#x}"), None, confs, Some(timeout), false).await?;
285-
sh_println!("{receipt}")?;
286-
}
262+
let tx_hash = *pending_tx.inner().tx_hash();
263+
cast.print_tx_result(tx_hash, cast_async, confs, timeout).await?;
287264
}
288265

289266
Ok(())

crates/cast/src/tx.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use alloy_dyn_abi::ErrorExt;
44
use alloy_ens::NameOrAddress;
55
use alloy_json_abi::Function;
66
use alloy_network::{Network, TransactionBuilder};
7-
use alloy_primitives::{Address, Bytes, TxHash, TxKind, U256, hex};
7+
use alloy_primitives::{Address, B256, Bytes, TxHash, TxKind, U256, hex};
88
use alloy_provider::{PendingTransactionBuilder, Provider};
99
use alloy_rpc_types::{AccessList, Authorization, TransactionInputKind};
1010
use alloy_signer::Signer;
@@ -236,6 +236,27 @@ where
236236
Ok(res)
237237
}
238238

239+
/// Prints the transaction hash (if async) or waits for the receipt and prints it.
240+
///
241+
/// This is the shared "output" path used by both the normal send flow and the browser wallet
242+
/// flow (which sends the transaction out-of-band and only has a tx hash).
243+
pub async fn print_tx_result(
244+
&self,
245+
tx_hash: B256,
246+
cast_async: bool,
247+
confs: u64,
248+
timeout: u64,
249+
) -> Result<()> {
250+
if cast_async {
251+
sh_println!("{tx_hash:#x}")?;
252+
} else {
253+
let receipt =
254+
self.receipt(format!("{tx_hash:#x}"), None, confs, Some(timeout), false).await?;
255+
sh_println!("{receipt}")?;
256+
}
257+
Ok(())
258+
}
259+
239260
/// # Example
240261
///
241262
/// ```

0 commit comments

Comments
 (0)