Skip to content

Commit

Permalink
Refactor libraries into their own pkgs under a libs workspace (#78)
Browse files Browse the repository at this point in the history
I came across this while experimenting with a prototype for
FuelLabs/sway#3752 which involved depending on
the `nft` lib, so I thought I'd put together a PR!

Currently, all libraries under sway-libs are combined under a single
forc package. This means that when using sway-libs as a dependency, forc
requires type-checking all inner libraries, even if the user only wishes
to use one of the libraries.

This PR aims to split up the sway-libs package into a unique package for
each inner library. A libs workspace is introduced to make building the
libraries together a little easier.

**Breaking**

Note that this will break downstream packages that currently depend on
sway-libs as a single library. The README has been updated to
demonstrate the new approach for importing libraries from the sway-libs
repo.
  • Loading branch information
mitchmindtree authored Jan 18, 2023
1 parent afc84fb commit c265eff
Show file tree
Hide file tree
Showing 108 changed files with 175 additions and 122 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ jobs:
components: forc@${{ env.FORC_VERSION }}, fuel-core@${{ env.CORE_VERSION }}

- name: Check Sway formatting
run: forc fmt --path sway_libs --check
run: forc fmt --path libs --check

- name: Check Rust formatting
run: cargo fmt --manifest-path tests/Cargo.toml --verbose --check

- name: Build sway-libs
run: forc build --path sway_libs
- name: Build All Libs
run: forc build --path libs

- name: Build All Tests
run: forc build --path tests
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Forc.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# Ignore forc's output artifacts
out
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ These libraries contain helper functions, generalized standards, and other tools
### Libraries

- [Binary Merkle Proof](./sway_libs/src/merkle_proof/) is used to verify Binary Merkle Trees computed off-chain.
- [Non-Fungible Token (NFT)](./sway_libs/src/nft/) is a token library which provides unqiue collectibles, identified and differentiated by token IDs.
- [Ownership](./sway_libs/src/ownership/) is used to apply restrictions on functions such that only a single user may call them.
- [String](./sway_libs/src/string/) is an interface to implement dynamic length strings that are UTF-8 encoded.
- [Signed Integers](./sway_libs/src/signed_integers/) is an interface to implement signed integers.
- [Unsigned Fixed Point Number](./sway_libs/src/fixed_point/ufp/) is an interface to implement fixed-point numbers.
- [StorageMapVec](./sway_libs/src/storagemapvec/) is a temporary workaround for a StorageMap<K, StorageVec<V>> type
- [Binary Merkle Proof](./libs/merkle_proof/) is used to verify Binary Merkle Trees computed off-chain.
- [Non-Fungible Token (NFT)](./libs/nft/) is a token library which provides unqiue collectibles, identified and differentiated by token IDs.
- [Ownership](./libs/ownership/) is used to apply restrictions on functions such that only a single user may call them.
- [String](./libs/string/) is an interface to implement dynamic length strings that are UTF-8 encoded.
- [Signed Integers](./libs/signed_integers/) is an interface to implement signed integers.
- [Unsigned Fixed Point Number](./libs/fixed_point/) is an interface to implement fixed-point numbers.
- [StorageMapVec](./libs/storagemapvec/) is a temporary workaround for a StorageMap<K, StorageVec<V>> type

## Using a library

To import the Merkle Proof library the following should be added to the project's `Forc.toml` file under `[dependencies]` with the most recent release:

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

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

```rust
use sway_libs::<library_name>::<library_function>;
use merkle_proof::<library_function>;
```

For example, to import the Merkle Proof library use the following statement:

```rust
use sway_libs::binary_merkle_proof::verify_proof;
merkle_proof::binary_merkle_proof::verify_proof;
```

## Running Tests
Expand Down
10 changes: 10 additions & 0 deletions libs/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[workspace]
members = [
"fixed_point",
"merkle_proof",
"nft",
"ownership",
"signed_integers",
"storagemapvec",
"string",
]
4 changes: 1 addition & 3 deletions sway_libs/Forc.toml → libs/fixed_point/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "sway_libs"

[dependencies]
name = "fixed_point"
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ For more information please see the [specification](./SPECIFICATION.md).

## Getting Started

In order to use the `UFP64` or `UFP128` type it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [README.md](../../../../../README.md).
First, add the `fixed_point` library as a dependency in your Forc.toml like so:

```toml
fixed_point = { git = "https://github.com/fuellabs/sway-libs", branch = "master" }
```

In order to use the `UFP64` or `UFP128` types, import them into your Sway project like so.

```rust
use sway_libs::ufp64::UFP64;
use sway_libs::ufp128::UFP128;
use fixed_point::ufp64::UFP64;
use fixed_point::ufp128::UFP128;
```

Once imported, a `UFP64` or `UFP128` type can be instantiated by defining a new variable and calling the `from` function.
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions libs/fixed_point/src/lib.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library fixed_point;

dep ufp64;
dep ufp128;
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions libs/merkle_proof/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "merkle_proof"
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions libs/merkle_proof/src/lib.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library merkle_proof;

dep binary_merkle_proof;
5 changes: 5 additions & 0 deletions libs/nft/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "nft"
10 changes: 5 additions & 5 deletions sway_libs/src/nft/README.md → libs/nft/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ For more information please see the [specification](./SPECIFICATION.md).
In order to use the `NFT` library, Sway-libs must be added as a dependency to the project's Forc.toml file. To add Sway-libs as a dependency to the Forc.toml file in your project please see the [README.md](../../../README.md). Once this has been done, you may import the `NFT` library using the `use` keyword and explicitly state which functions you will be using. For a list of all functions please see the [specification](./SPECIFICATION.md).

```rust
use sway_libs::nft::{
use nft::{
approve,
mint,
owner_of,
Expand Down Expand Up @@ -68,9 +68,9 @@ approve(approved_user, token_id);
There are a number of different extensions which you may use to further enchance your non-fungible tokens.

These include:
1. [Metadata](./extensions/meta_data/meta_data.sw)
2. [Burning functionality](./extensions/burnable/burnable.sw)
3. [Administrative capabilities](./extensions/administrator/administrator.sw)
4. [Supply limits](./extensions/supply/supply.sw)
1. [Metadata](./src/extensions/meta_data/meta_data.sw)
2. [Burning functionality](./src/extensions/burnable/burnable.sw)
3. [Administrative capabilities](./src/extensions/administrator/administrator.sw)
4. [Supply limits](./src/extensions/supply/supply.sw)

For more information please see the [specification](./SPECIFICATION.md).
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dep administrator_events;

use administrator_errors::AdminError;
use administrator_events::AdminEvent;
use ::nft::nft_storage::ADMIN;
use ::nft_storage::ADMIN;
use std::{auth::msg_sender, logging::log, storage::{get, store}};

abi Administrator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library burnable;
dep burnable_events;

use burnable_events::BurnEvent;
use ::nft::{errors::{AccessError, InputError}, nft_core::NFTCore, nft_storage::{BALANCES, TOKENS}};
use ::{errors::{AccessError, InputError}, nft_core::NFTCore, nft_storage::{BALANCES, TOKENS}};
use std::{auth::msg_sender, hash::sha256, logging::log, storage::{get, store}};

abi Burn {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library supply;
dep supply_errors;
dep supply_events;

use ::nft::nft_storage::MAX_SUPPLY;
use ::nft_storage::MAX_SUPPLY;
use std::{logging::log, storage::{get, store}};
use supply_errors::SupplyError;
use supply_events::SupplyEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library token_metadata;
dep token_metadata_structures;

use token_metadata_structures::NFTMetadata;
use ::nft::{errors::InputError, nft_core::NFTCore, nft_storage::{TOKEN_METADATA, TOKENS}};
use ::{errors::InputError, nft_core::NFTCore, nft_storage::{TOKEN_METADATA, TOKENS}};
use std::{hash::sha256, storage::{get, store}};

pub trait TokenMetadata<T> {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions libs/ownership/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "ownership"
10 changes: 8 additions & 2 deletions sway_libs/src/ownership/README.md → libs/ownership/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ For more information please see the [specification](./SPECIFICATION.md).

## Getting Started

In order to use the `Ownership` library it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [README.md](../../../README.md).
In order to use the `ownership` library it must be added to the Forc.toml file and then imported into your Sway project.

```toml
ownership = { git = "https://github.com/fuellabs/sway-libs", branch = "master" }
```

You may import items like so:

```rust
use sway_libs::ownable::{only_owner, owner, set_ownership};
use ownership::ownable::{only_owner, owner, set_ownership};
```

Once imported, an owner can be set by calling the `set_ownership` function.
Expand Down
File renamed without changes.
Binary file added libs/ownership/out/debug/ownership.bin
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions libs/ownership/src/lib.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library ownership;

dep ownable;
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions libs/signed_integers/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "signed_integers"
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ For more information please see the [specification](./SPECIFICATION.md).

## Getting Started

In order to use the `Signed Integers` type it must be added to the Forc.toml file and then imported into your Sway project. To add Sway-libs as a dependency to the Forc.toml in your project, please see the [README.md](../../../README.md).
In order to use the `Signed Integers` type it must be added to the Forc.toml file.

```toml
signed_integers = { git = "https://github.com/fuellabs/sway-libs", branch = "master" }
```

Then we can import types like so:

```rust
use sway_libs::i8::I8;
use signed_integers::i8::I8;
```

Once imported, a `Signed Integer` type can be instantiated defining a new variable and calling the `new` function.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library i128;

use std::u128::U128;
use ::signed_integers::errors::Error;
use ::errors::Error;

/// The 128-bit signed integer type.
/// Represented as an underlying U128 value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library i16;

use ::signed_integers::errors::Error;
use ::errors::Error;

/// The 16-bit signed integer type.
/// Represented as an underlying u16 value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library i256;

use std::u256::U256;
use ::signed_integers::errors::Error;
use ::errors::Error;

/// The 256-bit signed integer type.
/// Represented as an underlying U256 value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library i32;

use ::signed_integers::errors::Error;
use ::errors::Error;

/// The 32-bit signed integer type.
/// Represented as an underlying u32 value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library i64;

use ::signed_integers::errors::Error;
use ::errors::Error;

/// The 64-bit signed integer type.
/// Represented as an underlying u64 value.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library i8;

use ::signed_integers::errors::Error;
use ::errors::Error;

/// The 8-bit signed integer type.
/// Represented as an underlying u8 value.
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions libs/storagemapvec/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "storagemapvec"
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Once imported, using the StorageMapVec library is as simple as making a storage
## Example

```rust
use sway_libs::storagemapvec::StorageMapVec;
use storagemapvec::StorageMapVec;

storage {
mapvec: StorageMapVec<u64, u64> = StorageMapVec {},
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -162,7 +162,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down Expand Up @@ -349,7 +349,7 @@ impl<K, V> StorageMapVec<K, V> {
/// ### Examples
///
/// ```sway
/// use sway_libs::storagemapvec::StorageMapVec;
/// use storagemapvec::StorageMapVec;
///
/// storage {
/// map_vec: StorageMapVec<u64, bool> = StorageMapVec {}
Expand Down
Loading

0 comments on commit c265eff

Please sign in to comment.