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