-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add library code along with the imports info
- Loading branch information
Showing
4 changed files
with
54 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |