Skip to content

Commit

Permalink
more dynamic addresses (#54)
Browse files Browse the repository at this point in the history
* more dynamic addresses

* 1337 network as default
  • Loading branch information
error2215 authored Mar 27, 2024
1 parent 498ec56 commit bf4c496
Show file tree
Hide file tree
Showing 134 changed files with 879 additions and 662 deletions.
381 changes: 202 additions & 179 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cli/src/cmd/probe/create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct Create2Args {
#[clap(
short,
long,
default_value = "cb063edadf999cb7b8b3ebc71f5e97783176d289d640",
default_value = "cb063edadf999cb7b8b3ebc71f5e97783176d289d640", //todo:error2215 change to ce address
value_name = "ADDRESS"
)]
deployer: Address,
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cmd/probe/estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct EstimateArgs {
short,
long,
value_parser = NameOrAddress::from_str,
default_value = "0xcb656dadee521bea601692312454a655a0f49051ddc9",
default_value = "0xcb656dadee521bea601692312454a655a0f49051ddc9", //todo:error2215 change to ce address
env = "ETH_FROM",
)]
from: NameOrAddress,
Expand Down
11 changes: 6 additions & 5 deletions cli/src/cmd/spark/script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use eyre::{Context, ContextCompat};
use foxar_common::compile;
use foxar_utils::PostLinkInput;
use std::{collections::BTreeMap, fs, str::FromStr};
use tracing::{trace, warn};
use tracing::{info, trace, warn};

impl ScriptArgs {
/// Compiles the file or project and the verify metadata.
Expand Down Expand Up @@ -110,11 +110,12 @@ impl ScriptArgs {
}
}

if self.evm_opts.env.network_id.is_none() {
eyre::bail!("Network is not provided. Please specify the network with `--network {{network_id}}`")
let mut network = Network::Private(1337);
if self.evm_opts.env.network_id.is_some() {
network = self.evm_opts.env.network_id.unwrap();
} else {
info!("Running script in simulation mode with network id 1337");
};
let network_config = self.evm_opts.env.network_id.unwrap();
let network = network_config;

foxar_utils::link_with_nonce_or_address(
contracts.clone(),
Expand Down
13 changes: 5 additions & 8 deletions cli/src/cmd/spark/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ use foxar_common::{
use foxar_config::{figment, Config};
use probe::{
decode,
executor::inspector::{
cheatcodes::{util::BroadcastableTransactions, BroadcastableTransaction},
DEFAULT_CREATE2_DEPLOYER,
},
executor::inspector::cheatcodes::{util::{default_create2_address, BroadcastableTransactions}, BroadcastableTransaction},
};
use spark::{
debug::DebugArena,
Expand Down Expand Up @@ -209,7 +206,7 @@ impl ScriptArgs {

let mut local_identifier = LocalTraceIdentifier::new(known_contracts);
let mut decoder = CallTraceDecoderBuilder::new(
&script_config.evm_opts.get_remote_chain_id().unwrap_or(Network::Mainnet),
&script_config.evm_opts.get_remote_chain_id().unwrap_or(Network::Private(1337)),
)
.with_labels(result.labeled_addresses.clone())
.with_verbosity(verbosity)
Expand Down Expand Up @@ -573,7 +570,7 @@ impl ScriptArgs {

// Find if it's a CREATE or CREATE2. Otherwise, skip transaction.
if let Some(NameOrAddress::Address(to)) = to {
if *to == DEFAULT_CREATE2_DEPLOYER {
if *to == default_create2_address(self.evm_opts.env.network_id) {
// Size of the salt prefix.
offset = 32;
}
Expand All @@ -599,8 +596,8 @@ impl ScriptArgs {
}
}

if prompt_user &&
!Confirm::new().with_prompt("Do you wish to continue?".to_string()).interact()?
if prompt_user
&& !Confirm::new().with_prompt("Do you wish to continue?".to_string()).interact()?
{
eyre::bail!("User canceled the script.");
}
Expand Down
5 changes: 2 additions & 3 deletions cli/src/cmd/spark/script/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use corebc::types::{Address, Bytes, NameOrAddress, Network, U256};
use spark::{
default_caller,
executor::{CallResult, DeployResult, EvmError, ExecutionErr, Executor, RawCallResult},
revm::interpreter::{return_ok, InstructionResult},
trace::{TraceKind, Traces},
Expand Down Expand Up @@ -56,7 +55,7 @@ impl ScriptRunner {

// We max out their balance so that they can deploy and make calls.
self.executor.set_balance(
default_caller(&Network::from(self.executor.env().cfg.network_id)),
Config::default_sender(Some(&Network::from(self.executor.env().cfg.network_id))),
U256::MAX,
)?;

Expand Down Expand Up @@ -84,7 +83,7 @@ impl ScriptRunner {
} = self
.executor
.deploy(
default_caller(&Network::from(self.executor.env().cfg.network_id)),
Config::default_sender(Some(&Network::from(self.executor.env().cfg.network_id))),
code.0,
0u32.into(),
None,
Expand Down
7 changes: 5 additions & 2 deletions cli/src/cmd/spark/script/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use corebc::{
};
use eyre::{ContextCompat, WrapErr};
use foxar_common::{abi::format_token_raw, RpcUrl, SELECTOR_LEN};
use probe::{executor::inspector::DEFAULT_CREATE2_DEPLOYER, trace::CallTraceDecoder, CallKind};
use probe::{
executor::inspector::cheatcodes::util::default_create2_address, trace::CallTraceDecoder,
CallKind,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use tracing::error;
Expand Down Expand Up @@ -77,7 +80,7 @@ impl TransactionWithMetadata {

// Specify if any contract was directly created with this transaction
if let Some(NameOrAddress::Address(to)) = metadata.transaction.to().cloned() {
if to == DEFAULT_CREATE2_DEPLOYER {
if to == default_create2_address(Some(*network)) {
let address = to_ican(&H160::from_slice(&result.returned), network);
metadata.set_create(true, address, local_contracts, decoder)?;
} else {
Expand Down
16 changes: 8 additions & 8 deletions cli/src/cmd/spark/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ impl TestArgs {
let mut project = config.project()?;

// install missing dependencies
if install::install_missing_dependencies(&mut config, self.build_args().silent) &&
config.auto_detect_remappings
if install::install_missing_dependencies(&mut config, self.build_args().silent)
&& config.auto_detect_remappings
{
// need to re-configure here to also catch additional remappings
config = self.load_config();
Expand Down Expand Up @@ -597,7 +597,7 @@ async fn test(
if !result.traces.is_empty() {
// Identify addresses in each trace
let mut decoder = CallTraceDecoderBuilder::new(
&config.network_id.unwrap_or(Network::Mainnet),
&config.network_id.unwrap_or(Network::Private(1337)),
)
.with_labels(result.labeled_addresses.clone())
.with_events(local_identifier.events().cloned())
Expand All @@ -621,12 +621,12 @@ async fn test(
// tests At verbosity level 5, we display
// all traces for all tests
TraceKind::Setup => {
(verbosity >= 5) ||
(verbosity == 4 && result.status == TestStatus::Failure)
(verbosity >= 5)
|| (verbosity == 4 && result.status == TestStatus::Failure)
}
TraceKind::Execution => {
verbosity > 3 ||
(verbosity == 3 && result.status == TestStatus::Failure)
verbosity > 3
|| (verbosity == 3 && result.status == TestStatus::Failure)
}
_ => false,
};
Expand All @@ -648,7 +648,7 @@ async fn test(
}

if gas_reporting {
gas_report.analyze(&result.traces);
gas_report.analyze(&result.traces, config.network_id);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/src/opts/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ pub trait WalletTrait {
let private_key = private_key.strip_prefix("0x").unwrap_or(&private_key);

let mut wallet = LocalWallet::from_str(private_key)?;
wallet = wallet.with_network_id(self.network().unwrap_or(Network::Mainnet));
wallet = wallet.with_network_id(self.network().unwrap_or(Network::Private(1337)));

Ok(wallet)
}
Expand All @@ -276,7 +276,7 @@ pub trait WalletTrait {
fn get_from_private_key(&self, private_key: &str) -> Result<LocalWallet> {
let privk = private_key.trim().strip_prefix("0x").unwrap_or(private_key);
match LocalWallet::from_str(privk) {
Ok(pk) => Ok(pk.with_network_id(self.network().unwrap_or(Network::Mainnet))),
Ok(pk) => Ok(pk.with_network_id(self.network().unwrap_or(Network::Private(1337)))),
Err(err) => {
// helper closure to check if pk was meant to be an env var, this usually happens if
// `$` is missing
Expand Down Expand Up @@ -354,7 +354,7 @@ pub trait WalletTrait {
keystore_password: Option<&String>,
keystore_password_file: Option<&String>,
) -> Result<Option<LocalWallet>> {
let network = self.network().unwrap_or(Network::Mainnet);
let network = self.network().unwrap_or(Network::Private(1337));

Ok(match (keystore_path, keystore_password, keystore_password_file) {
(Some(path), Some(password), _) => {
Expand Down
39 changes: 36 additions & 3 deletions cli/src/opts/wallet/multi_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ impl MultiWallet {
let mut error_msg = String::new();

// This is an actual used address
if addresses
.contains(&Config::default_sender(Some(&self.network().unwrap_or(Network::Mainnet))))
{
if addresses.contains(&Config::default_sender(Some(
&self.network().unwrap_or(Network::Private(1337)),
))) {
error_msg += "\nYou seem to be using Foxar's default sender. Be sure to set your own --sender.\n";
}

Expand Down Expand Up @@ -458,6 +458,39 @@ mod tests {
Some(vec![keystore_password_file.to_str().unwrap().to_string()])
);

let wallets = args.keystores().unwrap().unwrap();
assert_eq!(wallets.len(), 1);
assert_eq!(
wallets[0].address(),
"ce56e49851f010cd7d81b5b4969f3b0e8325c415359d".parse().unwrap()
);
}

#[test]
fn parse_keystore_password_file_mainnet() {
let keystore = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/keystore");
let keystore_file = keystore.join(
"UTC--2023-11-17T08-49-29.100000000Z--cb65e49851f010cd7d81b5b4969f3b0e8325c415359d",
);

let keystore_password_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/keystore/password-ec554")
.into_os_string();

let args: MultiWallet = MultiWallet::parse_from([
"foxar-cli",
"--keystores",
keystore_file.to_str().unwrap(),
"--password-file",
keystore_password_file.to_str().unwrap(),
"--wallet-network",
"1",
]);
assert_eq!(
args.keystore_password_files,
Some(vec![keystore_password_file.to_str().unwrap().to_string()])
);

let wallets = args.keystores().unwrap().unwrap();
assert_eq!(wallets.len(), 1);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/fixtures/ScriptVerify.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract InnerContracts is Unique {

contract ScriptVerify {
function run() public {
address vm = address(0xcb69fc06a12b7a6f30e2a3c16a3b5d502cd71c20f2f8);
address vm = address(0xce60fc06a12b7a6f30e2a3c16a3b5d502cd71c20f2f8);
HEVM(vm).startBroadcast();
new Hello();
InnerContracts contracts = new InnerContracts(1);
Expand Down
20 changes: 10 additions & 10 deletions cli/tests/it/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,38 +1072,38 @@ contract ScriptTxOrigin is Script {
ContractB contractB = new ContractB();
contractA.test(address(contractB));
contractB.method(0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
contractB.method(0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xcb681804c8ab1f12e6bbf3894d4083f33e07309d1f38);
require(tx.origin == 0xce591804c8ab1f12e6bbf3894d4083f33e07309d1f38);
vm.stopBroadcast();
}
}
contract ContractA {
function test(address _contractB) public {
require(msg.sender == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(msg.sender == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
ContractB contractB = ContractB(_contractB);
ContractC contractC = new ContractC();
require(msg.sender == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(msg.sender == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
contractB.method(address(this));
contractC.method(address(this));
require(msg.sender == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(msg.sender == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
}
}
contract ContractB {
function method(address sender) public view {
require(msg.sender == sender);
require(tx.origin == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
}
}
contract ContractC {
function method(address sender) public view {
require(msg.sender == sender);
require(tx.origin == 0xcb58e5dd06163a480c22d540ec763325a0b5860fb56c);
require(tx.origin == 0xce49e5dd06163a480c22d540ec763325a0b5860fb56c);
}
}
"#,
Expand Down
2 changes: 1 addition & 1 deletion common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::time::Duration;

/// The dev chain-id, inherited from hardhat
pub const DEV_CHAIN_ID: u64 = 1;
pub const DEV_CHAIN_ID: u64 = 1337;

/// The first four bytes of the call data for a function call specifies the function to be called.
pub const SELECTOR_LEN: usize = 4;
Expand Down
4 changes: 2 additions & 2 deletions config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ match_path = "*/Foo*"
no_match_path = "*/Bar*"
ffi = false
# These are the default callers, generated using `address(uint160(uint256(keccak256("foxar default caller"))))`
sender = '0xcb681804c8ab1f12e6bbf3894d4083f33e07309d1f38'
tx_origin = '0xcb681804c8ab1f12e6bbf3894d4083f33e07309d1f38'
sender = '0xce591804c8ab1f12e6bbf3894d4083f33e07309d1f38'
tx_origin = '0xce591804c8ab1f12e6bbf3894d4083f33e07309d1f38'
initial_balance = '0xffffffffffffffffffffffff'
block_number = 0
fork_block_number = 0
Expand Down
Loading

0 comments on commit bf4c496

Please sign in to comment.