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

Add library section #43

Merged
merged 7 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
'variables',
'math_lib',
'imports',
'library',
'predicate',
]

Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions docs/src/library.md
Original file line number Diff line number Diff line change
@@ -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}}
```
2 changes: 2 additions & 0 deletions examples/library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
13 changes: 13 additions & 0 deletions examples/library/Forc.lock
Original file line number Diff line number Diff line change
@@ -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"]
7 changes: 7 additions & 0 deletions examples/library/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Nazeeh Vahora"]
entry = "main.sw"
license = "Apache-2.0"
name = "library"

[dependencies]
60 changes: 60 additions & 0 deletions examples/library/src/main.sw
Original file line number Diff line number Diff line change
@@ -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)
}
}
16 changes: 16 additions & 0 deletions examples/library/src/sqrt_lib.sw
Original file line number Diff line number Diff line change
@@ -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
}
Loading