-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.rs
77 lines (66 loc) · 2.44 KB
/
example.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use ethers::{
contract::abigen,
providers::{Http, Provider},
signers::Signer,
types::{Address, U256},
};
use ethers_userop::{
consts::{GETH_CHAIN_ID, GETH_ENTRY_POINT_ADDRESS, GETH_WETH_ADDRESS, SALT, SEED_PHRASE},
UserOpMiddleware,
};
use silius_primitives::Wallet as UoWallet;
use std::thread;
use std::time::Duration;
abigen!(WETH, "src/abi/WETH.json",);
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::Builder::new()
.filter_level(log::LevelFilter::Info)
.init();
// Setup the environment
let eth_client_address = "http://localhost:8545".to_string();
let seed = SEED_PHRASE.to_string();
let provider = Provider::<Http>::try_from(eth_client_address.to_string())?;
let rpc_address = format!("http://{}", "127.0.0.1:3000");
let chain_id = GETH_CHAIN_ID; // Geth testnet
let uo_wallet = UoWallet::from_phrase(seed.as_str(), &U256::from(chain_id), false).unwrap();
let signer_wallet_address = uo_wallet.clone().signer.address();
let wallet_name = "simple-account-test";
// Instantiate a UserOpMiddleware
let mut uo_middleware: UserOpMiddleware<Provider<Http>> = UserOpMiddleware::new(
provider,
GETH_ENTRY_POINT_ADDRESS.parse::<Address>().unwrap(),
rpc_address,
uo_wallet.clone(),
);
// Deploy a smart contract wallet
let (uo_hash, scw_address) = uo_middleware
.deploy_scw(wallet_name.into(), 10u64, SALT)
.await?;
println!(
"Smart contract wallet deployed at {:x}
: {:?}",
scw_address, uo_hash
);
// Force to wait for the smart contract wallet to be deployed on the next block
thread::sleep(Duration::from_secs(12));
// Send Eth
let uo_hash = uo_middleware
.send_eth(scw_address, wallet_name, signer_wallet_address, 1u64)
.await?;
println!("Sent ETH to {}: {:?}", signer_wallet_address, uo_hash);
// Force to wait for the smart contract wallet to be deployed on the next block
thread::sleep(Duration::from_secs(12));
// Calling Weth contract to deposit Eth
let weth = WETH::new(
GETH_WETH_ADDRESS.parse::<Address>()?,
uo_middleware.clone().into(),
);
let mut deposit = weth.deposit().tx;
deposit.set_value(U256::from(100u64));
let uo_hash = uo_middleware
.call(wallet_name, scw_address, deposit)
.await?;
println!("Deposited ETH into WETH contract: {:?}", uo_hash);
Ok(())
}