diff --git a/docs/book/spell-check-custom-words.txt b/docs/book/spell-check-custom-words.txt index 818c9a71b02..a96e5dfe38e 100644 --- a/docs/book/spell-check-custom-words.txt +++ b/docs/book/spell-check-custom-words.txt @@ -1,4 +1,5 @@ ABI +abi ABIs ASM IDE @@ -210,4 +211,4 @@ namespacing unsafety prioritizations polymorphism -ContractId \ No newline at end of file +ContractId diff --git a/docs/book/src/SUMMARY.md b/docs/book/src/SUMMARY.md index 78f0164d676..f259a5a97c4 100644 --- a/docs/book/src/SUMMARY.md +++ b/docs/book/src/SUMMARY.md @@ -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) diff --git a/docs/book/src/blockchain-development/abi_encoding.md b/docs/book/src/blockchain-development/abi_encoding.md new file mode 100644 index 00000000000..2a71278456a --- /dev/null +++ b/docs/book/src/blockchain-development/abi_encoding.md @@ -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}} +``` diff --git a/docs/book/src/blockchain-development/index.md b/docs/book/src/blockchain-development/index.md index 5d8645584f1..e3940fbbbe8 100644 --- a/docs/book/src/blockchain-development/index.md +++ b/docs/book/src/blockchain-development/index.md @@ -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) diff --git a/examples/Forc.lock b/examples/Forc.lock index 374ac64eae8..7c3215e3626 100644 --- a/examples/Forc.lock +++ b/examples/Forc.lock @@ -6,6 +6,14 @@ dependencies = [ "std", ] +[[package]] +name = "abi_encoding" +source = "member" +dependencies = [ + "core", + "std", +] + [[package]] name = "array" source = "member" diff --git a/examples/Forc.toml b/examples/Forc.toml index 2aa680daab9..4a110e8806c 100644 --- a/examples/Forc.toml +++ b/examples/Forc.toml @@ -3,6 +3,7 @@ [workspace] members = [ # "abi_supertraits", + "abi_encoding", "advanced_storage_variables", "arrays", "methods_and_associated_functions", diff --git a/examples/abi_encoding/Forc.toml b/examples/abi_encoding/Forc.toml new file mode 100644 index 00000000000..ce4b021fd06 --- /dev/null +++ b/examples/abi_encoding/Forc.toml @@ -0,0 +1,6 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +implicit-std = false +license = "Apache-2.0" +name = "abi_encoding" diff --git a/examples/abi_encoding/src/main.sw b/examples/abi_encoding/src/main.sw new file mode 100644 index 00000000000..b43679b603a --- /dev/null +++ b/examples/abi_encoding/src/main.sw @@ -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); +}