kabu_evm_utils/evm_tx_env.rs
1use alloy::primitives::private::alloy_rlp;
2use alloy::primitives::SignatureError;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum EnvError {
7 #[error(transparent)]
8 AlloyRplError(#[from] alloy_rlp::Error),
9 #[error(transparent)]
10 SignatureError(#[from] SignatureError),
11 #[error("Unsupported transaction type")]
12 UnsupportedTransactionType,
13}
14/*
15pub fn env_from_signed_tx(rpl_bytes: Bytes) -> Result<TxEnv, EnvError> {
16 match TxEnvelope::decode(&mut rpl_bytes.iter().as_slice())? {
17 TxEnvelope::Legacy(tx) => {
18 Ok(TxEnv {
19 caller: tx.recover_signer()?,
20 transact_to: tx.tx().to,
21 nonce: Some(tx.tx().nonce),
22 data: tx.tx().input.clone(),
23 value: tx.tx().value,
24 gas_price: U256::from(tx.tx().gas_price),
25 gas_limit: tx.tx().gas_limit,
26 chain_id: tx.tx().chain_id,
27
28 // not supported
29 access_list: vec![],
30 gas_priority_fee: None,
31 blob_hashes: vec![],
32 max_fee_per_blob_gas: None,
33 authorization_list: None,
34 })
35 }
36 TxEnvelope::Eip2930(tx) => {
37 Ok(TxEnv {
38 caller: tx.recover_signer()?,
39 transact_to: tx.tx().to,
40 nonce: Some(tx.tx().nonce),
41 data: tx.tx().input.clone(),
42 value: tx.tx().value,
43 gas_price: U256::from(tx.tx().gas_price),
44 gas_limit: tx.tx().gas_limit,
45 chain_id: Some(tx.tx().chain_id),
46 access_list: tx.tx().clone().access_list.0,
47
48 // not supported
49 gas_priority_fee: None,
50 blob_hashes: vec![],
51 max_fee_per_blob_gas: None,
52 authorization_list: None,
53 })
54 }
55 TxEnvelope::Eip1559(tx) => {
56 Ok(TxEnv {
57 caller: tx.recover_signer()?,
58 transact_to: tx.tx().to,
59 nonce: Some(tx.tx().nonce),
60 data: tx.tx().input.clone(),
61 value: tx.tx().value,
62 gas_price: U256::from(tx.tx().max_fee_per_gas),
63 gas_priority_fee: Some(U256::from(tx.tx().max_priority_fee_per_gas)),
64 gas_limit: tx.tx().gas_limit,
65 chain_id: Some(tx.tx().chain_id),
66 access_list: tx.tx().clone().access_list.0,
67
68 // not supported
69 blob_hashes: vec![],
70 max_fee_per_blob_gas: None,
71 authorization_list: None,
72 })
73 }
74 TxEnvelope::Eip4844(signed_tx) => {
75 let tx = match signed_tx.tx() {
76 TxEip4844Variant::TxEip4844(tx) => tx,
77 TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx(),
78 };
79 Ok(TxEnv {
80 caller: signed_tx.recover_signer()?,
81 transact_to: TxKind::Call(tx.to),
82 nonce: Some(tx.nonce),
83 data: tx.input.clone(),
84 value: tx.value,
85 gas_price: U256::from(tx.max_fee_per_gas),
86 gas_priority_fee: Some(U256::from(tx.max_priority_fee_per_gas)),
87 gas_limit: tx.gas_limit,
88 chain_id: Some(tx.chain_id),
89 access_list: tx.clone().access_list.0,
90 max_fee_per_blob_gas: Some(U256::from(tx.max_fee_per_blob_gas)),
91 blob_hashes: tx.blob_versioned_hashes.clone(),
92
93 // Not supported
94 authorization_list: None,
95 })
96 }
97 TxEnvelope::Eip7702(tx) => {
98 Ok(TxEnv {
99 caller: tx.recover_signer()?,
100 transact_to: TxKind::Call(tx.tx().to),
101 nonce: Some(tx.tx().nonce),
102 data: tx.tx().input.clone(),
103 value: tx.tx().value,
104 gas_price: U256::from(tx.tx().max_fee_per_gas),
105 gas_priority_fee: Some(U256::from(tx.tx().max_priority_fee_per_gas)),
106 gas_limit: tx.tx().gas_limit,
107 chain_id: Some(tx.tx().chain_id),
108 access_list: tx.tx().clone().access_list.0,
109 authorization_list: Some(AuthorizationList::Signed(tx.tx().clone().authorization_list)),
110
111 // Not supported
112 blob_hashes: vec![],
113 max_fee_per_blob_gas: None,
114 })
115 }
116 }
117}
118
119pub fn tx_to_evm_tx(tx: &Transaction) -> TxEnv {
120 let tx = &tx.inner;
121 TxEnv {
122 transact_to: match tx.to() {
123 Some(to) => TxKind::Call(to),
124 None => TxKind::Create,
125 },
126 nonce: Some(tx.nonce()),
127 chain_id: tx.chain_id(),
128 data: tx.input().clone(),
129 value: tx.value(),
130 caller: tx.signer(),
131 gas_limit: tx.gas_limit(),
132
133 // support type 1 and 2
134 gas_price: U256::from(tx.max_fee_per_gas()),
135 gas_priority_fee: Some(U256::from(tx.max_priority_fee_per_gas().unwrap_or_default())),
136
137 // Not used in kabu context
138 blob_hashes: vec![],
139 max_fee_per_blob_gas: None,
140 access_list: vec![],
141 authorization_list: None,
142 }
143}
144
145 */