Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transactions): encode eip1559 #45

Merged
merged 8 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ unnecessary_struct_initialization = "allow"
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "cc68b93", features = [
"consensus",
"contract",
"k256",
"eips",
"kzg",
"network",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ This repository contains the following examples:
- [x] [Trace transaction](./examples/transactions/examples/trace_transaction.rs)
- [x] [Transfer ERC20 token](./examples/transactions/examples/transfer_erc20.rs)
- [x] [Transfer ETH](./examples/transactions/examples/transfer_eth.rs)
- [x] [Sign and send a raw transaction](./examples/transactions/examples/sign_transaction.rs)
- [x] [Sign and send a raw transaction](./examples/transactions/examples/send_raw_transaction.rs)
- [x] [Send EIP-1559 transaction](./examples/transactions/examples/send_eip1559_transaction.rs)
- [x] [Send legacy transaction](./examples/transactions/examples/send_legacy_transaction.rs)
- [x] [Send EIP-4844 transaction](./examples/transactions/examples/send_eip4844_transaction.rs)
Expand Down
47 changes: 47 additions & 0 deletions examples/transactions/examples/encode_decode_eip1559.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Example showing how to encode and decode an [EIP-1559](https://eips.ethereum.org/EIPS/eip-1559) transaction.

use alloy::{
consensus::{SignableTransaction, TxEip1559},
eips::eip2930::AccessList,
primitives::{address, b256, hex, Signature, TxKind, U256},
};
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
// EIP1559 transaction: <https://etherscan.io/tx/0x0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0>
let tx_hash = b256!("0ec0b6a2df4d87424e5f6ad2a654e27aaeb7dac20ae9e8385cc09087ad532ee0");

// Signer of the transaction.
let signer = address!("DD6B8b3dC6B7AD97db52F08a275FF4483e024CEa");

// Construct the EIP-1559 transaction.
let tx = TxEip1559 {
chain_id: 1,
nonce: 0x42,
gas_limit: 44386,
to: TxKind::Call( address!("6069a6c32cf691f5982febae4faf8a6f3ab2f0f6")),
value: U256::from(0_u64),
input: hex!("a22cb4650000000000000000000000005eee75727d804a2b13038928d36f8b188945a57a0000000000000000000000000000000000000000000000000000000000000000").into(),
max_fee_per_gas: 0x4a817c800,
max_priority_fee_per_gas: 0x3b9aca00,
access_list: AccessList::default(),
};

// Construct the signature of the transaction.
let signature = Signature::from_scalars_and_parity(
b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565"),
b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1"),
false,
)?;

// Convert the transaction into a signed transaction.
let signed_tx = tx.into_signed(signature);
assert_eq!(*signed_tx.hash(), tx_hash);

// Recover the signer from the signed transaction to ensure it matches the expected signer.
let recovered_signer = signed_tx.recover_signer()?;
assert_eq!(recovered_signer, signer);

Ok(())
}
Loading