Skip to content

Commit

Permalink
feat: add contract filters to bind (#2613)
Browse files Browse the repository at this point in the history
* feat: add contract filters to bind

* bump ethers
  • Loading branch information
mattsse committed Aug 4, 2022
1 parent 185f45e commit 0e3d6da
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 35 deletions.
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

0 comments on commit 0e3d6da

Please sign in to comment.