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

Basic ABI Encoding Documentation #6198

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion docs/book/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ABI
abi
ABIs
ASM
IDE
Expand Down Expand Up @@ -210,4 +211,4 @@ namespacing
unsafety
prioritizations
polymorphism
ContractId
ContractId
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [Comments and Logging](./basics/comments_and_logging.md)
- [Control Flow](./basics/control_flow.md)
- [Blockchain Development with Sway](./blockchain-development/index.md)
- [ABI Encoding](./blockchain-development/abi_encoding.md)
- [Hashing and Cryptography](./blockchain-development/hashing_and_cryptography.md)
- [Contract Storage](./blockchain-development/storage.md)
- [Function Purity](./blockchain-development/purity.md)
Expand Down
21 changes: 21 additions & 0 deletions docs/book/src/blockchain-development/abi_encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ABI Encoding

Application binary interface (ABI) encoding typically enables programs to communicate with each other with the same data encoding system.

The Sway language provides helpful traits and utilities to help with [Fuel ABI Encoding](https://docs.fuel.network/docs/specs/abi/) within the language, which is used across Sway programs.

Sway, at its core, is agnostic to ABI encoding but preferences the [Fuel ABI Encoding](https://docs.fuel.network/docs/specs/abi/) format.

## ABI encoding with the `abi_encode` and `abi_decode` function

This function will encode a structure into an ABI encoded Buffer which contains a bytes vector.

All primitive and complex types have an `abi_encode` and `abi_decode` method.

For more information on how abi_encode works under the hood, please see the [ABI Encoding Specifications - Version 1](https://docs.fuel.network/docs/specs/abi/argument-encoding/#version-1).

## Example

```sway
{{#include ../../../../examples/abi_encoding/src/main.sw}}
```
1 change: 1 addition & 0 deletions docs/book/src/blockchain-development/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Sway is fundamentally a blockchain language. Because of this, it has some featur

These are also some concepts related to the FuelVM and Fuel ecosystem that you may utilize when writing Sway.

- [ABI Encoding](./abi_encoding.md)
- [Hashing and Cryptography](./hashing_and_cryptography.md)
- [Contract Storage](./storage.md)
- [Function Purity](./purity.md)
Expand Down
8 changes: 8 additions & 0 deletions examples/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ dependencies = [
"std",
]

[[package]]
name = "abi_encoding"
source = "member"
dependencies = [
"core",
"std",
]

[[package]]
name = "array"
source = "member"
Expand Down
1 change: 1 addition & 0 deletions examples/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[workspace]
members = [
# "abi_supertraits",
"abi_encoding",
"advanced_storage_variables",
"arrays",
"methods_and_associated_functions",
Expand Down
6 changes: 6 additions & 0 deletions examples/abi_encoding/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
implicit-std = false
license = "Apache-2.0"
name = "abi_encoding"
16 changes: 16 additions & 0 deletions examples/abi_encoding/src/main.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
script;

fn main() {
let value = u256::max();

// ABI Encode
let buffer = Buffer::new();
value.abi_encode(buffer);

// ABI Decode
let slice = buffer.as_raw_slice();
let mut reader = BufferReader::from_parts(slice.ptr(), slice.number_of_bytes());
let decoded_u256 = u256::abi_decode(reader);

assert(value == decoded_u256);
}
Loading