-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: document struct coded and add examples (#406)
* docs: document struct coded and add examples * refactor: move struct encoding to new misc section * fix: dont indent examples * chore: nits * Update src/reference/cast/cast-send.md Co-authored-by: Bjerg <onbjerg@users.noreply.github.com> * Update src/reference/cast/cast-send.md Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Misc | ||
|
||
- [Struct encoding](./struct-encoding.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters