Skip to content
Open
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
11 changes: 6 additions & 5 deletions crates/cheatcodes/src/evm/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_primitives::{B256, U256};
use alloy_provider::Provider;
use alloy_rpc_types::Filter;
use alloy_sol_types::SolValue;
use foundry_common::provider::ProviderBuilder;
use foundry_common::{provider::ProviderBuilder, sema::StructDefinitions};
use foundry_evm_core::{AsEnvMut, ContextExt, fork::CreateFork};

impl Cheatcode for activeForkCall {
Expand Down Expand Up @@ -208,15 +208,15 @@ impl Cheatcode for rpc_0Call {
.database
.active_fork_url()
.ok_or_else(|| fmt_err!("no active fork URL found"))?;
rpc_call(&url, method, params)
rpc_call(&ccx.state.struct_defs, &url, method, params)
}
}

impl Cheatcode for rpc_1Call {
fn apply(&self, state: &mut Cheatcodes) -> Result {
let Self { urlOrAlias, method, params } = self;
let url = state.config.rpc_endpoint(urlOrAlias)?.url()?;
rpc_call(&url, method, params)
rpc_call(&state.struct_defs, &url, method, params)
}
}

Expand Down Expand Up @@ -369,14 +369,15 @@ fn persist_caller(ccx: &mut CheatsCtxt) {
}

/// Performs an Ethereum JSON-RPC request to the given endpoint.
fn rpc_call(url: &str, method: &str, params: &str) -> Result {
fn rpc_call(struct_defs: &StructDefinitions, url: &str, method: &str, params: &str) -> Result {
Copy link
Member

Choose a reason for hiding this comment

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

let's pass along state instead of struct_defs to all necessary functions

let provider = ProviderBuilder::new(url).build()?;
let params_json: serde_json::Value = serde_json::from_str(params)?;
let result =
foundry_common::block_on(provider.raw_request(method.to_string().into(), params_json))
.map_err(|err| fmt_err!("{method:?}: {err}"))?;
let result_as_tokens = convert_to_bytes(
&json_value_to_token(&result).map_err(|err| fmt_err!("failed to parse result: {err}"))?,
&json_value_to_token(struct_defs, &result)
.map_err(|err| fmt_err!("failed to parse result: {err}"))?,
);

Ok(result_as_tokens.abi_encode())
Expand Down
3 changes: 2 additions & 1 deletion crates/cheatcodes/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ fn latest_broadcast(
mod tests {
use super::*;
use crate::CheatsConfig;
use foundry_common::sema::StructDefinitions;
use std::sync::Arc;

fn cheats() -> Cheatcodes {
Expand All @@ -863,7 +864,7 @@ mod tests {
root: PathBuf::from(&env!("CARGO_MANIFEST_DIR")),
..Default::default()
};
Cheatcodes::new(Arc::new(config))
Cheatcodes::new(Arc::new(config), StructDefinitions::default())
Copy link
Member

Choose a reason for hiding this comment

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

let's keep the new signature to the bare minimum required and add builder-style fns for extra stuff

}

#[test]
Expand Down
12 changes: 9 additions & 3 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ use alloy_rpc_types::{
request::{TransactionInput, TransactionRequest},
};
use alloy_sol_types::{SolCall, SolInterface, SolValue};
use foundry_common::{SELECTOR_LEN, TransactionMaybeSigned, evm::Breakpoints};
use foundry_common::{
SELECTOR_LEN, TransactionMaybeSigned, evm::Breakpoints, sema::StructDefinitions,
};
use foundry_evm_core::{
InspectorExt,
abi::Vm::stopExpectSafeMemoryCall,
Expand Down Expand Up @@ -453,6 +455,9 @@ pub struct Cheatcodes {
/// Used to prevent duplicate changes file executing non-committing calls.
pub fs_commit: bool,

/// Struct definitions in the contracts. Used to keep field order when parsing JSON values.
pub struct_defs: StructDefinitions,

/// Serialized JSON values.
// **Note**: both must a BTreeMap to ensure the order of the keys is deterministic.
pub serialized_jsons: BTreeMap<String, BTreeMap<String, Value>>,
Expand Down Expand Up @@ -500,13 +505,13 @@ pub struct Cheatcodes {
// create.
impl Default for Cheatcodes {
fn default() -> Self {
Self::new(Arc::default())
Self::new(Arc::default(), StructDefinitions::default())
}
}

impl Cheatcodes {
/// Creates a new `Cheatcodes` with the given settings.
pub fn new(config: Arc<CheatsConfig>) -> Self {
pub fn new(config: Arc<CheatsConfig>, struct_defs: StructDefinitions) -> Self {
Self {
fs_commit: true,
labels: config.labels.clone(),
Expand Down Expand Up @@ -535,6 +540,7 @@ impl Cheatcodes {
access_list: Default::default(),
test_context: Default::default(),
serialized_jsons: Default::default(),
struct_defs,
eth_deals: Default::default(),
gas_metering: Default::default(),
gas_snapshots: Default::default(),
Expand Down
Loading