Skip to content

Commit

Permalink
refactor: finish converting RPC methods
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed May 4, 2024
1 parent b5245de commit 9732eca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
3 changes: 2 additions & 1 deletion crates/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub use heart::{PendingTransaction, PendingTransactionBuilder, PendingTransactio

mod provider;
pub use provider::{
EthCall, FilterPollerBuilder, Provider, RootProvider, RpcWithBlock, SendableTx, WalletProvider,
EthCall, FilterPollerBuilder, Provider, RootProvider, RpcWithBlock, SendableTx, TraceCallList,
WalletProvider,
};

pub mod utils;
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod sendable;
pub use sendable::SendableTx;

mod r#trait;
pub use r#trait::{FilterPollerBuilder, Provider};
pub use r#trait::{FilterPollerBuilder, Provider, TraceCallList};

mod wallet;
pub use wallet::WalletProvider;
Expand Down
31 changes: 16 additions & 15 deletions crates/provider/src/provider/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use std::borrow::Cow;
/// See [`PollerBuilder`] for more details.
pub type FilterPollerBuilder<T, R> = PollerBuilder<T, (U256,), Vec<R>>;

/// List of trace calls for use with [`Provider::trace_call_many`]
pub type TraceCallList<'a, N> = &'a [(<N as Network>::TransactionRequest, Vec<TraceType>)];

// todo: adjust docs
// todo: reorder
/// Provider is parameterized with a network and a transport. The default
Expand Down Expand Up @@ -508,8 +511,8 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
}

/// Gets the balance of the account at the specified tag, which defaults to latest.
async fn get_balance(&self, address: Address, tag: BlockId) -> TransportResult<U256> {
self.client().request("eth_getBalance", (address, tag)).await
fn get_balance(&self, address: Address) -> RpcWithBlock<T, Address, U256> {
RpcWithBlock::new(self.weak_client(), "eth_getBalance", address)
}

/// Gets a block by either its hash, tag, or number, with full transactions or only hashes.
Expand Down Expand Up @@ -545,18 +548,17 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
}

/// Gets the specified storage value from [Address].
async fn get_storage_at(
fn get_storage_at(
&self,
address: Address,
key: U256,
tag: BlockId,
) -> TransportResult<StorageValue> {
self.client().request("eth_getStorageAt", (address, key, tag)).await
) -> RpcWithBlock<T, (Address, U256), StorageValue> {
RpcWithBlock::new(self.weak_client(), "eth_getStorageAt", (address, key))
}

/// Gets the bytecode located at the corresponding [Address].
async fn get_code_at(&self, address: Address, tag: BlockId) -> TransportResult<Bytes> {
self.client().request("eth_getCode", (address, tag)).await
fn get_code_at(&self, address: Address) -> RpcWithBlock<T, Address, Bytes> {
RpcWithBlock::new(self.weak_client(), "eth_getCode", address)
}

/// Gets a transaction by its [TxHash].
Expand Down Expand Up @@ -789,12 +791,11 @@ pub trait Provider<T: Transport + Clone = BoxTransport, N: Network = Ethereum>:
/// # Note
///
/// Not all nodes support this call.
async fn trace_call_many(
fn trace_call_many<'a>(
&self,
request: &[(N::TransactionRequest, Vec<TraceType>)],
block: BlockId,
) -> TransportResult<TraceResults> {
self.client().request("trace_callMany", (request, block)).await
request: TraceCallList<'a, N>,
) -> RpcWithBlock<T, TraceCallList<'a, N>, TraceResults> {
RpcWithBlock::new(self.weak_client(), "trace_callMany", request)
}

// todo: move to extension trait
Expand Down Expand Up @@ -1170,15 +1171,15 @@ 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::default()).await.unwrap();
let _code = provider.get_code_at(addr).await.unwrap();
}

#[tokio::test]
async fn gets_storage_at() {
init_tracing();
let provider = ProviderBuilder::new().on_anvil();
let addr = Address::with_last_byte(16);
let storage = provider.get_storage_at(addr, U256::ZERO, BlockId::default()).await.unwrap();
let storage = provider.get_storage_at(addr, U256::ZERO).await.unwrap();
assert_eq!(storage, U256::ZERO);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/provider/with_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
}
}

/// A future for [`BlockIdRpc`]. Simple wrapper around [`RpcCall`].
/// A future for [`RpcWithBlock`]. Simple wrapper around [`RpcCall`].
#[derive(Debug, Clone)]
pub struct RpcWithBlockFut<T, Params, Resp, Output, Map>
where
Expand Down

0 comments on commit 9732eca

Please sign in to comment.