Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Sway-Standards v0.6.0 #286

Merged
merged 8 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- [#285](https://github.com/FuelLabs/sway-libs/pull/285) Adds the `BytecodeRoot` and `ContractConfigurables` types to the Bytecode Library.
- [#286](https://github.com/FuelLabs/sway-libs/pull/286) Adds the `_metadata()` function to the Asset Library.

### Changed

- Something changed here 1
- Something changed here 2
- [#286](https://github.com/FuelLabs/sway-libs/pull/286) Updates the repository to Sway-Standards v0.6.0 and implements the new SRC-20 and SRC-7 logging specifications.
- [#286](https://github.com/FuelLabs/sway-libs/pull/286) `_set_metadata()`, `_set_name()` and `_set_symbol()` now revert if the metadata is an empty string.
- [#286](https://github.com/FuelLabs/sway-libs/pull/286) `_set_metadata()` now reverts if the metadata is empty bytes.
- [#286](https://github.com/FuelLabs/sway-libs/pull/286) `_mint()` and `_burn()` now revert if the `amount` argument is zero.

### Fixed

Expand Down Expand Up @@ -67,6 +70,79 @@ verify_predicate_address(my_predicate_address, my_bytecode, None); // No configu
verify_predicate_address(my_predicate_address, my_bytecode, Some(my_configurables)); // With configurables
```

- [#286](https://github.com/FuelLabs/sway-libs/pull/286) The support functions for `Metadata` have been removed. They have been moved to sway-standards.

Before:

```sway
use sway_libs::asset::metadata::*;

fn foo(my_metadata: Metadata) {
let res: bool = my_metadata.is_b256();
let res: bool = my_metadata.is_string();
let res: bool = my_metadata.is_bytes();
let res: bool = my_metadata.is_uint();
}
```

After:

```sway
use standards::src7::*;

fn foo(my_metadata: Metadata) {
let res: bool = my_metadata.is_b256();
let res: bool = my_metadata.is_string();
let res: bool = my_metadata.is_bytes();
let res: bool = my_metadata.is_uint();
}
```

- [#286](https://github.com/FuelLabs/sway-libs/pull/286) The `SetMetadata` abi `set_metadata` function definition has changed. The `metadata` argument is now an `Option<Metadata>` and the argument order has changed.

Before:

```sway
abi SetAssetMetadata {
#[storage(read, write)]
fn set_metadata(asset: AssetId, key: String, metadata: Metadata);
}
```

After:

```sway
abi SetAssetMetadata {
#[storage(read, write)]
fn set_metadata(asset: AssetId, metadata: Option<Metadata>, key: String);
}
```

- [#286](https://github.com/FuelLabs/sway-libs/pull/286) The `_set_name()`, `_set_symbol()`, `_mint()`, and `_set_metdata()` functions `name`, `symbol`, `sub_id`, and `metadata` arguments are now `Option`s.

Before:

```sway
fn foo(asset: AssetId, recipient: Identity, amount: u64, key: String, metadata: Metadata) {
_set_name(storage.name, asset, String::from_ascii_str("Ether"));
_set_symbol(storage.symbol, asset, String::from_ascii_str("ETH"));
_mint(storage.total_assets, storage.total_supply, recipient, SubId::zero(), amount);
_set_metadata(storage.metadata, asset, key, metadata);
}
```

After:

```sway
fn foo(asset: AssetId, recipient: Identity, amount: u64, metadata: Metadata, key: String) {
_set_name(storage.name, asset, Some(String::from_ascii_str("Ether")));
_set_symbol(storage.symbol, asset, Some(String::from_ascii_str("ETH")));
_mint(storage.total_assets, storage.total_supply, recipient, Some(SubId::zero()), amount);
// Note: Ordering of arguments has changed for `_set_metadata()`
_set_metadata(storage.metadata, asset, Some(metadata), key);
}
```

## [v0.23.1]

### Added v0.23.1
Expand Down
2 changes: 2 additions & 0 deletions docs/book/src/asset/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ To use the Asset Library's base functionly, simply pass the `StorageKey` from th

To set some the asset attributes for an Asset, use the `SetAssetAttributes` ABI provided by the Asset Library. The example below shows the implementation of the `SetAssetAttributes` ABI with no user defined restrictions or custom functionality. It is recommended that the [Ownership Library](../ownership/index.md) is used in conjunction with the `SetAssetAttributes` ABI to ensure only a single user has permissions to set an Asset's attributes.

The `_set_name()`, `_set_symbol()`, and `_set_decimals()` functions follows the SRC-20 standard for logging and will emit their respective log when called.

```sway
{{#include ../../../../examples/asset/setting_src20_attributes/src/main.sw:setting_src20_attributes}}
```
Expand Down
70 changes: 5 additions & 65 deletions docs/book/src/asset/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ The Asset Library has the following complimentary data type for the [SRC-7](http

- `StorageMetadata`

The following additional functionality for the [SRC-7](https://docs.fuel.network/docs/sway-standards/src-7-asset-metadata/)'s `Metadata` type is provided:

- `as_string()`
- `is_string()`
- `as_u64()`
- `is_u64()`
- `as_bytes()`
- `is_bytes()`
- `as_b256()`
- `is_b256()`

## Setting Up Storage

Once imported, the Asset Library's metadata functionality should be available. To use them, be sure to add the storage block bellow to your contract which enables the [SRC-7](https://docs.fuel.network/docs/sway-standards/src-7-asset-metadata/) standard.
Expand All @@ -47,7 +36,9 @@ 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. 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.
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.

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

```sway
{{#include ../../../../examples/asset/setting_src7_attributes/src/main.sw:setting_src7_attributes}}
Expand All @@ -57,59 +48,8 @@ To set some metadata for an Asset, use the `SetAssetMetadata` ABI provided by th

### Implementing the SRC-7 Standard with StorageMetadata

To use the `StorageMetadata` type, simply get the stored metadata with the associated `key` and `AssetId`. 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 with no user defined restrictions or custom functionality.

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

## Using the `Metadata` Extensions

The `Metadata` type defined by the [SRC-7](https://docs.fuel.network/docs/sway-standards/src-7-asset-metadata/) standard can be one of 4 states:

```sway
pub enum Metadata {
B256: b256,
Bytes: Bytes,
Int: u64,
String: String,
}
```

The Asset Library enables the following functionality for the `Metadata` type:

### `is_b256()` and `as_b256()`

The `is_b256()` check enables checking whether the `Metadata` type is a `b256`.
The `as_b256()` returns the `b256` of the `Metadata` type.

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

### `is_bytes()` and `as_bytes()`

The `is_bytes()` check enables checking whether the `Metadata` type is a `Bytes`.
The `as_bytes()` returns the `Bytes` of the `Metadata` type.

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

### `is_u64()` and `as_u64()`

The `is_u64()` check enables checking whether the `Metadata` type is a `u64`.
The `as_u64()` returns the `u64` of the `Metadata` type.

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

### `is_string()` and `as_string()`

The `is_string()` check enables checking whether the `Metadata` type is a `String`.
The `as_string()` returns the `String` of the `Metadata` type.
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/metadata_docs/src/main.sw:as_string}}
{{#include ../../../../examples/asset/basic_src7/src/main.sw:src7_metadata_convenience_function}}
```
4 changes: 3 additions & 1 deletion docs/book/src/asset/supply.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Once imported, the Asset Library's supply functionality should be available. To

## Implementing the SRC-3 Standard with the Asset Library

To use a base function, simply pass the `StorageKey` from the prescribed storage block. The example below shows the implementation of the [SRC-3](https://docs.fuel.network/docs/sway-standards/src-3-minting-and-burning/) standard in combination with the Asset Library with no user defined restrictions or custom functionality. It is recommended that the [Ownership Library](../ownership/index.md) is used in conjunction with the Asset Library;s supply functionality to ensure only a single user has permissions to mint an Asset.
To use either function, simply pass the `StorageKey` from the prescribed storage block. The example below shows the implementation of the [SRC-3](https://docs.fuel.network/docs/sway-standards/src-3-minting-and-burning/) standard in combination with the Asset Library with no user defined restrictions or custom functionality. It is recommended that the [Ownership Library](../ownership/index.md) is used in conjunction with the Asset Library;s supply functionality to ensure only a single user has permissions to mint an Asset.

The `_mint()` and `_burn()` functions follows the SRC-20 standard for logging and will emit the `TotalSupplyEvent` when called.

```sway
{{#include ../../../../examples/asset/basic_src3/src/main.sw:basic_src3}}
Expand Down
2 changes: 1 addition & 1 deletion examples/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ dependencies = [

[[package]]
name = "standards"
source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.5.2#270350e69bd7455b7e99f0aae2e29a94d42324bd"
source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.6.0#65e09f95ea8b9476b171a66c8a47108f352fa32c"
dependencies = ["std"]

[[package]]
Expand Down
2 changes: 1 addition & 1 deletion examples/admin/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "admin_examples"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../libs" }
2 changes: 1 addition & 1 deletion examples/asset/base_docs/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "base_docs"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
2 changes: 1 addition & 1 deletion examples/asset/basic_src20/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "basic_src20"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
2 changes: 1 addition & 1 deletion examples/asset/basic_src3/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "basic_src3"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
2 changes: 1 addition & 1 deletion examples/asset/basic_src3/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ storage {
// Implement the SRC-3 Standard for this contract
impl SRC3 for Contract {
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
// Pass the StorageKeys to the `_mint()` function from the Asset Library.
_mint(
storage
Expand Down
2 changes: 1 addition & 1 deletion examples/asset/basic_src7/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "basic_src7"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
2 changes: 1 addition & 1 deletion examples/asset/metadata_docs/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "metadata_docs"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
46 changes: 16 additions & 30 deletions examples/asset/metadata_docs/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract;
use std::{bytes::Bytes, string::String};

// ANCHOR: import
use sway_libs::asset::metadata::*;
use sway_libs::asset::metadata::{_metadata, _set_metadata, SetAssetMetadata, StorageMetadata};
use standards::src7::*;
// ANCHOR_END: import

Expand All @@ -20,34 +20,20 @@ storage {
}
// ANCHOR_END: src7_storage

// ANCHOR: as_b256
fn b256_type(my_metadata: Metadata) {
assert(my_metadata.is_b256());

let my_b256: b256 = my_metadata.as_b256().unwrap();
}
// ANCHOR_END: as_b256

// ANCHOR: as_bytes
fn bytes_type(my_metadata: Metadata) {
assert(my_metadata.is_bytes());

let my_bytes: Bytes = my_metadata.as_bytes().unwrap();
}
// ANCHOR_END: as_bytes

// ANCHOR: as_u64
fn u64_type(my_metadata: Metadata) {
assert(my_metadata.is_u64());

let my_u64: u64 = my_metadata.as_u64().unwrap();
// ANCHOR src7_metadata_convenience_function
impl SRC7 for Contract {
#[storage(read)]
fn metadata(asset: AssetId, key: String) -> Option<Metadata> {
_metadata(storage.metadata, asset, key)
}
}
// ANCHOR_END: as_u64

// ANCHOR: as_string
fn string_type(my_metadata: Metadata) {
assert(my_metadata.is_string());

let my_string: String = my_metadata.as_string().unwrap();
// ANCHOR src7_metadata_convenience_function

// ANCHOR: src7_set_metadata
impl SetAssetMetadata for Contract {
#[storage(read, write)]
fn set_metadata(asset: AssetId, metadata: Option<Metadata>, key: String) {
_set_metadata(storage.metadata, asset, metadata, key);
}
}
// ANCHOR_END: as_string
// ANCHOR_END: src7_set_metadata
2 changes: 1 addition & 1 deletion examples/asset/setting_src20_attributes/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "setting_src20_attributes"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
4 changes: 2 additions & 2 deletions examples/asset/setting_src20_attributes/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ storage {

impl SetAssetAttributes for Contract {
#[storage(write)]
fn set_name(asset: AssetId, name: String) {
fn set_name(asset: AssetId, name: Option<String>) {
_set_name(storage.name, asset, name);
}

#[storage(write)]
fn set_symbol(asset: AssetId, symbol: String) {
fn set_symbol(asset: AssetId, symbol: Option<String>) {
_set_symbol(storage.symbol, asset, symbol);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/asset/setting_src7_attributes/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "setting_src7_attributes"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
4 changes: 2 additions & 2 deletions examples/asset/setting_src7_attributes/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ storage {

impl SetAssetMetadata for Contract {
#[storage(read, write)]
fn set_metadata(asset: AssetId, key: String, metadata: Metadata) {
_set_metadata(storage.metadata, asset, key, metadata);
fn set_metadata(asset: AssetId, metadata: Option<Metadata>, key: String) {
_set_metadata(storage.metadata, asset, metadata, key);
}
}
// ANCHOR_END: setting_src7_attributes
2 changes: 1 addition & 1 deletion examples/asset/supply_docs/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "supply_docs"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../../libs" }
2 changes: 1 addition & 1 deletion examples/asset/supply_docs/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use standards::src3::*;
// ANCHOR: src3_abi
abi SRC3 {
#[storage(read, write)]
fn mint(recipient: Identity, vault_sub_id: SubId, amount: u64);
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64);
#[payable]
#[storage(read, write)]
fn burn(vault_sub_id: SubId, amount: u64);
Expand Down
2 changes: 1 addition & 1 deletion examples/ownership/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "ownership_examples"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../libs" }
2 changes: 1 addition & 1 deletion examples/upgradability/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ license = "Apache-2.0"
name = "upgradability_examples"

[dependencies]
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.5.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.0" }
sway_libs = { path = "../../libs" }
Loading
Loading