Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(provider): support ethCall optional blockId serialization #900

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ alloy-transport-http = { workspace = true, features = ["reqwest"] }

itertools.workspace = true
reqwest.workspace = true
serde_json = { workspace = true, features = ["preserve_order"] }
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
tokio = { workspace = true, features = ["macros"] }
tracing-subscriber = { workspace = true, features = ["fmt"] }
tempfile.workspace = true
Expand Down
73 changes: 68 additions & 5 deletions crates/provider/src/provider/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ struct EthCallParams<'req, 'state, N: Network> {

impl<N: Network> serde::Serialize for EthCallParams<'_, '_, N> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let len = 1 + self.block.is_some() as usize + self.overrides.is_some() as usize;
let len = if self.overrides.is_some() { 3 } else { 2 };

let mut seq = serializer.serialize_seq(Some(len))?;
seq.serialize_element(&self.data)?;

if let Some(block) = self.block {
seq.serialize_element(&block)?;
}

if let Some(overrides) = self.overrides {
seq.serialize_element(&self.block.unwrap_or_default())?;
seq.serialize_element(overrides)?;
} else if let Some(block) = self.block {
seq.serialize_element(&block)?;
}

seq.end()
Expand Down Expand Up @@ -260,3 +259,67 @@ where
})
}
}

#[cfg(test)]

DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
mod test {
use super::*;
use alloy_eips::BlockNumberOrTag;
use alloy_network::{Ethereum, TransactionBuilder};
use alloy_primitives::{address, U256};
use alloy_rpc_types_eth::{state::StateOverride, TransactionRequest};

#[test]
fn test_serialize_eth_call_params() {
let alice = address!("0000000000000000000000000000000000000001");
let bob = address!("0000000000000000000000000000000000000002");
let data = TransactionRequest::default()
.with_from(alice)
.with_to(bob)
.with_nonce(0)
.with_chain_id(1)
.value(U256::from(100))
.with_gas_limit(21_000)
.with_max_priority_fee_per_gas(1_000_000_000)
.with_max_fee_per_gas(20_000_000_000);

let block = BlockId::Number(BlockNumberOrTag::Number(1));
let overrides = StateOverride::default();

// Expected: [data]
let params: EthCallParams<'_, '_, Ethereum> =
EthCallParams { data: &data, block: None, overrides: None };

assert_eq!(params.data, &data);
assert_eq!(params.block, None);
assert_eq!(params.overrides, None);
assert_eq!(serde_json::to_string(&params).unwrap(), "[{\"from\":\"0x0000000000000000000000000000000000000001\",\"to\":\"0x0000000000000000000000000000000000000002\",\"maxFeePerGas\":\"0x4a817c800\",\"maxPriorityFeePerGas\":\"0x3b9aca00\",\"gas\":\"0x5208\",\"value\":\"0x64\",\"nonce\":\"0x0\",\"chainId\":\"0x1\"}]");

// Expected: [data, block, overrides]
let params: EthCallParams<'_, '_, Ethereum> =
EthCallParams { data: &data, block: Some(block), overrides: Some(&overrides) };

assert_eq!(params.data, &data);
assert_eq!(params.block, Some(block));
assert_eq!(params.overrides, Some(&overrides));
assert_eq!(serde_json::to_string(&params).unwrap(), "[{\"from\":\"0x0000000000000000000000000000000000000001\",\"to\":\"0x0000000000000000000000000000000000000002\",\"maxFeePerGas\":\"0x4a817c800\",\"maxPriorityFeePerGas\":\"0x3b9aca00\",\"gas\":\"0x5208\",\"value\":\"0x64\",\"nonce\":\"0x0\",\"chainId\":\"0x1\"},\"0x1\",{}]");

// Expected: [data, (default), overrides]
let params: EthCallParams<'_, '_, Ethereum> =
EthCallParams { data: &data, block: None, overrides: Some(&overrides) };

assert_eq!(params.data, &data);
assert_eq!(params.block, None);
assert_eq!(params.overrides, Some(&overrides));
assert_eq!(serde_json::to_string(&params).unwrap(), "[{\"from\":\"0x0000000000000000000000000000000000000001\",\"to\":\"0x0000000000000000000000000000000000000002\",\"maxFeePerGas\":\"0x4a817c800\",\"maxPriorityFeePerGas\":\"0x3b9aca00\",\"gas\":\"0x5208\",\"value\":\"0x64\",\"nonce\":\"0x0\",\"chainId\":\"0x1\"},\"latest\",{}]");

// Expected: [data, block]
let params: EthCallParams<'_, '_, Ethereum> =
EthCallParams { data: &data, block: Some(block), overrides: None };

assert_eq!(params.data, &data);
assert_eq!(params.block, Some(block));
assert_eq!(params.overrides, None);
assert_eq!(serde_json::to_string(&params).unwrap(), "[{\"from\":\"0x0000000000000000000000000000000000000001\",\"to\":\"0x0000000000000000000000000000000000000002\",\"maxFeePerGas\":\"0x4a817c800\",\"maxPriorityFeePerGas\":\"0x3b9aca00\",\"gas\":\"0x5208\",\"value\":\"0x64\",\"nonce\":\"0x0\",\"chainId\":\"0x1\"},\"0x1\"]");
}
}
Loading