From e97f62c372f1319f69b21b6f04cf80edbe3d0426 Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Thu, 15 Aug 2024 18:29:35 +0530 Subject: [PATCH 1/6] Add library example --- examples/library/.gitignore | 2 ++ examples/library/Forc.lock | 29 +++++++++++++++++++++++++++++ examples/library/Forc.toml | 8 ++++++++ examples/library/src/main.sw | 15 +++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 examples/library/.gitignore create mode 100644 examples/library/Forc.lock create mode 100644 examples/library/Forc.toml create mode 100644 examples/library/src/main.sw diff --git a/examples/library/.gitignore b/examples/library/.gitignore new file mode 100644 index 0000000..77d3844 --- /dev/null +++ b/examples/library/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/examples/library/Forc.lock b/examples/library/Forc.lock new file mode 100644 index 0000000..6e75904 --- /dev/null +++ b/examples/library/Forc.lock @@ -0,0 +1,29 @@ +[[package]] +name = "core" +source = "path+from-root-0DAF035ABF1276AD" + +[[package]] +name = "library" +source = "member" +dependencies = [ + "std", + "sway_libs", +] + +[[package]] +name = "standards" +source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.5.0#348f7175df4c012b23c86cdb18aab79025ca1f18" +dependencies = ["std"] + +[[package]] +name = "std" +source = "git+https://github.com/fuellabs/sway?rev#4c2c38430264472221dd4c68142013d819843b01" +dependencies = ["core"] + +[[package]] +name = "sway_libs" +source = "git+https://github.com/FuelLabs/sway-libs?tag=v0.22.0#172adbbcc36e561a56c5820209445f86f0856bfc" +dependencies = [ + "standards", + "std", +] diff --git a/examples/library/Forc.toml b/examples/library/Forc.toml new file mode 100644 index 0000000..6615afb --- /dev/null +++ b/examples/library/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Nazeeh Vahora"] +entry = "main.sw" +license = "Apache-2.0" +name = "library" + +[dependencies] +sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.22.0" } \ No newline at end of file diff --git a/examples/library/src/main.sw b/examples/library/src/main.sw new file mode 100644 index 0000000..46e3bbc --- /dev/null +++ b/examples/library/src/main.sw @@ -0,0 +1,15 @@ +library; + +use std::hash::sha256; +use std::hash::Hash; + +/// Generates a SHA-256 hash of the input. +pub fn generate_sha256(input: T) -> b256 { + sha256(input) +} + +/// Verifies if the SHA-256 hash of the input matches the provided hash. +pub fn verify_sha256(input: T, expected_hash: b256) -> bool { + let computed_hash = sha256(input); + computed_hash == expected_hash +} From 5de2061b0b26a627c09e80a4a14dbc1d720e9b8f Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Fri, 16 Aug 2024 18:32:39 +0530 Subject: [PATCH 2/6] Add library code along with the imports info --- examples/library/Forc.lock | 16 ----------- examples/library/Forc.toml | 1 - examples/library/src/main.sw | 49 +++++++++++++++++++++++++------- examples/library/src/sqrt_lib.sw | 16 +++++++++++ 4 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 examples/library/src/sqrt_lib.sw diff --git a/examples/library/Forc.lock b/examples/library/Forc.lock index 6e75904..df1470d 100644 --- a/examples/library/Forc.lock +++ b/examples/library/Forc.lock @@ -5,25 +5,9 @@ source = "path+from-root-0DAF035ABF1276AD" [[package]] name = "library" source = "member" -dependencies = [ - "std", - "sway_libs", -] - -[[package]] -name = "standards" -source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.5.0#348f7175df4c012b23c86cdb18aab79025ca1f18" dependencies = ["std"] [[package]] name = "std" source = "git+https://github.com/fuellabs/sway?rev#4c2c38430264472221dd4c68142013d819843b01" dependencies = ["core"] - -[[package]] -name = "sway_libs" -source = "git+https://github.com/FuelLabs/sway-libs?tag=v0.22.0#172adbbcc36e561a56c5820209445f86f0856bfc" -dependencies = [ - "standards", - "std", -] diff --git a/examples/library/Forc.toml b/examples/library/Forc.toml index 6615afb..05b70f9 100644 --- a/examples/library/Forc.toml +++ b/examples/library/Forc.toml @@ -5,4 +5,3 @@ license = "Apache-2.0" name = "library" [dependencies] -sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.22.0" } \ No newline at end of file diff --git a/examples/library/src/main.sw b/examples/library/src/main.sw index 46e3bbc..372a109 100644 --- a/examples/library/src/main.sw +++ b/examples/library/src/main.sw @@ -1,15 +1,42 @@ -library; +contract; -use std::hash::sha256; -use std::hash::Hash; +// 1. Importing within the same project +// Using "mod" keyword, you can define the library as a dependency within a program. +mod sqrt_lib; -/// Generates a SHA-256 hash of the input. -pub fn generate_sha256(input: T) -> b256 { - sha256(input) -} +// It is a good practice to import in ABI +// It is also a good practice to define events and custom errors using this way + +// Using "use" keyword imports in a library +// use srt_lib::*; + +// 2. Importing the standard library +// The standard library consists of +// a. language primitives +// b. blockchain contextual operations +// c. native asset management +// etc. +// Functions like msg.sender(), block.timestamp(),etc are found here https://github.com/FuelLabs/sway/tree/master/sway-lib-std +// use std::{ +// identity::*, +// address::*, +// constants::*, +// auth::msg_sender, +// }; -/// Verifies if the SHA-256 hash of the input matches the provided hash. -pub fn verify_sha256(input: T, expected_hash: b256) -> bool { - let computed_hash = sha256(input); - computed_hash == expected_hash +// 3. Importing from a different project +// If any library is not listed as a dependency, but present in forc.toml, you can use it as below. +// Math libraries copied from https://github.com/sway-libs/concentrated-liquidity/ +// use math_lib::full_math::*; + +use ::sqrt_lib::math_sqrt; + +abi TestMath { + fn test_square_root(x: u256) -> u256; } + +impl TestMath for Contract { + fn test_square_root(x: u256) -> u256 { + math_sqrt(x) + } +} \ No newline at end of file diff --git a/examples/library/src/sqrt_lib.sw b/examples/library/src/sqrt_lib.sw new file mode 100644 index 0000000..87d5de9 --- /dev/null +++ b/examples/library/src/sqrt_lib.sw @@ -0,0 +1,16 @@ +library; + +pub fn math_sqrt(y: u256) -> u256 { + let mut z: u256 = 0; + if y > 3 { + z = y; + let mut x = y / 2 + 1; + while x < z { + z = x; + x = (y / x + x) / 2; + } + } else if y != 0 { + z = 1; + } + z +} From 8e7540950293c2505e33fc08a58589ca18436fb6 Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Fri, 16 Aug 2024 18:42:09 +0530 Subject: [PATCH 3/6] Update main.sw --- examples/library/src/main.sw | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/examples/library/src/main.sw b/examples/library/src/main.sw index 372a109..154cd07 100644 --- a/examples/library/src/main.sw +++ b/examples/library/src/main.sw @@ -1,14 +1,31 @@ contract; // 1. Importing within the same project -// Using "mod" keyword, you can define the library as a dependency within a program. +// Using "mod" keyword, you can import an internal library that has been defined in this project. mod sqrt_lib; // It is a good practice to import in ABI // It is also a good practice to define events and custom errors using this way -// Using "use" keyword imports in a library -// use srt_lib::*; +// Using "use" keyword imports in a library. This method is used to import an external lilbray that is defined outside the main `src` directory. +// use sqrt_lib::math_sqrt; +// $ tree +// . +// ├── my_project +// │ ├── Cargo.toml +// │ ├── Forc.toml +// │ └─── src +// │ └── main.sw +// │ +// └── external_lib +// ├── Cargo.toml +// ├── Forc.toml +// └─── src +// └── lib.sw +// External library is outside the src directory of our project. Thus, it needs to be added as a dependency in the Forc.toml of our project. +// [dependencies] +// external_lib = { path = "../external_lib" } + // 2. Importing the standard library // The standard library consists of @@ -24,6 +41,7 @@ mod sqrt_lib; // auth::msg_sender, // }; + // 3. Importing from a different project // If any library is not listed as a dependency, but present in forc.toml, you can use it as below. // Math libraries copied from https://github.com/sway-libs/concentrated-liquidity/ From c92dc9e4d109fbe6faf84ef6c7ae36932c4d6166 Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Mon, 19 Aug 2024 20:14:12 +0530 Subject: [PATCH 4/6] Update `Summary.md` and `actions.yaml` --- .github/workflows/actions.yaml | 1 + docs/src/SUMMARY.md | 1 + docs/src/library.md | 5 +++++ 3 files changed, 7 insertions(+) create mode 100644 docs/src/library.md diff --git a/.github/workflows/actions.yaml b/.github/workflows/actions.yaml index 3249255..c8a29db 100644 --- a/.github/workflows/actions.yaml +++ b/.github/workflows/actions.yaml @@ -70,6 +70,7 @@ jobs: 'variables', 'math_lib' 'imports', + 'library' ] name: Forc build [ ${{ matrix.dir }} ] diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 0d8d2f5..e0a7db4 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -24,3 +24,4 @@ - [Storage Map](./storage-map.md) - [Vector](./vector.md) - [Base Asset](./base-asset.md) +- [Library](./library.md) \ No newline at end of file diff --git a/docs/src/library.md b/docs/src/library.md new file mode 100644 index 0000000..d46f5fa --- /dev/null +++ b/docs/src/library.md @@ -0,0 +1,5 @@ +# Library + +```sway +{{#include ../examples/library/src/main.sw}} +``` \ No newline at end of file From 74749338882aa758cb5a5e3e6c3e4f7ab375c157 Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Mon, 19 Aug 2024 20:21:39 +0530 Subject: [PATCH 5/6] Update library.md --- docs/src/library.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/src/library.md b/docs/src/library.md index d46f5fa..3a1f8a9 100644 --- a/docs/src/library.md +++ b/docs/src/library.md @@ -1,5 +1,8 @@ # Library +Example on how to create a library in Sway and how to use it in your Smart Contract. +This example also showcases how to use different types of imports in Sway depending on external library or library from the same project. + ```sway {{#include ../examples/library/src/main.sw}} ``` \ No newline at end of file From 8639803ef6c5f15269858f00ab9dd82df1a00b8a Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Mon, 19 Aug 2024 20:22:46 +0530 Subject: [PATCH 6/6] fix markdown linting error --- docs/src/library.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/library.md b/docs/src/library.md index 3a1f8a9..169936d 100644 --- a/docs/src/library.md +++ b/docs/src/library.md @@ -5,4 +5,4 @@ This example also showcases how to use different types of imports in Sway depend ```sway {{#include ../examples/library/src/main.sw}} -``` \ No newline at end of file +```