Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
FIX: Accept 'input' or 'data', (but not both), in transaction request (
Browse files Browse the repository at this point in the history
…#2697)

* FIX: Accept 'input' or 'data' or both in transaction request

* FMT: Apply formatting

* FIX: Serialize as 'data'

* FIX: Just use alias, not handling duplicate fields
  • Loading branch information
aakoshh authored Dec 8, 2023
1 parent f0e5b19 commit 40cc8cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
9 changes: 5 additions & 4 deletions ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,10 @@ impl Chain {
("https://alpha-blockscout.scroll.io/api", "https://alpha-blockscout.scroll.io/")
}

Metis => {
("https://api.routescan.io/v2/network/mainnet/evm/1088/etherscan/api", "https://explorer.metis.io/")
}
Metis => (
"https://api.routescan.io/v2/network/mainnet/evm/1088/etherscan/api",
"https://explorer.metis.io/",
),

Chiado => {
("https://blockscout.chiadochain.net/api", "https://blockscout.chiadochain.net")
Expand Down Expand Up @@ -599,7 +600,7 @@ impl Chain {
AnvilHardhat | Dev | Morden | MoonbeamDev | FilecoinMainnet => {
// this is explicitly exhaustive so we don't forget to add new urls when adding a
// new chain
return None;
return None
}
};

Expand Down
46 changes: 45 additions & 1 deletion ethers-core/src/types/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Eip1559TransactionRequest {

/// The compiled code of a contract OR the first 4 bytes of the hash of the
/// invoked method signature and encoded parameters. For details see Ethereum Contract ABI
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", alias = "input")]
pub data: Option<Bytes>,

/// Transaction nonce (None for next available nonce)
Expand Down Expand Up @@ -283,3 +283,47 @@ impl From<&Transaction> for Eip1559TransactionRequest {
}
}
}

#[cfg(test)]
mod tests {
use crate::types::Eip1559TransactionRequest;

#[test]
fn test_tx_with_input_or_data() {
for fragment in [
r#" "input":"0x01" "#,
r#" "data":"0x01" "#,
// Note that the `alias` method doesn't support both `data` and `input` appearing
// at the same time, but https://github.com/web3/web3.js/issues/6301 shows that
// there is a recognition doing so should be avoided. Supporting it is possible
// but awkward, and would have to be done for all transaction types with a `data`
// field which is a bit of an overkill.
//
// r#" "data":"0x02", "input":"0x01" "#,
] {
let json = format!(
"{{ \"gas\": \"0x186a0\",
\"maxFeePerGas\": \"0x77359400\",
\"maxPriorityFeePerGas\": \"0x77359400\",
\"nonce\": \"0x2\",
\"to\": \"0x96216849c49358B10257cb55b28eA603c874b05E\",
\"value\": \"0x5af3107a4000\",
\"chainId\": \"0x539\",
\"accessList\": [],
{fragment}
}}"
);

let request = serde_json::from_str::<Eip1559TransactionRequest>(&json)
.unwrap_or_else(|e| panic!("failed to parse request {json}: {e}"));

let data = request.data.clone().expect("data was empty");

assert_eq!(data.to_string(), "0x01");

let json = serde_json::to_string(&request).expect("failed to serialize request");

assert!(json.contains("\"data\":"), "uses data in serialization");
}
}
}
2 changes: 1 addition & 1 deletion ethers-core/src/types/transaction/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct TransactionRequest {

/// The compiled code of a contract OR the first 4 bytes of the hash of the
/// invoked method signature and encoded parameters. For details see Ethereum Contract ABI
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", alias = "input")]
pub data: Option<Bytes>,

/// Transaction nonce (None for next available nonce)
Expand Down
2 changes: 1 addition & 1 deletion ethers-core/src/utils/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ where
return Numeric::from_str(num)
.map(U256::from)
.map(Some)
.map_err(serde::de::Error::custom);
.map_err(serde::de::Error::custom)
}

if let serde_json::Value::Number(num) = val {
Expand Down

0 comments on commit 40cc8cc

Please sign in to comment.