near / near-cli

General purpose command line tools for interacting with NEAR Protocol
https://docs.near.org
MIT License
193 stars 93 forks source link

Show contract method names #849

Open swfsql opened 2 years ago

swfsql commented 2 years ago

Problem I think it's useful to know the method names from a wasm file. This can help on the verification of exported methods from a deployer PoV, but also from a user one.

Solution I'd like This information is contained in the wasm files. For example, this program would print the names:

 // rust
 for function in wasmer::Module::from_file(
 &wasmer::Store::default(),
 "/path/to/contract.wasm",
 )
 .unwrap()
 .exports()
 .filter(|e| matches!(e.ty(), wasmer::ExternType::Function(_fty)))
 {
 println!("{}", function.name());
 }

So I think the best place for this functionality to exist is within the near-cli. It would be great if we could pass in a local wasm file to show the methods, and also to verify the methods from a deployed wasm on Near - near-cli would have to first download the wasm though.

Expanding on this, if there are some way to make completion suggestions on function names when writing view calls for example, near-cli would also be able to do that.

This functionality also makes sense on an Explorer, although this might already exist and I'm not aware of!

Alternatives I believe the alternative to "extracting" the functions' names would be for some standard method within the wasm contracts to "output" the list of the function names, but I think this sort of interaction would be better for schematic information (eg. parameter information, etc), not the function names.

frol commented 2 years ago

@swfsql Cool idea! I shamelessly stole it for near-cli-rs extension: https://github.com/near/near-cli-rs/issues/70 :smile:

swfsql commented 2 years ago

@frol nice! I also took it from @H34D haha
I'll be subscribing to it

H34D commented 2 years ago

You could use rustc -Z ast-json foo.rs to generate the AST and parse it as JSON to extract another json file

swfsql commented 2 years ago

This SO answer has a good snippet for downloading wasm files from accounts, as we could see that near and testnet contracts would output:

send
claim
create_account_and_claim
create_account
on_account_created
on_account_created_and_claimed
get_key_balance

(where some are callbacks)