Skip to content

Commit

Permalink
Release v0.24.2 (#325)
Browse files Browse the repository at this point in the history
## [Version v0.24.2]

### Added v0.24.2

- [#318](#318) Adds further
documentation and examples for the `signed_integers` library.
- [#319](#319) Adds further
documentation and examples for the ownership library.
- [#322](#320) Adds further
documentation and examples for the asset metadata library.

### Changed v0.24.2

- [#323](#323) Updates the
repository to forc `v0.66.6`.
- [#324](#324) Prepares for
the `v0.24.2` release.

### Fixed v0.24.2

- None

### Breaking v0.24.2

- None
  • Loading branch information
bitzoic authored Feb 4, 2025
1 parent 9a94bfc commit 55d503c
Show file tree
Hide file tree
Showing 32 changed files with 527 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
CARGO_TERM_COLOR: always
REGISTRY: ghcr.io
RUST_VERSION: 1.83.0
FORC_VERSION: 0.66.2
FORC_VERSION: 0.66.6
CORE_VERSION: 0.40.0

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

env:
RUST_VERSION: 1.83.0
FORC_VERSION: 0.66.2
FORC_VERSION: 0.66.6
CORE_VERSION: 0.40.0

jobs:
Expand Down
25 changes: 12 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
## [Version v0.24.2]

### Added
### Added v0.24.2

- Something new here 1
- Something new here 2
- [#318](https://github.com/FuelLabs/sway-libs/pull/318) Adds further documentation and examples for the `signed_integers` library.
- [#319](https://github.com/FuelLabs/sway-libs/pull/319) Adds further documentation and examples for the ownership library.
- [#322](https://github.com/FuelLabs/sway-libs/pull/320) Adds further documentation and examples for the asset metadata library.

### Changed
### Changed v0.24.2

- Something changed here 1
- Something changed here 2
- [#323](https://github.com/FuelLabs/sway-libs/pull/323) Updates the repository to forc `v0.66.6`.
- [#324](https://github.com/FuelLabs/sway-libs/pull/324) Prepares for the `v0.24.2` release.

### Fixed
### Fixed v0.24.2

- Some fix here 1
- Some fix here 2
- None

### Breaking
### Breaking v0.24.2

- Some breaking change here 1
- Some breaking change here 2
- None

## [Version 0.24.1]

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[package]
name = "sway-libs"
version = "0.24.1"
version = "0.24.2"
edition = "2021"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<a href="https://github.com/FuelLabs/sway-libs/actions/workflows/ci.yml" alt="CI">
<img src="https://github.com/FuelLabs/sway-libs/actions/workflows/ci.yml/badge.svg" />
</a>
<a href="https://crates.io/crates/forc/0.63.3" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.63.3-orange" />
<a href="https://crates.io/crates/forc/0.66.6" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.66.6-orange" />
</a>
<a href="./LICENSE" alt="forc">
<img src="https://img.shields.io/github/license/FuelLabs/sway-libs" />
Expand Down Expand Up @@ -75,7 +75,7 @@ For implementation details on the libraries please see the [Sway Libs Docs](http
To import a library, the following dependency should be added to the project's `Forc.toml` file under `[dependencies]`.

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

> **NOTE:**
Expand All @@ -97,7 +97,7 @@ For more information about implementation please refer to the [Sway Libs Docs Hu

## 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.
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.6` 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>`.

Expand All @@ -119,7 +119,7 @@ forc test --path tests --release --locked && cargo test --manifest-path tests/Ca
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`.
> All projects currently use `forc v0.66.6`, `fuels-rs v0.66.9` and `fuel-core v0.40.0`.
## Contributing

Expand Down
3 changes: 2 additions & 1 deletion docs/book/spell-check-custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,5 @@ functionly
verifiably
upgradable
upgradability
Onchain
Onchain
representable
39 changes: 38 additions & 1 deletion docs/book/src/asset/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ Once imported, the Asset Library's metadata functionality should be available. T

### Setting Metadata

To set some metadata for an Asset, use the `SetAssetMetadata` ABI provided by the Asset Library with the `_set_metadata()` function. Be sure to follow the [SRC-9](https://docs.fuel.network/docs/sway-standards/src-9-metadata-keys/) standard for your `key`. It is recommended that the [Ownership Library](../ownership/index.md) is used in conjunction with the `SetAssetMetadata` ABI to ensure only a single user has permissions to set an Asset's metadata.
As described in the [SRC-7](https://docs.fuel.network/docs/sway-standards/src-7-asset-metadata/) standard, the metadata type is a simple enum of the following types:

- `b256`
- `Bytes`
- `u64`
- `String`

To set some metadata of any of the above types for an Asset, you can use the `SetAssetMetadata` ABI provided by the Asset Library with the `_set_metadata()` function. Be sure to follow the [SRC-9](https://docs.fuel.network/docs/sway-standards/src-9-metadata-keys/) standard for your `key`. It is recommended that the [Ownership Library](../ownership/index.md) is used in conjunction with the `SetAssetMetadata` ABI to ensure only a single user has permissions to set an Asset's metadata.

The `_set_metadata()` function follows the SRC-7 standard for logging and will emit the `SetMetadataEvent` when called.

Expand All @@ -46,10 +53,40 @@ The `_set_metadata()` function follows the SRC-7 standard for logging and will e

> **NOTE** The `_set_metadata()` function will set the metadata of an asset *unconditionally*. External checks should be applied to restrict the setting of metadata.
To set the metadata of an Asset, using only one of the above types, you can define a custom ABI and use it as such:

```sway
{{#include ../../../../examples/asset/setting_src7_attributes/src/main.sw:setting_src7_attributes_custom_abi}}
```

> **NOTE** The `_set_metadata()` function will set the metadata of an asset *unconditionally*. External checks should be applied to restrict the setting of metadata.
### Implementing the SRC-7 Standard with StorageMetadata

To use the `StorageMetadata` type, simply get the stored metadata with the associated `key` and `AssetId` using the provided `_metadata()` convenience function. The example below shows the implementation of the [SRC-7](https://docs.fuel.network/docs/sway-standards/src-7-asset-metadata/) standard in combination with the Asset Library's `StorageMetadata` type and the `_metadata()` function with no user defined restrictions or custom functionality.

```sway
{{#include ../../../../examples/asset/basic_src7/src/main.sw:basic_src7}}
```

### Getting Metadata

To get the metadata for an asset, apart from the above mentioned `_metadata()` convenience function, you can also use the `get()` method on the `StorageMetadata` type, which returns the `Metadata` type.

```sway
{{#include ../../../../examples/asset/metadata_docs/src/main.sw:get_metadata}}
```

This results an `Option` type as the metadata may not be set for the asset and key combination.

If you know that the metadata is set, but you don't know the type, you can use a match statement to access the metadata.

```sway
{{#include ../../../../examples/asset/metadata_docs/src/main.sw:get_metadata_match}}
```

If you know that the metadata is set and you know the type, you can use the `as_*` methods to access the metadata. We also provide `is_*` methods to check if the metadata is of a specific type.

```sway
{{#include ../../../../examples/asset/metadata_docs/src/main.sw:get_metadata_as}}
```
6 changes: 3 additions & 3 deletions docs/book/src/getting_started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
To import any library, the following dependency should be added to the project's `Forc.toml` file under `[dependencies]`.

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

For reference, here is a complete `Forc.toml` file:
Expand All @@ -18,7 +18,7 @@ license = "Apache-2.0"
name = "MyProject"
[dependencies]
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.1" }
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.2" }
```

> **NOTE:** Be sure to set the tag to the latest release.
Expand All @@ -38,7 +38,7 @@ use sway_libs::ownership::only_owner;
```

> **NOTE:**
> All projects currently use `forc 0.63.3`, `fuels-rs v0.66.2` and `fuel-core 0.34.0`.
> All projects currently use `forc 0.66.6`, `fuels-rs v0.66.6` and `fuel-core 0.40.0`.
## Using Sway Libs

Expand Down
104 changes: 86 additions & 18 deletions docs/book/src/ownership/index.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,119 @@
# Ownership Library

The Ownership Library provides a way to block anyone other than a **single** "owner" from calling functions. The Ownership Library is often used when needing administrative calls on a contract by a single user.
The **Ownership Library** provides a straightforward way to restrict specific calls in a Sway contract to a single _owner_. Its design follows the [SRC-5](https://docs.fuel.network/docs/sway-standards/src-5-ownership/) standard from [Sway Standards](https://docs.fuel.network/docs/sway-standards/) and offers a set of functions to initialize, verify, revoke, and transfer ownership.

For implementation details on the Ownership Library please see the [Sway Libs Docs](https://fuellabs.github.io/sway-libs/master/sway_libs/ownership/index.html).
For implementation details, visit the [Sway Libs Docs](https://fuellabs.github.io/sway-libs/master/sway_libs/ownership/index.html).

## Importing the Ownership Library

In order to use the Ownership library, Sway Libs and [Sway Standards](https://docs.fuel.network/docs/sway-standards/) 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` file in your project please see the [Getting Started](../getting_started/index.md). To add Sway Standards as a dependency please see the [Sway Standards Book](https://docs.fuel.network/docs/sway-standards/#using-a-standard).
1. **Add Sway Libs to `Forc.toml`**
Please see the [Getting Started](../getting_started/index.md) guide for instructions on adding **Sway Libs** as a dependency.

To import the Ownership Library and [SRC-5](https://docs.fuel.network/docs/sway-standards/src-5-ownership/) Standard to your Sway Smart Contract, add the following to your Sway file:
2. **Add Sway Standards to `Forc.toml`**
Refer to the [Sway Standards Book](https://docs.fuel.network/docs/sway-standards/#using-a-standard) to add **Sway Standards**.

```sway
{{#include ../../../../examples/ownership/src/main.sw:import}}
```
3. **Import the Ownership Library**
To import the Ownership Library and the [SRC-5](https://docs.fuel.network/docs/sway-standards/src-5-ownership/) standard, include the following in your Sway file:

```sway
{{#include ../../../../examples/ownership/src/lib.sw:import}}
```

## Integrating the Ownership Library into the SRC-5 Standard

To implement the [SRC-5](https://docs.fuel.network/docs/sway-standards/src-5-ownership/) standard with the Ownership library, be sure to add the Sway Standards dependency to your contract. The following demonstrates the integration of the Ownership library with the SRC-5 standard.
When integrating the Ownership Library with [SRC-5](https://docs.fuel.network/docs/sway-standards/src-5-ownership/), ensure that the `SRC5` trait from **Sway Standards** is implemented in your contract, as shown below. The `_owner()` function from this library is used to fulfill the SRC-5 requirement of exposing the ownership state.

```sway
{{#include ../../../../examples/ownership/src/main.sw:integrate_with_src5}}
{{#include ../../../../examples/ownership/src/lib.sw:integrate_with_src5}}
```

> **NOTE** A constructor method must be implemented to initialize the owner.
## Basic Functionality
## Basic Usage

### Setting a Contract Owner

Once imported, the Ownership Library's functions will be available. To use them initialize the owner for your contract by calling the `initialize_ownership()` function in your own constructor method.
Establishes the initial ownership state by calling `initialize_ownership(new_owner)`. This can only be done once, typically in your contract's constructor.

```sway
{{#include ../../../../examples/ownership/src/main.sw:initialize}}
{{#include ../../../../examples/ownership/src/lib.sw:initialize}}
```

### Applying Restrictions

To restrict a function to only the owner, call the `only_owner()` function.
Protect functions so only the owner can call them by invoking `only_owner()` at the start of those functions.

```sway
{{#include ../../../../examples/ownership/src/main.sw:only_owner}}
{{#include ../../../../examples/ownership/src/lib.sw:only_owner}}
```

### Checking the Ownership Status

To return the ownership state from storage, call the `_owner()` function.
To retrieve the current ownership state, call `_owner()`.

```sway
{{#include ../../../../examples/ownership/src/lib.sw:state}}
```

### Transferring Ownership

To transfer ownership from the current owner to a new owner, call `transfer_ownership(new_owner)`.

```sway
{{#include ../../../../examples/ownership/src/main.sw:state}}
{{#include ../../../../examples/ownership/src/lib.sw:transfer_ownership}}
```

### Renouncing Ownership

To revoke ownership entirely and disallow the assignment of a new owner, call `renounce_ownership()`.

```sway
{{#include ../../../../examples/ownership/src/lib.sw:renouncing_ownership}}
```

## Events

### `OwnershipRenounced`

Emitted when ownership is revoked.

- **Fields:**
- `previous_owner`: Identity of the owner prior to revocation.

### `OwnershipSet`

Emitted when initial ownership is set.

- **Fields:**
- `new_owner`: Identity of the newly set owner.

### `OwnershipTransferred`

Emitted when ownership is transferred from one owner to another.

- **Fields:**
- `new_owner`: Identity of the new owner.
- `previous_owner`: Identity of the prior owner.

## Errors

### `InitializationError`

- **Variants:**
- `CannotReinitialized`: Thrown when attempting to initialize ownership if the owner is already set.

### `AccessError`

- **Variants:**
- `NotOwner`: Thrown when a function restricted to the owner is called by a non-owner.

## Example Integration

Below is a example illustrating how to use this library within a Sway contract:

```sway
{{#include ../../../../examples/ownership/src/main.sw:example_contract}}
```

1. **Initialization:** Call `constructor(new_owner)` once to set the initial owner.
2. **Restricted Calls:** Use `only_owner()` to guard any owner-specific functions.
3. **Ownership Checks:** Retrieve the current owner state via `_owner()`.
4. **Transfer or Renounce:** Use `transfer_ownership(new_owner)` or `renounce_ownership()` for ownership modifications.
Loading

0 comments on commit 55d503c

Please sign in to comment.