Skip to content

Commit

Permalink
Add data transformer examples to docs (#2682)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2557

## Introduced changes

<!-- A brief description of the changes -->

-

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
kkawula authored Nov 25, 2024
1 parent a6b9785 commit ed61449
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
10 changes: 5 additions & 5 deletions crates/sncast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ $ sncast \
invoke \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function "some_function" \
--calldata 1 2 3
--arguments '1, 2, 3'
```

<details>
Expand All @@ -132,7 +132,7 @@ With arguments taken from `snfoundry.toml` file (default profile name):
$ sncast invoke \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function "some_function" \
--calldata 1 2 3
--arguments '1, 2, 3'
```

<details>
Expand All @@ -152,7 +152,7 @@ $ sncast \
call \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function "some_function" \
--calldata 1 2 3
--arguments '1, 2, 3'
```

<details>
Expand All @@ -171,8 +171,8 @@ With arguments taken from `snfoundry.toml` file (default profile name):
```shell
$ sncast call \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function some_function \
--calldata 1 2 3
--function "some_function" \
--arguments '1, 2, 3'
```

<details>
Expand Down
4 changes: 2 additions & 2 deletions docs/src/projects/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ $ sncast --profile myprofile \
call \
--contract-address 0x38b7b9507ccf73d79cb42c2cc4e58cf3af1248f342112879bfdf5aa4f606cc9 \
--function get \
--calldata 0x0 \
--arguments '0' \
--block-id latest
```

Expand Down Expand Up @@ -91,7 +91,7 @@ With this, there's no need to include the `--profile` argument when using `sncas
$ sncast call \
--contract-address 0x38b7b9507ccf73d79cb42c2cc4e58cf3af1248f342112879bfdf5aa4f606cc9 \
--function get \
--calldata 0x0 \
--arguments '0' \
--block-id latest
```

Expand Down
19 changes: 10 additions & 9 deletions docs/src/starknet/call.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ For a detailed CLI description, see the [call command reference](../appendix/snc
$ sncast \
call \
--url http://127.0.0.1:5050 \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function "some_function" \
--calldata 1 2 3
--contract-address 0x522dc7cbe288037382a02569af5a4169531053d284193623948eac8dd051716 \
--function "balance_of" \
--arguments '0x0554d15a839f0241ba465bb176d231730c01cf89cdcb95fe896c51d4a6f4bb8f'
```

<details>
<summary>Output:</summary>

```shell
command: call
response: [0x1, 0x23, 0x4]
response: [0x1, 0x0]
```
</details>
<br>
Expand All @@ -44,17 +44,18 @@ You can call a contract at the specific block by passing `--block-id` argument.

```shell
$ sncast call \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function "some_function" \
--calldata 1 2 3 \
--block-id 1234
--url http://127.0.0.1:5050 \
--contract-address 0x522dc7cbe288037382a02569af5a4169531053d284193623948eac8dd051716 \
--function "balance_of" \
--arguments '0x0554d15a839f0241ba465bb176d231730c01cf89cdcb95fe896c51d4a6f4bb8f' \
--block-id 77864
```

<details>
<summary>Output:</summary>

```shell
command: call
response: [0x1, 0x23]
response: [0x0, 0x0]
```
</details>
15 changes: 9 additions & 6 deletions docs/src/starknet/invoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ $ sncast \
--account example_user \
invoke \
--url http://127.0.0.1:5050 \
--fee-token strk \
--contract-address 0x4a739ab73aa3cac01f9da5d55f49fb67baee4919224454a2e3f85b16462a911 \
--function "some_function" \
--calldata 1 2 0x1e
--contract-address 0x522dc7cbe288037382a02569af5a4169531053d284193623948eac8dd051716 \
--function "add" \
--fee-token eth \
--arguments 'pokemons::model::PokemonData {'\
'name: "Magmar",'\
'element: pokemons::model::Element::Fire'\
'}'
```

<details>
<summary>Output:</summary>

```shell
command: invoke
transaction_hash: 0x7ad0d6e449e33b6581a4bb8df866c0fce3919a5ee05a30840ba521dafee217f
transaction_hash: 0x504f830428d0fcf462b4b814e2f67e12dfbcf3dc7847c1e36ba39d3eb7ac313

To see invocation details, visit:
transaction: https://starkscan.co/tx/0x7ad0d6e449...
transaction: https://sepolia.starkscan.co/tx/0x504f830428d0fcf462b4b814e2f67e12dfbcf3dc7847c1e36ba39d3eb7ac313
```
</details>
<br>
Expand Down
34 changes: 21 additions & 13 deletions docs/src/starknet/sncast-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ You can, however, overwrite their values by supplying them as flags directly to
Let's use `sncast` to call a contract's function:

```shell
$ sncast --account myuser \
call \
$ sncast call \
--url http://127.0.0.1:5050 \
--contract-address 0x38b7b9507ccf73d79cb42c2cc4e58cf3af1248f342112879bfdf5aa4f606cc9 \
--function get \
--calldata 0x0 \
--contract-address 0x522dc7cbe288037382a02569af5a4169531053d284193623948eac8dd051716 \
--function "pokemon" \
--arguments '"Charizard"' \
--block-id latest
```

Expand All @@ -42,7 +41,7 @@ $ sncast --account myuser \

```shell
command: call
response: [0x0]
response: [0x0, 0x0, 0x43686172697a617264, 0x9, 0x0, 0x0, 0x41a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf]
```
</details>
<br>
Expand All @@ -51,21 +50,30 @@ response: [0x0]
> In the above example we supply `sncast` with `--account` and `--url` flags. If `snfoundry.toml` is present, and have these properties set, values provided using these flags will override values from `snfoundry.toml`. Learn more about `snfoundry.toml` configuration [here](../projects/configuration.md#sncast).

### Calldata

Some `sncast` commands (namely `call`, `deploy` and `invoke`) allow passing *calldata* - a series of arguments to perform an action with on blockchain.
### Arguments

In the example above we called a function with an argument: `0x0`, passed using `--calldata` flag.
Some `sncast` commands (namely `call`, `deploy` and `invoke`) allow passing arguments to perform an action with on the blockchain.

Please note the notation of the argument. The default way of passing calldata is a list of hexadecimally encoded field elements - the *serialized* calldata.
To obtain the serialized form of the wished data, one must write a Cairo program calling `Serde::serialize` on subsequent arguments and displaying the results.
Under the hood cast always send request with serialized form of arguments, but it can be passed in
human-readable form thanks to the [calldata transformation](./calldata-transformation.md) feature present in Cast.

It is also possible to pass calldata in more friendly, human readable form thanks to the [calldata transformation](./calldata-transformation.md) feature present in Cast.
In the example above we called a function with a deserialized argument: `'"Charizard"'`, passed using `--arguments` flag.

> ⚠️ **Warning**
> Cast will not verify the serialized calldata. Any errors caused by passing improper calldata in a serialized form will originate from the network.
> Basic static analysis is possible only when passing expressions - see [calldata transformation](./calldata-transformation.md).

### Using Serialized Calldata

The same result can be achieved by passing serialized calldata, which is a list of hexadecimal-encoded field elements.

For example, this is equivalent to using the --calldata option with the following value: 0x0 0x43686172697a617264 0x9.

To obtain the serialized form of the wished data, you can write a Cairo program that calls `Serde::serialize` on subsequent arguments and displays the results.

Read more about it in the [Cairo documentation](https://book.cairo-lang.org/appendix-03-derivable-traits.html?highlight=seri#serializing-with-serde).

### How to Use `--wait` Flag

Let's invoke a transaction and wait for it to be `ACCEPTED_ON_L2`.
Expand Down

0 comments on commit ed61449

Please sign in to comment.