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

docs: document struct coded and add examples #406

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,4 @@
- [`addr`](./cheatcodes/addr.md)
- [`sign`](./cheatcodes/sign.md)
- [`label`](./cheatcodes/label.md)
- [Misc](./misc/README.md)
3 changes: 3 additions & 0 deletions src/misc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Misc

- [struct encoding](./struct-encoding.md)
onbjerg marked this conversation as resolved.
Show resolved Hide resolved
64 changes: 64 additions & 0 deletions src/misc/struct-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#### Struct Encoding
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

Structs are custom defined types that can group several variables:
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

```solidity
struct MyStruct {
address addr;
uint256 amount;
}
```

Only the new [ABI coder v2](https://docs.soliditylang.org/en/latest/layout-of-source-files.html#abi-coder-pragma) can encode and decode arbitrarily nested arrays and structs. Since Solidity 0.8.0 it is activated by default, prior to that it needs to be activated via `pragma experimental ABIEncoderV2`.

Structs map to the ABI type "tuple", See also [Mapping Solidity to ABI types](https://docs.soliditylang.org/en/latest/abi-spec.html#mapping-solidity-to-abi-types)
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

Structs are therefor encoded and decodes as tuples. So `MyStruct` is a `(address,uint256)` tuple when it comes to the ABI spec.
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

For example the ABI of
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

```solidity
pragma solidity =0.8.15;


contract Test {
struct MyStruct {
address addr;
uint256 amount;
}
function f(MyStruct memory t) public pure {}
}
```

results in the following ABI json object:
onbjerg marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"inputs": [
{
"components": [
{
"internalType": "address",
"name": "addr",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct Test.MyStruct",
"name": "t",
"type": "tuple"
}
],
"name": "f",
"outputs": [],
"stateMutability": "pure",
"type": "function"
}
```

which reads:
The function `f` takes 1 input of type `tuple` with two components of type `address` and `uint256`.
onbjerg marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 18 additions & 1 deletion src/reference/cast/cast-send.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ The destination (*to*) can be an ENS name or an address.
cast send --ledger 0x... "deposit(address,uint256)" 0x... 1
```

3. Call a function that expects a `struct`:

```solidity
contract Test {
struct MyStruct {
address addr;
uint256 amount;
}
function myfunction(MyStruct memory t) public pure {}
}
```

Structs are encoded as tuples, See [struct encoding](./reference/common/struct-encoding.md)

```sh
cast send --ledger 0x... "myfunction((address,uint256))" 0x... 1
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Structs are encoded as tuples, See [struct encoding](./reference/common/struct-encoding.md)
```sh
cast send --ledger 0x... "myfunction((address,uint256))" 0x... 1
```
Structs are encoded as tuples (see [struct encoding](./reference/common/struct-encoding.md))
```sh
cast send --ledger 0x... "myfunction((address,uint256))" 0x... 1
```

Also, would this not be e.g. cast send "myfunction((address,uint256))" 0x... "(0x..., 1)"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USAGE:
    cast send [OPTIONS] <TO> [ARGS]

ARGS:
    <TO>
            The destination of the transaction.

    <SIG>
            The signature of the function to call.

    <ARGS>...
            The arguments of the function to call.

so cast send <to> <sig> <args> is correct here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye but args in this case would be "(0x..., 1)". The current example is:

cast send --ledger 0x... "myfunction((address,uint256))" 0x... 1

In other words:

Send using Ledger addr 0x..., call "myfunction" at address 0x... with argument 1?

Copy link
Member Author

@mattsse mattsse Jun 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess that makes sense, I only copy pasted the other example tbh

### SEE ALSO

[cast](./cast.md), [cast call](./cast-call.md), [cast publish](./cast-publish.md), [cast receipt](./cast-receipt.md)
[cast](./cast.md), [cast call](./cast-call.md), [cast publish](./cast-publish.md), [cast receipt](./cast-receipt.md), [struct encoding](./misc/struct-encoding.md)
19 changes: 18 additions & 1 deletion src/reference/cast/cast-sig.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ The signature (*sig*) is a fragment in the form `<function name>(<types...>)`.
cast sig "transfer(address,uint256)"
```

2. Get the selector for a function that expects a `struct`:

```solidity
contract Test {
struct MyStruct {
address addr;
uint256 amount;
}
function myfunction(MyStruct memory t) public pure {}
}
```

Structs are encoded as tuples, See [struct encoding](./reference/common/struct-encoding.md)

```sh
cast sig "myfunction((address,uint256))"
```
onbjerg marked this conversation as resolved.
Show resolved Hide resolved
### SEE ALSO

[cast](./cast.md)
[cast](./cast.md), [struct encoding](./misc/struct-encoding.md)