Skip to content

feat: added block.time and block.number override in cast #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

Merged
merged 18 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
38 changes: 36 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,12 @@ 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 time field of a block
///
/// Format: block:time
#[arg(long = "override-time", value_name = "BLOCK:TIME")]
pub time_overrides: Option<Vec<String>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can name this just block.time because block overrides arent't that complex, this can be a simple u64

}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -180,6 +186,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 +315,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 +377,23 @@ 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.time_overrides.as_ref()].iter().all(Option::is_none) {
return Ok(None);
}
let mut block_overrides = BlockOverrides::default();

for override_str in self.time_overrides.as_ref().unwrap() {
let (block_number, time) = time_value_override(override_str)?;
block_overrides =
block_overrides.with_number(block_number.parse()?).with_time(time.parse()?);
}

Ok(Some(block_overrides))
}
}

impl figment::Provider for CallArgs {
Expand Down Expand Up @@ -410,6 +436,14 @@ fn address_slot_value_override(address_override: &str) -> Result<(Address, U256,
))
}

/// Parse an override string in the format block:time
fn time_value_override(input: &str) -> Result<(&str, &str), eyre::Report> {
let (block_str, time_str) = input.split_once(':').ok_or_else(|| {
eyre::eyre!("Invalid override `{input}`. Expected format: <block>:<time>")
})?;
Ok((block_str, time_str))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this we can change to a single u64 arg on the cli


#[cfg(test)]
mod tests {
use super::*;
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should now be a setter for BlockOverrides as Option

if let Some(state_override) = state_override {
Expand Down
Loading