From fd3cdaa117541aaa9dcb875e18cef9afdac18b30 Mon Sep 17 00:00:00 2001 From: EmperorOrokuSaki Date: Fri, 12 Apr 2024 14:05:57 +0400 Subject: [PATCH 1/2] refactor: make blockId optional in getCode and getUncle --- crates/provider/src/provider.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/provider/src/provider.rs b/crates/provider/src/provider.rs index 4d46ae8c38a..8e1f6fae120 100644 --- a/crates/provider/src/provider.rs +++ b/crates/provider/src/provider.rs @@ -766,8 +766,8 @@ pub trait Provider: } /// Gets the bytecode located at the corresponding [Address]. - async fn get_code_at(&self, address: Address, tag: BlockId) -> TransportResult { - self.client().request("eth_getCode", (address, tag)).await + async fn get_code_at(&self, address: Address, tag: Option) -> TransportResult { + self.client().request("eth_getCode", (address, tag.unwrap_or_default())).await } /// Gets a transaction by its [TxHash]. @@ -824,14 +824,20 @@ pub trait Provider: } /// Gets an uncle block through the tag [BlockId] and index [U64]. - async fn get_uncle(&self, tag: BlockId, idx: U64) -> TransportResult> { - match tag { - BlockId::Hash(hash) => { - self.client().request("eth_getUncleByBlockHashAndIndex", (hash, idx)).await - } - BlockId::Number(number) => { - self.client().request("eth_getUncleByBlockNumberAndIndex", (number, idx)).await + async fn get_uncle(&self, tag: Option, idx: U64) -> TransportResult> { + if let Some(tag) = tag { + match tag { + BlockId::Hash(hash) => { + self.client().request("eth_getUncleByBlockHashAndIndex", (hash, idx)).await + } + BlockId::Number(number) => { + self.client().request("eth_getUncleByBlockNumberAndIndex", (number, idx)).await + } } + } else { + self.client() + .request("eth_getUncleByBlockNumberAndIndex", (tag.unwrap_or_default(), idx)) + .await } } @@ -1385,10 +1391,7 @@ mod tests { // Set the code let addr = Address::with_last_byte(16); provider.set_code(addr, "0xbeef").await.unwrap(); - let _code = provider - .get_code_at(addr, BlockId::Number(alloy_rpc_types::BlockNumberOrTag::Latest)) - .await - .unwrap(); + let _code = provider.get_code_at(addr, None).await.unwrap(); } #[tokio::test] From a4fd0dae58e954827410f3fbf45a72f5b66ba79f Mon Sep 17 00:00:00 2001 From: EmperorOrokuSaki Date: Mon, 15 Apr 2024 10:42:18 +0400 Subject: [PATCH 2/2] feat: change all Option types to BlockId --- crates/contract/src/call.rs | 6 +- crates/provider/src/fillers/gas.rs | 8 +-- crates/provider/src/fillers/nonce.rs | 4 +- crates/provider/src/provider.rs | 89 +++++++++++----------------- 4 files changed, 43 insertions(+), 64 deletions(-) diff --git a/crates/contract/src/call.rs b/crates/contract/src/call.rs index bebcc70fe3a..e072601457d 100644 --- a/crates/contract/src/call.rs +++ b/crates/contract/src/call.rs @@ -190,7 +190,7 @@ impl CallDecoder for () { #[must_use = "call builders do nothing unless you `.call`, `.send`, or `.await` them"] pub struct CallBuilder { request: N::TransactionRequest, - block: Option, + block: BlockId, state: Option, /// The provider. // NOTE: This is public due to usage in `sol!`, please avoid changing it. @@ -262,7 +262,7 @@ impl, D: CallDecoder, N: Network> CallBu request: ::default().with_input(input), decoder, provider, - block: None, + block: BlockId::default(), state: None, transport: PhantomData, } @@ -322,7 +322,7 @@ impl, D: CallDecoder, N: Network> CallBu /// Sets the `block` field for sending the tx to the chain pub const fn block(mut self, block: BlockId) -> Self { - self.block = Some(block); + self.block = block; self } diff --git a/crates/provider/src/fillers/gas.rs b/crates/provider/src/fillers/gas.rs index d860c10e50f..c3327c4ee4a 100644 --- a/crates/provider/src/fillers/gas.rs +++ b/crates/provider/src/fillers/gas.rs @@ -6,7 +6,7 @@ use crate::{ }; use alloy_json_rpc::RpcError; use alloy_network::{Network, TransactionBuilder}; -use alloy_rpc_types::BlockNumberOrTag; +use alloy_rpc_types::{BlockId, BlockNumberOrTag}; use alloy_transport::{Transport, TransportResult}; use futures::FutureExt; @@ -84,7 +84,7 @@ impl GasFiller { let gas_limit_fut = if let Some(gas_limit) = tx.gas_limit() { async move { Ok(gas_limit) }.left_future() } else { - async { provider.estimate_gas(tx, None).await }.right_future() + async { provider.estimate_gas(tx, BlockId::default()).await }.right_future() }; let (gas_price, gas_limit) = futures::try_join!(gas_price_fut, gas_limit_fut)?; @@ -105,7 +105,7 @@ impl GasFiller { let gas_limit_fut = if let Some(gas_limit) = tx.gas_limit() { async move { Ok(gas_limit) }.left_future() } else { - async { provider.estimate_gas(tx, None).await }.right_future() + async { provider.estimate_gas(tx, BlockId::default()).await }.right_future() }; let eip1559_fees_fut = if let (Some(max_fee_per_gas), Some(max_priority_fee_per_gas)) = @@ -135,7 +135,7 @@ impl GasFiller { let gas_limit_fut = if let Some(gas_limit) = tx.gas_limit() { async move { Ok(gas_limit) }.left_future() } else { - async { provider.estimate_gas(tx, None).await }.right_future() + async { provider.estimate_gas(tx, BlockId::default()).await }.right_future() }; let eip1559_fees_fut = if let (Some(max_fee_per_gas), Some(max_priority_fee_per_gas)) = diff --git a/crates/provider/src/fillers/nonce.rs b/crates/provider/src/fillers/nonce.rs index 4218ea1a8af..a5fa8a09325 100644 --- a/crates/provider/src/fillers/nonce.rs +++ b/crates/provider/src/fillers/nonce.rs @@ -5,6 +5,7 @@ use crate::{ }; use alloy_network::{Network, TransactionBuilder}; use alloy_primitives::Address; +use alloy_rpc_types::BlockId; use alloy_transport::{Transport, TransportResult}; use dashmap::DashMap; use std::sync::Arc; @@ -103,7 +104,8 @@ impl NonceFiller { } None => { // initialize the nonce if we haven't seen this account before - let initial_nonce = provider.get_transaction_count(from, None).await?; + let initial_nonce = + provider.get_transaction_count(from, BlockId::default()).await?; *nonce = Some(initial_nonce); Ok(initial_nonce) } diff --git a/crates/provider/src/provider.rs b/crates/provider/src/provider.rs index 8e1f6fae120..d37cdc83191 100644 --- a/crates/provider/src/provider.rs +++ b/crates/provider/src/provider.rs @@ -627,13 +627,9 @@ pub trait Provider: /// Gets the transaction count (AKA "nonce") of the corresponding address. #[doc(alias = "get_nonce")] #[doc(alias = "get_account_nonce")] - async fn get_transaction_count( - &self, - address: Address, - tag: Option, - ) -> TransportResult { + async fn get_transaction_count(&self, address: Address, tag: BlockId) -> TransportResult { self.client() - .request("eth_getTransactionCount", (address, tag.unwrap_or_default())) + .request("eth_getTransactionCount", (address, tag)) .await .map(|count: U64| count.to::()) } @@ -714,13 +710,8 @@ pub trait Provider: } /// Gets the balance of the account at the specified tag, which defaults to latest. - async fn get_balance(&self, address: Address, tag: Option) -> TransportResult { - self.client() - .request( - "eth_getBalance", - (address, tag.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest))), - ) - .await + async fn get_balance(&self, address: Address, tag: BlockId) -> TransportResult { + self.client().request("eth_getBalance", (address, tag)).await } /// Gets a block by either its hash, tag, or number, with full transactions or only hashes. @@ -760,14 +751,14 @@ pub trait Provider: &self, address: Address, key: U256, - tag: Option, + tag: BlockId, ) -> TransportResult { - self.client().request("eth_getStorageAt", (address, key, tag.unwrap_or_default())).await + self.client().request("eth_getStorageAt", (address, key, tag)).await } /// Gets the bytecode located at the corresponding [Address]. - async fn get_code_at(&self, address: Address, tag: Option) -> TransportResult { - self.client().request("eth_getCode", (address, tag.unwrap_or_default())).await + async fn get_code_at(&self, address: Address, tag: BlockId) -> TransportResult { + self.client().request("eth_getCode", (address, tag)).await } /// Gets a transaction by its [TxHash]. @@ -824,20 +815,14 @@ pub trait Provider: } /// Gets an uncle block through the tag [BlockId] and index [U64]. - async fn get_uncle(&self, tag: Option, idx: U64) -> TransportResult> { - if let Some(tag) = tag { - match tag { - BlockId::Hash(hash) => { - self.client().request("eth_getUncleByBlockHashAndIndex", (hash, idx)).await - } - BlockId::Number(number) => { - self.client().request("eth_getUncleByBlockNumberAndIndex", (number, idx)).await - } + async fn get_uncle(&self, tag: BlockId, idx: U64) -> TransportResult> { + match tag { + BlockId::Hash(hash) => { + self.client().request("eth_getUncleByBlockHashAndIndex", (hash, idx)).await + } + BlockId::Number(number) => { + self.client().request("eth_getUncleByBlockNumberAndIndex", (number, idx)).await } - } else { - self.client() - .request("eth_getUncleByBlockNumberAndIndex", (tag.unwrap_or_default(), idx)) - .await } } @@ -847,12 +832,8 @@ pub trait Provider: } /// Execute a smart contract call with a transaction request, without publishing a transaction. - async fn call( - &self, - tx: &N::TransactionRequest, - block: Option, - ) -> TransportResult { - self.client().request("eth_call", (tx, block.unwrap_or_default())).await + async fn call(&self, tx: &N::TransactionRequest, block: BlockId) -> TransportResult { + self.client().request("eth_call", (tx, block)).await } /// Execute a smart contract call with a transaction request and state overrides, without @@ -864,10 +845,10 @@ pub trait Provider: async fn call_with_overrides( &self, tx: &N::TransactionRequest, - block: Option, + block: BlockId, state: StateOverride, ) -> TransportResult { - self.client().request("eth_call", (tx, block.unwrap_or_default(), state)).await + self.client().request("eth_call", (tx, block, state)).await } /// Returns a collection of historical gas information [FeeHistory] which @@ -886,16 +867,12 @@ pub trait Provider: async fn estimate_gas( &self, tx: &N::TransactionRequest, - block: Option, + block: BlockId, ) -> TransportResult { - if let Some(block_id) = block { - self.client() - .request("eth_estimateGas", (tx, block_id)) - .await - .map(|gas: U128| gas.to::()) - } else { - self.client().request("eth_estimateGas", (tx,)).await.map(|gas: U128| gas.to::()) - } + self.client() + .request("eth_estimateGas", (tx, block)) + .await + .map(|gas: U128| gas.to::()) } /// Estimates the EIP1559 `maxFeePerGas` and `maxPriorityFeePerGas` fields. @@ -942,9 +919,9 @@ pub trait Provider: &self, address: Address, keys: Vec, - block: Option, + block: BlockId, ) -> TransportResult { - self.client().request("eth_getProof", (address, keys, block.unwrap_or_default())).await + self.client().request("eth_getProof", (address, keys, block)).await } /// Create an [EIP-2930] access list. @@ -953,9 +930,9 @@ pub trait Provider: async fn create_access_list( &self, request: &N::TransactionRequest, - block: Option, + block: BlockId, ) -> TransportResult { - self.client().request("eth_createAccessList", (request, block.unwrap_or_default())).await + self.client().request("eth_createAccessList", (request, block)).await } /// Executes the given transaction and returns a number of possible traces. @@ -967,7 +944,7 @@ pub trait Provider: &self, request: &N::TransactionRequest, trace_type: &[TraceType], - block: Option, + block: BlockId, ) -> TransportResult { self.client().request("trace_call", (request, trace_type, block)).await } @@ -983,7 +960,7 @@ pub trait Provider: async fn trace_call_many( &self, request: &[(N::TransactionRequest, Vec)], - block: Option, + block: BlockId, ) -> TransportResult { self.client().request("trace_callMany", (request, block)).await } @@ -1291,7 +1268,7 @@ mod tests { let count = provider .get_transaction_count( address!("328375e18E7db8F1CA9d9bA8bF3E9C94ee34136A"), - Some(BlockNumberOrTag::Latest.into()), + BlockNumberOrTag::Latest.into(), ) .await .unwrap(); @@ -1391,7 +1368,7 @@ mod tests { // Set the code let addr = Address::with_last_byte(16); provider.set_code(addr, "0xbeef").await.unwrap(); - let _code = provider.get_code_at(addr, None).await.unwrap(); + let _code = provider.get_code_at(addr, BlockId::default()).await.unwrap(); } #[tokio::test] @@ -1400,7 +1377,7 @@ mod tests { let (provider, _anvil) = spawn_anvil(); let addr = Address::with_last_byte(16); - let storage = provider.get_storage_at(addr, U256::ZERO, None).await.unwrap(); + let storage = provider.get_storage_at(addr, U256::ZERO, BlockId::default()).await.unwrap(); assert_eq!(storage, U256::ZERO); }