-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add creation-code flags and creation-args
- Loading branch information
Showing
7 changed files
with
195 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use alloy_primitives::{Address, Bytes}; | ||
use clap::{command, Parser}; | ||
use eyre::Result; | ||
use foundry_block_explorers::Client; | ||
use foundry_cli::{ | ||
opts::{EtherscanOpts, RpcOpts}, | ||
utils, | ||
}; | ||
use foundry_config::Config; | ||
|
||
use super::{creation_code::fetch_creation_code, interface::fetch_abi_from_etherscan}; | ||
|
||
/// CLI arguments for `cast creation-code`. | ||
#[derive(Parser)] | ||
pub struct CreationArgsArgs { | ||
/// An Ethereum address, for which the bytecode will be fetched. | ||
contract: Address, | ||
|
||
#[command(flatten)] | ||
etherscan: EtherscanOpts, | ||
#[command(flatten)] | ||
rpc: RpcOpts, | ||
} | ||
|
||
impl CreationArgsArgs { | ||
pub async fn run(self) -> Result<()> { | ||
let Self { contract, etherscan, rpc } = self; | ||
|
||
let config = Config::from(ðerscan); | ||
let chain = config.chain.unwrap_or_default(); | ||
let api_key = config.get_etherscan_api_key(Some(chain)).unwrap_or_default(); | ||
let client = Client::new(chain, api_key)?; | ||
|
||
let config = Config::from(&rpc); | ||
let provider = utils::get_provider(&config)?; | ||
|
||
let bytecode = fetch_creation_code(contract, client, provider).await?; | ||
|
||
let args_arr = parse_creation_args(bytecode, contract, ðerscan).await?; | ||
|
||
for arg in args_arr { | ||
println!("{arg}"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
async fn parse_creation_args( | ||
bytecode: Bytes, | ||
contract: Address, | ||
etherscan: &EtherscanOpts, | ||
) -> Result<Vec<String>> { | ||
let abi = fetch_abi_from_etherscan(contract, etherscan).await?; | ||
let abi = abi.into_iter().next().ok_or_else(|| eyre::eyre!("No ABI found."))?; | ||
let (abi, _) = abi; | ||
|
||
if abi.constructor.is_none() { | ||
return Err(eyre::eyre!("No constructor found.")); | ||
} | ||
|
||
let constructor = abi.constructor.unwrap(); | ||
|
||
if constructor.inputs.is_empty() { | ||
return Err(eyre::eyre!("No constructor arguments found.")); | ||
} | ||
|
||
let args_size = constructor.inputs.len() * 32; | ||
|
||
let args_bytes = Bytes::from(bytecode[bytecode.len() - args_size..].to_vec()); | ||
|
||
let display_args: Vec<String> = args_bytes | ||
.chunks(32) | ||
.enumerate() | ||
.map(|(i, arg)| { | ||
let arg = arg.to_vec(); | ||
format!("{} {}", constructor.inputs[i].ty, Bytes::from(arg)) | ||
}) | ||
.collect(); | ||
|
||
Ok(display_args) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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