Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.