|
1 | 1 | use alloy::{ |
2 | 2 | eips::eip7702::Authorization, |
3 | | - network::{TransactionBuilder, TransactionBuilder7702, TxSigner}, |
4 | | - node_bindings::Anvil, |
5 | | - primitives::{Address, U256, address}, |
| 3 | + network::{TransactionBuilder, TransactionBuilder7702}, |
| 4 | + primitives::{Address, Bytes, TxKind, U256, address}, |
6 | 5 | providers::{Provider, ProviderBuilder}, |
7 | 6 | rpc::types::TransactionRequest, |
8 | | - signers::{Signer, SignerSync, local::PrivateKeySigner}, |
| 7 | + signers::{SignerSync, local::PrivateKeySigner}, |
9 | 8 | sol, |
10 | 9 | }; |
11 | | -use eyre::{ErrReport, Result}; |
| 10 | +use bytes::{BufMut, BytesMut}; |
| 11 | +use eyre::Result; |
12 | 12 | use std::env; |
13 | 13 | use std::str::FromStr; |
14 | 14 |
|
15 | 15 | sol!( |
| 16 | + #[allow(missing_docs)] |
16 | 17 | #[sol(rpc)] |
17 | | - "../src/EOAMultisend.sol" |
| 18 | + EOAMultisend, |
| 19 | + "../out/EOAMultisend.sol/EOAMultisend.json" |
18 | 20 | ); |
19 | 21 |
|
| 22 | +const EOA_MULTISEND_ADDRESS: Address = address!("0xDa51eBfBb740D2183e91FAf762666B169A1A9a62"); |
| 23 | + |
20 | 24 | async fn eoa_multisend(pk: PrivateKeySigner, calls: Vec<TransactionRequest>) -> Result<()> { |
21 | | - let sepolia = "https://sepolia.etherscan.io".parse()?; |
| 25 | + let rpc_url = "https://sepolia.drpc.org".parse()?; |
22 | 26 | let provider = ProviderBuilder::new() |
23 | 27 | .wallet(pk.clone()) |
24 | | - .connect_http(sepolia); |
| 28 | + .connect_http(rpc_url); |
| 29 | + |
| 30 | + let contract = EOAMultisend::new(EOA_MULTISEND_ADDRESS, provider.clone()); |
| 31 | + |
| 32 | + let authorization = Authorization { |
| 33 | + chain_id: U256::from(11155111), |
| 34 | + address: *contract.address(), |
| 35 | + nonce: provider.get_transaction_count(pk.address()).await?, |
| 36 | + }; |
| 37 | + |
| 38 | + let signature = pk.sign_hash_sync(&authorization.signature_hash())?; |
| 39 | + let signed_authorization = authorization.into_signed(signature); |
| 40 | + |
| 41 | + let batched = pack_multisend(&calls)?; |
| 42 | + |
| 43 | + let call = contract.execute_0(batched); |
| 44 | + let execute_calldata = call.calldata().to_owned(); |
| 45 | + |
| 46 | + let tx = TransactionRequest::default() |
| 47 | + .with_to(pk.address()) |
| 48 | + .with_authorization_list(vec![signed_authorization]) |
| 49 | + .with_input(execute_calldata); |
| 50 | + |
| 51 | + // Send the transaction and wait for the broadcast. |
| 52 | + let pending_tx = provider.send_transaction(tx).await?; |
| 53 | + |
| 54 | + println!("Pending transaction... {}", pending_tx.tx_hash()); |
25 | 55 |
|
26 | | - // let contract = EOAMultisend::deploy(&provider).await?; |
| 56 | + let receipt = pending_tx.get_receipt().await?; |
| 57 | + |
| 58 | + let etherscan_url = String::from("https://sepolia.etherscan.io/tx/"); |
| 59 | + println!( |
| 60 | + " Tx Receipt: {}", |
| 61 | + etherscan_url + &receipt.transaction_hash.to_string() |
| 62 | + ); |
27 | 63 |
|
28 | | - // let authorization = Authorization { |
29 | | - // chain_id: U256::from(11155111), |
30 | | - // // Reference to the contract that will be set as code for the authority. |
31 | | - // address: *contract.address(), |
32 | | - // nonce: provider.get_transaction_count(pk.address()).await?, |
33 | | - // }; |
34 | 64 | Ok(()) |
35 | 65 | } |
36 | 66 |
|
| 67 | +/// Encode a list of EOA-style txs for MultiSend/MultiSendCallOnly. |
| 68 | +fn pack_multisend(calls: &[TransactionRequest]) -> eyre::Result<Bytes> { |
| 69 | + let mut out = BytesMut::new(); |
| 70 | + |
| 71 | + for tx in calls { |
| 72 | + // operation ─ normal CALL |
| 73 | + out.put_u8(0); |
| 74 | + |
| 75 | + // to ─ 20 bytes (error if missing) |
| 76 | + let to = match tx.to.as_ref() { |
| 77 | + Some(TxKind::Call(addr)) => addr, |
| 78 | + _ => return Err(eyre::eyre!("Tx is missing `to` address")), |
| 79 | + }; |
| 80 | + out.extend_from_slice(to.as_slice()); |
| 81 | + |
| 82 | + // value ─ 32 bytes big-endian |
| 83 | + let value: U256 = tx.value.unwrap_or_default(); |
| 84 | + out.extend_from_slice(&value.to_be_bytes::<32>()); |
| 85 | + |
| 86 | + // data length & data |
| 87 | + let data: Bytes = tx |
| 88 | + .input |
| 89 | + .input // first try the “input” field |
| 90 | + .clone() |
| 91 | + .or_else(|| tx.input.data.clone()) // else fall back to “data” |
| 92 | + .unwrap_or_default(); // or empty |
| 93 | + out.extend_from_slice(&U256::from(data.len()).to_be_bytes::<32>()); |
| 94 | + out.extend_from_slice(&data); |
| 95 | + } |
| 96 | + |
| 97 | + Ok(Bytes::from(out.freeze())) |
| 98 | +} |
| 99 | + |
37 | 100 | #[tokio::main] |
38 | | -fn main() -> Result<()> { |
39 | | - let anvil = Anvil::new().arg("--hardfork").arg("prague").try_spawn()?; |
| 101 | +async fn main() -> Result<()> { |
40 | 102 | let private_key: PrivateKeySigner = match env::var("PK") { |
41 | 103 | Ok(pk) => PrivateKeySigner::from_str(pk.as_str())?, |
42 | | - Err(_) => anvil.keys()[0].clone().into(), |
| 104 | + Err(_) => PrivateKeySigner::from_str( |
| 105 | + "0xe4dc8cbe94cbc139084c9c7adc5c2a829d3246f76282679e0c067147a47eb3f8", |
| 106 | + )?, |
43 | 107 | }; |
44 | 108 |
|
45 | 109 | let tx1 = TransactionRequest::default() |
46 | 110 | .with_to(Address::ZERO) |
47 | | - .with_value(U256::from(10000000000000)); |
| 111 | + .with_value(U256::from(10000000000000_i64)); |
48 | 112 |
|
49 | 113 | let tx2 = TransactionRequest::default() |
50 | 114 | .with_to(address!("0x1111111111111111111111111111111111111111")) |
51 | | - .with_value(U256::from(20000000000000)); |
| 115 | + .with_value(U256::from(20000000000000_i64)); |
52 | 116 |
|
53 | 117 | let calls = vec![tx1, tx2]; |
54 | 118 |
|
| 119 | + eoa_multisend(private_key, calls).await?; |
| 120 | + |
55 | 121 | Ok(()) |
56 | 122 | } |
0 commit comments