diff --git a/.github/workflows/actions.yaml b/.github/workflows/actions.yaml index 40446c4..3253b62 100644 --- a/.github/workflows/actions.yaml +++ b/.github/workflows/actions.yaml @@ -73,6 +73,7 @@ jobs: 'variables', 'math_lib', 'imports', + 'library', 'predicate', ] diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 1d22e5e..6f446d5 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -24,5 +24,6 @@ - [Storage Map](./storage-map.md) - [Vector](./vector.md) - [Base Asset](./base-asset.md) +- [Library](./library.md) - [Predicate](./predicate.md) - [SRC20](./src20.md) diff --git a/docs/src/library.md b/docs/src/library.md new file mode 100644 index 0000000..169936d --- /dev/null +++ b/docs/src/library.md @@ -0,0 +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}} +``` 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..df1470d --- /dev/null +++ b/examples/library/Forc.lock @@ -0,0 +1,13 @@ +[[package]] +name = "core" +source = "path+from-root-0DAF035ABF1276AD" + +[[package]] +name = "library" +source = "member" +dependencies = ["std"] + +[[package]] +name = "std" +source = "git+https://github.com/fuellabs/sway?rev#4c2c38430264472221dd4c68142013d819843b01" +dependencies = ["core"] diff --git a/examples/library/Forc.toml b/examples/library/Forc.toml new file mode 100644 index 0000000..05b70f9 --- /dev/null +++ b/examples/library/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Nazeeh Vahora"] +entry = "main.sw" +license = "Apache-2.0" +name = "library" + +[dependencies] diff --git a/examples/library/src/main.sw b/examples/library/src/main.sw new file mode 100644 index 0000000..154cd07 --- /dev/null +++ b/examples/library/src/main.sw @@ -0,0 +1,60 @@ +contract; + +// 1. Importing within the same project +// 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. 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 +// 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, +// }; + + +// 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 +}