-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(forge): add forge selectors
cmd
#5072
Changes from all commits
23046d8
ae4f94a
62cd5cd
a857e49
ed0321f
5f41039
4e9e059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use crate::{ | ||
cmd::forge::build::{CoreBuildArgs, ProjectPathsArgs}, | ||
opts::forge::CompilerArgs, | ||
utils::FoundryPathExt, | ||
}; | ||
use clap::Parser; | ||
use ethers::prelude::artifacts::output_selection::ContractOutputSelection; | ||
use foundry_common::{ | ||
compile, | ||
selectors::{import_selectors, SelectorImportData}, | ||
}; | ||
|
||
/// CLI arguments for `forge selectors`. | ||
#[derive(Debug, Clone, Parser)] | ||
pub enum SelectorsSubcommands { | ||
/// Upload selectors to registry | ||
#[clap(visible_alias = "up")] | ||
Upload { | ||
/// The name of the contract to upload selectors for. | ||
#[clap(required_unless_present = "all")] | ||
contract: Option<String>, | ||
|
||
/// Upload selectors for all contracts in the project. | ||
#[clap(long, required_unless_present = "contract")] | ||
all: bool, | ||
|
||
#[clap(flatten)] | ||
project_paths: ProjectPathsArgs, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also document this field There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbf I don't really know how to document this field: it doesn't look settable by the user and it's not documented over on fourbytes.rs either.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these will be flattened and will use the existing documentation |
||
}, | ||
} | ||
|
||
impl SelectorsSubcommands { | ||
pub async fn run(self) -> eyre::Result<()> { | ||
match self { | ||
SelectorsSubcommands::Upload { contract, all, project_paths } => { | ||
let build_args = CoreBuildArgs { | ||
project_paths: project_paths.clone(), | ||
compiler: CompilerArgs { | ||
extra_output: vec![ContractOutputSelection::Abi], | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}; | ||
|
||
let project = build_args.project()?; | ||
let outcome = compile::suppress_compile(&project)?; | ||
let artifacts = if all { | ||
outcome | ||
.into_artifacts_with_files() | ||
.filter(|(file, _, _)| { | ||
let is_sources_path = file | ||
.starts_with(&project.paths.sources.to_string_lossy().to_string()); | ||
let is_test = file.is_sol_test(); | ||
|
||
is_sources_path && !is_test | ||
}) | ||
.map(|(_, contract, artifact)| (contract, artifact)) | ||
.collect() | ||
} else { | ||
let contract = contract.unwrap(); | ||
let found_artifact = outcome.find_first(&contract); | ||
let artifact = found_artifact | ||
.ok_or_else(|| { | ||
eyre::eyre!( | ||
"Could not find artifact `{contract}` in the compiled artifacts" | ||
) | ||
})? | ||
.clone(); | ||
vec![(contract, artifact)] | ||
}; | ||
|
||
let mut artifacts = artifacts.into_iter().peekable(); | ||
while let Some((contract, artifact)) = artifacts.next() { | ||
let abi = artifact.abi.ok_or(eyre::eyre!("Unable to fetch abi"))?; | ||
if abi.abi.functions.is_empty() && | ||
abi.abi.events.is_empty() && | ||
abi.abi.errors.is_empty() | ||
{ | ||
continue | ||
} | ||
|
||
println!("Uploading selectors for {contract}..."); | ||
|
||
// upload abi to selector database | ||
import_selectors(SelectorImportData::Abi(vec![abi])).await?.describe(); | ||
|
||
if artifacts.peek().is_some() { | ||
println!() | ||
} | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave some docs on what this is 🙏