Skip to content

Commit cb77139

Browse files
feat(transactions): encode eip1559 (#45)
* feat(transactions): encode eip1559 * recover signer * nit * improve docs * rename sign_transaction --------- Co-authored-by: zerosnacks <[email protected]>
1 parent 0f6beee commit cb77139

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ unnecessary_struct_initialization = "allow"
8484
alloy = { git = "https://github.com/alloy-rs/alloy", rev = "f6ebef2", features = [
8585
"consensus",
8686
"contract",
87+
"k256",
8788
"eips",
8889
"kzg",
8990
"network",

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ This repository contains the following examples:
7373
- [x] [Trace transaction](./examples/transactions/examples/trace_transaction.rs)
7474
- [x] [Transfer ERC20 token](./examples/transactions/examples/transfer_erc20.rs)
7575
- [x] [Transfer ETH](./examples/transactions/examples/transfer_eth.rs)
76-
- [x] [Sign and send a raw transaction](./examples/transactions/examples/sign_transaction.rs)
76+
- [x] [Sign and send a raw transaction](./examples/transactions/examples/send_raw_transaction.rs)
7777
- [x] [Send EIP-1559 transaction](./examples/transactions/examples/send_eip1559_transaction.rs)
7878
- [x] [Send legacy transaction](./examples/transactions/examples/send_legacy_transaction.rs)
7979
- [x] [Send EIP-4844 transaction](./examples/transactions/examples/send_eip4844_transaction.rs)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}
File renamed without changes.

0 commit comments

Comments
 (0)