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: eth_callMany #2664

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions ethers-core/src/types/trace/erigon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use ethabi::ethereum_types::U256;
use serde::{Deserialize, Serialize};

use crate::types::{transaction::eip2718::TypedTransaction, BlockNumber, Bytes};

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct EthCallManyBlockOverride {
pub block_number: U256,
}

#[derive(Serialize, Debug, Clone)]
pub struct EthCallManyBundle {
pub transactions: Vec<TypedTransaction>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub block_override: Option<EthCallManyBlockOverride>,
}

#[derive(Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct EthCallManyStateContext {
pub block_number: BlockNumber,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub transaction_index: Option<i32>,
}

#[derive(Serialize, Debug, Clone)]
pub struct EthCallManyBalanceDiff {
pub balance: U256,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have this as Option, in case we need more fields

}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EthCallManyOutputEmpty {}
Comment on lines +32 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant type?


#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EthCallManyOutput {
pub value: Option<Bytes>,
pub error: Option<EthCallManyOutputEmpty>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a string

}
3 changes: 3 additions & 0 deletions ethers-core/src/types/trace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub use filter::*;
mod geth;
pub use geth::*;

mod erigon;
pub use erigon::*;

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
/// Description of the type of trace to make
pub enum TraceType {
Expand Down
15 changes: 14 additions & 1 deletion ethers-providers/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ethers_core::types::{
};
use futures_util::future::join_all;
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
use std::{collections::HashMap, fmt::Debug};
use url::Url;

use crate::{
Expand Down Expand Up @@ -727,6 +727,19 @@ pub trait Middleware: Sync + Send + Debug {
self.inner().txpool_status().await.map_err(MiddlewareError::from_err)
}

/// Simulates a set of bundles
/// Implementation:
/// Ref:
/// [Here](https://github.com/ledgerwatch/erigon/blob/513fd50fa501ab6385dc3f58b18079d806d6ff5a/turbo/jsonrpc/eth_callMany.go#L72-L72)
async fn eth_call_many(
&self,
bundles: Vec<EthCallManyBundle>,
state_context: EthCallManyStateContext,
state_override: Option<HashMap<H160, Option<EthCallManyBalanceDiff>>>,
) -> Result<Vec<Vec<EthCallManyOutput>>, Self::Error> {
self.inner().eth_call_many(bundles, state_context, state_override).await.map_err(MiddlewareError::from_err)
}

// Geth `trace` support

/// After replaying any previous transactions in the same block,
Expand Down
30 changes: 24 additions & 6 deletions ethers-providers/src/rpc/provider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use ethers_core::types::SyncingStatus;

use crate::{
call_raw::CallBuilder,
errors::ProviderError,
Expand All @@ -13,7 +11,7 @@ use crate::{

#[cfg(not(target_arch = "wasm32"))]
use crate::{HttpRateLimitRetryPolicy, RetryClient};
use std::net::Ipv4Addr;
use std::{collections::HashMap, net::Ipv4Addr};

pub use crate::Middleware;

Expand All @@ -24,10 +22,11 @@ use ethers_core::{
types::{
transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed},
Address, Block, BlockId, BlockNumber, BlockTrace, Bytes, Chain, EIP1186ProofResponse,
EthCallManyBalanceDiff, EthCallManyBundle, EthCallManyStateContext, EthCallManyOutput,
FeeHistory, Filter, FilterBlockOption, GethDebugTracingCallOptions,
GethDebugTracingOptions, GethTrace, Log, NameOrAddress, Selector, Signature, Trace,
TraceFilter, TraceType, Transaction, TransactionReceipt, TransactionRequest, TxHash,
TxpoolContent, TxpoolInspect, TxpoolStatus, H256, U256, U64,
GethDebugTracingOptions, GethTrace, Log, NameOrAddress, Selector, Signature, SyncingStatus,
Trace, TraceFilter, TraceType, Transaction, TransactionReceipt, TransactionRequest, TxHash,
TxpoolContent, TxpoolInspect, TxpoolStatus, H160, H256, U256, U64,
},
utils,
};
Expand Down Expand Up @@ -927,6 +926,25 @@ impl<P: JsonRpcClient> Middleware for Provider<P> {
self.request("txpool_status", ()).await
}

async fn eth_call_many(
&self,
bundles: Vec<EthCallManyBundle>,
state_context: EthCallManyStateContext,
state_override: Option<HashMap<H160, Option<EthCallManyBalanceDiff>>>,
) -> Result<Vec<Vec<EthCallManyOutput>>, ProviderError> {
let bundles: Vec<EthCallManyBundle> = bundles
.into_iter()
.map(|tx_bundle| EthCallManyBundle {
transactions: tx_bundle.transactions.into_iter().map(|tx| tx.into()).collect(),
block_override: tx_bundle.block_override,
})
.collect();
let bundles = utils::serialize(&bundles);
let options_state_context = utils::serialize(&state_context);
let options_state_override = utils::serialize(&state_override);
self.request("eth_callMany", [bundles, options_state_context, options_state_override]).await
}

async fn debug_trace_transaction(
&self,
tx_hash: TxHash,
Expand Down
74 changes: 74 additions & 0 deletions examples/transactions/examples/eth_call_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use ethers::{
providers::{Http, Middleware, Provider},
types::{
transaction::eip2718::TypedTransaction, Address, BlockNumber, Bytes,
EthCallManyBalanceDiff, EthCallManyBundle, EthCallManyStateContext, TransactionRequest,
H160, U256,
},
};
use eyre::Result;
use std::{collections::HashMap, str::FromStr};

/// use `eth_callMany` to simulate output
/// requires, a valid endpoint in `RPC_URL` env var that supports `eth_callMany` (Erigon)
/// Example 1: of approving SHIBA INU (token) with Uniswap V2 as spender returns bool
/// Expected output: 0x0000000000000000000000000000000000000000000000000000000000000001
/// Example 2: transferring tokens while not having enough balance
/// Expected output: Empty error object
#[tokio::main]
async fn main() -> Result<()> {
if let Ok(url) = std::env::var("RPC_URL") {
let client = Provider::<Http>::try_from(url)?;
let block_number = client.get_block_number().await.unwrap();
let gas_fees = client.get_gas_price().await.unwrap();
{
let tx = TransactionRequest::new().from(Address::from_str("0xdeadbeef29292929192939494959594933929292").unwrap()).to(Address::from_str("0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce").unwrap()).gas_price(gas_fees).gas("0x7a120").data(Bytes::from_str("0x095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000b3827a7d5189a7b76dac0000").unwrap());
let req = vec![EthCallManyBundle {
transactions: vec![TypedTransaction::Legacy(tx)],
block_override: None,
}];
let mut hashmap: HashMap<H160, Option<EthCallManyBalanceDiff>> = HashMap::new();
hashmap.insert(
"0xDeaDbEEF29292929192939494959594933929292".parse::<H160>().unwrap(),
Some(EthCallManyBalanceDiff {
balance: U256::from(
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
),
}),
);

let state_context = EthCallManyStateContext {
block_number: BlockNumber::Number(block_number),
transaction_index: None,
};
let traces = client.eth_call_many(req, state_context, Some(hashmap)).await?;
println!("{traces:?}");
}

{
let tx = TransactionRequest::new().from(Address::from_str("0xdeadbeef29292929192939494959594933929292").unwrap()).to(Address::from_str("0x95ad61b0a150d79219dcf64e1e6cc01f0b64c4ce").unwrap()).gas_price(gas_fees).gas("0x7a120").data(Bytes::from_str("0xa9059cbb000000000000000000000000deadbeef292929291929394949595949339292920000000000000000000000000000000000000000000000000000000000000005").unwrap());
let req = vec![EthCallManyBundle {
transactions: vec![TypedTransaction::Legacy(tx)],
block_override: None,
}];
let mut hashmap: HashMap<H160, Option<EthCallManyBalanceDiff>> = HashMap::new();
hashmap.insert(
"0xDeaDbEEF29292929192939494959594933929292".parse::<H160>().unwrap(),
Some(EthCallManyBalanceDiff {
balance: U256::from(
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
),
}),
);

let state_context = EthCallManyStateContext {
block_number: BlockNumber::Number(block_number),
transaction_index: None,
};
let traces = client.eth_call_many(req, state_context, Some(hashmap)).await?;
println!("{traces:?}");
}
}

Ok(())
}