Skip to content

FuelLabs/sway-libs

Repository files navigation

SwayLibs logo

Overview

The purpose of this repository is to contain libraries which users can import and use that are not part of the standard library.

These libraries contain helper functions and other tools valuable to blockchain development.

NOTE: Sway is a language under heavy development therefore the libraries may not be the most ergonomic. Over time they should receive updates / improvements in order to demonstrate how Sway can be used in real use cases.

Sway Libs Docs Hub

Please refer to the Sway Libs Docs Hub for documentation for a general overview on Sway Libs and how to implement libraries.

Library Docs

For implementation details on the libraries please see the Sway Libs Docs.

Libraries

Assets

Access Control and Security

  • Ownership is used to apply restrictions on functions such that only a single user may call them.
  • Admin is used to apply restrictions on functions such that only a select few users may call them like a whitelist.
  • Pausable allows contracts to implement an emergency stop mechanism.
  • Reentrancy is used to detect and prevent reentrancy attacks.

Cryptography

  • Bytecode is used for on-chain verification and computation of bytecode roots for contracts and predicates.
  • Merkle Proof is used to verify Binary Merkle Trees computed off-chain.

Math

NOTE: The Fixed Point Number library has been deprecated pending a re-write.

Data Structures

  • Queue is a linear data structure that provides First-In-First-Out (FIFO) operations.

Upgradability Libraries

  • Upgradability provides functions that can be used to implement contract upgrades via simple upgradable proxies.

Using a library

To import a 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.1" }

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

You may then import your desired library in your Sway Smart Contract as so:

use sway_libs::<library>::<library_function>;

For example, to import the only_owner() function use the following statement:

use sway_libs::ownership::only_owner;

For more information about implementation please refer to the Sway Libs Docs Hub

Running Tests

There are two sets of tests that should be run: inline tests and sdk-harness tests. Please make sure you are using forc v0.66.2 and fuel-core v0.40.0. You can check what version you are using by running the fuelup show command.

Make sure you are in the source directory of this repository sway-libs/<you are here>.

Run the inline tests:

forc test --path libs --release --locked

Once these tests have passed, run the sdk-harness tests:

forc test --path tests --release --locked && cargo test --manifest-path tests/Cargo.toml

NOTE: This may take a while depending on your hardware, future improvements to Sway will decrease build times. After this has been run once, individual test projects may be built on their own to save time.

Any instructions related to using a specific library should be found within the README.md of that library.

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

Contributing

Check out the contributing book for more info!