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

Make the SubId an Option in SRC-3's mint() function #131

Merged
merged 6 commits into from
Aug 23, 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
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Breaking Unreleased

- Some breaking change here 1
- Some breaking change here 2
- [#131](https://github.com/FuelLabs/sway-standards/pull/131) Makes the SRC-3 `mint()` function's `SubId` argument an `Option`.

Before:

```sway
mint(Identity::Address(Address::zero()), SubId::zero(), 100);
```

After:

```sway
mint(Identity::Address(Address::zero()), Some(SubId::zero()), 100);
```

## [Version 0.5.2]

Expand Down
9 changes: 5 additions & 4 deletions docs/src/src-3-minting-and-burning.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ Minting and burning were initially added to the [SRC-20](./src-20-native-asset.m

The following functions MUST be implemented to follow the SRC-3 standard:

#### `fn mint(recipient: Identity, sub_id: SubId, amount: u64)`
#### `fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64)`

This function MUST mint `amount` coins with sub-identifier `sub_id` and transfer them to the `recipient`.
This function MUST mint `amount` coins with a sub-identifier and transfer them to the `recipient`.
This function MUST use the `sub_id` as the sub-identifier IF `sub_id` is `Some`, otherwise this function MUST assign a `SubId` if the `sub_id` argument is `None`.
This function MAY contain arbitrary conditions for minting, and revert if those conditions are not met.

##### Mint Arguments

* `recipient` - The `Identity` to which the newly minted asset is transferred to.
* `sub_id` - The sub-identifier of the asset to mint.
* `sub_id` - The sub-identifier of the asset to mint. If this is `None`, a `SubId` MUST be assigned.
* `amount` - The quantity of coins to mint.

#### `fn burn(sub_id: SubId, amount: u64)`
Expand Down Expand Up @@ -57,7 +58,7 @@ The burn function may also introduce a security consideration if the total suppl
```sway
abi MySRC3Asset {
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64);
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64);
#[payable]
#[storage(read, write)]
fn burn(sub_id: SubId, amount: u64);
Expand Down
11 changes: 8 additions & 3 deletions examples/src3-mint-burn/multi_asset/src/multi_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
mint_to,
},
call_frames::msg_asset_id,
constants::DEFAULT_SUB_ID,
context::msg_amount,
hash::Hash,
storage::storage_string::*,
Expand Down Expand Up @@ -36,7 +37,7 @@ impl SRC3 for Contract {
/// # Arguments
///
/// * `recipient`: [Identity] - The user to which the newly minted asset is transferred to.
/// * `sub_id`: [SubId] - The sub-identifier of the newly minted asset.
/// * `sub_id`: [Option<SubId>] - The sub-identifier of the newly minted asset.
/// * `amount`: [u64] - The quantity of coins to mint.
///
/// # Number of Storage Accesses
Expand All @@ -52,11 +53,15 @@ impl SRC3 for Contract {
///
/// fn foo(contract_id: ContractId) {
/// let contract_abi = abi(SRC3, contract_id);
/// contract_abi.mint(Identity::ContractId(contract_id), DEFAULT_SUB_ID, 100);
/// contract_abi.mint(Identity::ContractId(contract_id), Some(DEFAULT_SUB_ID), 100);
/// }
/// ```
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
let sub_id = match sub_id {
Some(s) => s,
None => DEFAULT_SUB_ID,
};
let asset_id = AssetId::new(ContractId::this(), sub_id);

// If this SubId is new, increment the total number of distinguishable assets this contract has minted.
Expand Down
11 changes: 8 additions & 3 deletions examples/src3-mint-burn/single_asset/src/single_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ impl SRC3 for Contract {
///
/// fn foo(contract_id: ContractId) {
/// let contract_abi = abi(SRC3, contract);
/// contract_abi.mint(Identity::ContractId(contract_id), DEFAULT_SUB_ID, 100);
/// contract_abi.mint(Identity::ContractId(contract_id), Some(DEFAULT_SUB_ID), 100);
/// }
/// ```
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
require(sub_id == DEFAULT_SUB_ID, "Incorrect Sub Id");
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64) {
require(
sub_id
.is_some() && sub_id
bitzoic marked this conversation as resolved.
Show resolved Hide resolved
.unwrap() == DEFAULT_SUB_ID,
"Incorrect Sub Id",
);

// Increment total supply of the asset and mint to the recipient.
storage
Expand Down
4 changes: 2 additions & 2 deletions standards/src/src3.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ abi SRC3 {
/// # Arguments
///
/// * `recipient`: [Identity] - The user to which the newly minted asset is transferred to.
/// * `sub_id`: [SubId] - The sub-identifier of the newly minted asset.
/// * `sub_id`: [Option<SubId>] - The sub-identifier of the newly minted asset.
/// * `amount`: [u64] - The quantity of coins to mint.
///
/// # Examples
Expand All @@ -20,7 +20,7 @@ abi SRC3 {
/// }
/// ```
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64);
fn mint(recipient: Identity, sub_id: Option<SubId>, amount: u64);

/// Burns assets sent with the given `sub_id`.
///
Expand Down
Loading