-
Notifications
You must be signed in to change notification settings - Fork 2k
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
+171
−125
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c3a984a
added time override
Soubhik-10 fd840da
docs
Soubhik-10 215409c
docs
Soubhik-10 9376f47
Merge branch 'master' into cast-block
grandizzy d2a4717
docs
Soubhik-10 5782552
Merge branch 'cast-block' of https://github.com/Soubhik-10/foundry in…
Soubhik-10 b0b78c3
again doc
Soubhik-10 cf6d0c0
block.time as u64
Soubhik-10 148122e
Merge branch 'master' into cast-block
Soubhik-10 3d29e67
block.number
Soubhik-10 32078bd
Merge branch 'master' into cast-block
Soubhik-10 84ba0c4
Merge branch 'master' into cast-block
Soubhik-10 b5f9988
alloy bump
Soubhik-10 c1c9320
fixes
Soubhik-10 6e300cb
typo
Soubhik-10 33d4aa5
cargo update
Soubhik-10 fa235cd
fix test
klkvr 3fbc3a5
apply review
klkvr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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>>, | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
|
@@ -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, | ||
|
@@ -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(()) | ||
|
@@ -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 { | ||
|
@@ -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)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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::*; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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}; | ||
|
@@ -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(()) | ||
/// # } | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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