Skip to content

Commit 2ef21ce

Browse files
authored
feat(cast): apply network precompiles for cast run and call (#11720)
feat(cast): apply precompiles for cast run and call
1 parent 91a4b2d commit 2ef21ce

File tree

10 files changed

+62
-7
lines changed

10 files changed

+62
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/anvil/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ impl NodeConfig {
592592
pub fn set_chain_id(&mut self, chain_id: Option<impl Into<u64>>) {
593593
self.chain_id = chain_id.map(Into::into);
594594
let chain_id = self.get_chain_id();
595+
self.networks.with_chain_id(chain_id);
595596
self.genesis_accounts.iter_mut().for_each(|wallet| {
596597
*wallet = wallet.clone().with_chain_id(Some(chain_id));
597598
});

crates/cast/src/cmd/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl CallArgs {
256256

257257
let create2_deployer = evm_opts.create2_deployer;
258258
let (mut env, fork, chain, networks) =
259-
TracingExecutor::get_fork_material(&config, evm_opts).await?;
259+
TracingExecutor::get_fork_material(&mut config, evm_opts).await?;
260260

261261
// modify settings that usually set in eth_call
262262
env.evm_env.cfg_env.disable_block_gas_limit = true;

crates/cast/src/cmd/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl RunArgs {
158158

159159
let create2_deployer = evm_opts.create2_deployer;
160160
let (mut env, fork, chain, networks) =
161-
TracingExecutor::get_fork_material(&config, evm_opts).await?;
161+
TracingExecutor::get_fork_material(&mut config, evm_opts).await?;
162162
let mut evm_version = self.evm_version;
163163

164164
env.evm_env.cfg_env.disable_block_gas_limit = self.disable_block_gas_limit;

crates/cast/tests/cli/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,3 +4062,25 @@ casttest!(abi_encode_event_dynamic_indexed, |_prj, cmd| {
40624062
40634063
"#]]);
40644064
});
4065+
4066+
// Test cast run Celo transfer with precompiles.
4067+
casttest!(run_celo_with_precompiles, |_prj, cmd| {
4068+
let rpc = next_rpc_endpoint(NamedChain::Celo);
4069+
cmd.args([
4070+
"run",
4071+
"0xa652b9f41bb1a617ea6b2835b3316e79f0f21b8264e7bcd20e57c4092a70a0f6",
4072+
"--rpc-url",
4073+
rpc.as_str(),
4074+
])
4075+
.assert_success()
4076+
.stdout_eq(str![[r#"
4077+
Executing previous transactions from the block.
4078+
Traces:
4079+
[17776] 0x471EcE3750Da237f93B8E339c536989b8978a438::transfer(0xD2eB2d37d238Caeff39CFA36A013299C6DbAC56A, 138000000000000000 [1.38e17])
4080+
├─ [12370] 0xFeA1B35f1D5f2A58532a70e7A32e6F2D3Bc4F7B1::transfer(0xD2eB2d37d238Caeff39CFA36A013299C6DbAC56A, 138000000000000000 [1.38e17]) [delegatecall]
4081+
│ ├─ [9000] CELO_TRANSFER_PRECOMPILE::00000000(00000000000000008106680ba7095cfd8f4351a8b7041da3060afb83000000000000000000000000d2eb2d37d238caeff39cfa36a013299c6dbac56a00000000000000000000000000000000000000000000000001ea4644d3010000)
4082+
│ │ └─ ← [Return]
4083+
...
4084+
4085+
"#]]);
4086+
});

crates/evm/evm/src/executors/trace.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl TracingExecutor {
7777

7878
/// uses the fork block number from the config
7979
pub async fn get_fork_material(
80-
config: &Config,
80+
config: &mut Config,
8181
mut evm_opts: EvmOpts,
8282
) -> eyre::Result<(Env, Option<CreateFork>, Option<Chain>, NetworkConfigs)> {
8383
evm_opts.fork_url = Some(config.get_rpc_url_or_localhost_http()?.into_owned());
@@ -86,8 +86,10 @@ impl TracingExecutor {
8686
let env = evm_opts.evm_env().await?;
8787

8888
let fork = evm_opts.get_fork(config, env.clone());
89+
let networks = evm_opts.networks.with_chain_id(env.evm_env.cfg_env.chain_id);
90+
config.labels.extend(networks.precompiles_label());
8991

90-
Ok((env, fork, evm_opts.get_remote_chain_id().await, evm_opts.networks))
92+
Ok((env, fork, evm_opts.get_remote_chain_id().await, networks))
9193
}
9294
}
9395

crates/evm/networks/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ repository.workspace = true
1414
workspace = true
1515

1616
[dependencies]
17+
alloy-chains.workspace = true
1718
alloy-evm.workspace = true
1819
alloy-primitives = { workspace = true, features = [
1920
"serde",

crates/evm/networks/src/celo/transfer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use alloy_evm::precompiles::{DynPrecompile, PrecompileInput};
1717
use alloy_primitives::{Address, U256, address};
1818
use revm::precompile::{PrecompileError, PrecompileId, PrecompileOutput, PrecompileResult};
1919

20+
/// Label of the Celo transfer precompile to display in traces.
21+
pub const CELO_TRANSFER_LABEL: &str = "CELO_TRANSFER_PRECOMPILE";
22+
2023
/// Address of the Celo transfer precompile.
2124
pub const CELO_TRANSFER_ADDRESS: Address = address!("0x00000000000000000000000000000000000000fd");
2225

crates/evm/networks/src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
//!
33
//! Foundry EVM network custom precompiles.
44
5-
use crate::celo::transfer::{CELO_TRANSFER_ADDRESS, PRECOMPILE_ID_CELO_TRANSFER};
5+
use crate::celo::transfer::{
6+
CELO_TRANSFER_ADDRESS, CELO_TRANSFER_LABEL, PRECOMPILE_ID_CELO_TRANSFER,
7+
};
8+
use alloy_chains::NamedChain;
69
use alloy_evm::precompiles::PrecompilesMap;
7-
use alloy_primitives::Address;
10+
use alloy_primitives::{Address, map::AddressHashMap};
811
use clap::Parser;
912
use serde::{Deserialize, Serialize};
1013
use std::collections::BTreeMap;
@@ -33,6 +36,13 @@ impl NetworkConfigs {
3336
Self { optimism: true, ..Default::default() }
3437
}
3538

39+
pub fn with_chain_id(mut self, chain_id: u64) -> Self {
40+
if let Ok(NamedChain::Celo | NamedChain::CeloSepolia) = NamedChain::try_from(chain_id) {
41+
self.celo = true;
42+
}
43+
self
44+
}
45+
3646
/// Inject precompiles for configured networks.
3747
pub fn inject_precompiles(self, precompiles: &mut PrecompilesMap) {
3848
if self.celo {
@@ -42,6 +52,15 @@ impl NetworkConfigs {
4252
}
4353
}
4454

55+
/// Returns precompiles label for configured networks, to be used in traces.
56+
pub fn precompiles_label(self) -> AddressHashMap<String> {
57+
let mut labels = AddressHashMap::default();
58+
if self.celo {
59+
labels.insert(CELO_TRANSFER_ADDRESS, CELO_TRANSFER_LABEL.to_string());
60+
}
61+
labels
62+
}
63+
4564
/// Returns precompiles for configured networks.
4665
pub fn precompiles(self) -> BTreeMap<String, Address> {
4766
let mut precompiles = BTreeMap::new();

crates/test-utils/src/rpc.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
use foundry_config::{
44
NamedChain,
5-
NamedChain::{Arbitrum, Base, BinanceSmartChainTestnet, Mainnet, Optimism, Polygon, Sepolia},
5+
NamedChain::{
6+
Arbitrum, Base, BinanceSmartChainTestnet, Celo, Mainnet, Optimism, Polygon, Sepolia,
7+
},
68
};
79
use rand::seq::SliceRandom;
810
use std::sync::{
@@ -150,6 +152,10 @@ fn next_url(is_ws: bool, chain: NamedChain) -> String {
150152
return "https://bsc-testnet-rpc.publicnode.com".to_string();
151153
}
152154

155+
if matches!(chain, Celo) {
156+
return "https://celo.drpc.org".to_string();
157+
}
158+
153159
let reth_works = true;
154160
let domain = if reth_works && matches!(chain, Mainnet) {
155161
*next(if is_ws { &WS_DOMAINS } else { &HTTP_DOMAINS })

0 commit comments

Comments
 (0)