Skip to content

Commit

Permalink
script: fetch create2_deployer from env or default
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <delweng@gmail.com>
  • Loading branch information
jsvisa committed Nov 7, 2024
1 parent b48c58f commit fcaa2a7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 42 deletions.
7 changes: 4 additions & 3 deletions crates/script/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use foundry_compilers::{
utils::source_files_iter,
ArtifactId, ProjectCompileOutput,
};
use foundry_evm::traces::debug::ContractSources;
use foundry_evm::{constants::get_create2_deployer, traces::debug::ContractSources};
use foundry_linking::Linker;
use std::{path::PathBuf, str::FromStr, sync::Arc};

Expand All @@ -40,9 +40,10 @@ impl BuildData {
/// Links contracts. Uses CREATE2 linking when possible, otherwise falls back to
/// default linking with sender nonce and address.
pub async fn link(self, script_config: &ScriptConfig) -> Result<LinkedBuildData> {
let create2_deployer = get_create2_deployer();
let can_use_create2 = if let Some(fork_url) = &script_config.evm_opts.fork_url {
let provider = try_get_http_provider(fork_url)?;
let deployer_code = provider.get_code_at(script_config.create2_deployer).await?;
let deployer_code = provider.get_code_at(create2_deployer).await?;

!deployer_code.is_empty()
} else {
Expand All @@ -57,7 +58,7 @@ impl BuildData {
self.get_linker()
.link_with_create2(
known_libraries.clone(),
script_config.create2_deployer,
create2_deployer,
script_config.config.create2_library_salt,
&self.target,
)
Expand Down
23 changes: 7 additions & 16 deletions crates/script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use foundry_config::{
Config,
};
use foundry_evm::{
constants::get_create2_deployer,
backend::Backend,
executors::ExecutorBuilder,
inspectors::{
Expand Down Expand Up @@ -71,9 +72,6 @@ mod verify;
// Loads project's figment and merges the build cli arguments into it
foundry_config::merge_impl_figment_convert!(ScriptArgs, opts, evm_opts);

// https://etherscan.io/address/0x4e59b44847b379578588920ca78fbf26c0b4956c#code
const DEPLOYER: &str = "0x4e59b44847b379578588920ca78fbf26c0b4956c";

/// CLI arguments for `forge script`.
#[derive(Clone, Debug, Default, Parser)]
pub struct ScriptArgs {
Expand Down Expand Up @@ -198,9 +196,6 @@ pub struct ScriptArgs {
#[arg(long, env = "ETH_TIMEOUT")]
pub timeout: Option<u64>,

#[arg(long, value_name = "ADDRESS", default_value = DEPLOYER)]
pub create2_deployer: Address,

#[command(flatten)]
pub opts: CoreBuildArgs,

Expand Down Expand Up @@ -228,7 +223,7 @@ impl ScriptArgs {
evm_opts.sender = sender;
}

let script_config = ScriptConfig::new(config, evm_opts, self.create2_deployer).await?;
let script_config = ScriptConfig::new(config, evm_opts).await?;

Ok(PreprocessedState { args: self, script_config, script_wallets })
}
Expand Down Expand Up @@ -409,6 +404,7 @@ impl ScriptArgs {
None => CONTRACT_MAX_SIZE,
};

let create2_deployer = get_create2_deployer();
for (data, to) in result.transactions.iter().flat_map(|txes| {
txes.iter().filter_map(|tx| {
tx.transaction
Expand All @@ -421,7 +417,7 @@ impl ScriptArgs {

// Find if it's a CREATE or CREATE2. Otherwise, skip transaction.
if let Some(TxKind::Call(to)) = to {
if to == self.create2_deployer {
if to == create2_deployer {
// Size of the salt prefix.
offset = 32;
} else {
Expand Down Expand Up @@ -536,18 +532,17 @@ pub struct ScriptConfig {
pub sender_nonce: u64,
/// Maps a rpc url to a backend
pub backends: HashMap<String, Backend>,
pub create2_deployer: Address,
}

impl ScriptConfig {
pub async fn new(config: Config, evm_opts: EvmOpts, create2_deployer: Address) -> Result<Self> {
pub async fn new(config: Config, evm_opts: EvmOpts) -> Result<Self> {
let sender_nonce = if let Some(fork_url) = evm_opts.fork_url.as_ref() {
next_nonce(evm_opts.sender, fork_url).await?
} else {
// dapptools compatibility
1
};
Ok(Self { config, evm_opts, sender_nonce, backends: HashMap::default(), create2_deployer })
Ok(Self { config, evm_opts, sender_nonce, backends: HashMap::default() })
}

pub async fn update_sender(&mut self, sender: Address) -> Result<()> {
Expand Down Expand Up @@ -629,11 +624,7 @@ impl ScriptConfig {
});
}

Ok(ScriptRunner::new(
builder.build(env, db),
self.evm_opts.clone(),
self.create2_deployer.clone(),
))
Ok(ScriptRunner::new(builder.build(env, db), self.evm_opts.clone()))
}
}

Expand Down
14 changes: 7 additions & 7 deletions crates/script/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use eyre::Result;
use foundry_cheatcodes::BroadcastableTransaction;
use foundry_config::Config;
use foundry_evm::{
constants::CALLER,
constants::{get_create2_deployer, CALLER},
executors::{DeployResult, EvmError, ExecutionErr, Executor, RawCallResult},
opts::EvmOpts,
revm::interpreter::{return_ok, InstructionResult},
Expand All @@ -20,12 +20,11 @@ use yansi::Paint;
pub struct ScriptRunner {
pub executor: Executor,
pub evm_opts: EvmOpts,
pub create2_deployer: Address,
}

impl ScriptRunner {
pub fn new(executor: Executor, evm_opts: EvmOpts, create2_deployer: Address) -> Self {
Self { executor, evm_opts, create2_deployer }
pub fn new(executor: Executor, evm_opts: EvmOpts) -> Self {
Self { executor, evm_opts }
}

/// Deploys the libraries and broadcast contract. Calls setUp method if requested.
Expand Down Expand Up @@ -84,8 +83,9 @@ impl ScriptRunner {
})
}),
ScriptPredeployLibraries::Create2(libraries, salt) => {
let create2_deployer = get_create2_deployer();
for library in libraries {
let address = self.create2_deployer.create2_from_code(salt, library.as_ref());
let address = create2_deployer.create2_from_code(salt, library.as_ref());
// Skip if already deployed
if !self.executor.is_empty_code(address)? {
continue;
Expand All @@ -95,7 +95,7 @@ impl ScriptRunner {
.executor
.transact_raw(
self.evm_opts.sender,
self.create2_deployer,
create2_deployer,
calldata.clone().into(),
U256::from(0),
)
Expand All @@ -111,7 +111,7 @@ impl ScriptRunner {
from: Some(self.evm_opts.sender),
input: calldata.into(),
nonce: Some(sender_nonce + library_transactions.len() as u64),
to: Some(TxKind::Call(self.create2_deployer)),
to: Some(TxKind::Call(create2_deployer)),
..Default::default()
}
.into(),
Expand Down
6 changes: 1 addition & 5 deletions crates/script/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ impl PreSimulationState {
let nonce = tx.transaction.nonce().expect("all transactions should have a sender");
let to = tx.transaction.to();

let mut builder = ScriptTransactionBuilder::new(
tx.transaction,
rpc,
self.script_config.create2_deployer.clone(),
);
let mut builder = ScriptTransactionBuilder::new(tx.transaction, rpc);

if let Some(TxKind::Call(_)) = to {
builder.set_call(&address_to_abi, &self.execution_artifacts.decoder)?;
Expand Down
18 changes: 7 additions & 11 deletions crates/script/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ use alloy_primitives::{hex, Address, TxKind, B256};
use eyre::Result;
use forge_script_sequence::TransactionWithMetadata;
use foundry_common::{fmt::format_token_raw, ContractData, TransactionMaybeSigned, SELECTOR_LEN};
use foundry_evm::{constants::DEFAULT_CREATE2_DEPLOYER, traces::CallTraceDecoder};
use foundry_evm::{constants::get_create2_deployer, traces::CallTraceDecoder};
use itertools::Itertools;
use revm_inspectors::tracing::types::CallKind;
use std::collections::BTreeMap;

#[derive(Debug)]
pub struct ScriptTransactionBuilder {
transaction: TransactionWithMetadata,
create2_deployer: Address,
}

impl ScriptTransactionBuilder {
pub fn new(
transaction: TransactionMaybeSigned,
rpc: String,
create2_deployer: Address,
) -> Self {
pub fn new(transaction: TransactionMaybeSigned, rpc: String) -> Self {
let mut transaction = TransactionWithMetadata::from_tx_request(transaction);
transaction.rpc = rpc;
// If tx.gas is already set that means it was specified in script
transaction.is_fixed_gas_limit = transaction.tx().gas().is_some();

Self { transaction, create2_deployer }
Self { transaction }
}

/// Populate the transaction as CALL tx
Expand All @@ -36,13 +31,14 @@ impl ScriptTransactionBuilder {
decoder: &CallTraceDecoder,
) -> Result<()> {
if let Some(TxKind::Call(to)) = self.transaction.transaction.to() {
if to == self.create2_deployer {
let create2_deployer = get_create2_deployer();
if to == create2_deployer {
if let Some(input) = self.transaction.transaction.input() {
let (salt, init_code) = input.split_at(32);

self.set_create(
true,
self.create2_deployer.create2_from_code(B256::from_slice(salt), init_code),
create2_deployer.create2_from_code(B256::from_slice(salt), init_code),
local_contracts,
)?;
}
Expand Down Expand Up @@ -178,6 +174,6 @@ impl ScriptTransactionBuilder {

impl From<TransactionWithMetadata> for ScriptTransactionBuilder {
fn from(transaction: TransactionWithMetadata) -> Self {
Self { transaction, create2_deployer: DEFAULT_CREATE2_DEPLOYER }
Self { transaction }
}
}

0 comments on commit fcaa2a7

Please sign in to comment.