Skip to content

Commit

Permalink
chore: remove Cow from TempProvider (#43)
Browse files Browse the repository at this point in the history
* chore: start removing cows

* chore: uncow tempProvider

* Apply suggestions from code review

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>

* switch to single element tuple

---------

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
  • Loading branch information
Evalir and DaniPopes authored Nov 22, 2023
1 parent 20447c0 commit f9d823e
Showing 1 changed file with 38 additions and 41 deletions.
79 changes: 38 additions & 41 deletions crates/providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ use alloy_rpc_client::{ClientBuilder, RpcClient};
use alloy_rpc_types::{
trace::{GethDebugTracingOptions, GethTrace, LocalizedTransactionTrace},
AccessListWithGasUsed, Block, BlockId, BlockNumberOrTag, CallRequest,
EIP1186AccountProofResponse, FeeHistory, Filter, Log, RpcBlockHash, SyncStatus, Transaction,
EIP1186AccountProofResponse, FeeHistory, Filter, Log, SyncStatus, Transaction,
TransactionReceipt,
};
use alloy_transport::{BoxTransport, Transport, TransportErrorKind, TransportResult};
use alloy_transport_http::Http;
use auto_impl::auto_impl;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use thiserror::Error;

#[derive(Debug, Error, Serialize, Deserialize)]
Expand Down Expand Up @@ -280,10 +279,10 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getTransactionCount",
Cow::<(Address, BlockId)>::Owned((
(
address,
tag.unwrap_or(BlockNumberOrTag::Latest.into()),
)),
),
)
.await
}
Expand All @@ -294,7 +293,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_blockNumber", Cow::<()>::Owned(())).await.map(|num: U64| num.to::<u64>())
self.inner.prepare("eth_blockNumber", ()).await.map(|num: U64| num.to::<u64>())
}

/// Gets the balance of the account at the specified tag, which defaults to latest.
Expand All @@ -305,10 +304,10 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getBalance",
Cow::<(Address, BlockId)>::Owned((
(
address,
tag.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)),
)),
),
)
.await
}
Expand All @@ -319,7 +318,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
Self: Sync,
{
self.inner
.prepare("eth_getBlockByHash", Cow::<(BlockHash, bool)>::Owned((hash, full)))
.prepare("eth_getBlockByHash", (hash, full))
.await
}

Expand All @@ -335,7 +334,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getBlockByNumber",
Cow::<(BlockNumberOrTag, bool)>::Owned((number.into(), full)),
(number.into(), full),
)
.await
}
Expand All @@ -345,7 +344,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_chainId", Cow::<()>::Owned(())).await
self.inner.prepare("eth_chainId", ()).await
}

/// Gets the specified storage value from [Address].
Expand All @@ -358,11 +357,11 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getStorageAt",
Cow::<(Address, StorageKey, BlockId)>::Owned((
(
address,
key,
tag.unwrap_or(BlockNumberOrTag::Latest.into()),
)),
),
)
.await
}
Expand All @@ -377,7 +376,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
Self: Sync,
{
self.inner
.prepare("eth_getCode", Cow::<(Address, BlockId)>::Owned((address, tag.into())))
.prepare("eth_getCode", (address, tag.into()))
.await
}

Expand All @@ -389,9 +388,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getTransactionByHash",
// Force alloy-rs/alloy to encode this an array of strings,
// even if we only need to send one hash.
Cow::<Vec<TxHash>>::Owned(vec![hash]),
(hash,),
)
.await
}
Expand All @@ -401,7 +398,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_getLogs", Cow::<Vec<Filter>>::Owned(vec![filter])).await
self.inner.prepare("eth_getLogs", vec![filter]).await
}

/// Gets the accounts in the remote node. This is usually empty unless you're using a local
Expand All @@ -410,15 +407,15 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_accounts", Cow::<()>::Owned(())).await
self.inner.prepare("eth_accounts", ()).await
}

/// Gets the current gas price.
async fn get_gas_price(&self) -> TransportResult<U256>
where
Self: Sync,
{
self.inner.prepare("eth_gasPrice", Cow::<()>::Owned(())).await
self.inner.prepare("eth_gasPrice", ()).await
}

/// Gets a [TransactionReceipt] if it exists, by its [TxHash].
Expand All @@ -429,7 +426,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_getTransactionReceipt", Cow::<Vec<TxHash>>::Owned(vec![hash])).await
self.inner.prepare("eth_getTransactionReceipt", (hash,)).await
}

/// Returns a collection of historical gas information [FeeHistory] which
Expand All @@ -446,11 +443,11 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_feeHistory",
Cow::<(U256, BlockNumberOrTag, Vec<f64>)>::Owned((
(
block_count,
last_block.into(),
reward_percentiles.to_vec(),
)),
reward_percentiles,
),
)
.await
}
Expand All @@ -463,7 +460,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_getBlockReceipts", Cow::<BlockNumberOrTag>::Owned(block)).await
self.inner.prepare("eth_getBlockReceipts", block).await
}

/// Gets an uncle block through the tag [BlockId] and index [U64].
Expand All @@ -481,15 +478,15 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getUncleByBlockHashAndIndex",
Cow::<(RpcBlockHash, U64)>::Owned((hash, idx)),
(hash, idx),
)
.await
}
BlockId::Number(number) => {
self.inner
.prepare(
"eth_getUncleByBlockNumberAndIndex",
Cow::<(BlockNumberOrTag, U64)>::Owned((number, idx)),
(number, idx),
)
.await
}
Expand All @@ -501,7 +498,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("eth_syncing", Cow::<()>::Owned(())).await
self.inner.prepare("eth_syncing", ()).await
}

/// Execute a smart contract call with [CallRequest] without publishing a transaction.
Expand All @@ -512,10 +509,10 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_call",
Cow::<(CallRequest, BlockId)>::Owned((
(
tx,
block.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)),
)),
),
)
.await
}
Expand All @@ -526,20 +523,20 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
Self: Sync,
{
if let Some(block_id) = block {
let params = Cow::<(CallRequest, BlockId)>::Owned((tx, block_id));
let params = (tx, block_id);
self.inner.prepare("eth_estimateGas", params).await
} else {
let params = Cow::<CallRequest>::Owned(tx);
let params = tx;
self.inner.prepare("eth_estimateGas", params).await
}
}

/// Sends an already-signed transaction.
/// Sends an already-signed transaction.
async fn send_raw_transaction(&self, tx: Bytes) -> TransportResult<TxHash>
where
Self: Sync,
{
self.inner.prepare("eth_sendRawTransaction", Cow::<Bytes>::Owned(tx)).await
self.inner.prepare("eth_sendRawTransaction", tx).await
}

/// Estimates the EIP1559 `maxFeePerGas` and `maxPriorityFeePerGas` fields.
Expand Down Expand Up @@ -601,11 +598,11 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_getProof",
Cow::<(Address, Vec<StorageKey>, BlockId)>::Owned((
(
address,
keys,
block.unwrap_or(BlockNumberOrTag::Latest.into()),
)),
),
)
.await
}
Expand All @@ -621,10 +618,10 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"eth_createAccessList",
Cow::<(CallRequest, BlockId)>::Owned((
(
request,
block.unwrap_or(BlockNumberOrTag::Latest.into()),
)),
),
)
.await
}
Expand All @@ -637,7 +634,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("trace_transaction", Cow::<Vec<TxHash>>::Owned(vec![hash])).await
self.inner.prepare("trace_transaction", vec![hash]).await
}

async fn debug_trace_transaction(
Expand All @@ -651,7 +648,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
self.inner
.prepare(
"debug_traceTransaction",
Cow::<(TxHash, GethDebugTracingOptions)>::Owned((hash, trace_options)),
(hash, trace_options),
)
.await
}
Expand All @@ -663,7 +660,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
where
Self: Sync,
{
self.inner.prepare("trace_block", Cow::<BlockNumberOrTag>::Owned(block)).await
self.inner.prepare("trace_block", block).await
}

#[cfg(feature = "anvil")]
Expand All @@ -672,7 +669,7 @@ impl<T: Transport + Clone + Send + Sync> TempProvider for Provider<T> {
Self: Sync,
{
self.inner
.prepare("anvil_setCode", Cow::<(Address, &'static str)>::Owned((address, code)))
.prepare("anvil_setCode", (address, code))
.await
}
}
Expand Down

0 comments on commit f9d823e

Please sign in to comment.