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

plugin: forc-call #6791

Merged
merged 77 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
be0a1bb
initialized forc-call CLI in forc-plugins - forc-client
zees-dev Dec 17, 2024
a655a0c
initial implementation using local or remote abi
zees-dev Dec 17, 2024
cfd9490
sway contract testing all primitives for forc-call
zees-dev Dec 17, 2024
9555932
cargo.lock upd
zees-dev Dec 17, 2024
1d939f3
added proper support for wallet, default and explicit signer, added s…
zees-dev Dec 18, 2024
91a9b57
param_type_val_to_token_conversion unit tests
zees-dev Dec 29, 2024
d9e47ed
token_to_string_conversion unit tests
zees-dev Dec 29, 2024
17325f9
updated code and tests to handle more complex examples
zees-dev Dec 30, 2024
93516b1
FuncType FromStr function impl
zees-dev Jan 3, 2025
4093b48
added execution mode strategies, supporting dry-run, simulate and liv…
zees-dev Jan 3, 2025
ef08ec2
added forc-tx cargo.lock due to prev change
zees-dev Jan 3, 2025
4dc6ec9
cargo fmt --all
zees-dev Jan 3, 2025
fe56e95
cargo fmt --all
zees-dev Jan 3, 2025
b5308d4
typo fixes
zees-dev Jan 3, 2025
c405516
extract contract auto-extraction from input params
zees-dev Jan 3, 2025
e53eefe
integrated call parameters
zees-dev Jan 13, 2025
cac170c
added get_explorer_url function to getcanonical explorer urls
zees-dev Jan 13, 2025
5b00190
printing out tx details and link for viewing
zees-dev Jan 13, 2025
7458f36
external contract simulation and address retrieval for ergonomic CLI …
zees-dev Jan 13, 2025
cc45a2e
address cargo clippy warnings
zees-dev Jan 13, 2025
9551a57
cargo fmt --all
zees-dev Jan 13, 2025
f2854c0
added clone to core cmd structs
zees-dev Jan 14, 2025
7447143
fixed clippy warning, missing contract retrieval refactor; tx url onl…
zees-dev Jan 14, 2025
e1c4ab4
tests refactor; explicit fn args input
zees-dev Jan 14, 2025
bac84b5
added another test to forward value
zees-dev Jan 14, 2025
b285433
updated test contract
zees-dev Jan 14, 2025
b01ea16
rename test function
zees-dev Jan 14, 2025
98a95a3
cargo fmt --all
zees-dev Jan 14, 2025
bc30b7e
updated external contracts description
zees-dev Jan 14, 2025
4f2fa8f
fix build (#6831)
sdankel Jan 15, 2025
219d920
contract upd
zees-dev Jan 15, 2025
4288f66
node target get explorer url function migration
zees-dev Jan 15, 2025
17c1d30
updated tests for contract value forwarding
zees-dev Jan 15, 2025
f903876
cargo clippy fixes
zees-dev Jan 15, 2025
ffd47c9
consolidated select_account and select_local_wallet_account functions
zees-dev Jan 15, 2025
ece1107
removed redundant comment
zees-dev Jan 15, 2025
b21334f
split up forc call lib.rs file to mod.rs and parser.rs
zees-dev Jan 16, 2025
38144cf
split up forc call mod.rs into missing_contracts.rs
zees-dev Jan 16, 2025
90495f5
refactored imports
zees-dev Jan 16, 2025
460b17f
hint on function should be selector
zees-dev Jan 16, 2025
569e457
helper function to format check and build sway file for tests
zees-dev Jan 16, 2025
1c78736
removed temp writing to file
zees-dev Jan 30, 2025
a27a005
raw output format support; small test refactor
zees-dev Jan 31, 2025
19b3e99
forc-client devnet node url and faucet url support
zees-dev Jan 31, 2025
cc9e032
Merge branch 'master' into feat/forc-call
zees-dev Feb 1, 2025
486c8d7
cargo.lock file upd
zees-dev Feb 1, 2025
027e443
removed redundant gas cost import
zees-dev Feb 1, 2025
9b0bbc8
updated base_asset_id retrieval code
zees-dev Feb 1, 2025
386e27d
removed lock file since this updates with forc versions
zees-dev Feb 2, 2025
b4d65d6
removed setup function in forc-call test; creates bin changes if fuel…
zees-dev Feb 2, 2025
6e1b4e8
added devnet as help output for node target
zees-dev Feb 3, 2025
d969c38
forc call mdbook docs
zees-dev Feb 3, 2025
ce1d709
function signature support removal; only func selector is possible fu…
zees-dev Feb 3, 2025
c761f09
fixed issues with docs
zees-dev Feb 3, 2025
4c127e4
Merge branch 'master' into feat/forc-call
zees-dev Feb 3, 2025
29273d3
either default bug
zees-dev Feb 3, 2025
6ba6fa0
fixed spellcheck errors
zees-dev Feb 3, 2025
95571b7
forc-client removed redundant swayfmt dep
zees-dev Feb 3, 2025
f56393f
forc-client removed redundant swayfmt dep
zees-dev Feb 3, 2025
9219310
testing cli_examples macro
zees-dev Feb 3, 2025
7eab51d
improved help text - with providing examples to CLI help output
zees-dev Feb 4, 2025
5a0958e
removed redundant println
zees-dev Feb 4, 2025
0207e42
removed redundant test
zees-dev Feb 4, 2025
e7e0c41
linking gh issue for todo
zees-dev Feb 4, 2025
330596d
removed regex check for function selector
zees-dev Feb 4, 2025
236e178
Merge branch 'master' into feat/forc-call
zees-dev Feb 4, 2025
3392e6b
updated todo for decoding logs
zees-dev Feb 4, 2025
0cacf4c
Merge branch 'master' into feat/forc-call
zees-dev Feb 4, 2025
7851cc6
Merge branch 'master' into feat/forc-call
JoshuaBatty Feb 5, 2025
66451e7
fixed target
zees-dev Feb 5, 2025
870e4aa
doc for get_missing_contracts func
zees-dev Feb 5, 2025
f99b8ab
pub get_wallet func improved
zees-dev Feb 5, 2025
46e5007
added todo to retrieve ABI from registry as an option
zees-dev Feb 5, 2025
0eb0225
args -> function_args
zees-dev Feb 5, 2025
3984308
fixed panic when fn not found in abi; using anyhow
zees-dev Feb 6, 2025
1f40359
Merge branch 'master' into feat/forc-call
zees-dev Feb 6, 2025
476ccb7
Merge branch 'master' into feat/forc-call
JoshuaBatty Feb 7, 2025
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
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion docs/book/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,8 @@ FuelLabs
github
toml
hardcoded
subdirectories
subdirectories
RawSlice
StringArray
StringSlice
calldata
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
- [forc deploy](./forc/plugins/forc_client/forc_deploy.md)
- [forc run](./forc/plugins/forc_client/forc_run.md)
- [forc submit](./forc/plugins/forc_client/forc_submit.md)
- [forc call](./forc/plugins/forc_client/forc_call.md)
- [forc crypto](./forc/plugins/forc_crypto.md)
- [forc debug](./forc/plugins/forc_debug.md)
- [forc doc](./forc/plugins/forc_doc.md)
Expand Down
307 changes: 307 additions & 0 deletions docs/book/src/forc/plugins/forc_client/forc_call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
# Forc Call

`forc-call` is a command-line tool for interacting with deployed Fuel contracts. It allows you to make contract calls, query contract state, and interact with any deployed contract on the Fuel network - all from your command line!

The `forc call` command is part of the Forc toolchain and is installed alongside other Forc tools.

## Getting Started

Here are a few examples of what you can do with `forc call`:

Call a simple addition function on a deployed contract (in dry-run mode):

```bash
forc call 0xe18de7c7c8c61a1c706dccb3533caa00ba5c11b5230da4428582abf1b6831b4d \
--abi ./out/debug/counter-contract-abi.json \
add 1 2
```

Query the owner of a deployed DEX contract on testnet:

```bash
forc call \
--testnet \
--abi https://raw.githubusercontent.com/mira-amm/mira-v1-periphery/refs/heads/main/fixtures/mira-amm/mira_amm_contract-abi.json \
0xd5a716d967a9137222219657d7877bd8c79c64e1edb5de9f2901c98ebe74da80 \
owner
```

## Usage

The basic syntax for `forc call` is:

```bash
forc call [OPTIONS] --abi <ABI-PATH/URL> <CONTRACT_ID> <SELECTOR> [ARGS]...
```

Where the following arguments are required:

- `CONTRACT_ID` is the ID of the deployed contract you want to interact with
- `ABI-PATH/URL` is the path or URL to the contract's JSON ABI file
- `SELECTOR` is the function name (selector) you want to call
- `ARGS` are the arguments to pass to the function

## CLI Reference

<details>
<summary><b>Forc Call CLI reference</b></summary>

```sh
forc call --help
```

```output
Perform Fuel RPC calls from the comfort of your command line

Usage: forc call [OPTIONS] --abi <ABI> <CONTRACT_ID> <FUNCTION> [FUNCTION_ARGS]...

Arguments:
<CONTRACT_ID>
The contract ID to call

<FUNCTION>
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)")

[FUNCTION_ARGS]...
Arguments to pass into the function to be called

Options:
--abi <ABI>
Path or URI to a JSON ABI file

--node-url <NODE_URL>
The URL of the Fuel node to which we're submitting the transaction. If unspecified, checks the manifest's `network` table, then falls back to `http://127.0.0.1:4000`

You can also use `--target`, `--testnet`, or `--mainnet` to specify the Fuel node.

[env: FUEL_NODE_URL=]

--target <TARGET>
Preset configurations for using a specific target.

You can also use `--node-url`, `--testnet`, or `--mainnet` to specify the Fuel node.

Possible values are: [local, testnet, mainnet]

--mainnet
Use preset configuration for mainnet.

You can also use `--node-url`, `--target`, or `--testnet` to specify the Fuel node.

--testnet
Use preset configuration for testnet.

You can also use `--node-url`, `--target`, or `--mainnet` to specify the Fuel node.

--devnet
Use preset configuration for devnet.

You can also use `--node-url`, `--target`, or `--testnet` to specify the Fuel node.

--signing-key <SIGNING_KEY>
Derive an account from a secret key to make the call

[env: SIGNING_KEY=]

--wallet
Use forc-wallet to make the call

--amount <AMOUNT>
Amount of native assets to forward with the call

[default: 0]

--asset-id <ASSET_ID>
Asset ID to forward with the call

--gas-forwarded <GAS_FORWARDED>
Amount of gas to forward with the call

--mode <MODE>
The execution mode to use for the call; defaults to dry-run; possible values: dry-run, simulate, live

[default: dry-run]

--gas-price <PRICE>
Gas price for the transaction

--script-gas-limit <SCRIPT_GAS_LIMIT>
Gas limit for the transaction

--max-fee <MAX_FEE>
Max fee for the transaction

--tip <TIP>
The tip for the transaction

--external-contracts <EXTERNAL_CONTRACTS>
The external contract addresses to use for the call If none are provided, the call will automatically populate external contracts by making a dry-run calls to the node, and extract the contract addresses based on the revert reason

--output <OUTPUT>
The output format to use; possible values: default, raw

[default: default]

-h, --help
Print help (see a summary with '-h')

-V, --version
Print version
```

</details>

## Type Encoding

When passing arguments to contract functions, values are encoded according to their Sway types.
Here's how to format different types:

| Types | Example input | Notes |
|-----------------------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| bool | `true` or `false` | |
| u8, u16, u32, u64, u128, u256 | `42` | |
| b256 | `0x0000000000000000000000000000000000000000000000000000000000000042` or `0000000000000000000000000000000000000000000000000000000000000042` | `0x` prefix is optional |
| bytes, RawSlice | `0x42` or `42` | `0x` prefix is optional |
| String, StringSlice, StringArray (Fixed-size) | `"abc"` | |
| Tuple | `(42, true)` | The types in tuple can be different |
| Array (Fixed-size), Vector (Dynamic) | `[42, 128]` | The types in array or vector must be the same; i.e. you cannot have `[42, true]` |
| Struct | `{42, 128}` | Since structs are packed encoded, the attribute names are not encoded; i.e. `{42, 128}`; this could represent the following `struct Polygon { x: u64, y: u64 }` |
| Enum | `(Active: true)` or `(1: true)` | Enums are key-val pairs with keys as being variant name (case-sensitive) or variant index (starting from 0) and values as being the variant value; this could represent the following `enum MyEnum { Inactive, Active(bool) }` |

## ABI Support

The ABI (Application Binary Interface) can be provided in two ways.

### Local file

```bash
forc call <CONTRACT_ID> --abi ./path/to/abi.json <FUNCTION> [ARGS...]
```

### Remote ABI file/URL

```bash
forc call <CONTRACT_ID> --abi https://example.com/abi.json <FUNCTION> [ARGS...]
```

## Network Configuration

```bash
forc call --node-url http://127.0.0.1:4000 ...
# or
forc call --target local ...
```

## Advanced Usage

### Using Wallets

```sh
# utilising the forc-wallet
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --wallet
```

```sh
# with an explicit signing key
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --signing-key <KEY>
```

### Asset Transfers

```sh
# Native asset transfer
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --amount 100 --live
```

```sh
# Custom asset transfer
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> \
--amount 100 \
--asset-id 0x1234... \
--live
```

### Gas Configuration

```sh
# Set gas price
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --gas-price 1

# Forward gas to contract
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --gas-forwarded 1000

# Set maximum fee
forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --max-fee 5000
```

### Common Use Cases

- 1. Contract State Queries

```sh
# Read contract state
forc call <CONTRACT_ID> --abi <PATH> get_balance

# Query with parameters
forc call <CONTRACT_ID> --abi <PATH> get_user_info 0x1234...
```

- 2. Token Operations

```sh
# Check token balance
forc call <CONTRACT_ID> --abi <PATH> balance_of 0x1234...

# Transfer tokens
forc call <CONTRACT_ID> --abi <PATH> transfer 0x1234... 100 --live
```

- 3. Contract Administration

```sh
# Check contract owner
forc call <CONTRACT_ID> --abi <PATH> owner

# Update contract parameters
forc call <CONTRACT_ID> --abi <PATH> update_params 42 --live
```

## Tips and Tricks

- Use `--mode simulate` to estimate gas costs before making live transactions
- External contracts are automatically detected (via internal simulations), but can be manually specified with `--external-contracts`
- For complex parameter types (tuples, structs, enums), refer to the parameter types table above
- Always verify contract addresses and ABIs before making live calls
- Use environment variables for sensitive data like signing keys: `SIGNING_KEY=<key>`

## Troubleshooting

### Common issues and solutions

- **ABI Mismatch**:
- Ensure the ABI matches the deployed contract
- Verify function selectors match exactly

- **Parameter Type Errors**:
- Check parameter formats in the types table
- Ensure correct number of parameters

- **Network Issues**:
- Verify node connection
- Check network selection (testnet/mainnet)

- **Transaction Failures**:
- Use simulation mode to debug
- Check gas settings
- Verify wallet has sufficient balance

## Future Features

The following features are planned for future releases:

- Decode and display logs for contract calls
- Support direct transfer of asset(s) to addresses
- Function signature based calls without ABI
- Raw calldata input support
- Function selector completion
- Enhanced error messages, debugging, and logging (additional verbosity modes)
Loading
Loading