From f79120ad88e5ed3260e248ab1605cc0adedc8d0e Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 07:56:07 -0700 Subject: [PATCH 01/13] Create abi_encoding.md --- .../blockchain-development/abi_encoding.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 docs/book/src/blockchain-development/abi_encoding.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..f88dd5ebd4b --- /dev/null +++ b/docs/book/src/blockchain-development/abi_encoding.md @@ -0,0 +1,32 @@ +# 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 format. + +## ABI encoding with the `abi_encode` and `abi_decode` function +This function will encode a structure into an ABI encoded bytes vector. + +All primitive and complex types have an `abi_encode` and `abi_decode` method. + +## Example +```sway +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); +} +``` From 294adadf06d3256179b36cf5b5d1679b7d23265f Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 07:59:46 -0700 Subject: [PATCH 02/13] Create main.sw --- examples/abi_encoding/src/main.sw | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/abi_encoding/src/main.sw diff --git a/examples/abi_encoding/src/main.sw b/examples/abi_encoding/src/main.sw new file mode 100644 index 00000000000..8b137891791 --- /dev/null +++ b/examples/abi_encoding/src/main.sw @@ -0,0 +1 @@ + From fdbd196d1fffefae14eceb482beb0a474c8a4165 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:00:15 -0700 Subject: [PATCH 03/13] Create Forc.toml --- examples/abi_encoding/Forc.toml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 examples/abi_encoding/Forc.toml 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" From ad037824c3ca6de7d01675182168c1520c475754 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:00:37 -0700 Subject: [PATCH 04/13] Update main.sw --- examples/abi_encoding/src/main.sw | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/examples/abi_encoding/src/main.sw b/examples/abi_encoding/src/main.sw index 8b137891791..b35cd381368 100644 --- a/examples/abi_encoding/src/main.sw +++ b/examples/abi_encoding/src/main.sw @@ -1 +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); +} From 2cb5204593a255152bf6d35abe4f97ab4b5e2f6b Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:01:23 -0700 Subject: [PATCH 05/13] Update abi_encoding.md --- .../src/blockchain-development/abi_encoding.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/docs/book/src/blockchain-development/abi_encoding.md b/docs/book/src/blockchain-development/abi_encoding.md index f88dd5ebd4b..5f233757a44 100644 --- a/docs/book/src/blockchain-development/abi_encoding.md +++ b/docs/book/src/blockchain-development/abi_encoding.md @@ -13,20 +13,5 @@ All primitive and complex types have an `abi_encode` and `abi_decode` method. ## Example ```sway -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); -} +{{#include ../../../../examples/abi_encoding/src/main.sw}} ``` From 7aec6872c08541f9672f82fc63cb28aa9ba243b8 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:02:16 -0700 Subject: [PATCH 06/13] Update Forc.toml --- examples/Forc.toml | 1 + 1 file changed, 1 insertion(+) 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", From 8346f97deb7e672356bcdf57e5102640b2df8c91 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:02:46 -0700 Subject: [PATCH 07/13] Update Forc.lock --- examples/Forc.lock | 8 ++++++++ 1 file changed, 8 insertions(+) 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" From aaf110668fde043c8fc1111b652c394cd606750d Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:04:13 -0700 Subject: [PATCH 08/13] Update index.md --- docs/book/src/blockchain-development/index.md | 1 + 1 file changed, 1 insertion(+) 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) From ea82b4b223085021b51e5ff228539b32328d28f5 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 28 Jun 2024 08:05:02 -0700 Subject: [PATCH 09/13] Update SUMMARY.md --- docs/book/src/SUMMARY.md | 1 + 1 file changed, 1 insertion(+) 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) From 1b33bdeb0e5d266e62d004267efa8576ebc4bfa7 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 30 Jun 2024 04:54:58 -0700 Subject: [PATCH 10/13] Update abi_encoding.md --- docs/book/src/blockchain-development/abi_encoding.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/book/src/blockchain-development/abi_encoding.md b/docs/book/src/blockchain-development/abi_encoding.md index 5f233757a44..fc733b8cea6 100644 --- a/docs/book/src/blockchain-development/abi_encoding.md +++ b/docs/book/src/blockchain-development/abi_encoding.md @@ -4,13 +4,15 @@ Application binary interface (ABI) encoding typically enables programs to commun 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 format. +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 bytes vector. +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}} From 4508e2d49707e606256fa0b77bf7749f5c920dfc Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 30 Jun 2024 04:56:56 -0700 Subject: [PATCH 11/13] Update abi_encoding.md Small nit for Markdown formatter. --- docs/book/src/blockchain-development/abi_encoding.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/book/src/blockchain-development/abi_encoding.md b/docs/book/src/blockchain-development/abi_encoding.md index fc733b8cea6..2a71278456a 100644 --- a/docs/book/src/blockchain-development/abi_encoding.md +++ b/docs/book/src/blockchain-development/abi_encoding.md @@ -7,6 +7,7 @@ The Sway language provides helpful traits and utilities to help with [Fuel ABI E 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. @@ -14,6 +15,7 @@ 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}} ``` From 9eaef17925de86e0b422877426be079e81bb1407 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 1 Jul 2024 01:52:50 -0700 Subject: [PATCH 12/13] Update main.sw --- examples/abi_encoding/src/main.sw | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/abi_encoding/src/main.sw b/examples/abi_encoding/src/main.sw index b35cd381368..b43679b603a 100644 --- a/examples/abi_encoding/src/main.sw +++ b/examples/abi_encoding/src/main.sw @@ -2,15 +2,15 @@ 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); } From 5c414816aba24214a7d3cdc6bf28807f7be59fb7 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 1 Jul 2024 01:54:27 -0700 Subject: [PATCH 13/13] Update spell-check-custom-words.txt --- docs/book/spell-check-custom-words.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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