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

FIX: Accept 'input' or 'data', (but not both), in transaction request #2697

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading