-
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
plugin: forc-call #6791
Conversation
ead0e1f
to
945e38e
Compare
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.
…upport for dry-run
…e execution modes; added gas -> txpolicies conversion 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.
great work with this
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'm getting the following error:
$ forc call --testnet --abi examples/counter/out/debug/counter-abi.json 0xd8b98e8ceebf08e2fbc4bf0599e8eae239608636b9aab76b5348c2174d197848 initialize_counter 1
error: io: Response errors; Unknown field "indexation" on type "NodeInfo".
Does this command work for you? Could be I'm misreading the docs and the request is malformed. If that's the case we should have a more helpful error message.
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.
🚀
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.