Skip to content
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: add contract filters to bind #2613

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 34 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 39 additions & 2 deletions cli/src/cmd/forge/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cmd::utils::Cmd;

use crate::{cmd::forge::build::CoreBuildArgs, compile};
use clap::{Parser, ValueHint};
use ethers::contract::MultiAbigen;
use ethers::contract::{ContractFilter, ExcludeContracts, MultiAbigen, SelectContracts};
use foundry_config::{
figment::{
self,
Expand Down Expand Up @@ -32,6 +32,29 @@ pub struct BindArgs {
#[serde(skip)]
pub bindings: Option<PathBuf>,

#[clap(
long,
help = "Create bindings only for contracts whose names match the specified filter(s)"
)]
#[serde(skip)]
pub select: Vec<regex::Regex>,

#[clap(
long,
help = "Create bindings only for contracts whose names do not match the specified filter(s)",
conflicts_with = "select"
)]
#[serde(skip)]
pub skip: Vec<regex::Regex>,

#[clap(
long,
help ="By default all contracts ending with `Test` or `Script` are excluded. This will explicitly generate bindings for all contracts",
conflicts_with_all = &["select", "skip"]
)]
#[serde(skip)]
pub select_all: bool,

#[clap(
long = "crate-name",
help = "The name of the Rust crate to generate. This should be a valid crates.io crate name. However, it is not currently validated by this command.",
Expand Down Expand Up @@ -93,9 +116,23 @@ impl BindArgs {
self.bindings_root().is_dir()
}

/// Returns the filter to use for `MultiAbigen`
fn get_filter(&self) -> ContractFilter {
if self.select_all {
return ContractFilter::All
}
if !self.select.is_empty() {
return SelectContracts::default().extend_regex(self.select.clone()).into()
}
if !self.skip.is_empty() {
return ExcludeContracts::default().extend_regex(self.skip.clone()).into()
}
ExcludeContracts::default().add_pattern(".*Test").add_pattern(".*Script").into()
}

/// Instantiate the multi-abigen
fn get_multi(&self) -> eyre::Result<MultiAbigen> {
let multi = MultiAbigen::from_json_files(self.artifacts())?;
let multi = MultiAbigen::from_json_files(self.artifacts())?.with_filter(self.get_filter());

eyre::ensure!(
!multi.is_empty(),
Expand Down