11//! Example of sending a EIP-1559 transaction with access list.
2+
23use alloy:: {
34 node_bindings:: Anvil ,
45 providers:: { Provider , ProviderBuilder } ,
@@ -7,6 +8,7 @@ use alloy::{
78} ;
89use eyre:: Result ;
910
11+ // Codegen from artifact.
1012sol ! (
1113 #[ allow( missing_docs) ]
1214 #[ sol( rpc) ]
@@ -16,44 +18,49 @@ sol!(
1618
1719#[ tokio:: main]
1820async fn main ( ) -> Result < ( ) > {
19- // Spin up anvil node.
20-
21+ // Spin up a forked Anvil node.
22+ // Ensure `anvil` is available in $PATH.
2123 let anvil = Anvil :: new ( ) . try_spawn ( ) ?;
2224
2325 // Create a provider.
2426 let provider =
2527 ProviderBuilder :: new ( ) . with_recommended_fillers ( ) . on_builtin ( & anvil. endpoint ( ) ) . await ?;
2628
29+ // Create two users, Alice and Bob.
2730 let alice = anvil. addresses ( ) [ 0 ] ;
2831 let bob = anvil. addresses ( ) [ 1 ] ;
2932
30- let contract_addr = SimpleStorage :: deploy_builder ( provider. clone ( ) , "foo" . to_string ( ) )
33+ // Deploy the `SimpleStorage` contract.
34+ let contract_address = SimpleStorage :: deploy_builder ( provider. clone ( ) , "initial" . to_string ( ) )
3135 . from ( alice)
3236 . deploy ( )
3337 . await ?;
38+ let contract = SimpleStorage :: new ( contract_address, provider. clone ( ) ) ;
3439
35- let contract = SimpleStorage :: new ( contract_addr, provider. clone ( ) ) ;
36-
40+ // Build a transaction to set the values.
3741 let set_value_call = contract. setValues ( "hello" . to_string ( ) , "world" . to_string ( ) ) ;
3842 let calldata = set_value_call. calldata ( ) . to_owned ( ) ;
3943
4044 let eip1559_fees = provider. estimate_eip1559_fees ( None ) . await ?;
4145 let tx = TransactionRequest :: default ( )
4246 . from ( bob)
43- . to ( contract_addr )
47+ . to ( contract_address )
4448 . input ( calldata. into ( ) )
4549 . max_fee_per_gas ( eip1559_fees. max_fee_per_gas )
4650 . max_priority_fee_per_gas ( eip1559_fees. max_priority_fee_per_gas ) ;
4751
52+ // Create an access list for the transaction.
4853 let access_list_with_gas_used = provider. create_access_list ( & tx, BlockId :: latest ( ) ) . await ?;
4954
50- let tx_with_access_list = tx. clone ( ) . access_list ( access_list_with_gas_used. access_list ) ;
55+ // Add the access list to the transaction.
56+ let tx_with_access_list = tx. access_list ( access_list_with_gas_used. access_list ) ;
5157
52- let pending_tx = provider. send_transaction ( tx_with_access_list) . await ?;
53-
54- let _receipt = pending_tx. get_receipt ( ) . await ?;
58+ // Send the transaction with the access list.
59+ provider. send_transaction ( tx_with_access_list) . await ?. get_receipt ( ) . await ?;
5560
61+ // Check the value of the contract.
5662 let value = contract. getValue ( ) . call ( ) . await ?;
63+
5764 assert_eq ! ( value. _0, "hello" . to_string( ) ) ;
5865
5966 Ok ( ( ) )
0 commit comments