Skip to content

feat(forge-selectors-list): add --no-group option #11270

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 68 additions & 16 deletions crates/forge/src/cmd/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use foundry_common::{
selectors::{SelectorImportData, import_selectors},
};
use foundry_compilers::{artifacts::output_selection::ContractOutputSelection, info::ContractInfo};
use std::fs::canonicalize;
use std::{collections::BTreeMap, fs::canonicalize};

/// CLI arguments for `forge selectors`.
#[derive(Clone, Debug, Parser)]
Expand Down Expand Up @@ -56,6 +56,9 @@ pub enum SelectorsSubcommands {

#[command(flatten)]
project_paths: ProjectPathOpts,

#[arg(long, help = "Do not group the selectors by contract in separate tables.")]
no_group: bool,
},

/// Find if a selector is present in the project
Expand Down Expand Up @@ -225,7 +228,7 @@ impl SelectorsSubcommands {
sh_println!("\n{table}\n")?;
}
}
Self::List { contract, project_paths } => {
Self::List { contract, project_paths, no_group } => {
sh_println!("Listing selectors for contracts in the project...")?;
let build_args = BuildOpts {
project_paths,
Expand Down Expand Up @@ -273,41 +276,90 @@ impl SelectorsSubcommands {

let mut artifacts = artifacts.into_iter().peekable();

while let Some((contract, artifact)) = artifacts.next() {
let abi = artifact.abi.ok_or_else(|| eyre::eyre!("Unable to fetch abi"))?;
if abi.functions.is_empty() && abi.events.is_empty() && abi.errors.is_empty() {
continue;
#[derive(PartialEq, PartialOrd, Eq, Ord)]
enum SelectorType {
Function,
Event,
Error,
}
impl std::fmt::Display for SelectorType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Function => write!(f, "Function"),
Self::Event => write!(f, "Event"),
Self::Error => write!(f, "Error"),
}
}
}

sh_println!("{contract}")?;
let mut selectors =
BTreeMap::<String, BTreeMap<SelectorType, Vec<(String, String)>>>::new();

let mut table = Table::new();
table.apply_modifier(UTF8_ROUND_CORNERS);
for (contract, artifact) in artifacts.by_ref() {
let abi = artifact.abi.ok_or_else(|| eyre::eyre!("Unable to fetch abi"))?;

table.set_header(["Type", "Signature", "Selector"]);
let contract_selectors = selectors.entry(contract.clone()).or_default();

for func in abi.functions() {
let sig = func.signature();
let selector = func.selector();
table.add_row(["Function", &sig, &hex::encode_prefixed(selector)]);
contract_selectors
.entry(SelectorType::Function)
.or_default()
.push((hex::encode_prefixed(selector), sig));
}

for event in abi.events() {
let sig = event.signature();
let selector = event.selector();
table.add_row(["Event", &sig, &hex::encode_prefixed(selector)]);
contract_selectors
.entry(SelectorType::Event)
.or_default()
.push((hex::encode_prefixed(selector), sig));
}

for error in abi.errors() {
let sig = error.signature();
let selector = error.selector();
table.add_row(["Error", &sig, &hex::encode_prefixed(selector)]);
contract_selectors
.entry(SelectorType::Error)
.or_default()
.push((hex::encode_prefixed(selector), sig));
}
}

sh_println!("\n{table}\n")?;
if no_group {
let mut table = Table::new();
table.apply_modifier(UTF8_ROUND_CORNERS);
table.set_header(["Type", "Signature", "Selector", "Contract"]);

for (contract, contract_selectors) in selectors {
for (selector_type, selectors) in contract_selectors {
for (selector, sig) in selectors {
table.add_row([
selector_type.to_string(),
sig,
selector,
contract.to_string(),
]);
}
}
}

if artifacts.peek().is_some() {
sh_println!()?
sh_println!("\n{table}")?;
} else {
for (idx, (contract, contract_selectors)) in selectors.into_iter().enumerate() {
sh_println!("{}{contract}", if idx == 0 { "" } else { "\n" })?;
let mut table = Table::new();
table.apply_modifier(UTF8_ROUND_CORNERS);
table.set_header(["Type", "Signature", "Selector"]);

for (selector_type, selectors) in contract_selectors {
for (selector, sig) in selectors {
table.add_row([selector_type.to_string(), sig, selector]);
}
}
sh_println!("\n{table}")?;
}
}
}
Expand Down
100 changes: 100 additions & 0 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,106 @@ Selectors successfully uploaded to OpenChain
"#]]);
});

forgetest_init!(selectors_list_cmd, |prj, cmd| {
prj.add_source(
"Counter.sol",
r"
contract Counter {
uint256 public number;
event Incremented(uint256 newNumber);
error IncrementError();

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}
}
",
)
.unwrap();

prj.add_source(
"CounterV2.sol",
r"
contract CounterV2 {
uint256 public number;

function setNumberV2(uint256 newNumber) public {
number = newNumber;
}

function incrementV2() public {
number++;
}
}
",
)
.unwrap();

cmd.args(["selectors", "list"]).assert_success().stdout_eq(str![[r#"
Listing selectors for contracts in the project...
Counter

╭----------+----------------------+--------------------------------------------------------------------╮
| Type | Signature | Selector |
+======================================================================================================+
| Function | increment() | 0xd09de08a |
|----------+----------------------+--------------------------------------------------------------------|
| Function | number() | 0x8381f58a |
|----------+----------------------+--------------------------------------------------------------------|
| Function | setNumber(uint256) | 0x3fb5c1cb |
|----------+----------------------+--------------------------------------------------------------------|
| Event | Incremented(uint256) | 0x20d8a6f5a693f9d1d627a598e8820f7a55ee74c183aa8f1a30e8d4e8dd9a8d84 |
|----------+----------------------+--------------------------------------------------------------------|
| Error | IncrementError() | 0x46544c04 |
╰----------+----------------------+--------------------------------------------------------------------╯

CounterV2

╭----------+----------------------+------------╮
| Type | Signature | Selector |
+==============================================+
| Function | incrementV2() | 0x49365a69 |
|----------+----------------------+------------|
| Function | number() | 0x8381f58a |
|----------+----------------------+------------|
| Function | setNumberV2(uint256) | 0xb525b68c |
╰----------+----------------------+------------╯

"#]]);

cmd.forge_fuse()
.args(["selectors", "list", "--no-group"])
.assert_success()
.stdout_eq(str![[r#"
Listing selectors for contracts in the project...

╭----------+----------------------+--------------------------------------------------------------------+-----------╮
| Type | Signature | Selector | Contract |
+==================================================================================================================+
| Function | increment() | 0xd09de08a | Counter |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Function | number() | 0x8381f58a | Counter |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Function | setNumber(uint256) | 0x3fb5c1cb | Counter |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Event | Incremented(uint256) | 0x20d8a6f5a693f9d1d627a598e8820f7a55ee74c183aa8f1a30e8d4e8dd9a8d84 | Counter |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Error | IncrementError() | 0x46544c04 | Counter |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Function | incrementV2() | 0x49365a69 | CounterV2 |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Function | number() | 0x8381f58a | CounterV2 |
|----------+----------------------+--------------------------------------------------------------------+-----------|
| Function | setNumberV2(uint256) | 0xb525b68c | CounterV2 |
╰----------+----------------------+--------------------------------------------------------------------+-----------╯

"#]]);
});

// tests `interceptInitcode` function
forgetest_init!(intercept_initcode, |prj, cmd| {
prj.wipe_contracts();
Expand Down
Loading