11//! Example of using the `GasFiller` in the provider.
22
33use alloy:: {
4- network:: { EthereumWallet , TransactionBuilder } ,
5- node_bindings:: Anvil ,
4+ network:: TransactionBuilder ,
65 primitives:: { address, U256 } ,
76 providers:: { Provider , ProviderBuilder } ,
87 rpc:: types:: request:: TransactionRequest ,
9- signers:: local:: PrivateKeySigner ,
108} ;
119use eyre:: Result ;
1210
1311#[ tokio:: main]
1412async fn main ( ) -> Result < ( ) > {
1513 // Spin up a local Anvil node.
1614 // Ensure `anvil` is available in $PATH.
17- let anvil = Anvil :: new ( ) . try_spawn ( ) ?;
18-
19- // Set up signer from the first default Anvil account (Alice).
20- let signer: PrivateKeySigner = anvil. keys ( ) [ 0 ] . clone ( ) . into ( ) ;
21- let wallet = EthereumWallet :: from ( signer) ;
22-
23- // Create a provider with the wallet.
24- let rpc_url = anvil. endpoint ( ) . parse ( ) ?;
2515 let provider = ProviderBuilder :: new ( )
2616 // Add the `GasFiller` to the provider.
2717 // It is generally recommended to use the `.with_recommended_fillers()` method, which
2818 // includes the `GasFiller`.
2919 . with_gas_estimation ( )
30- . wallet ( wallet)
31- . on_http ( rpc_url) ;
20+ . on_anvil_with_wallet ( ) ;
3221
3322 // Build an EIP-1559 type transaction to send 100 wei to Vitalik.
3423 let vitalik = address ! ( "d8dA6BF26964aF9D7eEd9e03E53415D37aA96045" ) ;
@@ -38,7 +27,7 @@ async fn main() -> Result<()> {
3827 // Notice that without the `NonceFiller`, you need to set `nonce` field.
3928 . with_nonce ( 0 )
4029 // Notice that without the `ChainIdFiller`, you need to set the `chain_id` field.
41- . with_chain_id ( anvil . chain_id ( ) ) ;
30+ . with_chain_id ( provider . get_chain_id ( ) . await ? ) ;
4231
4332 // Send the transaction, the nonce (0) is automatically managed by the provider.
4433 let builder = provider. send_transaction ( tx. clone ( ) ) . await ?;
0 commit comments