forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgas_filler.rs
More file actions
58 lines (49 loc) · 2.3 KB
/
Copy pathgas_filler.rs
File metadata and controls
58 lines (49 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! Example of using the `GasFiller` in the provider.
use alloy::{
consensus::Transaction,
network::TransactionBuilder,
primitives::{address, U256},
providers::{Provider, ProviderBuilder},
rpc::types::request::TransactionRequest,
};
use eyre::Result;
#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let provider = ProviderBuilder::new()
// You can disable the recommended fillers by calling the `disable_recommended_fillers()`
// and pick the fillers of your choice.
.disable_recommended_fillers()
// Add the `GasFiller` to the provider.
// It is generally recommended to use the recommended fillers which includes the GasFiller,
// enabled by building the provider using ProviderBuilder::new().
.with_gas_estimation()
.connect_anvil_with_wallet();
// Build an EIP-1559 type transaction to send 100 wei to Vitalik.
let vitalik = address!("d8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
let tx = TransactionRequest::default()
.with_to(vitalik)
.with_value(U256::from(100))
// Notice that without the `NonceFiller`, you need to set `nonce` field.
.with_nonce(0)
// Notice that without the `ChainIdFiller`, you need to set the `chain_id` field.
.with_chain_id(provider.get_chain_id().await?);
// Send the transaction, the nonce (0) is automatically managed by the provider.
let builder = provider.send_transaction(tx.clone()).await?;
let node_hash = *builder.tx_hash();
let pending_tx =
provider.get_transaction_by_hash(node_hash).await?.expect("Pending transaction not found");
assert_eq!(pending_tx.nonce(), 0);
println!("Transaction sent with nonce: {}", pending_tx.nonce());
// Update the nonce and send the transaction again.
let tx = tx.with_nonce(1);
// Send the transaction, the nonce (1) is automatically managed by the provider.
let builder = provider.send_transaction(tx).await?;
let node_hash = *builder.tx_hash();
let pending_tx =
provider.get_transaction_by_hash(node_hash).await?.expect("Pending transaction not found");
assert_eq!(pending_tx.nonce(), 1);
println!("Transaction sent with nonce: {}", pending_tx.nonce());
Ok(())
}