|
| 1 | +use crate::utils::http_provider; |
| 2 | +use alloy_consensus::{transaction::TxEip7702, SignableTransaction}; |
| 3 | +use alloy_eips::eip7702::OptionalNonce; |
| 4 | +use alloy_network::{ReceiptResponse, TransactionBuilder, TxSignerSync}; |
| 5 | +use alloy_primitives::{bytes, TxKind}; |
| 6 | +use alloy_provider::Provider; |
| 7 | +use alloy_rpc_types::{Authorization, TransactionRequest}; |
| 8 | +use alloy_serde::WithOtherFields; |
| 9 | +use alloy_signer::SignerSync; |
| 10 | +use anvil::{spawn, Hardfork, NodeConfig}; |
| 11 | + |
| 12 | +#[tokio::test(flavor = "multi_thread")] |
| 13 | +async fn can_send_eip7702_tx() { |
| 14 | + let node_config = NodeConfig::test().with_hardfork(Some(Hardfork::Prague)); |
| 15 | + let (_api, handle) = spawn(node_config).await; |
| 16 | + let provider = http_provider(&handle.http_endpoint()); |
| 17 | + |
| 18 | + let wallets = handle.dev_wallets().collect::<Vec<_>>(); |
| 19 | + |
| 20 | + // deploy simple contract forwarding calldata to LOG0 |
| 21 | + // PUSH7(CALLDATASIZE PUSH0 PUSH0 CALLDATACOPY CALLDATASIZE PUSH0 LOG0) PUSH0 MSTORE PUSH1(7) |
| 22 | + // PUSH1(25) RETURN |
| 23 | + let logger_bytecode = bytes!("66365f5f37365fa05f5260076019f3"); |
| 24 | + |
| 25 | + let eip1559_est = provider.estimate_eip1559_fees(None).await.unwrap(); |
| 26 | + |
| 27 | + let from = wallets[0].address(); |
| 28 | + let tx = TransactionRequest::default() |
| 29 | + .with_from(from) |
| 30 | + .into_create() |
| 31 | + .with_nonce(0) |
| 32 | + .with_max_fee_per_gas(eip1559_est.max_fee_per_gas) |
| 33 | + .with_max_priority_fee_per_gas(eip1559_est.max_priority_fee_per_gas) |
| 34 | + .with_input(logger_bytecode); |
| 35 | + |
| 36 | + let receipt = provider |
| 37 | + .send_transaction(WithOtherFields::new(tx)) |
| 38 | + .await |
| 39 | + .unwrap() |
| 40 | + .get_receipt() |
| 41 | + .await |
| 42 | + .unwrap(); |
| 43 | + |
| 44 | + assert!(receipt.status()); |
| 45 | + |
| 46 | + let contract = receipt.contract_address.unwrap(); |
| 47 | + let authorization = Authorization { |
| 48 | + chain_id: 31337, |
| 49 | + address: contract, |
| 50 | + nonce: OptionalNonce::new(Some(provider.get_transaction_count(from).await.unwrap())), |
| 51 | + }; |
| 52 | + let signature = wallets[0].sign_hash_sync(&authorization.signature_hash()).unwrap(); |
| 53 | + let authorization = authorization.into_signed(signature); |
| 54 | + |
| 55 | + let log_data = bytes!("11112222"); |
| 56 | + let mut tx = TxEip7702 { |
| 57 | + max_fee_per_gas: eip1559_est.max_fee_per_gas, |
| 58 | + max_priority_fee_per_gas: eip1559_est.max_priority_fee_per_gas, |
| 59 | + gas_limit: 100000, |
| 60 | + chain_id: 31337, |
| 61 | + to: TxKind::Call(from), |
| 62 | + input: bytes!("11112222"), |
| 63 | + authorization_list: vec![authorization], |
| 64 | + ..Default::default() |
| 65 | + }; |
| 66 | + let signature = wallets[1].sign_transaction_sync(&mut tx).unwrap(); |
| 67 | + |
| 68 | + let tx = tx.into_signed(signature); |
| 69 | + let mut encoded = Vec::new(); |
| 70 | + tx.tx().encode_with_signature(tx.signature(), &mut encoded, false); |
| 71 | + |
| 72 | + let receipt = |
| 73 | + provider.send_raw_transaction(&encoded).await.unwrap().get_receipt().await.unwrap(); |
| 74 | + let log = &receipt.inner.inner.logs()[0]; |
| 75 | + // assert that log was from EOA which signed authorization |
| 76 | + assert_eq!(log.address(), from); |
| 77 | + assert_eq!(log.topics().len(), 0); |
| 78 | + assert_eq!(log.data().data, log_data); |
| 79 | +} |
0 commit comments