Skip to content

Commit

Permalink
Revert breaking changes to asset library (#294)
Browse files Browse the repository at this point in the history
## Type of change

<!--Delete points that do not apply-->

- Improvement (refactoring, restructuring repository, cleaning tech
debt, ...)

## Changes

The following changes have been made:

- Removes the breaking changes from the asset library made in
#286
- Leaves improvements made in
#286

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
- [x] I have updated the changelog to reflect the changes on this PR.
  • Loading branch information
bitzoic authored Aug 30, 2024
1 parent 93cfa35 commit b03cc28
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 155 deletions.
73 changes: 0 additions & 73 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,79 +48,6 @@ Description of the upcoming release here.

### Breaking v0.24.0

- [#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);
}
```

- [#290](https://github.com/FuelLabs/sway-libs/pull/290) The `_proxy_owner()`, `only_proxy_owner()` and `_set_proxy_owner()` functions no longer take `storage.proxy_owner` as a parameter. Instead they directly read and write to the storage slot `0xbb79927b15d9259ea316f2ecb2297d6cc8851888a98278c0a2e03e1a091ea754` which is `sha256("storage_SRC14_1")`.

Before:
Expand Down
3 changes: 2 additions & 1 deletion examples/asset/basic_src3/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl SRC3 for Contract {
storage
.total_supply,
recipient,
sub_id,
sub_id
.unwrap_or(b256::zero()),
amount,
);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/asset/metadata_docs/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl SRC7 for Contract {
// 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);
fn set_metadata(asset: AssetId, key: String, metadata: Metadata) {
_set_metadata(storage.metadata, asset, key, metadata);
}
}
// ANCHOR_END: src7_set_metadata
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: Option<String>) {
fn set_name(asset: AssetId, name: String) {
_set_name(storage.name, asset, name);
}

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

Expand Down
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, metadata: Option<Metadata>, key: String) {
_set_metadata(storage.metadata, asset, metadata, key);
fn set_metadata(asset: AssetId, key: String, metadata: Metadata) {
_set_metadata(storage.metadata, asset, key, metadata);
}
}
// ANCHOR_END: setting_src7_attributes
54 changes: 20 additions & 34 deletions libs/src/asset/base.sw
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub fn _decimals(
///
/// * `name_key`: [StorageKey<StorageMap<AssetId, StorageKey<StorageString>>>] - The location in storage which the `StorageMap` that stores the names of assets is stored.
/// * `asset`: [AssetId] - The asset of which to set the name.
/// * `name`: [Option<String>] - The name of the asset.
/// * `name`: [String] - The name of the asset.
///
/// # Reverts
///
Expand All @@ -223,31 +223,24 @@ pub fn _decimals(
///
/// fn foo(asset: AssetId) {
/// let name = String::from_ascii_str("Ether");
/// _set_name(storage.name, asset, Some(name));
/// _set_name(storage.name, asset, name);
/// assert(_name(storage.name, asset).unwrap() == name);
/// }
/// ```
#[storage(write)]
pub fn _set_name(
name_key: StorageKey<StorageMap<AssetId, StorageString>>,
asset: AssetId,
name: Option<String>,
name: String,
) {
match name {
Some(name) => {
require(!name.is_empty(), SetMetadataError::EmptyString);
require(!name.is_empty(), SetMetadataError::EmptyString);

name_key.insert(asset, StorageString {});
name_key.get(asset).write_slice(name);
},
None => {
let _ = name_key.get(asset).clear();
}
}
name_key.insert(asset, StorageString {});
name_key.get(asset).write_slice(name);

log(SetNameEvent {
asset,
name,
name: Some(name),
sender: msg_sender().unwrap(),
});
}
Expand All @@ -262,7 +255,7 @@ pub fn _set_name(
///
/// * `symbol_key`: [StorageKey<StorageMap<AssetId, StorageKey<StorageString>>>] - The location in storage which the `StorageMap` that stores the symbols of assets is stored.
/// * `asset`: [AssetId] - The asset of which to set the symbol.
/// * `symbol`: [Option<String>] - The symbol of the asset.
/// * `symbol`: [String] - The symbol of the asset.
///
/// # Reverts
///
Expand All @@ -284,31 +277,24 @@ pub fn _set_name(
///
/// fn foo(asset: AssetId) {
/// let symbol = String::from_ascii_str("ETH");
/// _set_symbol(storage.symbol, asset, Some(symbol));
/// _set_symbol(storage.symbol, asset, symbol);
/// assert(_symbol(storage.symbol, asset).unwrap() == symbol);
/// }
/// ```
#[storage(write)]
pub fn _set_symbol(
symbol_key: StorageKey<StorageMap<AssetId, StorageString>>,
asset: AssetId,
symbol: Option<String>,
symbol: String,
) {
match symbol {
Some(symbol) => {
require(!symbol.is_empty(), SetMetadataError::EmptyString);
require(!symbol.is_empty(), SetMetadataError::EmptyString);

symbol_key.insert(asset, StorageString {});
symbol_key.get(asset).write_slice(symbol);
},
None => {
let _ = symbol_key.get(asset).clear();
}
}
symbol_key.insert(asset, StorageString {});
symbol_key.get(asset).write_slice(symbol);

log(SetSymbolEvent {
asset,
symbol: symbol,
symbol: Some(symbol),
sender: msg_sender().unwrap(),
});
}
Expand Down Expand Up @@ -365,7 +351,7 @@ abi SetAssetAttributes {
/// # Arguments
///
/// * `asset`: [AssetId] - The asset for the name to be stored.
/// * `name`: [Option<String>] - The name which to be stored.
/// * `name`: [String] - The name which to be stored.
///
/// # Example
///
Expand All @@ -374,20 +360,20 @@ abi SetAssetAttributes {
/// use sway_libs::asset::base::*;
/// use std::string::String;
///
/// fn foo(contract_id: ContractId, asset: AssetId, name: Option<String>) {
/// fn foo(contract_id: ContractId, asset: AssetId, name: String) {
/// let contract_abi = abi(SetAssetAttributes, contract_id.bits());
/// contract_abi.set_name(asset, name);
/// assert(contract_abi.name(asset) == name);
/// }
/// ```
#[storage(write)]
fn set_name(asset: AssetId, name: Option<String>);
fn set_name(asset: AssetId, name: String);
/// Stores the symbol for a specific asset.
///
/// # Arguments
///
/// * `asset`: [AssetId] - The asset for the symbol to be stored.
/// * `symbol`: [Option<String>] - The symbol which to be stored.
/// * `symbol`: [String] - The symbol which to be stored.
///
/// # Example
///
Expand All @@ -396,14 +382,14 @@ abi SetAssetAttributes {
/// use sway_libs::asset::base::*;
/// use std::string::String;
///
/// fn foo(contract_id: ContractId, asset: AssetId, symbol: Option<String>) {
/// fn foo(contract_id: ContractId, asset: AssetId, symbol: String) {
/// let contract_abi = abi(SetAssetAttributes, contract_id.bits());
/// contract_abi.set_symbol(asset, symbol);
/// assert(contract_abi.symbol(asset) == symbol);
/// }
/// ```
#[storage(write)]
fn set_symbol(asset: AssetId, symbol: Option<String>);
fn set_symbol(asset: AssetId, symbol: String);
/// Stores the decimals for a specific asset.
///
/// # Arguments
Expand Down
Loading

0 comments on commit b03cc28

Please sign in to comment.