ethers_types_rs/request/
eip1559.rs

1use ethabi::ethereum_types::{Address, Signature, H256, U256};
2use ethers_hash_rs::keccak256;
3use rlp::Encodable;
4use serde::{Deserialize, Serialize};
5
6use crate::{signature::SignatureVRS, Bytecode};
7
8use super::{rlp_opt, AccessList};
9
10#[derive(Debug, Serialize, Deserialize, Clone, Default)]
11#[serde(rename_all = "camelCase")]
12pub struct Eip1559TransactionRequest {
13    pub chain_id: U256,
14
15    /// Transaction nonce
16    pub nonce: U256,
17    /// Gas price
18    pub max_priority_fee_per_gas: U256,
19
20    pub max_fee_per_gas: U256,
21    /// Supplied gas
22    pub gas: U256,
23    /// Recipient address (None for contract creation)
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub to: Option<Address>,
26    /// Transferred value
27    pub value: Option<U256>,
28    /// The compiled code of a contract OR the first 4 bytes of the hash of the
29    /// invoked method signature and encoded parameters. For details see Ethereum Contract ABI
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub data: Option<Bytecode>,
32
33    pub access_list: AccessList,
34}
35
36impl Encodable for Eip1559TransactionRequest {
37    fn rlp_append(&self, s: &mut rlp::RlpStream) {
38        s.begin_unbounded_list();
39
40        self.rlp_base(s);
41
42        s.finalize_unbounded_list();
43    }
44}
45
46impl Eip1559TransactionRequest {
47    /// Generate legacy transaction sign hash.
48    pub fn sign_hash(&self) -> H256 {
49        keccak256(self.rlp()).into()
50    }
51    fn to_bytecode(mut buff: Vec<u8>) -> Bytecode {
52        buff.insert(0, 0x2);
53
54        buff.into()
55    }
56    pub fn rlp(&self) -> Bytecode {
57        let mut s = rlp::RlpStream::new();
58
59        self.rlp_append(&mut s);
60
61        Self::to_bytecode(s.out().freeze().to_vec())
62    }
63
64    fn rlp_base(&self, s: &mut rlp::RlpStream) {
65        s.append(&self.chain_id);
66        s.append(&self.nonce);
67        s.append(&self.max_priority_fee_per_gas);
68        s.append(&self.max_fee_per_gas);
69        s.append(&self.gas);
70
71        rlp_opt(s, &self.to);
72        rlp_opt(s, &self.value);
73        rlp_opt(s, &self.data);
74
75        s.append(&self.access_list);
76    }
77
78    /// Returns signed tx rlp encoding stream.
79    pub fn rlp_signed(&self, signature: Signature) -> Bytecode {
80        let mut rlp = rlp::RlpStream::new();
81
82        rlp.begin_unbounded_list();
83
84        self.rlp_base(&mut rlp);
85
86        rlp.append(&signature.v());
87        rlp.append(&signature.r());
88        rlp.append(&signature.s());
89
90        rlp.finalize_unbounded_list();
91
92        Self::to_bytecode(rlp.out().freeze().to_vec())
93    }
94}