-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
plugin: forc-call #6791
base: master
Are you sure you want to change the base?
plugin: forc-call #6791
Conversation
ead0e1f
to
945e38e
Compare
if is_temp_file { | ||
std::fs::remove_file(file_path).expect("Failed to remove ABI file"); | ||
} | ||
let parsed_abi = UnifiedProgramABI::from_json_abi(&abi_str)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add validation to ensure ABI files are well-formed and compatible before progressing?
// Validate minimum required fields
if parsed_abi.functions.is_empty() {
bail!("Invalid ABI: No functions found in ABI file");
}
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we may want to support calling a contract without any functions - where you may want to only specify an amount.
In the case of solidity, this calls the default fallback
or receive
function - allowing the contract to receive funds.
I was unsure whether sway has an equivalent default function - or an approach where the contract can receive funds without calling an explicit function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is indeed possible, rather than using a CALL
instruction, you can use a TR
or transfer instruction to send funds into a contract.
Sway also has a notion of default functions which are used by proxy contracts to pass-through calls and args to an implementation contract, but its not necessary to call functions to send funds into a contract.
|
||
forc_util::cli_examples! { | ||
super::Command { | ||
[ Call a contract with function parameters => "forc call <CONTRACT_ID> <FUNCTION_SIGNATURE> <ARGS>" ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if being a bit more explicit on what these calls look like might be easier for people to reference? maybe the above could be
[ Call a simple contract function => "forc call 0x123..abc add 1 2" ]
thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed; makes sense 👍
Note: Currently CI fails since there is reliance on new release from rust sdk - which contains the relevant changes: |
b515853
to
bd4069d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Impressive work! It's still rough around the edges but I'm excited to use this.
} | ||
|
||
let cmd::call::FuncType::Signature(function_signature) = function else { | ||
bail!("Function must be a signature if no ABI is provided"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: It would be good to give an example here.
bail!("Function must be a signature if no ABI is provided"); | |
bail!("Function must be a signature if no ABI is provided, e.g. "transfer(address,u64)" |
I wasn't able to get it to work without providing the ABI. These commands I tried all gave me panics:
forc call --testnet 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 "initialize_counter(1)"
forc call --testnet 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 "initialize_counter(1, u64)"
forc call --testnet 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 "initialize_counter(value,u64) 1"
forc call --testnet 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 "initialize_counter(value,u64)" 1
forc call --testnet 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 "initialize_counter(value:u64)" 1
This worked however: forc call --testnet --abi examples/counter/out/debug/counter-abi.json 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 "initialize_counter" 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeap; function signature is not supported; an ABI must be provided for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, we should just make sure the CLI isn't panicking in any scenario, and update the error messages so that they indicate it's not supported. And for now --abi
should be a required argument.
/// Optional path or URI to a JSON ABI file. | ||
#[clap(long, value_parser = parse_abi_path)] | ||
pub abi: Option<Either<PathBuf, Url>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Optional path or URI to a JSON ABI file. | |
#[clap(long, value_parser = parse_abi_path)] | |
pub abi: Option<Either<PathBuf, Url>>, | |
/// Path or URI to a JSON ABI file. | |
#[clap(long, value_parser = parse_abi_path)] | |
pub abi: Either<PathBuf, Url>, |
/// The function signature to call. | ||
/// When ABI is provided, this should be a selector (e.g. "transfer") | ||
/// When no ABI is provided, this should be the full function signature (e.g. "transfer(address,u64)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since ABI is required:
/// The function signature to call. | |
/// When ABI is provided, this should be a selector (e.g. "transfer") | |
/// When no ABI is provided, this should be the full function signature (e.g. "transfer(address,u64)") | |
/// The function signature to call. This should be a selector (e.g. "transfer") |
…upport for dry-run
…e execution modes; added gas -> txpolicies conversion function
…calls to contracts without known addresses at call-time
## Description ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers.
3aa26f9
to
569e457
Compare
Description
The following PR introduces a new forc plugin;
forc-call
.This plugin allows users to call functions on deployed contracts using the
forc call
command.This is ideal for quickly querying the state of a deployed contract.
In this first implementation; the contract ABI is required (as a path to a local JSON file or a URL to a remote JSON file).
This is inspired by the
cast call
tool; which is a popular tool for interacting with deployed contracts on Ethereum.The implementation is based on the following Github issue: #6725
In the current implementation, you can query a contract state using the
forc call
command by providing the target contract ID, it's respective ABI file, and the function name (selector) and arguments.Forc Call CLI
Example usage
0xe18de7c7c8c61a1c706dccb3533caa00ba5c11b5230da4428582abf1b6831b4d
is the contract ID./out/debug/counter-contract-abi.json
is the path to the ABI fileadd
is the function name (selector)1 2
are the arguments to the function^ the sway code for the add function could be:
Implementation details
add
is matched with the extracted functions from the ABIToken
enum, which is then ABI encoded as part of theContractCall
structContractCall
struct is then used to make a request to the node to call the function^ In the implementation, we don't use the
abigen!
macro since this is a compile time parser of the ABI file; instead we make use of the lower level encoding and decoding primitives and functions from the Rust SDK.Live example on testnet
Example 1
The example contract above with
add
function has been deployed on testnet - with ABI file available here.The add function can be called via the CLI:
Example 2 - get
owner
of Mira DEX contractNote: Testnet contract address here
Encoding of primitive types
When passing in function arguments, the following types are encoded as follows:
true
orfalse
42
0x0000000000000000000000000000000000000000000000000000000000000042
or0000000000000000000000000000000000000000000000000000000000000042
0x
prefix is optional0x42
or42
0x
prefix is optional"abc"
(42, true)
[42, 128]
[42, true]
{42, 128}
{42, 128}
; this could represent the followingstruct Polygon { x: u64, y: u64 }
(Active: true)
or(1: true)
enum MyEnum { Inactive, Active(bool) }
Encoding cheat-sheet
A few of the common types are encoded as follows:
bool
: 0x00 (false) or 0x01 (true);u8
is the byte itself.bool(true) = 0x01
,u8(42) = 0x2A
u16(42) = 0x002A
u32(42) = 0x0000002A
u64(42) = 0x000000000000002A
u128(42) = 0x0000000000000000000000000000002A
u256(42) = 32-bytes ending with ...0000002A
,b256(...) = exactly the 32-byte array
(u8(1), bool(true)) = 0x01 0x01
;[u16;2]: [42,100] = 0x002A0064
;struct {u8,u8}: 0x0102
u64
variant index + encoded variant data with no extra paddingMySumType::X(42): 0x0000000000000000 000000000000002A
u64
length prefix + raw data, no padding"abc" = length=3: 0x0000000000000003 0x61 0x62 0x63
^ This is based on the docs here: https://docs.fuel.network/docs/specs/abi/argument-encoding
Future improvements
Checklist
Breaking*
orNew Feature
labels where relevant.