Skip to content

Commit

Permalink
Add library code along with the imports info
Browse files Browse the repository at this point in the history
  • Loading branch information
naz3eh committed Aug 16, 2024
1 parent e97f62c commit 5de2061
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
16 changes: 0 additions & 16 deletions examples/library/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
1 change: 0 additions & 1 deletion examples/library/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ license = "Apache-2.0"
name = "library"

[dependencies]
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.22.0" }
49 changes: 38 additions & 11 deletions examples/library/src/main.sw
Original file line number Diff line number Diff line change
@@ -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<T: Hash>(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<T: Hash>(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)
}
}
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
}

0 comments on commit 5de2061

Please sign in to comment.