Sitelet https://github.com/SharkHand3/examples/commit/e070b08a07d97bdbbd11155dbabecc9d98b33988
Skip to content

Commit e070b08

Browse files
authored
Prefer watch instead of get_receipt if only a transaction hash is required (alloy-rs#97)
* prefer lighter 'watch' over 'get_receipt' * improve uniformity * clean up
1 parent c793470 commit e070b08

12 files changed

Lines changed: 48 additions & 44 deletions

File tree

‎examples/anvil/examples/deploy_contract_anvil.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ async fn main() -> Result<()> {
4949

5050
// Set the number to 42.
5151
let builder = contract.setNumber(U256::from(42));
52-
let receipt = builder.send().await?.get_receipt().await?;
52+
let tx_hash = builder.send().await?.watch().await?;
5353

54-
println!("Set number to 42: {}", receipt.transaction_hash);
54+
println!("Set number to 42: {}", tx_hash);
5555

5656
// Increment the number to 43.
5757
let builder = contract.increment();
58-
let receipt = builder.send().await?.get_receipt().await?;
58+
let tx_hash = builder.send().await?.watch().await?;
5959

60-
println!("Incremented number: {}", receipt.transaction_hash);
60+
println!("Incremented number: {}", tx_hash);
6161

6262
// Retrieve the number, which should be 43.
6363
let Counter::numberReturn { _0 } = contract.number().call().await?;

‎examples/contracts/examples/deploy_from_artifact.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ async fn main() -> Result<()> {
3939
println!("Deployed contract at address: {}", contract.address());
4040

4141
let builder = contract.setNumber(U256::from(42));
42-
let receipt = builder.send().await?.get_receipt().await?;
42+
let tx_hash = builder.send().await?.watch().await?;
4343

44-
println!("Set number to 42: {}", receipt.transaction_hash);
44+
println!("Set number to 42: {}", tx_hash);
4545

4646
// Increment the number to 43.
4747
let builder = contract.increment();
48-
let receipt = builder.send().await?.get_receipt().await?;
48+
let tx_hash = builder.send().await?.watch().await?;
4949

50-
println!("Incremented number: {}", receipt.transaction_hash);
50+
println!("Incremented number: {}", tx_hash);
5151

5252
// Retrieve the number, which should be 43.
5353
let Counter::numberReturn { _0 } = contract.number().call().await?;

‎examples/contracts/examples/deploy_from_bytecode.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ async fn main() -> Result<()> {
5656
let data: Vec<u8> = FromHex::from_hex(contract_bytecode)?;
5757
let tx = TransactionRequest::default().with_deploy_code(data);
5858

59-
let deploy_result = provider.send_transaction(tx).await?.get_receipt().await?;
59+
// Deploy the contract.
60+
let receipt = provider.send_transaction(tx).await?.get_receipt().await?;
6061

61-
let contract_address =
62-
deploy_result.contract_address().expect("Failed to get contract address");
62+
let contract_address = receipt.contract_address().expect("Failed to get contract address");
6363
let contract = Counter::new(contract_address, &provider);
6464
println!("Deployed contract at address: {}", contract.address());
6565

6666
// Set number
6767
let builder = contract.setNumber(U256::from(42));
68-
let receipt = builder.send().await?.get_receipt().await?;
68+
let tx_hash = builder.send().await?.watch().await?;
6969

70-
println!("Set number to 42: {}", receipt.transaction_hash);
70+
println!("Set number to 42: {}", tx_hash);
7171

7272
// Increment the number to 43.
7373
let builder = contract.increment();
74-
let receipt = builder.send().await?.get_receipt().await?;
74+
let tx_hash = builder.send().await?.watch().await?;
7575

76-
println!("Incremented number: {}", receipt.transaction_hash);
76+
println!("Incremented number: {}", tx_hash);
7777

7878
// Retrieve the number, which should be 43.
7979
let Counter::numberReturn { _0 } = contract.number().call().await?;

‎examples/contracts/examples/deploy_from_contract.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ async fn main() -> Result<()> {
5151
println!("Deployed contract at address: {}", contract.address());
5252

5353
let builder = contract.setNumber(U256::from(42));
54-
let receipt = builder.send().await?.get_receipt().await?;
54+
let tx_hash = builder.send().await?.watch().await?;
5555

56-
println!("Set number to 42: {}", receipt.transaction_hash);
56+
println!("Set number to 42: {}", tx_hash);
5757

5858
// Increment the number to 43.
5959
let builder = contract.increment();
60-
let receipt = builder.send().await?.get_receipt().await?;
60+
let tx_hash = builder.send().await?.watch().await?;
6161

62-
println!("Incremented number: {}", receipt.transaction_hash);
62+
println!("Incremented number: {}", tx_hash);
6363

6464
// Retrieve the number, which should be 43.
6565
let Counter::numberReturn { _0 } = contract.number().call().await?;

‎examples/fillers/examples/nonce_filler.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ async fn main() -> Result<()> {
5757
// Send the transaction, the nonce (0) is automatically managed by the provider.
5858
let builder = provider.send_transaction(tx.clone()).await?;
5959
let node_hash = *builder.tx_hash();
60-
let pending_tx = provider.get_transaction_by_hash(node_hash).await?.unwrap();
60+
let pending_tx =
61+
provider.get_transaction_by_hash(node_hash).await?.expect("Transaction not found");
6162
assert_eq!(pending_tx.nonce, 0);
6263

6364
println!("Transaction sent with nonce: {}", pending_tx.nonce);
6465

6566
// Send the transaction, the nonce (1) is automatically managed by the provider.
6667
let builder = provider.send_transaction(tx).await?;
6768
let node_hash = *builder.tx_hash();
68-
let pending_tx = provider.get_transaction_by_hash(node_hash).await?.unwrap();
69+
let pending_tx =
70+
provider.get_transaction_by_hash(node_hash).await?.expect("Transaction not found");
6971
assert_eq!(pending_tx.nonce, 1);
7072

7173
println!("Transaction sent with nonce: {}", pending_tx.nonce);

‎examples/transactions/examples/transfer_erc20.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ async fn main() -> Result<()> {
3232
let alice_before_balance = contract.balanceOf(alice).call().await?._0;
3333
let bob_before_balance = contract.balanceOf(bob).call().await?._0;
3434

35-
// Transfer and wait for the receipt.
35+
// Transfer and wait for inclusion.
3636
let amount = U256::from(100);
37-
let receipt = contract.transfer(bob, amount).send().await?.get_receipt().await?;
37+
let tx_hash = contract.transfer(bob, amount).send().await?.watch().await?;
3838

39-
println!("Send transaction: {}", receipt.transaction_hash);
39+
println!("Send transaction: {}", tx_hash);
4040

4141
// Register the balances of Alice and Bob after the transfer.
4242
let alice_after_balance = contract.balanceOf(alice).call().await?._0;

‎examples/transactions/examples/with_access_list.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ async fn main() -> Result<()> {
5656
let tx_with_access_list = tx.access_list(access_list_with_gas_used.access_list);
5757

5858
// Send the transaction with the access list.
59-
provider.send_transaction(tx_with_access_list).await?.get_receipt().await?;
59+
let tx_hash = provider.send_transaction(tx_with_access_list).await?.watch().await?;
60+
61+
println!("Transaction hash: {}", tx_hash);
6062

6163
// Check the value of the contract.
6264
let value = contract.getValue().call().await?;

‎examples/wallets/examples/keystore_signer.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ async fn main() -> Result<()> {
4040
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
4141
.with_value(U256::from(100));
4242

43-
// Send the transaction and wait for the receipt.
44-
let receipt = provider.send_transaction(tx).await?.get_receipt().await?;
43+
// Send the transaction and wait for inclusion.
44+
let tx_hash = provider.send_transaction(tx).await?.watch().await?;
4545

46-
println!("Send transaction: {}", receipt.transaction_hash);
46+
println!("Send transaction: {}", tx_hash);
4747

4848
Ok(())
4949
}

‎examples/wallets/examples/ledger_signer.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ async fn main() -> Result<()> {
3333
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
3434
.with_value(U256::from(100));
3535

36-
// Send the transaction and wait for the receipt.
37-
let receipt =
38-
provider.send_transaction(tx).await?.with_required_confirmations(3).get_receipt().await?;
36+
// Send the transaction and wait for inclusion with 3 confirmations.
37+
let tx_hash =
38+
provider.send_transaction(tx).await?.with_required_confirmations(3).watch().await?;
3939

40-
println!("Send transaction: {}", receipt.transaction_hash);
40+
println!("Send transaction: {}", tx_hash);
4141

4242
Ok(())
4343
}

‎examples/wallets/examples/private_key_signer.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async fn main() -> Result<()> {
2020
let signer: LocalWallet = anvil.keys()[0].clone().into();
2121
// [RISK WARNING! Writing a private key in the code file is insecure behavior.]
2222
// The following code is for testing only. Set up signer from private key, be aware of danger.
23-
// let signer: LocalWallet = "<THE_PRIVATE_KEY>".parse().unwrap();
23+
// let signer: LocalWallet = "<THE_PRIVATE_KEY>".parse().expect("Failed to parse private key");
2424
let alice = signer.address();
2525

2626
// Create a provider with the signer.
@@ -36,10 +36,10 @@ async fn main() -> Result<()> {
3636
.with_to(address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045"))
3737
.with_value(U256::from(100));
3838

39-
// Send the transaction and wait for the receipt.
40-
let receipt = provider.send_transaction(tx).await?.get_receipt().await?;
39+
// Send the transaction and wait for inclusion.
40+
let tx_hash = provider.send_transaction(tx).await?.watch().await?;
4141

42-
println!("Send transaction: {}", receipt.transaction_hash);
42+
println!("Send transaction: {}", tx_hash);
4343

4444
Ok(())
4545
}

0 commit comments

Comments
 (0)