From b03cc281241b36945da0fa3bd816862219334c25 Mon Sep 17 00:00:00 2001 From: Cameron Carstens Date: Fri, 30 Aug 2024 21:52:16 +0800 Subject: [PATCH] Revert breaking changes to asset library (#294) ## Type of change - Improvement (refactoring, restructuring repository, cleaning tech debt, ...) ## Changes The following changes have been made: - Removes the breaking changes from the asset library made in https://github.com/FuelLabs/sway-libs/pull/286 - Leaves improvements made in https://github.com/FuelLabs/sway-libs/pull/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. --- CHANGELOG.md | 73 ----- examples/asset/basic_src3/src/main.sw | 3 +- examples/asset/metadata_docs/src/main.sw | 4 +- .../setting_src20_attributes/src/main.sw | 4 +- .../asset/setting_src7_attributes/src/main.sw | 4 +- libs/src/asset/base.sw | 54 ++-- libs/src/asset/metadata.sw | 252 ++++++++++++++++-- libs/src/asset/supply.sw | 9 +- tests/src/native_asset/src/main.sw | 89 ++++++- .../tests/functions/set_metadata.rs | 1 + .../native_asset/tests/functions/set_name.rs | 2 + .../tests/functions/set_symbol.rs | 2 + .../src/native_asset/tests/utils/interface.rs | 8 +- 13 files changed, 350 insertions(+), 155 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a19ede7..bbc60b46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` 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, 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: diff --git a/examples/asset/basic_src3/src/main.sw b/examples/asset/basic_src3/src/main.sw index 3851570b..c2caf72f 100644 --- a/examples/asset/basic_src3/src/main.sw +++ b/examples/asset/basic_src3/src/main.sw @@ -22,7 +22,8 @@ impl SRC3 for Contract { storage .total_supply, recipient, - sub_id, + sub_id + .unwrap_or(b256::zero()), amount, ); } diff --git a/examples/asset/metadata_docs/src/main.sw b/examples/asset/metadata_docs/src/main.sw index 0150f09a..cf0e13b1 100644 --- a/examples/asset/metadata_docs/src/main.sw +++ b/examples/asset/metadata_docs/src/main.sw @@ -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, 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 diff --git a/examples/asset/setting_src20_attributes/src/main.sw b/examples/asset/setting_src20_attributes/src/main.sw index 0023e984..0ac8df9a 100644 --- a/examples/asset/setting_src20_attributes/src/main.sw +++ b/examples/asset/setting_src20_attributes/src/main.sw @@ -12,12 +12,12 @@ storage { impl SetAssetAttributes for Contract { #[storage(write)] - fn set_name(asset: AssetId, name: Option) { + fn set_name(asset: AssetId, name: String) { _set_name(storage.name, asset, name); } #[storage(write)] - fn set_symbol(asset: AssetId, symbol: Option) { + fn set_symbol(asset: AssetId, symbol: String) { _set_symbol(storage.symbol, asset, symbol); } diff --git a/examples/asset/setting_src7_attributes/src/main.sw b/examples/asset/setting_src7_attributes/src/main.sw index 1c20f771..be17835f 100644 --- a/examples/asset/setting_src7_attributes/src/main.sw +++ b/examples/asset/setting_src7_attributes/src/main.sw @@ -12,8 +12,8 @@ storage { impl SetAssetMetadata for Contract { #[storage(read, write)] - fn set_metadata(asset: AssetId, metadata: Option, 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 diff --git a/libs/src/asset/base.sw b/libs/src/asset/base.sw index 656ca1c9..40c53e40 100644 --- a/libs/src/asset/base.sw +++ b/libs/src/asset/base.sw @@ -201,7 +201,7 @@ pub fn _decimals( /// /// * `name_key`: [StorageKey>>] - 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] - The name of the asset. +/// * `name`: [String] - The name of the asset. /// /// # Reverts /// @@ -223,7 +223,7 @@ 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); /// } /// ``` @@ -231,23 +231,16 @@ pub fn _decimals( pub fn _set_name( name_key: StorageKey>, asset: AssetId, - name: Option, + 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(), }); } @@ -262,7 +255,7 @@ pub fn _set_name( /// /// * `symbol_key`: [StorageKey>>] - 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] - The symbol of the asset. +/// * `symbol`: [String] - The symbol of the asset. /// /// # Reverts /// @@ -284,7 +277,7 @@ 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); /// } /// ``` @@ -292,23 +285,16 @@ pub fn _set_name( pub fn _set_symbol( symbol_key: StorageKey>, asset: AssetId, - symbol: Option, + 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(), }); } @@ -365,7 +351,7 @@ abi SetAssetAttributes { /// # Arguments /// /// * `asset`: [AssetId] - The asset for the name to be stored. - /// * `name`: [Option] - The name which to be stored. + /// * `name`: [String] - The name which to be stored. /// /// # Example /// @@ -374,20 +360,20 @@ abi SetAssetAttributes { /// use sway_libs::asset::base::*; /// use std::string::String; /// - /// fn foo(contract_id: ContractId, asset: AssetId, name: Option) { + /// 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); + 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] - The symbol which to be stored. + /// * `symbol`: [String] - The symbol which to be stored. /// /// # Example /// @@ -396,14 +382,14 @@ abi SetAssetAttributes { /// use sway_libs::asset::base::*; /// use std::string::String; /// - /// fn foo(contract_id: ContractId, asset: AssetId, symbol: Option) { + /// 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); + fn set_symbol(asset: AssetId, symbol: String); /// Stores the decimals for a specific asset. /// /// # Arguments diff --git a/libs/src/asset/metadata.sw b/libs/src/asset/metadata.sw index fac9a69b..ae3a01ec 100644 --- a/libs/src/asset/metadata.sw +++ b/libs/src/asset/metadata.sw @@ -61,45 +61,37 @@ impl StorageKey { /// } /// ``` #[storage(read, write)] - pub fn insert( - self, - asset: AssetId, - metadata: Option, - key: String, -) { + pub fn insert(self, asset: AssetId, key: String, metadata: Metadata) { let hashed_key = sha256((asset, key)); match metadata { - Some(Metadata::Int(data)) => { + Metadata::Int(data) => { write(hashed_key, 0, data); write(sha256((hashed_key, self.slot())), 0, 1); }, - Some(Metadata::B256(data)) => { + Metadata::B256(data) => { write(hashed_key, 0, data); write(sha256((hashed_key, self.slot())), 0, 2); }, - Some(Metadata::String(data)) => { + Metadata::String(data) => { require(!data.is_empty(), SetMetadataError::EmptyString); let storage_string: StorageKey = StorageKey::new(hashed_key, 0, hashed_key); storage_string.write_slice(data); write(sha256((hashed_key, self.slot())), 0, 3); }, - Some(Metadata::Bytes(data)) => { + Metadata::Bytes(data) => { require(!data.is_empty(), SetMetadataError::EmptyBytes); let storage_bytes: StorageKey = StorageKey::new(hashed_key, 0, hashed_key); storage_bytes.write_slice(data); write(sha256((hashed_key, self.slot())), 0, 4); }, - None => { - write(sha256((hashed_key, self.slot())), 0, 0); - } } log(SetMetadataEvent { asset, - metadata: metadata, + metadata: Some(metadata), key, sender: msg_sender().unwrap(), }); @@ -192,10 +184,10 @@ impl StorageKey { pub fn _set_metadata( metadata_key: StorageKey, asset: AssetId, - metadata: Option, key: String, + metadata: Metadata, ) { - metadata_key.insert(asset, metadata, key); + metadata_key.insert(asset, key, metadata); } /// Returns metadata for a specific asset and key pair. @@ -240,7 +232,7 @@ abi SetAssetMetadata { /// # Arguments /// /// * `asset`: [AssetId] - The asset for the metadata to be stored. - /// * `metadata`: [Option] - The metadata which to be stored. + /// * `metadata`: [Metadata] - The metadata which to be stored. /// * `key`: [String] - The key for the metadata to be stored. /// /// # Example @@ -250,12 +242,234 @@ abi SetAssetMetadata { /// use sway_libs::asset::metadata::*; /// use std::string::String; /// - /// fn foo(contract_id: ContractId, asset: AssetId, metadata: Option, key: String) { + /// fn foo(contract_id: ContractId, asset: AssetId, key: String, metadata: Metadata) { /// let contract_abi = abi(SetAssetMetadata, contract_id.bits()); /// contract_abi.set_metadata(asset, metadata, key); /// assert(contract_abi.metadata(asset, key).unwrap() == Metadata); /// } /// ``` #[storage(read, write)] - fn set_metadata(asset: AssetId, metadata: Option, key: String); + fn set_metadata(asset: AssetId, key: String, metadata: Metadata); +} + +impl Metadata { + /// Returns the underlying metadata as a `String`. + /// + /// # Returns + /// + /// * [Option] - `Some` if the underlying type is a `String`, otherwise `None`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::String; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// let string = metadata.unwrap().as_string(); + /// assert(string.len() != 0); + /// } + /// ``` + pub fn as_string(self) -> Option { + match self { + Self::String(data) => Option::Some(data), + _ => Option::None, + } + } + + /// Returns whether the underlying metadata is a `String`. + /// + /// # Returns + /// + /// * [bool] - `true` if the metadata is a `String`, otherwise `false`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::String; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// assert(metadata.unwrap().is_string()); + /// } + /// ``` + pub fn is_string(self) -> bool { + match self { + Self::String(_) => true, + _ => false, + } + } + + /// Returns the underlying metadata as a `u64`. + /// + /// # Returns + /// + /// * [Option] - `Some` if the underlying type is a `u64`, otherwise `None`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::String; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// let int = metadata.unwrap().as_u64(); + /// assert(int != 0); + /// } + /// ``` + pub fn as_u64(self) -> Option { + match self { + Self::Int(data) => Option::Some(data), + _ => Option::None, + } + } + + /// Returns whether the underlying metadata is a `u64`. + /// + /// # Returns + /// + /// * [bool] - `true` if the metadata is a `u64`, otherwise `false`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::String; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// assert(metadata.unwrap().is_u64()); + /// } + /// ``` + pub fn is_u64(self) -> bool { + match self { + Self::Int(_) => true, + _ => false, + } + } + + /// Returns the underlying metadata as `Bytes`. + /// + /// # Returns + /// + /// * [Option] - `Some` if the underlying type is `Bytes`, otherwise `None`. + /// + /// # Examples + /// + /// ```sway + /// use std::{bytes::Bytes, string::String}; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// let bytes = metadata.unwrap().as_bytes(); + /// assert(bytes.len() != 0); + /// } + /// ``` + pub fn as_bytes(self) -> Option { + match self { + Self::Bytes(data) => Option::Some(data), + _ => Option::None, + } + } + + /// Returns whether the underlying metadata is `Bytes`. + /// + /// # Returns + /// + /// * [bool] - `true` if the metadata is `Bytes`, otherwise `false`. + /// + /// # Examples + /// + /// ```sway + /// use std::{bytes::Bytes, string::String}; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// assert(metadata.unwrap().is_bytes()); + /// } + /// ``` + pub fn is_bytes(self) -> bool { + match self { + Self::Bytes(_) => true, + _ => false, + } + } + + /// Returns the underlying metadata as a `b256`. + /// + /// # Returns + /// + /// * [Option] - `Some` if the underlying type is a `b256`, otherwise `None`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::String; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// let val = metadata.unwrap().as_b256(); + /// assert(val != b256::zero()); + /// } + /// ``` + pub fn as_b256(self) -> Option { + match self { + Self::B256(data) => Option::Some(data), + _ => Option::None, + } + } + + /// Returns whether the underlying metadata is a `b256`. + /// + /// # Returns + /// + /// * [bool] - `true` if the metadata is a `b256`, otherwise `false`. + /// + /// # Examples + /// + /// ```sway + /// use std::string::String; + /// use sway_libs::asset::metadata::*; + /// use standards::src7::{SRC7, Metadata}; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, key: String) { + /// let metadata_abi = abi(SRC7, contract_id); + /// let metadata = metadata_abi.metadata(asset, key); + /// + /// assert(metadata.unwrap().is_b256()); + /// } + /// ``` + pub fn is_b256(self) -> bool { + match self { + Self::B256(_) => true, + _ => false, + } + } } diff --git a/libs/src/asset/supply.sw b/libs/src/asset/supply.sw index 4e2e5490..b30b043c 100644 --- a/libs/src/asset/supply.sw +++ b/libs/src/asset/supply.sw @@ -30,7 +30,7 @@ use standards::src20::TotalSupplyEvent; /// * `total_assets_key`: [StorageKey] - The location in storage that the `u64` which represents the total assets is stored. /// * `total_supply_key`: [StorageKey>] - The location in storage which the `StorageMap` that stores the total supply of assets is stored. /// * `recipient`: [Identity] - The user to which the newly minted asset is transferred to. -/// * `sub_id`: [Option] - The sub-identifier of the newly minted asset. +/// * `sub_id`: [SubId] - The sub-identifier of the newly minted asset. /// * `amount`: [u64] - The quantity of coins to mint. /// /// # Returns @@ -68,14 +68,11 @@ pub fn _mint( total_assets_key: StorageKey, total_supply_key: StorageKey>, recipient: Identity, - sub_id: Option, + sub_id: SubId, amount: u64, ) -> AssetId { require(amount > 0, MintError::ZeroAmount); - let sub_id = match sub_id { - Some(id) => id, - None => b256::zero(), - }; + let asset_id = AssetId::new(ContractId::this(), sub_id); let supply = _total_supply(total_supply_key, asset_id); diff --git a/tests/src/native_asset/src/main.sw b/tests/src/native_asset/src/main.sw index 1ee749e4..0bff7605 100644 --- a/tests/src/native_asset/src/main.sw +++ b/tests/src/native_asset/src/main.sw @@ -60,13 +60,14 @@ impl SRC20 for Contract { impl SRC3 for Contract { #[storage(read, write)] fn mint(recipient: Identity, sub_id: Option, amount: u64) { - let _ = _mint( + _mint( storage .total_assets, storage .total_supply, recipient, - sub_id, + sub_id + .unwrap_or(b256::zero()), amount, ); } @@ -87,12 +88,12 @@ impl SRC7 for Contract { impl SetAssetAttributes for Contract { #[storage(write)] - fn set_name(asset: AssetId, name: Option) { + fn set_name(asset: AssetId, name: String) { _set_name(storage.name, asset, name); } #[storage(write)] - fn set_symbol(asset: AssetId, symbol: Option) { + fn set_symbol(asset: AssetId, symbol: String) { _set_symbol(storage.symbol, asset, symbol); } @@ -104,8 +105,8 @@ impl SetAssetAttributes for Contract { impl SetAssetMetadata for Contract { #[storage(read, write)] - fn set_metadata(asset: AssetId, metadata: Option, 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); } } @@ -157,7 +158,7 @@ fn test_name() { assert(src20_abi.name(asset_id).is_none()); - attributes_abi.set_name(asset_id, Some(name)); + attributes_abi.set_name(asset_id, name); assert(src20_abi.name(asset_id).unwrap().as_bytes() == name.as_bytes()); } @@ -173,7 +174,7 @@ fn test_symbol() { assert(src20_abi.symbol(asset_id).is_none()); - attributes_abi.set_symbol(asset_id, Some(symbol)); + attributes_abi.set_symbol(asset_id, symbol); assert(src20_abi.symbol(asset_id).unwrap().as_bytes() == symbol.as_bytes()); } @@ -228,6 +229,70 @@ fn test_burn() { assert(balance_of(ContractId::from(CONTRACT_ID), asset_id) == 0); } +#[test] +fn test_metadata_as_string() { + let data_string = String::from_ascii_str("Fuel is blazingly fast"); + let metadata = Metadata::String(data_string); + + assert(data_string == metadata.as_string().unwrap()); +} + +#[test] +fn test_metadata_is_string() { + let data_string = String::from_ascii_str("Fuel is blazingly fast"); + let metadata = Metadata::String(data_string); + + assert(metadata.is_string()); +} + +#[test] +fn test_metadata_as_u64() { + let data_int = 1; + let metadata = Metadata::Int(data_int); + + assert(data_int == metadata.as_u64().unwrap()); +} + +#[test] +fn test_metadata_is_u64() { + let data_int = 1; + let metadata = Metadata::Int(data_int); + + assert(metadata.is_u64()); +} + +#[test] +fn test_metadata_as_bytes() { + let data_bytes = String::from_ascii_str("Fuel is blazingly fast").as_bytes(); + let metadata = Metadata::Bytes(data_bytes); + + assert(data_bytes == metadata.as_bytes().unwrap()); +} + +#[test] +fn test_metadata_is_bytes() { + let data_bytes = String::from_ascii_str("Fuel is blazingly fast").as_bytes(); + let metadata = Metadata::Bytes(data_bytes); + + assert(metadata.is_bytes()); +} + +#[test] +fn test_metadata_as_b256() { + let data_b256 = 0x0000000000000000000000000000000000000000000000000000000000000001; + let metadata = Metadata::B256(data_b256); + + assert(data_b256 == metadata.as_b256().unwrap()); +} + +#[test] +fn test_metadata_is_b256() { + let data_b256 = 0x0000000000000000000000000000000000000000000000000000000000000001; + let metadata = Metadata::B256(data_b256); + + assert(metadata.is_b256()); +} + #[test] fn test_set_metadata_b256() { let data_b256 = 0x0000000000000000000000000000000000000000000000000000000000000001; @@ -237,7 +302,7 @@ fn test_set_metadata_b256() { let set_metadata_abi = abi(SetAssetMetadata, CONTRACT_ID); let key = String::from_ascii_str("my_key"); - set_metadata_abi.set_metadata(asset_id, Some(metadata), key); + set_metadata_abi.set_metadata(asset_id, key, metadata); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -253,7 +318,7 @@ fn test_set_metadata_u64() { let set_metadata_abi = abi(SetAssetMetadata, CONTRACT_ID); let key = String::from_ascii_str("my_key"); - set_metadata_abi.set_metadata(asset_id, Some(metadata), key); + set_metadata_abi.set_metadata(asset_id, key, metadata); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -269,7 +334,7 @@ fn test_set_metadata_string() { let set_metadata_abi = abi(SetAssetMetadata, CONTRACT_ID); let key = String::from_ascii_str("my_key"); - set_metadata_abi.set_metadata(asset_id, Some(metadata), key); + set_metadata_abi.set_metadata(asset_id, key, metadata); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -285,7 +350,7 @@ fn test_set_metadata_bytes() { let set_metadata_abi = abi(SetAssetMetadata, CONTRACT_ID); let key = String::from_ascii_str("my_key"); - set_metadata_abi.set_metadata(asset_id, Some(metadata), key); + set_metadata_abi.set_metadata(asset_id, key, metadata); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); diff --git a/tests/src/native_asset/tests/functions/set_metadata.rs b/tests/src/native_asset/tests/functions/set_metadata.rs index 84d20f45..bfde0d08 100644 --- a/tests/src/native_asset/tests/functions/set_metadata.rs +++ b/tests/src/native_asset/tests/functions/set_metadata.rs @@ -47,6 +47,7 @@ mod success { } #[tokio::test] + #[ignore] async fn sets_none() { let (owner_wallet, other_wallet, id, instance_1, _instance_2) = setup().await; let (asset_id_1, _asset_id_2, _sub_id_1, _sub_id_2, _identity1, _other_identity) = diff --git a/tests/src/native_asset/tests/functions/set_name.rs b/tests/src/native_asset/tests/functions/set_name.rs index 03246153..ab5552af 100644 --- a/tests/src/native_asset/tests/functions/set_name.rs +++ b/tests/src/native_asset/tests/functions/set_name.rs @@ -31,7 +31,9 @@ mod success { ); } + // TODO: Renable when set_name() accepts an Option #[tokio::test] + #[ignore] async fn sets_none() { let (owner_wallet, other_wallet, id, instance_1, _instance_2) = setup().await; let (asset_id_1, _asset_id_2, _sub_id_1, _sub_id_2, _identity1, _other_identity) = diff --git a/tests/src/native_asset/tests/functions/set_symbol.rs b/tests/src/native_asset/tests/functions/set_symbol.rs index bd18cc43..d65b2026 100644 --- a/tests/src/native_asset/tests/functions/set_symbol.rs +++ b/tests/src/native_asset/tests/functions/set_symbol.rs @@ -34,7 +34,9 @@ mod success { ); } + // TODO: Renable when set_symbol() accepts an Option #[tokio::test] + #[ignore] async fn sets_none() { let (owner_wallet, other_wallet, id, instance_1, _instance_2) = setup().await; let (asset_id_1, _asset_id_2, _sub_id_1, _sub_id_2, _identity1, _other_identity) = diff --git a/tests/src/native_asset/tests/utils/interface.rs b/tests/src/native_asset/tests/utils/interface.rs index 7294ea43..caad919a 100644 --- a/tests/src/native_asset/tests/utils/interface.rs +++ b/tests/src/native_asset/tests/utils/interface.rs @@ -87,7 +87,7 @@ pub(crate) async fn set_name( ) -> CallResponse<()> { contract .methods() - .set_name(asset, name) + .set_name(asset, name.unwrap()) .call() .await .unwrap() @@ -96,11 +96,11 @@ pub(crate) async fn set_name( pub(crate) async fn set_symbol( contract: &AssetLib, asset: AssetId, - name: Option, + symbol: Option, ) -> CallResponse<()> { contract .methods() - .set_symbol(asset, name) + .set_symbol(asset, symbol.unwrap()) .call() .await .unwrap() @@ -141,7 +141,7 @@ pub(crate) async fn set_metadata( ) -> CallResponse<()> { contract .methods() - .set_metadata(asset, metadata, key) + .set_metadata(asset, key, metadata.unwrap()) .call() .await .unwrap()