-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
2 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,93 @@ | ||
use alloy_primitives::Address; | ||
use clap::{command, Parser}; | ||
use eyre::Result; | ||
use foundry_block_explorers::Client; | ||
use foundry_cli::{ | ||
opts::{EtherscanOpts, RpcOpts}, | ||
utils, | ||
}; | ||
use foundry_common::fs; | ||
use foundry_config::Config; | ||
use serde_json::json; | ||
use std::path::PathBuf; | ||
|
||
use super::{ | ||
creation_code::{fetch_creation_code, parse_code_output}, | ||
interface::{fetch_abi_from_etherscan, load_abi_from_file}, | ||
}; | ||
|
||
/// CLI arguments for `cast artifact`. | ||
#[derive(Parser)] | ||
pub struct ArtifactArgs { | ||
/// An Ethereum address, for which the artifact will be produced. | ||
contract: Address, | ||
|
||
/// Path to file containing the contract's JSON ABI. It's necessary if the target contract is | ||
/// not verified on Etherscan. | ||
#[arg(long)] | ||
abi_path: Option<String>, | ||
|
||
/// The path to the output file. | ||
/// | ||
/// If not specified, the artifact will be output to stdout. | ||
#[arg( | ||
short, | ||
long, | ||
value_hint = clap::ValueHint::FilePath, | ||
value_name = "PATH", | ||
)] | ||
output: Option<PathBuf>, | ||
|
||
#[command(flatten)] | ||
etherscan: EtherscanOpts, | ||
|
||
#[command(flatten)] | ||
rpc: RpcOpts, | ||
} | ||
|
||
impl ArtifactArgs { | ||
pub async fn run(self) -> Result<()> { | ||
let Self { contract, etherscan, rpc, output: output_location, abi_path } = 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 abi = if let Some(abi_path) = abi_path.clone() { | ||
load_abi_from_file(&abi_path, None)? | ||
} else { | ||
fetch_abi_from_etherscan(contract, ðerscan).await? | ||
}; | ||
|
||
let (abi, _) = abi.first().ok_or_else(|| eyre::eyre!("No ABI found"))?; | ||
|
||
let bytecode = fetch_creation_code(contract, client, provider).await?; | ||
let bytecode = | ||
parse_code_output(bytecode, contract, ðerscan, abi_path, true, false).await?; | ||
|
||
let artifact = json!({ | ||
"abi": abi, | ||
"bytecode": { | ||
"object": bytecode | ||
} | ||
}); | ||
|
||
let artifact = serde_json::to_string_pretty(&artifact)?; | ||
|
||
if let Some(loc) = output_location { | ||
if let Some(parent) = loc.parent() { | ||
fs::create_dir_all(parent)?; | ||
} | ||
fs::write(&loc, artifact)?; | ||
sh_println!("Saved artifact at {}", loc.display())?; | ||
} else { | ||
sh_println!("{artifact}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
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