Skip to content

Commit

Permalink
feat: add helpers for account overrides (#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 11, 2025
1 parent bd478a9 commit d8c2bfc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
32 changes: 28 additions & 4 deletions crates/contract/src/eth_call.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::{future::IntoFuture, marker::PhantomData};

use crate::{Error, Result};
use alloy_dyn_abi::{DynSolValue, FunctionExt};
use alloy_json_abi::Function;
use alloy_network::Network;
use alloy_primitives::Bytes;
use alloy_rpc_types_eth::{state::StateOverride, BlockId};
use alloy_primitives::{Address, Bytes};
use alloy_rpc_types_eth::{
state::{AccountOverride, StateOverride},
BlockId,
};
use alloy_sol_types::SolCall;

use crate::{Error, Result};

/// Raw coder.
const RAW_CODER: () = ();

Expand Down Expand Up @@ -76,6 +78,28 @@ where
self
}

/// Appends a single [AccountOverride] to the state override.
///
/// Creates a new [`StateOverride`] if none has been set yet.
pub fn account_override(
mut self,
address: Address,
account_overrides: AccountOverride,
) -> Self {
self.inner = self.inner.account_override(address, account_overrides);
self
}
/// Extends the the given [AccountOverride] to the state override.
///
/// Creates a new [`StateOverride`] if none has been set yet.
pub fn account_overrides(
mut self,
overrides: impl IntoIterator<Item = (Address, AccountOverride)>,
) -> Self {
self.inner = self.inner.account_overrides(overrides);
self
}

/// Set the block to use for this call.
pub fn block(mut self, block: BlockId) -> Self {
self.inner = self.inner.block(block);
Expand Down
30 changes: 27 additions & 3 deletions crates/provider/src/provider/eth_call/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::ProviderCall;
use alloy_eips::BlockId;
use alloy_json_rpc::RpcRecv;
use alloy_network::Network;
use alloy_rpc_types_eth::state::StateOverride;
use alloy_primitives::Address;
use alloy_rpc_types_eth::state::{AccountOverride, StateOverride};
use alloy_transport::TransportResult;
use futures::FutureExt;
use std::{borrow::Cow, future::Future, marker::PhantomData, sync::Arc, task::Poll};

use crate::ProviderCall;

mod params;
pub use params::{EthCallManyParams, EthCallParams};

Expand Down Expand Up @@ -239,6 +239,30 @@ where
self
}

/// Appends a single [AccountOverride] to the state override.
///
/// Creates a new [`StateOverride`] if none has been set yet.
pub fn account_override(mut self, address: Address, account_override: AccountOverride) -> Self {
let mut overrides = self.params.overrides.unwrap_or_default();
overrides.to_mut().insert(address, account_override);
self.params.overrides = Some(overrides);

self
}

/// Extends the the given [AccountOverride] to the state override.
///
/// Creates a new [`StateOverride`] if none has been set yet.
pub fn account_overrides(
mut self,
overrides: impl IntoIterator<Item = (Address, AccountOverride)>,
) -> Self {
for (addr, account_override) in overrides.into_iter() {
self = self.account_override(addr, account_override);
}
self
}

/// Set the block to use for this call.
pub const fn block(mut self, block: BlockId) -> Self {
self.params.block = Some(block);
Expand Down

0 comments on commit d8c2bfc

Please sign in to comment.