To import any library, the following dependency should be added to the project's Forc.toml
file under [dependencies]
.
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" }
For reference, here is a complete Forc.toml
file:
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "MyProject"
[dependencies]
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" }
NOTE: Be sure to set the tag to the latest release.
Once Sway Libs is a dependency to your project, you may then import a library in your Sway Smart Contract as so:
use sway_libs::<library>::<library_function>;
For example, to import the only_owner()
from the Ownership Library, use the following statement at the top of your Sway file:
use sway_libs::ownership::only_owner;
NOTE: All projects currently use
forc 0.63.3
,fuels-rs v0.66.2
andfuel-core 0.34.0
.
Once the library you require has been imported to your project, you may call or use any functions and structures the library provides.
In the following example, we import the Pausable Library and implement the Pausable
ABI with it's associated functions.
use sway_libs::pausable::{_is_paused, _pause, _unpause, Pausable};
// Implement the Pausable ABI for our contract
impl Pausable for Contract {
#[storage(write)]
fn pause() {
_pause(); // Call the provided pause function.
}
#[storage(write)]
fn unpause() {
_unpause(); // Call the provided unpause function.
}
#[storage(read)]
fn is_paused() -> bool {
_is_paused() // Call the provided is paused function.
}
}
Any instructions related to using a specific library should be found within the libraries section of the Sway Libs Book.
For implementation details on the libraries please see the Sway Libs Docs .