forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_access_list.rs
59 lines (46 loc) · 1.9 KB
/
with_access_list.rs
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
59
//! Example of sending a EIP-1559 transaction with access list.
use alloy::{
providers::{Provider, ProviderBuilder},
rpc::types::TransactionRequest,
sol,
};
use eyre::Result;
// Codegen from artifact.
sol!(
#[allow(missing_docs)]
#[sol(rpc)]
SimpleStorage,
"examples/artifacts/SimpleStorage.json"
);
#[tokio::main]
async fn main() -> Result<()> {
// Spin up a local Anvil node.
// Ensure `anvil` is available in $PATH.
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil();
// Create two users, Alice and Bob.
let accounts = provider.get_accounts().await?;
let alice = accounts[0];
let bob = accounts[1];
// Deploy the `SimpleStorage` contract.
let contract_address = SimpleStorage::deploy_builder(provider.clone(), "initial".to_string())
.from(alice)
.deploy()
.await?;
let contract = SimpleStorage::new(contract_address, provider.clone());
// Build a transaction to set the values of the contract.
// The `from` field is automatically filled to the first signer's address (Alice).
let set_value_call = contract.setValues("hello".to_string(), "world".to_string());
let calldata = set_value_call.calldata().to_owned();
let tx = TransactionRequest::default().from(bob).to(contract_address).input(calldata.into());
// Create an access list for the transaction.
let access_list_with_gas_used = provider.create_access_list(&tx).await?;
// Add the access list to the transaction.
let tx_with_access_list = tx.access_list(access_list_with_gas_used.access_list);
// Send the transaction with the access list.
let tx_hash = provider.send_transaction(tx_with_access_list).await?.watch().await?;
println!("Transaction hash: {tx_hash}");
// Check the value of the contract.
let value = contract.getValue().call().await?._0;
assert_eq!(value, "hello");
Ok(())
}