ethers_types_rs/
request.rs1use ethabi::ethereum_types::{Signature, H256};
2use serde::{Deserialize, Serialize};
3
4fn rlp_opt<T: rlp::Encodable>(rlp: &mut rlp::RlpStream, opt: &Option<T>) {
5 if let Some(inner) = opt {
6 rlp.append(inner);
7 } else {
8 rlp.append(&"");
9 }
10}
11
12macro_rules! tx_json_support {
13 ($ty:ident) => {
14 impl TryFrom<&str> for $ty {
15 type Error = serde_json::Error;
16
17 fn try_from(value: &str) -> Result<Self, Self::Error> {
18 serde_json::from_str(value)
19 }
20 }
21
22 impl TryFrom<String> for $ty {
23 type Error = serde_json::Error;
24 fn try_from(value: String) -> Result<Self, Self::Error> {
25 Self::try_from(value.as_ref())
26 }
27 }
28
29 impl TryFrom<serde_json::Value> for $ty {
30 type Error = serde_json::Error;
31 fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
32 serde_json::from_value(value)
33 }
34 }
35 };
36}
37
38#[derive(Debug, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum NameOrAddress {
41 Name(String),
42 Address(crate::Address),
43}
44
45mod legacy;
46pub use legacy::*;
47
48mod eip2930;
49pub use eip2930::*;
50
51mod eip1559;
52pub use eip1559::*;
53
54use crate::Bytecode;
55
56tx_json_support!(LegacyTransactionRequest);
57tx_json_support!(Eip2930TransactionRequest);
58tx_json_support!(Eip1559TransactionRequest);
59
60#[derive(Serialize, Deserialize, Debug, Clone)]
61#[serde(tag = "type")]
62pub enum TypedTransactionRequest {
63 #[serde(rename = "0x00")]
65 Legacy(LegacyTransactionRequest),
66 #[serde(rename = "0x01")]
68 Eip2930(Eip2930TransactionRequest),
69 #[serde(rename = "0x02")]
71 Eip1559(Eip1559TransactionRequest),
72}
73
74impl From<LegacyTransactionRequest> for TypedTransactionRequest {
75 fn from(tx: LegacyTransactionRequest) -> Self {
76 TypedTransactionRequest::Legacy(tx)
77 }
78}
79
80impl From<Eip2930TransactionRequest> for TypedTransactionRequest {
81 fn from(tx: Eip2930TransactionRequest) -> Self {
82 TypedTransactionRequest::Eip2930(tx)
83 }
84}
85
86impl From<Eip1559TransactionRequest> for TypedTransactionRequest {
87 fn from(tx: Eip1559TransactionRequest) -> Self {
88 TypedTransactionRequest::Eip1559(tx)
89 }
90}
91
92impl TypedTransactionRequest {
93 pub fn sign_hash(&self) -> H256 {
94 match self {
95 Self::Legacy(tx) => tx.sign_hash(),
96 Self::Eip2930(tx) => tx.sign_hash(),
97 Self::Eip1559(tx) => tx.sign_hash(),
98 }
99 }
100
101 pub fn rlp_signed(&self, signature: Signature) -> Bytecode {
102 match self {
103 Self::Legacy(tx) => tx.rlp_signed(signature),
104 Self::Eip2930(tx) => tx.rlp_signed(signature),
105 Self::Eip1559(tx) => tx.rlp_signed(signature),
106 }
107 }
108}