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

forc call help for callable functions #6950

Open
wants to merge 2 commits 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
14 changes: 11 additions & 3 deletions forc-plugins/forc-client/src/bin/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ use forc_tracing::{init_tracing_subscriber, println_error};
async fn main() {
init_tracing_subscriber(Default::default());
let command = forc_client::cmd::Call::parse();
if let Err(err) = forc_client::op::call(command).await {
println_error(&format!("{}", err));
std::process::exit(1);
// Check if the print_functions flag is set
if command.print_functions {
if let Err(err) = forc_client::op::print_callable_functions(command).await {
println_error(&format!("{}", err));
std::process::exit(1);
}
} else {
if let Err(err) = forc_client::op::call(command).await {
println_error(&format!("{}", err));
std::process::exit(1);
}
}
}
4 changes: 4 additions & 0 deletions forc-plugins/forc-client/src/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ pub struct Command {
/// The output format to use; possible values: default, raw
#[clap(long, default_value = "default")]
pub output: OutputFormat,

/// Print all callable functions from the ABI
#[clap(long)]
pub print_functions: bool,
}

fn parse_abi_path(s: &str) -> Result<Either<PathBuf, Url>, String> {
Expand Down
33 changes: 33 additions & 0 deletions forc-plugins/forc-client/src/op/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub async fn call(cmd: cmd::Call) -> anyhow::Result<String> {
gas,
external_contracts,
output,
print_functions
} = cmd;
let node_url = node.get_node_url(&None)?;
let provider: Provider = Provider::connect(node_url).await?;
Expand All @@ -69,6 +70,18 @@ pub async fn call(cmd: cmd::Call) -> anyhow::Result<String> {
}
};
let parsed_abi = UnifiedProgramABI::from_json_abi(&abi_str)?;
if print_functions {
println!("Contract Functions:");
for func in parsed_abi.functions {
let inputs = func.inputs.iter()
.map(|input| format!("{}({})", input.name, input.kind.name))
.collect::<Vec<_>>().join(", ");
println!("{}({})", func.name, inputs);
println!("> forc call --abi <ABI_PATH> <CONTRACT-ID> {} {}", func.name, inputs);
println!();
}
return Ok("".to_string());
}

let type_lookup = parsed_abi
.types
Expand Down Expand Up @@ -250,7 +263,27 @@ pub async fn call(cmd: cmd::Call) -> anyhow::Result<String> {

Ok(result)
}
pub async fn print_callable_functions(cmd: cmd::Call) -> anyhow::Result<()> {
let abi_str = match cmd.abi {
Either::Left(path) => std::fs::read_to_string(&path)?,
Either::Right(url) => {
let response = reqwest::get(url).await?.bytes().await?;
String::from_utf8(response.to_vec())?
}
};
let parsed_abi = UnifiedProgramABI::from_json_abi(&abi_str)?;

println!("Contract Functions:");
for func in parsed_abi.functions {
let inputs = func.inputs.iter()
.map(|input| format!("{}({})", input.name, input.kind.name))
.collect::<Vec<_>>().join(", ");
println!("{}({})", func.name, inputs);
println!("> forc call --abi <ABI_PATH> <CONTRACT-ID> {} {}", func.name, inputs);
println!();
}
Ok(())
}
/// Get the wallet to use for the call - based on optionally provided signing key and wallet flag.
async fn get_wallet(
signing_key: Option<SecretKey>,
Expand Down