Skip to content

added time override #10727

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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
22 changes: 20 additions & 2 deletions crates/cast/src/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_ens::NameOrAddress;
use alloy_primitives::{Address, Bytes, TxKind, U256};
use alloy_rpc_types::{
state::{StateOverride, StateOverridesBuilder},
BlockId, BlockNumberOrTag,
BlockId, BlockNumberOrTag, BlockOverrides,
};
use clap::Parser;
use eyre::Result;
Expand Down Expand Up @@ -148,6 +148,10 @@ pub struct CallArgs {
/// Format: address:slot:value
#[arg(long = "override-state-diff", value_name = "ADDRESS:SLOT:VALUE")]
pub state_diff_overrides: Option<Vec<String>>,

/// Override the block timestamp.
#[arg(long = "block.time", value_name = "TIME")]
pub block_time: Option<u64>,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -180,6 +184,7 @@ impl CallArgs {
let evm_opts = figment.extract::<EvmOpts>()?;
let mut config = Config::from_provider(figment)?.sanitized();
let state_overrides = self.get_state_overrides()?;
let block_overrides = self.get_block_overrides()?;

let Self {
to,
Expand Down Expand Up @@ -308,7 +313,9 @@ impl CallArgs {

sh_println!(
"{}",
Cast::new(provider).call(&tx, func.as_ref(), block, state_overrides).await?
Cast::new(provider)
.call(&tx, func.as_ref(), block, state_overrides, block_overrides)
.await?
)?;

Ok(())
Expand Down Expand Up @@ -368,6 +375,17 @@ impl CallArgs {

Ok(Some(state_overrides_builder.build()))
}

/// Parse state overrides from command line arguments.
pub fn get_block_overrides(&self) -> eyre::Result<Option<BlockOverrides>> {
// Early return if no override set - <https://github.com/foundry-rs/foundry/issues/10705>
if self.block_time.as_ref().is_none() {
return Ok(None);
}

let block_overrides = BlockOverrides::default();
Ok(Some(block_overrides.with_time(self.block_time.unwrap())))
}
}

impl figment::Provider for CallArgs {
Expand Down
8 changes: 5 additions & 3 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use alloy_provider::{
};
use alloy_rlp::Decodable;
use alloy_rpc_types::{
state::StateOverride, BlockId, BlockNumberOrTag, Filter, TransactionRequest,
state::StateOverride, BlockId, BlockNumberOrTag, BlockOverrides, Filter, TransactionRequest,
};
use alloy_serde::WithOtherFields;
use alloy_sol_types::sol;
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
///
/// ```
/// use alloy_primitives::{Address, U256, Bytes};
/// use alloy_rpc_types::{TransactionRequest, state::{StateOverride, AccountOverride}};
/// use alloy_rpc_types::{TransactionRequest, BlockOverrides, state::{StateOverride, AccountOverride}};
/// use alloy_serde::WithOtherFields;
/// use cast::Cast;
/// use alloy_provider::{RootProvider, ProviderBuilder, network::AnyNetwork};
Expand All @@ -135,9 +135,10 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
/// account_override.balance = Some(U256::from(1000));
/// state_override.insert(to, account_override);
/// let state_override_object = StateOverridesBuilder::default().build();
/// let block_override_object = BlockOverrides::default();
///
/// let cast = Cast::new(alloy_provider);
/// let data = cast.call(&tx, None, None, Some(state_override_object)).await?;
/// let data = cast.call(&tx, None, None, Some(state_override_object), Some(block_override_object)).await?;
/// println!("{}", data);
/// # Ok(())
/// # }
Expand All @@ -148,6 +149,7 @@ impl<P: Provider<AnyNetwork>> Cast<P> {
func: Option<&Function>,
block: Option<BlockId>,
state_override: Option<StateOverride>,
_block_override: Option<BlockOverrides>,
) -> Result<String> {
let mut call = self.provider.call(req.clone()).block(block.unwrap_or_default());
if let Some(state_override) = state_override {
Expand Down