Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Getting Started

Icon LinkGetting Started

Icon LinkAdding Sway Libs as a Dependency

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 <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "MyProject"
 
[dependencies]
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" }
Icon InfoCircle

NOTE: Be sure to set the tag to the latest release.

Icon LinkImporting Sway Libs to Your Project

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;
Icon InfoCircle

NOTE: All projects currently use forc 0.63.3, fuels-rs v0.66.2 and fuel-core 0.34.0.

Icon LinkUsing Sway Libs

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 Icon Link.