|
| 1 | +//! Example showing how to encode and decode an [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) transaction. |
| 2 | +
|
| 3 | +use alloy::{ |
| 4 | + consensus::{SignableTransaction, TxEip1559}, |
| 5 | + eips::eip2930::AccessList, |
| 6 | + primitives::{address, b256, hex, Signature, TxKind, U256}, |
| 7 | +}; |
| 8 | +use eyre::Result; |
| 9 | + |
| 10 | +#[tokio::main] |
| 11 | +async fn main() -> Result<()> { |
| 12 | + // EIP1559 transaction: <https://etherscan.io/tx/0x0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0> |
| 13 | + let tx_hash = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0"); |
| 14 | + |
| 15 | + // Signer of the transaction. |
| 16 | + let signer = address!("DD6B8b3dC6B7AD97db52F08a275FF4483e024CEa"); |
| 17 | + |
| 18 | + // Construct the EIP-1559 transaction. |
| 19 | + let tx = TxEip1559 { |
| 20 | + chain_id: 1, |
| 21 | + nonce: 0x42, |
| 22 | + gas_limit: 44386, |
| 23 | + to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")), |
| 24 | + value: U256::from(0_u64), |
| 25 | + input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(), |
| 26 | + max_fee_per_gas: 0x4a817c800, |
| 27 | + max_priority_fee_per_gas: 0x3b9aca00, |
| 28 | + access_list: AccessList::default(), |
| 29 | + }; |
| 30 | + |
| 31 | + // Construct the signature of the transaction. |
| 32 | + let signature = Signature::from_scalars_and_parity( |
| 33 | + b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"), |
| 34 | + b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"), |
| 35 | + false, |
| 36 | + )?; |
| 37 | + |
| 38 | + // Convert the transaction into a signed transaction. |
| 39 | + let signed_tx = tx.into_signed(signature); |
| 40 | + assert_eq!(*signed_tx.hash(), tx_hash); |
| 41 | + |
| 42 | + // Recover the signer from the signed transaction to ensure it matches the expected signer. |
| 43 | + let recovered_signer = signed_tx.recover_signer()?; |
| 44 | + assert_eq!(recovered_signer, signer); |
| 45 | + |
| 46 | + Ok(()) |
| 47 | +} |
0 commit comments