Skip to content

Commit

Permalink
fix(anvil): add chain id field (foundry-rs#4366)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 15, 2023
1 parent 54e02a7 commit d70aea4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 2 additions & 1 deletion anvil/core/src/eth/transaction/ethers_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl From<MaybeImpersonatedTransaction> for EthersTransaction {

impl From<TransactionRequest> for EthTransactionRequest {
fn from(req: TransactionRequest) -> Self {
let TransactionRequest { from, to, gas, gas_price, value, data, nonce, .. } = req;
let TransactionRequest { from, to, gas, gas_price, value, data, nonce, chain_id, .. } = req;
EthTransactionRequest {
from,
to: to.and_then(|to| match to {
Expand All @@ -198,6 +198,7 @@ impl From<TransactionRequest> for EthTransactionRequest {
value,
data,
nonce,
chain_id,
access_list: None,
transaction_type: None,
}
Expand Down
13 changes: 9 additions & 4 deletions anvil/core/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::eth::{
use ethers_core::{
types::{
transaction::eip2930::{AccessList, AccessListItem},
Address, Bloom, Bytes, Signature, SignatureError, TxHash, H256, U256,
Address, Bloom, Bytes, Signature, SignatureError, TxHash, H256, U256, U64,
},
utils::{
keccak256, rlp,
Expand Down Expand Up @@ -66,6 +66,9 @@ pub struct EthTransactionRequest {
pub data: Option<Bytes>,
/// Transaction nonce
pub nonce: Option<U256>,
/// chain id
#[cfg_attr(feature = "serde", serde(default))]
pub chain_id: Option<U64>,
/// warm storage access pre-payment
#[cfg_attr(feature = "serde", serde(default))]
pub access_list: Option<Vec<AccessListItem>>,
Expand All @@ -89,8 +92,10 @@ impl EthTransactionRequest {
data,
nonce,
mut access_list,
chain_id,
..
} = self;
let chain_id = chain_id.map(|id| id.as_u64());
match (gas_price, max_fee_per_gas, access_list.take()) {
// legacy transaction
(Some(_), None, None) => {
Expand All @@ -104,7 +109,7 @@ impl EthTransactionRequest {
Some(to) => TransactionKind::Call(to),
None => TransactionKind::Create,
},
chain_id: None,
chain_id,
}))
}
// EIP2930
Expand All @@ -119,7 +124,7 @@ impl EthTransactionRequest {
Some(to) => TransactionKind::Call(to),
None => TransactionKind::Create,
},
chain_id: 0,
chain_id: chain_id.unwrap_or_default(),
access_list,
}))
}
Expand All @@ -137,7 +142,7 @@ impl EthTransactionRequest {
Some(to) => TransactionKind::Call(to),
None => TransactionKind::Create,
},
chain_id: 0,
chain_id: chain_id.unwrap_or_default(),
access_list: access_list.unwrap_or_default(),
}))
}
Expand Down
2 changes: 1 addition & 1 deletion anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2190,7 +2190,7 @@ impl EthApi {
request: EthTransactionRequest,
nonce: U256,
) -> Result<TypedTransactionRequest> {
let chain_id = self.chain_id();
let chain_id = request.chain_id.map(|c| c.as_u64()).unwrap_or_else(|| self.chain_id());
let max_fee_per_gas = request.max_fee_per_gas;
let gas_price = request.gas_price;

Expand Down

0 comments on commit d70aea4

Please sign in to comment.