-
Notifications
You must be signed in to change notification settings - Fork 55
/
ethers.rs
312 lines (287 loc) · 11.7 KB
/
ethers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2023 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Convert from Ethers types.
use alloy_primitives::{Address, Bloom, Bytes, B256, U256};
use anyhow::{anyhow, Context};
use ethers_core::types::{
transaction::eip2930::{
AccessList as EthersAccessList, AccessListItem as EthersAccessListItem,
},
Block as EthersBlock, Bytes as EthersBytes, EIP1186ProofResponse,
Transaction as EthersTransaction, TransactionReceipt as EthersReceipt,
Withdrawal as EthersWithdrawal, H160 as EthersH160, H256 as EthersH256, U256 as EthersU256,
U64,
};
use crate::{
access_list::{AccessList, AccessListItem},
block::Header,
receipt::{Log, Receipt, ReceiptPayload},
transactions::{
ethereum::{
EthereumTxEssence, TransactionKind, TxEssenceEip1559, TxEssenceEip2930, TxEssenceLegacy,
},
optimism::{OptimismTxEssence, TxEssenceOptimismDeposited},
signature::TxSignature,
Transaction, TxEssence,
},
trie::StateAccount,
withdrawal::Withdrawal,
};
/// Convert an `EthersU256` type to the `U256` type.
#[inline]
pub fn from_ethers_u256(v: EthersU256) -> U256 {
U256::from_limbs(v.0)
}
/// Convert an `U256` type to the `EthersU256` type.
#[inline]
pub fn to_ethers_u256(v: U256) -> EthersU256 {
EthersU256(v.into_limbs())
}
/// Convert an `EthersH160` type to the `Address` type.
#[inline]
pub fn from_ethers_h160(v: EthersH160) -> Address {
v.0.into()
}
/// Convert an `EthersH256` type to the `B256` type.
#[inline]
pub fn from_ethers_h256(v: EthersH256) -> B256 {
v.0.into()
}
/// Convert an `EthersBytes` type to the `Bytes` type.
#[inline]
pub fn from_ethers_bytes(v: EthersBytes) -> Bytes {
v.0.into()
}
/// Conversion from `EthersAccessListItem` to the local [AccessListItem].
impl From<EthersAccessListItem> for AccessListItem {
fn from(item: EthersAccessListItem) -> Self {
AccessListItem {
address: item.address.0.into(),
storage_keys: item
.storage_keys
.into_iter()
.map(|key| key.0.into())
.collect(),
}
}
}
/// Conversion from `EthersAccessList` to the local [AccessList].
impl From<EthersAccessList> for AccessList {
fn from(list: EthersAccessList) -> Self {
AccessList(list.0.into_iter().map(|item| item.into()).collect())
}
}
/// Convert an optional `EthersH160` to the local [TransactionKind].
impl From<Option<EthersH160>> for TransactionKind {
fn from(addr: Option<EthersH160>) -> Self {
match addr {
Some(address) => TransactionKind::Call(address.0.into()),
None => TransactionKind::Create,
}
}
}
/// Conversion from `EthersBlock` to the local [Header].
/// This conversion may fail if certain expected fields are missing.
impl<T> TryFrom<EthersBlock<T>> for Header {
type Error = anyhow::Error;
fn try_from(block: EthersBlock<T>) -> Result<Self, Self::Error> {
Ok(Header {
parent_hash: from_ethers_h256(block.parent_hash),
ommers_hash: from_ethers_h256(block.uncles_hash),
beneficiary: from_ethers_h160(block.author.context("author missing")?),
state_root: from_ethers_h256(block.state_root),
transactions_root: from_ethers_h256(block.transactions_root),
receipts_root: from_ethers_h256(block.receipts_root),
logs_bloom: Bloom::from_slice(
block.logs_bloom.context("logs_bloom missing")?.as_bytes(),
),
difficulty: from_ethers_u256(block.difficulty),
number: block.number.context("number missing")?.as_u64(),
gas_limit: from_ethers_u256(block.gas_limit),
gas_used: from_ethers_u256(block.gas_used),
timestamp: from_ethers_u256(block.timestamp),
extra_data: block.extra_data.0.into(),
mix_hash: block.mix_hash.context("mix_hash missing")?.0.into(),
nonce: block.nonce.context("nonce missing")?.0.into(),
base_fee_per_gas: from_ethers_u256(
block.base_fee_per_gas.context("base_fee_per_gas missing")?,
),
withdrawals_root: block.withdrawals_root.map(from_ethers_h256),
})
}
}
/// Conversion from `EthersTransaction` to the local [Transaction].
/// This conversion may fail if certain expected fields are missing.
impl<E: TxEssence + TryFrom<EthersTransaction>> TryFrom<EthersTransaction> for Transaction<E> {
type Error = <E as TryFrom<EthersTransaction>>::Error;
fn try_from(value: EthersTransaction) -> Result<Self, Self::Error> {
let signature = TxSignature {
v: value.v.as_u64(),
r: from_ethers_u256(value.r),
s: from_ethers_u256(value.s),
};
let essence = value.try_into()?;
Ok(Transaction { essence, signature })
}
}
/// Conversion from `EthersTransaction` to the local [EthereumTxEssence].
/// This conversion may fail if certain expected fields are missing.
impl TryFrom<EthersTransaction> for EthereumTxEssence {
type Error = anyhow::Error;
fn try_from(tx: EthersTransaction) -> Result<Self, Self::Error> {
let essence = match tx.transaction_type.map(|t| t.as_u64()) {
None | Some(0) => EthereumTxEssence::Legacy(TxEssenceLegacy {
chain_id: match tx.chain_id {
None => None,
Some(chain_id) => Some(
chain_id
.try_into()
.map_err(|err| anyhow!("invalid chain_id: {}", err))?,
),
},
nonce: tx
.nonce
.try_into()
.map_err(|err| anyhow!("invalid nonce: {}", err))?,
gas_price: from_ethers_u256(tx.gas_price.context("gas_price missing")?),
gas_limit: from_ethers_u256(tx.gas),
to: tx.to.into(),
value: from_ethers_u256(tx.value),
data: tx.input.0.into(),
}),
Some(1) => EthereumTxEssence::Eip2930(TxEssenceEip2930 {
chain_id: tx
.chain_id
.context("chain_id missing")?
.try_into()
.map_err(|err| anyhow!("invalid chain_id: {}", err))?,
nonce: tx
.nonce
.try_into()
.map_err(|err| anyhow!("invalid nonce: {}", err))?,
gas_price: from_ethers_u256(tx.gas_price.context("gas_price missing")?),
gas_limit: from_ethers_u256(tx.gas),
to: tx.to.into(),
value: from_ethers_u256(tx.value),
access_list: tx.access_list.context("access_list missing")?.into(),
data: tx.input.0.into(),
}),
Some(2) => EthereumTxEssence::Eip1559(TxEssenceEip1559 {
chain_id: tx
.chain_id
.context("chain_id missing")?
.try_into()
.map_err(|err| anyhow!("invalid chain_id: {}", err))?,
nonce: tx
.nonce
.try_into()
.map_err(|err| anyhow!("invalid nonce: {}", err))?,
max_priority_fee_per_gas: from_ethers_u256(
tx.max_priority_fee_per_gas
.context("max_priority_fee_per_gas missing")?,
),
max_fee_per_gas: from_ethers_u256(
tx.max_fee_per_gas.context("max_fee_per_gas missing")?,
),
gas_limit: from_ethers_u256(tx.gas),
to: tx.to.into(),
value: from_ethers_u256(tx.value),
access_list: tx.access_list.context("access_list missing")?.into(),
data: tx.input.0.into(),
}),
_ => unreachable!(),
};
Ok(essence)
}
}
/// Conversion from `EthersTransaction` to the local [OptimismTxEssence].
/// This conversion may fail if certain expected fields are missing.
impl TryFrom<EthersTransaction> for OptimismTxEssence {
type Error = anyhow::Error;
fn try_from(tx: EthersTransaction) -> Result<Self, Self::Error> {
let essence = match tx.transaction_type.map(|t| t.as_u64()) {
Some(0x7E) => OptimismTxEssence::OptimismDeposited(TxEssenceOptimismDeposited {
gas_limit: from_ethers_u256(tx.gas),
from: tx.from.0.into(),
to: tx.to.into(),
value: from_ethers_u256(tx.value),
data: tx.input.0.into(),
source_hash: from_ethers_h256(tx.source_hash),
mint: from_ethers_u256(tx.mint.context("mint missing")?),
is_system_tx: tx.is_system_tx,
}),
_ => OptimismTxEssence::Ethereum(tx.try_into()?),
};
Ok(essence)
}
}
/// Conversion from `EthersWithdrawal` to the local [Withdrawal].
/// This conversion may fail if certain expected fields are missing.
impl TryFrom<EthersWithdrawal> for Withdrawal {
type Error = anyhow::Error;
fn try_from(withdrawal: EthersWithdrawal) -> Result<Self, Self::Error> {
Ok(Withdrawal {
index: withdrawal.index.as_u64(),
validator_index: withdrawal.validator_index.as_u64(),
address: withdrawal.address.0.into(),
amount: withdrawal
.amount
.try_into()
.map_err(|err| anyhow!("invalid amount: {}", err))?,
})
}
}
impl TryFrom<EthersReceipt> for Receipt {
type Error = anyhow::Error;
fn try_from(receipt: EthersReceipt) -> Result<Self, Self::Error> {
Ok(Receipt {
tx_type: receipt
.transaction_type
.context("transaction_type missing")?
.as_u64()
.try_into()
.context("invalid transaction_type")?,
payload: ReceiptPayload {
success: receipt.status.context("status missing")? == U64::one(),
cumulative_gas_used: from_ethers_u256(receipt.cumulative_gas_used),
logs_bloom: Bloom::from_slice(receipt.logs_bloom.as_bytes()),
logs: receipt
.logs
.into_iter()
.map(|log| {
let address = log.address.0.into();
let topics = log.topics.into_iter().map(from_ethers_h256).collect();
let data = log.data.0.into();
Log {
address,
topics,
data,
}
})
.collect(),
},
})
}
}
/// Conversion from `EIP1186ProofResponse` to the local [StateAccount].
impl From<EIP1186ProofResponse> for StateAccount {
fn from(response: EIP1186ProofResponse) -> Self {
StateAccount {
nonce: response.nonce.as_u64(),
balance: from_ethers_u256(response.balance),
storage_root: from_ethers_h256(response.storage_hash),
code_hash: from_ethers_h256(response.code_hash),
}
}
}