/sway-libs

Miscellaneous Sway libraries.

Primary LanguageRustApache License 2.0Apache-2.0

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.

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

Data Structures

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

Using a library

To import a library, a dependency should be added to the project's Forc.toml file under [dependencies]. The following shows adding the Merkle Proof Library.

merkle_proof = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.1.0" }

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 merkle_proof::<library_function>;

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

use merkle_proof::binary_merkle_proof::verify_proof;

Running Tests

There are two sets of tests that should be run: inline tests and sdk-harness tests.

In order to run the inline tests, make sure you are in the libs/ folder of this repository sway-libs/libs/<you are here>.

Run the tests:

forc test

Once these tests have passed, make sure you are in the tests/ folder of this repository sway-libs/tests/<you are here>.

Run the tests:

forc test && cargo test

NOTE: This may take a while depending on your hardware, future improvements to Sway will decrease build times. After this has been run once, indiviual 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.49.1, fuels-rs v0.53.0 and fuel-core 0.22.0.

Contributing

Check out the book for more info!