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 all 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)
63 changes: 63 additions & 0 deletions src/misc/struct-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## Struct Encoding

Structs are user defined types that can group several variables:

```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`.

Solidity structs map to the ABI type "tuple". For more information on how Solidity types map to ABI types see [Mapping Solidity to ABI types](https://docs.soliditylang.org/en/latest/abi-spec.html#mapping-solidity-to-abi-types) in the Solidity documentation.

Structs are therefore encoded and decodes as tuples. So the struct we defined above, `MyStruct`, maps to the tuple `(address,uint256)` in terms of the ABI.

Let's see how this works in a contract:

```solidity
pragma solidity =0.8.15;


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

The ABI of the `f` function in this contract is:

```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`.
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 "myfunction((address,uint256))" 0x... "(0x...,1)"
```
### 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))"
```
### SEE ALSO

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