From 0c4b53308d29fb7e034515b86674e7b8786a72ad Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:13:42 +0800 Subject: [PATCH 01/17] Update sway-libs to sway-standards v0.6.0 --- libs/Forc.lock | 2 +- libs/Forc.toml | 2 +- libs/src/asset/base.sw | 62 +++++++- libs/src/asset/errors.sw | 16 ++ libs/src/asset/metadata.sw | 313 ++++++------------------------------- libs/src/asset/supply.sw | 30 +++- 6 files changed, 150 insertions(+), 275 deletions(-) diff --git a/libs/Forc.lock b/libs/Forc.lock index a516af41..8710191c 100644 --- a/libs/Forc.lock +++ b/libs/Forc.lock @@ -4,7 +4,7 @@ source = "path+from-root-E19CE48B3E858B72" [[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]] diff --git a/libs/Forc.toml b/libs/Forc.toml index 4db34db5..abc5f2ec 100644 --- a/libs/Forc.toml +++ b/libs/Forc.toml @@ -5,4 +5,4 @@ license = "Apache-2.0" name = "sway_libs" [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" } diff --git a/libs/src/asset/base.sw b/libs/src/asset/base.sw index b4f73cfa..7565ed22 100644 --- a/libs/src/asset/base.sw +++ b/libs/src/asset/base.sw @@ -1,6 +1,8 @@ library; use std::{hash::{Hash, sha256}, storage::storage_string::*, string::String}; +use standards::src20::{SetDecimalsEvent, SetNameEvent, SetSymbolEvent}; +use ::asset::errors::SetMetadataError; /// Returns the total number of individual assets for a contract. /// @@ -201,6 +203,10 @@ pub fn _decimals( /// * `asset`: [AssetId] - The asset of which to set the name. /// * `name`: [String] - The name of the asset. /// +/// # Reverts +/// +/// * When passing an empty string. +/// /// # Number of Storage Accesses /// /// * Writes: `2` @@ -225,10 +231,25 @@ pub fn _decimals( pub fn _set_name( name_key: StorageKey>, asset: AssetId, - name: String, + name: Option, ) { - name_key.insert(asset, StorageString {}); - name_key.get(asset).write_slice(name); + match name { + Some(name) => { + 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(); + } + } + + log(SetNameEvent { + asset, + name, + sender: msg_sender().unwrap(), + }); } /// Unconditionally sets the symbol of an asset. @@ -243,6 +264,10 @@ pub fn _set_name( /// * `asset`: [AssetId] - The asset of which to set the symbol. /// * `symbol`: [String] - The symbol of the asset. /// +/// # Reverts +/// +/// * When passing an empty string. +/// /// # Number of Storage Accesses /// /// * Writes: `2` @@ -267,10 +292,25 @@ pub fn _set_name( pub fn _set_symbol( symbol_key: StorageKey>, asset: AssetId, - symbol: String, + symbol: Option, ) { - symbol_key.insert(asset, StorageString {}); - symbol_key.get(asset).write_slice(symbol); + match symbol { + Some(symbol) => { + 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(); + } + } + + log(SetSymbolEvent { + asset, + symbol: symbol, + sender: msg_sender().unwrap(), + }); } /// Unconditionally sets the decimals of an asset. @@ -312,13 +352,19 @@ pub fn _set_decimals( decimals: u8, ) { decimals_key.insert(asset, decimals); + + log(SetDecimalsEvent { + asset, + decimals, + sender: msg_sender().unwrap(), + }); } abi SetAssetAttributes { #[storage(write)] - fn set_name(asset: AssetId, name: String); + fn set_name(asset: AssetId, name: Option); #[storage(write)] - fn set_symbol(asset: AssetId, symbol: String); + fn set_symbol(asset: AssetId, symbol: Option); #[storage(write)] fn set_decimals(asset: AssetId, decimals: u8); } diff --git a/libs/src/asset/errors.sw b/libs/src/asset/errors.sw index 0f74207e..53dc36ea 100644 --- a/libs/src/asset/errors.sw +++ b/libs/src/asset/errors.sw @@ -4,4 +4,20 @@ library; pub enum BurnError { /// Emitted when there are not enough coins owned by the contract to burn. NotEnoughCoins: (), + /// Emitted when attempting to burn zero coins. + ZeroAmount: (), +} + +/// Error log for when something goes wrong when minting assets. +pub enum MintError { + /// Emitted when attempting to mint zero coins. + ZeroAmount: (), +} + +/// Error log for when something goes wrong when setting metadata. +pub enum SetMetadataError { + /// Emitted when the metadata is an empty string. + EmptyString: (), + /// Emitted when the metadata is empty bytes. + EmptyBytes: (), } diff --git a/libs/src/asset/metadata.sw b/libs/src/asset/metadata.sw index 4af81c82..29d1ae93 100644 --- a/libs/src/asset/metadata.sw +++ b/libs/src/asset/metadata.sw @@ -1,6 +1,6 @@ library; -use standards::src7::Metadata; +use standards::src7::{Metadata, SetMetadataEvent}; use std::{ auth::msg_sender, bytes::Bytes, @@ -18,18 +18,7 @@ use std::{ }, string::String, }; - -/// The event emitted when metadata is set via the `_set_metadata()` function. -pub struct SetMetadataEvent { - /// The asset for which metadata is set. - asset: AssetId, - /// The `Identity` of the caller that set the metadata. - sender: Identity, - /// The Metadata that is set. - metadata: Metadata, - /// The key used for the metadata. - key: String, -} +use ::asset::errors::SetMetadataError; /// A persistent storage type to store the SRC-7; Metadata Standard type. /// @@ -47,6 +36,11 @@ impl StorageKey { /// * `key`: [String] - The key for the metadata to be stored. /// * `metadata`: [Metadata] - The metadata which to be stored. /// + /// # Reverts + /// + /// * When the metadata is an empty string. + /// * When the metadata is an empty bytes. + /// /// # Number of Storage Accesses /// /// * Writes: `2` @@ -66,29 +60,43 @@ impl StorageKey { /// } /// ``` #[storage(read, write)] - pub fn insert(self, asset: AssetId, key: String, metadata: Metadata) { + pub fn insert(self, asset: AssetId, metadata: Option, key: String) { let hashed_key = sha256((asset, key)); match metadata { - Metadata::Int(data) => { + Some(Metadata::Int(data)) => { write(hashed_key, 0, data); - write(sha256((hashed_key, self.slot())), 0, 0); + write(sha256((hashed_key, self.slot())), 0, 1); }, - Metadata::B256(data) => { + Some(Metadata::B256(data)) => { write(hashed_key, 0, data); - write(sha256((hashed_key, self.slot())), 0, 1); + write(sha256((hashed_key, self.slot())), 0, 2); }, - Metadata::String(data) => { + Some(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, 2); + write(sha256((hashed_key, self.slot())), 0, 3); }, - Metadata::Bytes(data) => { + Some(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, 3); + write(sha256((hashed_key, self.slot())), 0, 4); + }, + None => { + write(sha256((hashed_key, self.slot())), 0, 0); } } + + log(SetMetadataEvent { + asset, + metadata: metadata, + key, + sender: msg_sender().unwrap(), + }); } /// Returns metadata for a specific asset and key pair. @@ -126,21 +134,21 @@ impl StorageKey { let hashed_key = sha256((asset, key)); match read::(sha256((hashed_key, self.slot())), 0) { - Option::Some(0) => { - Option::Some(Metadata::Int(read::(hashed_key, 0).unwrap())) + Some(1) => { + Some(Metadata::Int(read::(hashed_key, 0).unwrap())) }, - Option::Some(1) => { - Option::Some(Metadata::B256(read::(hashed_key, 0).unwrap())) + Some(2) => { + Some(Metadata::B256(read::(hashed_key, 0).unwrap())) }, - Option::Some(2) => { + Some(3) => { let storage_string: StorageKey = StorageKey::new(hashed_key, 0, hashed_key); - Option::Some(Metadata::String(storage_string.read_slice().unwrap())) + Some(Metadata::String(storage_string.read_slice().unwrap_or(String::new()))) }, - Option::Some(3) => { + Some(4) => { let storage_bytes: StorageKey = StorageKey::new(hashed_key, 0, hashed_key); - Option::Some(Metadata::Bytes(storage_bytes.read_slice().unwrap())) + Some(Metadata::Bytes(storage_bytes.read_slice().unwrap_or(Bytes::new()))) }, - _ => Option::None, + _ => None, } } } @@ -176,241 +184,22 @@ impl StorageKey { pub fn _set_metadata( metadata_key: StorageKey, asset: AssetId, + metadata: Option, key: String, - metadata: Metadata, ) { - log(SetMetadataEvent { - asset, - sender: msg_sender().unwrap(), - metadata, - key, - }); - metadata_key.insert(asset, key, metadata); + metadata_key.insert(asset, metadata, key); } +#[storage(read)] +pub fn _metadata( + metadata_key: StorageKey, + asset: AssetId, + key: String +) -> Option { + metadata_key.get(asset, key) +} + abi SetAssetMetadata { #[storage(read, write)] - 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, - } - } + fn set_metadata(asset: AssetId, metadata: Option, key: String); } diff --git a/libs/src/asset/supply.sw b/libs/src/asset/supply.sw index bce20fcd..4e2e5490 100644 --- a/libs/src/asset/supply.sw +++ b/libs/src/asset/supply.sw @@ -1,6 +1,6 @@ library; -use ::asset::errors::BurnError; +use ::asset::errors::{BurnError, MintError}; use ::asset::base::{_total_assets, _total_supply}; use std::{ asset::{ @@ -16,25 +16,31 @@ use std::{ storage::storage_string::*, string::String, }; +use standards::src20::TotalSupplyEvent; /// Unconditionally mints new assets using the `sub_id` sub-identifier. /// /// # Additional Information /// /// **Warning** This function increases the total supply by the number of coins minted. +/// **Note:** If `None` is passed for the `sub_id` argument, `b256::zero()` is used as the `SubId`. /// /// # Arguments /// /// * `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`: [SubId] - The sub-identifier of the newly minted asset. +/// * `sub_id`: [Option] - The sub-identifier of the newly minted asset. /// * `amount`: [u64] - The quantity of coins to mint. /// /// # Returns /// /// * [AssetId] - The `AssetId` of the newly minted asset. /// +/// # Reverts +/// +/// * When `amount` is zero. +/// /// # Number of Storage Accesses /// /// * Reads: `2` @@ -62,9 +68,14 @@ pub fn _mint( total_assets_key: StorageKey, total_supply_key: StorageKey>, recipient: Identity, - sub_id: SubId, + sub_id: Option, 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); @@ -77,6 +88,12 @@ pub fn _mint( total_supply_key.insert(asset_id, current_supply + amount); mint_to(recipient, sub_id, amount); + log(TotalSupplyEvent { + asset: asset_id, + supply: current_supply + amount, + sender: msg_sender().unwrap(), + }); + asset_id } @@ -96,6 +113,7 @@ pub fn _mint( /// # Reverts /// /// * When the calling contract does not have enough assets. +/// * When `amount` is zero. /// /// # Number of Storage Accesses /// @@ -124,6 +142,7 @@ pub fn _burn( sub_id: SubId, amount: u64, ) { + require(amount > 0, BurnError::ZeroAmount); let asset_id = AssetId::new(ContractId::this(), sub_id); require(this_balance(asset_id) >= amount, BurnError::NotEnoughCoins); @@ -133,4 +152,9 @@ pub fn _burn( total_supply_key.insert(asset_id, supply - amount); burn(sub_id, amount); + log(TotalSupplyEvent { + asset: asset_id, + supply: supply - amount, + sender: msg_sender().unwrap(), + }); } From 6f84a989adbacfed87edfb6ef75764f38e1902b5 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:13:50 +0800 Subject: [PATCH 02/17] Update tests --- tests/Forc.lock | 2 +- tests/src/admin/Forc.toml | 2 +- tests/src/native_asset/Forc.toml | 2 +- tests/src/native_asset/src/main.sw | 106 ++----- .../src/native_asset/tests/functions/burn.rs | 287 ++++++++++++++---- .../native_asset/tests/functions/decimals.rs | 42 +-- .../native_asset/tests/functions/metadata.rs | 64 ++-- .../src/native_asset/tests/functions/mint.rs | 189 +++++++++++- .../src/native_asset/tests/functions/name.rs | 94 +++--- .../tests/functions/set_decimals.rs | 195 ++++++++++-- .../tests/functions/set_metadata.rs | 181 ++++++++--- .../native_asset/tests/functions/set_name.rs | 248 +++++++++++++-- .../tests/functions/set_symbol.rs | 247 +++++++++++++-- .../native_asset/tests/functions/symbol.rs | 90 +++--- .../tests/functions/total_assets.rs | 18 +- .../tests/functions/total_supply.rs | 20 +- .../src/native_asset/tests/utils/interface.rs | 10 +- tests/src/ownership/Forc.toml | 2 +- tests/src/upgradability/Forc.toml | 2 +- 19 files changed, 1340 insertions(+), 461 deletions(-) diff --git a/tests/Forc.lock b/tests/Forc.lock index 3f4107b2..88243c18 100644 --- a/tests/Forc.lock +++ b/tests/Forc.lock @@ -210,7 +210,7 @@ dependencies = ["std"] [[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]] diff --git a/tests/src/admin/Forc.toml b/tests/src/admin/Forc.toml index 82663096..5319dd7a 100644 --- a/tests/src/admin/Forc.toml +++ b/tests/src/admin/Forc.toml @@ -5,5 +5,5 @@ license = "Apache-2.0" name = "admin_test" [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" } diff --git a/tests/src/native_asset/Forc.toml b/tests/src/native_asset/Forc.toml index feaebf2b..1040f253 100644 --- a/tests/src/native_asset/Forc.toml +++ b/tests/src/native_asset/Forc.toml @@ -5,5 +5,5 @@ license = "Apache-2.0" name = "native_asset_lib" [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" } diff --git a/tests/src/native_asset/src/main.sw b/tests/src/native_asset/src/main.sw index bc019e00..1ee749e4 100644 --- a/tests/src/native_asset/src/main.sw +++ b/tests/src/native_asset/src/main.sw @@ -59,8 +59,8 @@ impl SRC20 for Contract { impl SRC3 for Contract { #[storage(read, write)] - fn mint(recipient: Identity, sub_id: SubId, amount: u64) { - _mint( + fn mint(recipient: Identity, sub_id: Option, amount: u64) { + let _ = _mint( storage .total_assets, storage @@ -81,18 +81,18 @@ impl SRC3 for Contract { impl SRC7 for Contract { #[storage(read)] fn metadata(asset: AssetId, key: String) -> Option { - storage.metadata.get(asset, key) + _metadata(storage.metadata, asset, key) } } impl SetAssetAttributes for Contract { #[storage(write)] - fn set_name(asset: AssetId, name: String) { + fn set_name(asset: AssetId, name: Option) { _set_name(storage.name, asset, name); } #[storage(write)] - fn set_symbol(asset: AssetId, symbol: String) { + fn set_symbol(asset: AssetId, symbol: Option) { _set_symbol(storage.symbol, asset, symbol); } @@ -104,8 +104,8 @@ impl SetAssetAttributes for Contract { 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, key: String) { + _set_metadata(storage.metadata, asset, metadata, key); } } @@ -120,10 +120,10 @@ fn test_total_assets() { assert(src20_abi.total_assets() == 0); - src3_abi.mint(recipient, sub_id1, 10); + src3_abi.mint(recipient, Some(sub_id1), 10); assert(src20_abi.total_assets() == 1); - src3_abi.mint(recipient, sub_id2, 10); + src3_abi.mint(recipient, Some(sub_id2), 10); assert(src20_abi.total_assets() == 2); } @@ -138,10 +138,10 @@ fn test_total_supply() { assert(src20_abi.total_supply(asset_id).is_none()); - src3_abi.mint(recipient, sub_id, 10); + src3_abi.mint(recipient, Some(sub_id), 10); assert(src20_abi.total_supply(asset_id).unwrap() == 10); - src3_abi.mint(recipient, sub_id, 10); + src3_abi.mint(recipient, Some(sub_id), 10); assert(src20_abi.total_supply(asset_id).unwrap() == 20); } @@ -157,7 +157,7 @@ fn test_name() { assert(src20_abi.name(asset_id).is_none()); - attributes_abi.set_name(asset_id, name); + attributes_abi.set_name(asset_id, Some(name)); assert(src20_abi.name(asset_id).unwrap().as_bytes() == name.as_bytes()); } @@ -173,7 +173,7 @@ fn test_symbol() { assert(src20_abi.symbol(asset_id).is_none()); - attributes_abi.set_symbol(asset_id, symbol); + attributes_abi.set_symbol(asset_id, Some(symbol)); assert(src20_abi.symbol(asset_id).unwrap().as_bytes() == symbol.as_bytes()); } @@ -206,7 +206,7 @@ fn test_mint() { assert(balance_of(ContractId::from(CONTRACT_ID), asset_id) == 0); - src3_abi.mint(recipient, sub_id, 10); + src3_abi.mint(recipient, Some(sub_id), 10); assert(balance_of(ContractId::from(CONTRACT_ID), asset_id) == 10); } @@ -221,77 +221,13 @@ fn test_burn() { let sub_id = SubId::zero(); let asset_id = AssetId::new(ContractId::from(CONTRACT_ID), sub_id); - src3_abi.mint(recipient, sub_id, 10); + src3_abi.mint(recipient, Some(sub_id), 10); assert(balance_of(ContractId::from(CONTRACT_ID), asset_id) == 10); src3_abi.burn(sub_id, 10); 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; @@ -301,7 +237,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, key, metadata); + set_metadata_abi.set_metadata(asset_id, Some(metadata), key); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -317,7 +253,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, key, metadata); + set_metadata_abi.set_metadata(asset_id, Some(metadata), key); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -333,7 +269,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, key, metadata); + set_metadata_abi.set_metadata(asset_id, Some(metadata), key); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -349,7 +285,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, key, metadata); + set_metadata_abi.set_metadata(asset_id, Some(metadata), key); let returned_metadata = src7_abi.metadata(asset_id, key); assert(returned_metadata.is_some()); @@ -369,7 +305,7 @@ fn total_assets_only_incremented_once() { assert(src20_abi.total_assets() == 0); - src3_abi.mint(recipient, sub_id, 10); + src3_abi.mint(recipient, Some(sub_id), 10); assert(balance_of(ContractId::from(CONTRACT_ID), asset_id) == 10); assert(src20_abi.total_assets() == 1); @@ -379,7 +315,7 @@ fn total_assets_only_incremented_once() { assert(src20_abi.total_assets() == 1); - src3_abi.mint(recipient, sub_id, 10); + src3_abi.mint(recipient, Some(sub_id), 10); assert(balance_of(ContractId::from(CONTRACT_ID), asset_id) == 10); assert(src20_abi.total_assets() == 1); diff --git a/tests/src/native_asset/tests/functions/burn.rs b/tests/src/native_asset/tests/functions/burn.rs index 85b9fa39..f0bcdd3c 100644 --- a/tests/src/native_asset/tests/functions/burn.rs +++ b/tests/src/native_asset/tests/functions/burn.rs @@ -1,7 +1,8 @@ use crate::native_asset::tests::utils::{ interface::{burn, mint, total_assets, total_supply}, - setup::{defaults, get_wallet_balance, setup}, + setup::{defaults, get_wallet_balance, setup, TotalSupplyEvent}, }; +use fuels::types::Identity; mod success { @@ -11,82 +12,203 @@ mod success { async fn burn_assets() { 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, _owner_identity, identity2) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let burn_amount_1 = 25; + + assert!(mint_amount_1 >= burn_amount_1); - mint(&instance_1, identity2, sub_id_1, 100).await; + mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 100); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); assert_eq!(total_assets(&instance_1).await, 1); - burn(&instance_2, asset_id_1, sub_id_1, 50).await; + let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 50); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(50)); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1 - burn_amount_1, + sender: Identity::Address(other_wallet.address().into()), + } + ); } #[tokio::test] - async fn burns_multiple_assets() { + async fn burn_twice() { 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, _owner_identity, identity2) = - defaults(id, owner_wallet, other_wallet.clone()); + let (asset_id_1, _asset_id_2, sub_id_1, _sub_id_2, _owner_identity, identity2) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let burn_amount_1 = 25; + let burn_amount_2 = 30; + let burn_amount_3 = 3; - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; - mint(&instance_1, identity2, sub_id_2, 200).await; + assert!(mint_amount_1 >= burn_amount_1 + burn_amount_2 + burn_amount_3); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 100); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, 200); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(200)); - assert_eq!(total_assets(&instance_1).await, 2); + mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - burn(&instance_2, asset_id_1, sub_id_1, 50).await; + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!(total_assets(&instance_1).await, 1); + + let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1 - burn_amount_1, + sender: Identity::Address(other_wallet.address().into()), + } + ); + + let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_2).await; + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1 - burn_amount_2); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1 - burn_amount_2)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1 - burn_amount_1 - burn_amount_2, + sender: Identity::Address(other_wallet.address().into()), + } + ); + + let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_3).await; + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3, + sender: Identity::Address(other_wallet.address().into()), + } + ); + } - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 50); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, 200); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(50)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(200)); + #[tokio::test] + async fn burns_multiple_assets() { + 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, _owner_identity, identity2) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let mint_amount_2 = 200; + let burn_amount_1 = 25; + let burn_amount_2 = 150; + + assert!(mint_amount_1 >= burn_amount_1); + assert!(mint_amount_2 >= burn_amount_2); + + mint(&instance_1, identity2.clone(), Some(sub_id_1), mint_amount_1).await; + mint(&instance_1, identity2, Some(sub_id_2), mint_amount_2).await; + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2)); assert_eq!(total_assets(&instance_1).await, 2); - burn(&instance_2, asset_id_2, sub_id_2, 100).await; + let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 50); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, 100); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(50)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(100)); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); + assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2)); + assert_eq!(total_assets(&instance_1).await, 2); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1 - burn_amount_1, + sender: Identity::Address(other_wallet.address().into()), + } + ); + + let response = burn(&instance_2, asset_id_2, sub_id_2, burn_amount_2).await; + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2 - burn_amount_2); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); + assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2 - burn_amount_2)); assert_eq!(total_assets(&instance_1).await, 2); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_2, + supply: mint_amount_2 - burn_amount_2, + sender: Identity::Address(other_wallet.address().into()), + } + ); } #[tokio::test] async fn burn_to_zero() { 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, _owner_identity, identity2) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let burn_amount_1 = 100; - mint(&instance_1, identity2, sub_id_1, 100).await; - - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 100); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); - assert_eq!(total_assets(&instance_1).await, 1); - - burn(&instance_2, asset_id_1, sub_id_1, 50).await; - - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 50); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(50)); - assert_eq!(total_assets(&instance_1).await, 1); + assert!(mint_amount_1 == burn_amount_1); - burn(&instance_2, asset_id_1, sub_id_1, 25).await; + mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 25); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(25)); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); assert_eq!(total_assets(&instance_1).await, 1); - burn(&instance_2, asset_id_1, sub_id_1, 25).await; + let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 0); assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(0)); assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: 0, + sender: Identity::Address(other_wallet.address().into()), + } + ); } } @@ -100,17 +222,48 @@ mod revert { #[tokio::test] #[should_panic(expected = "NotEnoughCoins")] - async fn when_not_enough_coins() { + async fn when_not_enough_coins_in_transaction() { + 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, identity2) = + defaults(id, owner_wallet, other_wallet.clone()); + let mint_amount = 100; + let sent_burn_amount = 50; + let claimed_burn_amount = 75; + + assert!(sent_burn_amount < claimed_burn_amount); + + mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; + + let call_params = CallParameters::new(sent_burn_amount, asset_id_1, 1_000_000); + instance_2 + .methods() + .burn(sub_id_1, claimed_burn_amount) + .with_tx_policies(TxPolicies::default().with_script_gas_limit(2_000_000)) + .call_params(call_params) + .unwrap() + .call() + .await + .unwrap(); + } + + #[tokio::test] + #[should_panic(expected = "NotEnoughCoins")] + async fn when_greater_than_supply() { 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, identity2) = defaults(id, owner_wallet, other_wallet.clone()); + let mint_amount = 100; + let sent_burn_amount = 100; + let claimed_burn_amount = 150; - mint(&instance_1, identity2, sub_id_1, 100).await; + assert!(mint_amount < claimed_burn_amount); - let call_params = CallParameters::new(50, asset_id_1, 1_000_000); + mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; + + let call_params = CallParameters::new(sent_burn_amount, asset_id_1, 1_000_000); instance_2 .methods() - .burn(sub_id_1, 150) + .burn(sub_id_1, claimed_burn_amount) .with_tx_policies(TxPolicies::default().with_script_gas_limit(2_000_000)) .call_params(call_params) .unwrap() @@ -125,13 +278,18 @@ mod revert { 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, identity2) = defaults(id, owner_wallet, other_wallet.clone()); + let mint_amount = 100; + let sent_burn_amount = 50; + let claimed_burn_amount = 50; + + assert!(sent_burn_amount == claimed_burn_amount); - mint(&instance_1, identity2, sub_id_1, 100).await; + mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; - let call_params = CallParameters::new(50, asset_id_1, 1_000_000); + let call_params = CallParameters::new(sent_burn_amount, asset_id_1, 1_000_000); instance_2 .methods() - .burn(sub_id_2, 50) + .burn(sub_id_2, claimed_burn_amount) .with_tx_policies(TxPolicies::default().with_script_gas_limit(2_000_000)) .call_params(call_params) .unwrap() @@ -146,13 +304,18 @@ mod revert { 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, identity2) = defaults(id, owner_wallet, other_wallet.clone()); + let mint_amount = 100; + let sent_burn_amount = 50; + let claimed_burn_amount = 50; - mint(&instance_1, identity2, sub_id_1, 100).await; + assert!(sent_burn_amount == claimed_burn_amount); - let call_params = CallParameters::new(50, AssetId::zeroed(), 1_000_000); + mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; + + let call_params = CallParameters::new(sent_burn_amount, AssetId::zeroed(), 1_000_000); instance_2 .methods() - .burn(sub_id_1, 50) + .burn(sub_id_1, claimed_burn_amount) .with_tx_policies(TxPolicies::default().with_script_gas_limit(2_000_000)) .call_params(call_params) .unwrap() @@ -160,4 +323,20 @@ mod revert { .await .unwrap(); } + + #[tokio::test] + #[should_panic(expected = "ZeroAmount")] + async fn when_burn_zero() { + 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, _owner_identity, identity2) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let burn_amount_1 = 0; + + assert!(burn_amount_1 == 0); + + mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; + + burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; + } } diff --git a/tests/src/native_asset/tests/functions/decimals.rs b/tests/src/native_asset/tests/functions/decimals.rs index d1377f00..a87c8a19 100644 --- a/tests/src/native_asset/tests/functions/decimals.rs +++ b/tests/src/native_asset/tests/functions/decimals.rs @@ -13,11 +13,12 @@ mod success { 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) = defaults(id, owner_wallet, other_wallet.clone()); + let decimals_1 = 9u8; assert_eq!(decimals(&instance_1, asset_id_1).await, None); - set_decimals(&instance_1, asset_id_1, 9u8).await; - assert_eq!(decimals(&instance_1, asset_id_1).await, Some(9u8)); + set_decimals(&instance_1, asset_id_1, decimals_1).await; + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); } #[tokio::test] @@ -25,28 +26,31 @@ mod success { 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) = defaults(id, owner_wallet, other_wallet.clone()); + let decimals_1 = 9u8; + let decimals_2 = u8::MIN; + let decimals_3 = u8::MAX; + let decimals_4 = 16u8; + let decimals_5 = 9u8; + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_1).await, None); - set_decimals(&instance_1, asset_id_1, 9u8).await; - assert_eq!(decimals(&instance_1, asset_id_1).await, Some(9u8)); - assert_eq!(decimals(&instance_1, asset_id_2).await, None); - set_decimals(&instance_1, asset_id_2, 8u8).await; - assert_eq!(decimals(&instance_1, asset_id_2).await, Some(8u8)); - - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_3).await, None); - set_decimals(&instance_1, asset_id_3, 7u8).await; - assert_eq!(decimals(&instance_1, asset_id_3).await, Some(7u8)); - - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_4).await, None); - set_decimals(&instance_1, asset_id_4, 6u8).await; - assert_eq!(decimals(&instance_1, asset_id_4).await, Some(6u8)); - - let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_5).await, None); - set_decimals(&instance_1, asset_id_5, 5u8).await; - assert_eq!(decimals(&instance_1, asset_id_5).await, Some(5u8)); + + set_decimals(&instance_1, asset_id_1, decimals_1).await; + set_decimals(&instance_1, asset_id_2, decimals_2).await; + set_decimals(&instance_1, asset_id_3, decimals_3).await; + set_decimals(&instance_1, asset_id_4, decimals_4).await; + set_decimals(&instance_1, asset_id_5, decimals_5).await; + + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); + assert_eq!(decimals(&instance_1, asset_id_2).await, Some(decimals_2)); + assert_eq!(decimals(&instance_1, asset_id_3).await, Some(decimals_3)); + assert_eq!(decimals(&instance_1, asset_id_4).await, Some(decimals_4)); + assert_eq!(decimals(&instance_1, asset_id_5).await, Some(decimals_5)); } } diff --git a/tests/src/native_asset/tests/functions/metadata.rs b/tests/src/native_asset/tests/functions/metadata.rs index 19a7c5a0..b06a4c17 100644 --- a/tests/src/native_asset/tests/functions/metadata.rs +++ b/tests/src/native_asset/tests/functions/metadata.rs @@ -2,12 +2,22 @@ use crate::native_asset::tests::utils::{ interface::{metadata, set_metadata}, setup::{defaults, get_asset_id, setup, Metadata}, }; -use fuels::types::{Bytes, Bytes32}; +use fuels::types::{Bits256, Bytes, Bytes32}; mod success { use super::*; + #[tokio::test] + async fn gets_none_set() { + 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) = + defaults(id, owner_wallet, other_wallet.clone()); + let key = String::from("key1"); + + assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); + } + #[tokio::test] async fn gets_one_asset() { let (owner_wallet, other_wallet, id, instance_1, _instance_2) = setup().await; @@ -18,7 +28,7 @@ mod success { assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key.clone(), metadata1.clone()).await; + set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key).await, Some(metadata1) @@ -33,25 +43,25 @@ mod success { let metadata1 = Metadata::String(String::from("Fuel NFT Metadata 1")); let metadata2 = Metadata::String(String::from("Fuel NFT Metadata 2")); let metadata3 = Metadata::String(String::from("Fuel NFT Metadata 3")); + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); let key = String::from("key1"); assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key.clone(), metadata1.clone()).await; + assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); + assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); + + set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + set_metadata(&instance_1, asset_id_2, key.clone(), Some(metadata2.clone())).await; + set_metadata(&instance_1, asset_id_3, key.clone(), Some(metadata3.clone())).await; + assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1) ); - - assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); - set_metadata(&instance_1, asset_id_2, key.clone(), metadata2.clone()).await; assert_eq!( metadata(&instance_1, asset_id_2, key.clone()).await, Some(metadata2) ); - - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); - assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); - set_metadata(&instance_1, asset_id_3, key.clone(), metadata3.clone()).await; assert_eq!( metadata(&instance_1, asset_id_3, key).await, Some(metadata3) @@ -69,41 +79,37 @@ mod success { Bytes::from_hex_str("0101010101010101010101010101010101010101010101010101010101010101") .expect("failed to convert to bytes"), ); + let metadata4 = Metadata::B256(Bits256([1u8; 32])); let key1 = String::from("key1"); let key2 = String::from("key2"); let key3 = String::from("key3"); + let key4 = String::from("key4"); assert_eq!(metadata(&instance_1, asset_id_1, key1.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key1.clone(), metadata1.clone()).await; + assert_eq!(metadata(&instance_1, asset_id_1, key2.clone()).await, None); + assert_eq!(metadata(&instance_1, asset_id_1, key3.clone()).await, None); + assert_eq!(metadata(&instance_1, asset_id_1, key4.clone()).await, None); + + set_metadata(&instance_1, asset_id_1, key1.clone(), Some(metadata1.clone())).await; + set_metadata(&instance_1, asset_id_1, key2.clone(), Some(metadata2.clone())).await; + set_metadata(&instance_1, asset_id_1, key3.clone(), Some(metadata3.clone())).await; + set_metadata(&instance_1, asset_id_1, key4.clone(), Some(metadata4.clone())).await; + assert_eq!( metadata(&instance_1, asset_id_1, key1.clone()).await, - Some(metadata1.clone()) + Some(metadata1) ); - - assert_eq!(metadata(&instance_1, asset_id_1, key2.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key2.clone(), metadata2.clone()).await; assert_eq!( metadata(&instance_1, asset_id_1, key2.clone()).await, - Some(metadata2.clone()) - ); - assert_eq!( - metadata(&instance_1, asset_id_1, key1.clone()).await, - Some(metadata1.clone()) + Some(metadata2) ); - - assert_eq!(metadata(&instance_1, asset_id_1, key3.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key3.clone(), metadata3.clone()).await; assert_eq!( metadata(&instance_1, asset_id_1, key3).await, Some(metadata3) ); assert_eq!( - metadata(&instance_1, asset_id_1, key2.clone()).await, - Some(metadata2) - ); - assert_eq!( - metadata(&instance_1, asset_id_1, key1.clone()).await, - Some(metadata1) + metadata(&instance_1, asset_id_1, key4).await, + Some(metadata4) ); } } diff --git a/tests/src/native_asset/tests/functions/mint.rs b/tests/src/native_asset/tests/functions/mint.rs index a98b0fc2..4a25bcb0 100644 --- a/tests/src/native_asset/tests/functions/mint.rs +++ b/tests/src/native_asset/tests/functions/mint.rs @@ -1,7 +1,8 @@ use crate::native_asset::tests::utils::{ interface::{mint, total_assets, total_supply}, - setup::{defaults, get_wallet_balance, setup}, + setup::{defaults, get_asset_id, get_wallet_balance, setup, TotalSupplyEvent}, }; +use fuels::types::{Bytes32, Identity}; mod success { @@ -11,39 +12,199 @@ mod success { async fn mints_assets() { 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, identity2) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount = 100; assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 0); assert_eq!(total_supply(&instance_1, asset_id_1).await, None); assert_eq!(total_assets(&instance_1).await, 0); - mint(&instance_1, identity2, sub_id_1, 100).await; + let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 100); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount)); assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + async fn mints_twice() { + 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, identity2) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let mint_amount_2 = 200; + + let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + + let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount_2).await; + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 + mint_amount_2); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 + mint_amount_2)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1 + mint_amount_2, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + async fn mints_max() { + 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, identity2) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount = u64::MAX; + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 0); + assert_eq!(total_supply(&instance_1, asset_id_1).await, None); + assert_eq!(total_assets(&instance_1).await, 0); + + let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + async fn mints_sub_id_none_assets() { + 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, identity2) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount = 100; + let asset_id = get_asset_id(Bytes32::zeroed(), id); + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id).await, 0); + assert_eq!(total_supply(&instance_1, asset_id).await, None); + assert_eq!(total_assets(&instance_1).await, 0); + + let response = mint(&instance_1, identity2, None, mint_amount).await; + + assert_eq!(get_wallet_balance(&other_wallet, &asset_id).await, mint_amount); + assert_eq!(total_supply(&instance_1, asset_id).await, Some(mint_amount)); + assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id, + supply: mint_amount, + sender: Identity::Address(owner_wallet.address().into()), + } + ); } #[tokio::test] async fn mints_multiple_assets() { 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, identity2) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let mint_amount_1 = 100; + let mint_amount_2 = 200; - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + let response = mint(&instance_1, identity2.clone(), Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 100); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, 0); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); assert_eq!(total_supply(&instance_1, asset_id_2).await, None); assert_eq!(total_assets(&instance_1).await, 1); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_1, + supply: mint_amount_1, + sender: Identity::Address(owner_wallet.address().into()), + } + ); - mint(&instance_1, identity2, sub_id_2, 200).await; + let response = mint(&instance_1, identity2, Some(sub_id_2), mint_amount_2).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, 100); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, 200); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(200)); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2); + assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2)); assert_eq!(total_assets(&instance_1).await, 2); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + TotalSupplyEvent { + asset: asset_id_2, + supply: mint_amount_2, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } +} + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic(expected = "ZeroAmount")] + async fn mints_zero() { + 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, identity2) = + defaults(id, owner_wallet, other_wallet); + let mint_amount = 0; + + mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; } } diff --git a/tests/src/native_asset/tests/functions/name.rs b/tests/src/native_asset/tests/functions/name.rs index 75312b1b..f6717fc3 100644 --- a/tests/src/native_asset/tests/functions/name.rs +++ b/tests/src/native_asset/tests/functions/name.rs @@ -9,88 +9,76 @@ mod success { use super::*; #[tokio::test] - async fn one_asset() { + async fn get_none_asset_name() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + + assert_eq!(name(&instance_1, asset_id_1).await, None); + } + + #[tokio::test] + async fn get_one_asset_name() { + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); assert_eq!(name(&instance_1, asset_id_1).await, None); - set_name(&instance_1, asset_id_1, String::from("Fuel Asset 1")).await; + set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; assert_eq!( name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) + Some(name_1) ); } #[tokio::test] - async fn multiple_assets() { + async fn get_multiple_assets_name() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); + let name_2 = String::from("Fuel Asset 2"); + let name_3 = String::from("Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3"); + let name_4 = String::from("4"); + let name_5 = String::from("Fuel Asset 1"); + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); + assert_eq!(name(&instance_1, asset_id_1).await, None); - set_name(&instance_1, asset_id_1, String::from("Fuel Asset 1")).await; + assert_eq!(name(&instance_1, asset_id_2).await, None); + assert_eq!(name(&instance_1, asset_id_3).await, None); + assert_eq!(name(&instance_1, asset_id_4).await, None); + assert_eq!(name(&instance_1, asset_id_5).await, None); + + set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; + set_name(&instance_1, asset_id_2, Some(name_2.clone())).await; + set_name(&instance_1, asset_id_3, Some(name_3.clone())).await; + set_name(&instance_1, asset_id_4, Some(name_4.clone())).await; + set_name(&instance_1, asset_id_5, Some(name_5.clone())).await; + assert_eq!( name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) + Some(name_1) ); - - assert_eq!(name(&instance_1, asset_id_2).await, None); - set_name(&instance_1, asset_id_2, String::from("Fuel Asset 2")).await; assert_eq!( name(&instance_1, asset_id_2).await, - Some(String::from("Fuel Asset 2")) + Some(name_2) ); - - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); - assert_eq!(name(&instance_1, asset_id_3).await, None); - set_name(&instance_1, asset_id_3, String::from("Fuel Asset 3")).await; assert_eq!( name(&instance_1, asset_id_3).await, - Some(String::from("Fuel Asset 3")) + Some(name_3) ); - - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); - assert_eq!(name(&instance_1, asset_id_4).await, None); - set_name(&instance_1, asset_id_4, String::from("Fuel Asset 4")).await; assert_eq!( name(&instance_1, asset_id_4).await, - Some(String::from("Fuel Asset 4")) + Some(name_4) ); - - let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); - assert_eq!(name(&instance_1, asset_id_5).await, None); - set_name(&instance_1, asset_id_5, String::from("Fuel Asset 5")).await; assert_eq!( name(&instance_1, asset_id_5).await, - Some(String::from("Fuel Asset 5")) - ); - } - - #[tokio::test] - async fn does_not_overwrite_other_names() { - 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) = - defaults(id, owner_wallet, other_wallet.clone()); - - assert_eq!(name(&instance_1, asset_id_1).await, None); - set_name(&instance_1, asset_id_1, String::from("Fuel Asset 1")).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) - ); - - assert_eq!(name(&instance_1, asset_id_2).await, None); - set_name(&instance_1, asset_id_2, String::from("Fuel Asset 2")).await; - - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) - ); - assert_eq!( - name(&instance_1, asset_id_2).await, - Some(String::from("Fuel Asset 2")) + Some(name_5) ); } } diff --git a/tests/src/native_asset/tests/functions/set_decimals.rs b/tests/src/native_asset/tests/functions/set_decimals.rs index feef6b62..9a1e457a 100644 --- a/tests/src/native_asset/tests/functions/set_decimals.rs +++ b/tests/src/native_asset/tests/functions/set_decimals.rs @@ -1,8 +1,8 @@ use crate::native_asset::tests::utils::{ interface::{decimals, set_decimals}, - setup::{defaults, get_asset_id, setup}, + setup::{defaults, get_asset_id, setup, SetDecimalsEvent}, }; -use fuels::types::Bytes32; +use fuels::types::{Bytes32, Identity}; mod success { @@ -12,58 +12,201 @@ mod success { async fn sets_one_asset() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let decimals_1 = 9u8; assert_eq!(decimals(&instance_1, asset_id_1).await, None); - set_decimals(&instance_1, asset_id_1, 9u8).await; - assert_eq!(decimals(&instance_1, asset_id_1).await, Some(9u8)); + let response = set_decimals(&instance_1, asset_id_1, decimals_1).await; + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_1, + decimals: decimals_1, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + async fn sets_decimals_twice() { + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let decimals_1 = 9u8; + let decimals_2 = 8u8; + + let response = set_decimals(&instance_1, asset_id_1, decimals_1).await; + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_1, + decimals: decimals_1, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + + let response = set_decimals(&instance_1, asset_id_1, decimals_2).await; + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_2)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_1, + decimals: decimals_2, + sender: Identity::Address(owner_wallet.address().into()), + } + ); } #[tokio::test] async fn sets_multiple_assets() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let decimals_1 = 9u8; + let decimals_2 = u8::MIN; + let decimals_3 = u8::MAX; + let decimals_4 = 16u8; + let decimals_5 = 9u8; + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_1).await, None); - set_decimals(&instance_1, asset_id_1, 9u8).await; - assert_eq!(decimals(&instance_1, asset_id_1).await, Some(9u8)); + let response = set_decimals(&instance_1, asset_id_1, decimals_1).await; + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_1, + decimals: decimals_1, + sender: Identity::Address(owner_wallet.address().into()), + } + ); assert_eq!(decimals(&instance_1, asset_id_2).await, None); - set_decimals(&instance_1, asset_id_2, 8u8).await; - assert_eq!(decimals(&instance_1, asset_id_2).await, Some(8u8)); + let response = set_decimals(&instance_1, asset_id_2, decimals_2).await; + assert_eq!(decimals(&instance_1, asset_id_2).await, Some(decimals_2)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_2, + decimals: decimals_2, + sender: Identity::Address(owner_wallet.address().into()), + } + ); - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_3).await, None); - set_decimals(&instance_1, asset_id_3, 7u8).await; - assert_eq!(decimals(&instance_1, asset_id_3).await, Some(7u8)); + let response = set_decimals(&instance_1, asset_id_3, decimals_3).await; + assert_eq!(decimals(&instance_1, asset_id_3).await, Some(decimals_3)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_3, + decimals: decimals_3, + sender: Identity::Address(owner_wallet.address().into()), + } + ); - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_4).await, None); - set_decimals(&instance_1, asset_id_4, 6u8).await; - assert_eq!(decimals(&instance_1, asset_id_4).await, Some(6u8)); + let response = set_decimals(&instance_1, asset_id_4, decimals_4).await; + assert_eq!(decimals(&instance_1, asset_id_4).await, Some(decimals_4)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_4, + decimals: decimals_4, + sender: Identity::Address(owner_wallet.address().into()), + } + ); - let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_5).await, None); - set_decimals(&instance_1, asset_id_5, 5u8).await; - assert_eq!(decimals(&instance_1, asset_id_5).await, Some(5u8)); + let response = set_decimals(&instance_1, asset_id_5, decimals_5).await; + assert_eq!(decimals(&instance_1, asset_id_5).await, Some(decimals_5)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_5, + decimals: decimals_5, + sender: Identity::Address(owner_wallet.address().into()), + } + ); } #[tokio::test] - async fn does_not_overwrite_other_decimals() { + async fn does_not_overwrite_other_assets() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let decimals_1 = 9u8; + let decimals_2 = 8u8; assert_eq!(decimals(&instance_1, asset_id_1).await, None); - set_decimals(&instance_1, asset_id_1, 9u8).await; - assert_eq!(decimals(&instance_1, asset_id_1).await, Some(9u8)); + let response = set_decimals(&instance_1, asset_id_1, decimals_1).await; + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_1, + decimals: decimals_1, + sender: Identity::Address(owner_wallet.address().into()), + } + ); assert_eq!(decimals(&instance_1, asset_id_2).await, None); - set_decimals(&instance_1, asset_id_2, 8u8).await; + let response = set_decimals(&instance_1, asset_id_2, decimals_2).await; + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetDecimalsEvent { + asset: asset_id_2, + decimals: decimals_2, + sender: Identity::Address(owner_wallet.address().into()), + } + ); - assert_eq!(decimals(&instance_1, asset_id_1).await, Some(9u8)); - assert_eq!(decimals(&instance_1, asset_id_2).await, Some(8u8)); + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); + assert_eq!(decimals(&instance_1, asset_id_2).await, Some(decimals_2)); } } diff --git a/tests/src/native_asset/tests/functions/set_metadata.rs b/tests/src/native_asset/tests/functions/set_metadata.rs index 6b257ca1..b0e29289 100644 --- a/tests/src/native_asset/tests/functions/set_metadata.rs +++ b/tests/src/native_asset/tests/functions/set_metadata.rs @@ -2,7 +2,7 @@ use crate::native_asset::tests::utils::{ interface::{metadata, set_metadata}, setup::{defaults, get_asset_id, setup, Metadata, SetMetadataEvent}, }; -use fuels::types::{Bytes, Bytes32, Identity}; +use fuels::types::{Bits256, Bytes, Bytes32, Identity}; mod success { @@ -18,7 +18,7 @@ mod success { assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key.clone(), metadata1.clone()).await; + let response = set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -33,9 +33,41 @@ mod success { *event, SetMetadataEvent { asset: asset_id_1, + metadata: Some(metadata1), + key: key, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let metadata1 = Metadata::String(String::from("Fuel NFT Metadata")); + let key = String::from("key1"); + + set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + assert_eq!( + metadata(&instance_1, asset_id_1, key.clone()).await, + Some(metadata1.clone()) + ); + + let response = set_metadata(&instance_1, asset_id_1, key.clone(), None).await; + assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + + assert_eq!( + *event, + SetMetadataEvent { + asset: asset_id_1, + metadata: None, + key: key, sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata1, - key: key } ); } @@ -51,7 +83,7 @@ mod success { let key = String::from("key1"); assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key.clone(), metadata1.clone()).await; + let response = set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -66,14 +98,14 @@ mod success { *event, SetMetadataEvent { asset: asset_id_1, + metadata: Some(metadata1), + key: key.clone(), sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata1, - key: key.clone() } ); assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_2, key.clone(), metadata2.clone()).await; + let response = set_metadata(&instance_1, asset_id_2, key.clone(), Some(metadata2.clone())).await; assert_eq!( metadata(&instance_1, asset_id_2, key.clone()).await, Some(metadata2.clone()) @@ -88,15 +120,15 @@ mod success { *event, SetMetadataEvent { asset: asset_id_2, + metadata: Some(metadata2), + key: key.clone(), sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata2, - key: key.clone() } ); let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_3, key.clone(), metadata3.clone()).await; + let response = set_metadata(&instance_1, asset_id_3, key.clone(), Some(metadata3.clone())).await; assert_eq!( metadata(&instance_1, asset_id_3, key.clone()).await, Some(metadata3.clone()) @@ -111,15 +143,15 @@ mod success { *event, SetMetadataEvent { asset: asset_id_3, + metadata: Some(metadata3), + key: key, sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata3, - key: key } ); } #[tokio::test] - async fn does_not_overwrite_other_names() { + async fn does_not_overwrite_other_assets_with_same_key() { 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) = defaults(id, owner_wallet.clone(), other_wallet.clone()); @@ -127,32 +159,30 @@ mod success { let metadata2 = Metadata::String(String::from("Fuel NFT Metadata 2")); let metadata3 = Metadata::String(String::from("Fuel NFT Metadata 3")); let key = String::from("key1"); + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key.clone(), metadata1.clone()).await; + let response = set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1.clone(), key.clone()).await, Some(metadata1.clone()) ); - let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); - assert_eq!( *event, SetMetadataEvent { asset: asset_id_1, + metadata: Some(metadata1.clone()), + key: key.clone(), sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata1.clone(), - key: key.clone() } ); assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_2, key.clone(), metadata2.clone()).await; - + let response = set_metadata(&instance_1, asset_id_2, key.clone(), Some(metadata2.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -161,26 +191,22 @@ mod success { metadata(&instance_1, asset_id_2, key.clone()).await, Some(metadata2.clone()) ); - let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); - assert_eq!( *event, SetMetadataEvent { asset: asset_id_2, + metadata: Some(metadata2.clone()), + key: key.clone(), sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata2.clone(), - key: key.clone() } ); - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_3, key.clone(), metadata3.clone()).await; - + let response = set_metadata(&instance_1, asset_id_3, key.clone(), Some(metadata3.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1) @@ -193,19 +219,17 @@ mod success { metadata(&instance_1, asset_id_3, key.clone()).await, Some(metadata3.clone()) ); - let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); - assert_eq!( *event, SetMetadataEvent { asset: asset_id_3, + metadata: Some(metadata3), + key: key, sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata3, - key: key } ); } @@ -221,34 +245,34 @@ mod success { Bytes::from_hex_str("0101010101010101010101010101010101010101010101010101010101010101") .expect("failed to convert to bytes"), ); + let metadata4 = Metadata::B256(Bits256([1u8; 32])); let key1 = String::from("key1"); let key2 = String::from("key2"); let key3 = String::from("key3"); + let key4 = String::from("key4"); assert_eq!(metadata(&instance_1, asset_id_1, key1.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key1.clone(), metadata1.clone()).await; + let response = set_metadata(&instance_1, asset_id_1, key1.clone(), Some(metadata1.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key1.clone()).await, Some(metadata1.clone()) ); - let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); - assert_eq!( *event, SetMetadataEvent { asset: asset_id_1, + metadata: Some(metadata1.clone()), + key: key1.clone(), sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata1.clone(), - key: key1.clone() } ); assert_eq!(metadata(&instance_1, asset_id_1, key2.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key2.clone(), metadata2.clone()).await; + let response = set_metadata(&instance_1, asset_id_1, key2.clone(), Some(metadata2.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key2.clone()).await, Some(metadata2.clone()) @@ -257,50 +281,111 @@ mod success { metadata(&instance_1, asset_id_1, key1.clone()).await, Some(metadata1.clone()) ); - let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); - assert_eq!( *event, SetMetadataEvent { asset: asset_id_1, + metadata: Some(metadata2.clone()), + key: key2.clone(), sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata2.clone(), - key: key2.clone() } ); assert_eq!(metadata(&instance_1, asset_id_1, key3.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key3.clone(), metadata3.clone()).await; + let response = set_metadata(&instance_1, asset_id_1, key3.clone(), Some(metadata3.clone())).await; assert_eq!( metadata(&instance_1, asset_id_1, key3.clone()).await, Some(metadata3.clone()) ); assert_eq!( metadata(&instance_1, asset_id_1, key2.clone()).await, - Some(metadata2) + Some(metadata2.clone()) ); assert_eq!( metadata(&instance_1, asset_id_1, key1.clone()).await, - Some(metadata1) + Some(metadata1.clone()) ); - let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); + assert_eq!( + *event, + SetMetadataEvent { + asset: asset_id_1, + metadata: Some(metadata3.clone()), + key: key3.clone(), + sender: Identity::Address(owner_wallet.address().into()), + } + ); + assert_eq!(metadata(&instance_1, asset_id_1, key4.clone()).await, None); + let response = set_metadata(&instance_1, asset_id_1, key4.clone(), Some(metadata4.clone())).await; + assert_eq!( + metadata(&instance_1, asset_id_1, key4.clone()).await, + Some(metadata4.clone()) + ); + assert_eq!( + metadata(&instance_1, asset_id_1, key3).await, + Some(metadata3) + ); + assert_eq!( + metadata(&instance_1, asset_id_1, key2).await, + Some(metadata2) + ); + assert_eq!( + metadata(&instance_1, asset_id_1, key1).await, + Some(metadata1) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); assert_eq!( *event, SetMetadataEvent { asset: asset_id_1, + metadata: Some(metadata4), + key: key4, sender: Identity::Address(owner_wallet.address().into()), - metadata: metadata3, - key: key3 } ); } } + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic(expected = "EmptyString")] + async fn when_empty_string() { + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let metadata1 = Metadata::String(String::from("")); + let key = String::from("key1"); + + set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + } + + #[tokio::test] + #[should_panic(expected = "EmptyBytes")] + async fn when_empty_bytes() { + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let metadata1 = Metadata::Bytes( + Bytes::from_hex_str("") + .expect("failed to convert to bytes"), + ); + let key = String::from("key1"); + + set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + } + +} diff --git a/tests/src/native_asset/tests/functions/set_name.rs b/tests/src/native_asset/tests/functions/set_name.rs index b7e45ba2..3453cb58 100644 --- a/tests/src/native_asset/tests/functions/set_name.rs +++ b/tests/src/native_asset/tests/functions/set_name.rs @@ -1,8 +1,8 @@ use crate::native_asset::tests::utils::{ interface::{name, set_name}, - setup::{defaults, get_asset_id, setup}, + setup::{defaults, get_asset_id, setup, SetNameEvent}, }; -use fuels::types::Bytes32; +use fuels::types::{Bytes32, Identity}; mod success { @@ -12,14 +12,101 @@ mod success { async fn sets_one_asset() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); assert_eq!(name(&instance_1, asset_id_1).await, None); - set_name(&instance_1, asset_id_1, String::from("Fuel Asset 1")).await; + let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; assert_eq!( name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) + Some(name_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_1, + name: Some(name_1), + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); + + set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; + assert_eq!( + name(&instance_1, asset_id_1).await, + Some(name_1.clone()) + ); + + let response = set_name(&instance_1, asset_id_1, None).await; + assert_eq!(name(&instance_1, asset_id_1).await, None); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_1, + name: None, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + async fn sets_name_twice() { + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); + let name_2 = String::from("Fuel Asset 2"); + + let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; + assert_eq!( + name(&instance_1, asset_id_1).await, + Some(name_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_1, + name: Some(name_1), + sender: Identity::Address(owner_wallet.address().into()), + } + ); + + let response = set_name(&instance_1, asset_id_1, Some(name_2.clone())).await; + assert_eq!( + name(&instance_1, asset_id_1).await, + Some(name_2.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_1, + name: Some(name_2), + sender: Identity::Address(owner_wallet.address().into()), + } ); } @@ -27,70 +114,177 @@ mod success { async fn sets_multiple_assets() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); + let name_2 = String::from("Fuel Asset 2"); + let name_3 = String::from("Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3 Fuel Asset 3"); + let name_4 = String::from("4"); + let name_5 = String::from("Fuel Asset 1"); + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(name(&instance_1, asset_id_1).await, None); - set_name(&instance_1, asset_id_1, String::from("Fuel Asset 1")).await; + let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; assert_eq!( name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) + Some(name_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_1, + name: Some(name_1), + sender: Identity::Address(owner_wallet.address().into()), + } ); assert_eq!(name(&instance_1, asset_id_2).await, None); - set_name(&instance_1, asset_id_2, String::from("Fuel Asset 2")).await; + let response = set_name(&instance_1, asset_id_2, Some(name_2.clone())).await; assert_eq!( name(&instance_1, asset_id_2).await, - Some(String::from("Fuel Asset 2")) + Some(name_2.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_2, + name: Some(name_2), + sender: Identity::Address(owner_wallet.address().into()), + } ); - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(name(&instance_1, asset_id_3).await, None); - set_name(&instance_1, asset_id_3, String::from("Fuel Asset 3")).await; + let response = set_name(&instance_1, asset_id_3, Some(name_3.clone())).await; assert_eq!( name(&instance_1, asset_id_3).await, - Some(String::from("Fuel Asset 3")) + Some(name_3.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_3, + name: Some(name_3), + sender: Identity::Address(owner_wallet.address().into()), + } ); - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); assert_eq!(name(&instance_1, asset_id_4).await, None); - set_name(&instance_1, asset_id_4, String::from("Fuel Asset 4")).await; + let response = set_name(&instance_1, asset_id_4, Some(name_4.clone())).await; assert_eq!( name(&instance_1, asset_id_4).await, - Some(String::from("Fuel Asset 4")) + Some(name_4.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_4, + name: Some(name_4), + sender: Identity::Address(owner_wallet.address().into()), + } ); - let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(name(&instance_1, asset_id_5).await, None); - set_name(&instance_1, asset_id_5, String::from("Fuel Asset 5")).await; + let response = set_name(&instance_1, asset_id_5, Some(name_5.clone())).await; assert_eq!( name(&instance_1, asset_id_5).await, - Some(String::from("Fuel Asset 5")) + Some(name_5.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_5, + name: Some(name_5), + sender: Identity::Address(owner_wallet.address().into()), + } ); } #[tokio::test] - async fn does_not_overwrite_other_names() { + async fn does_not_overwrite_other_assets() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); - + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let name_1 = String::from("Fuel Asset 1"); + let name_2 = String::from("Fuel Asset 2"); + assert_eq!(name(&instance_1, asset_id_1).await, None); - set_name(&instance_1, asset_id_1, String::from("Fuel Asset 1")).await; + let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; assert_eq!( name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) + Some(name_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_1, + name: Some(name_1.clone()), + sender: Identity::Address(owner_wallet.address().into()), + } ); assert_eq!(name(&instance_1, asset_id_2).await, None); - set_name(&instance_1, asset_id_2, String::from("Fuel Asset 2")).await; + let response = set_name(&instance_1, asset_id_2, Some(name_2.clone())).await; + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetNameEvent { + asset: asset_id_2, + name: Some(name_2.clone()), + sender: Identity::Address(owner_wallet.address().into()), + } + ); assert_eq!( name(&instance_1, asset_id_1).await, - Some(String::from("Fuel Asset 1")) + Some(name_1) ); assert_eq!( name(&instance_1, asset_id_2).await, - Some(String::from("Fuel Asset 2")) + Some(name_2) ); } } + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic(expected = "EmptyString")] + async fn when_empty_string() { + 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) = + defaults(id, owner_wallet, other_wallet); + let name_1 = String::from(""); + + set_name(&instance_1, asset_id_1, Some(name_1)).await; + } +} diff --git a/tests/src/native_asset/tests/functions/set_symbol.rs b/tests/src/native_asset/tests/functions/set_symbol.rs index b46c6bce..38aa6ec8 100644 --- a/tests/src/native_asset/tests/functions/set_symbol.rs +++ b/tests/src/native_asset/tests/functions/set_symbol.rs @@ -1,8 +1,8 @@ use crate::native_asset::tests::utils::{ interface::{set_symbol, symbol}, - setup::{defaults, get_asset_id, setup}, + setup::{defaults, get_asset_id, setup, SetSymbolEvent}, }; -use fuels::types::Bytes32; +use fuels::types::{Bytes32, Identity}; mod success { @@ -12,14 +12,102 @@ mod success { async fn sets_one_asset() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let symbol_1 = String::from("FA1"); assert_eq!(symbol(&instance_1, asset_id_1).await, None); - set_symbol(&instance_1, asset_id_1, String::from("FA1")).await; + let response = set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; assert_eq!( symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) + Some(symbol_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_1, + symbol: Some(symbol_1), + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let symbol_1 = String::from("FA1"); + + set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; + assert_eq!( + symbol(&instance_1, asset_id_1).await, + Some(symbol_1.clone()) + ); + + let response = set_symbol(&instance_1, asset_id_1, None).await; + assert_eq!(symbol(&instance_1, asset_id_1).await, None); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_1, + symbol: None, + sender: Identity::Address(owner_wallet.address().into()), + } + ); + } + + #[tokio::test] + async fn sets_symbol_twice() { + 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) = + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let symbol_1 = String::from("FA1"); + let symbol_2 = String::from("FA2"); + + + let response = set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; + assert_eq!( + symbol(&instance_1, asset_id_1).await, + Some(symbol_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_1, + symbol: Some(symbol_1), + sender: Identity::Address(owner_wallet.address().into()), + } + ); + + let response = set_symbol(&instance_1, asset_id_1, Some(symbol_2.clone())).await; + assert_eq!( + symbol(&instance_1, asset_id_1).await, + Some(symbol_2.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_1, + symbol: Some(symbol_2), + sender: Identity::Address(owner_wallet.address().into()), + } ); } @@ -27,70 +115,177 @@ mod success { async fn sets_multiple_assets() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let symbol_1 = String::from("FA1"); + let symbol_2 = String::from("FA2"); + let symbol_3 = String::from("FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1"); + let symbol_4 = String::from("F"); + let symbol_5 = String::from("FA1"); + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(symbol(&instance_1, asset_id_1).await, None); - set_symbol(&instance_1, asset_id_1, String::from("FA1")).await; + let response = set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; assert_eq!( symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) + Some(symbol_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_1, + symbol: Some(symbol_1), + sender: Identity::Address(owner_wallet.address().into()), + } ); assert_eq!(symbol(&instance_1, asset_id_2).await, None); - set_symbol(&instance_1, asset_id_2, String::from("FA2")).await; + let response = set_symbol(&instance_1, asset_id_2, Some(symbol_2.clone())).await; assert_eq!( symbol(&instance_1, asset_id_2).await, - Some(String::from("FA2")) + Some(symbol_2.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_2, + symbol: Some(symbol_2), + sender: Identity::Address(owner_wallet.address().into()), + } ); - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(symbol(&instance_1, asset_id_3).await, None); - set_symbol(&instance_1, asset_id_3, String::from("FA3")).await; + let response = set_symbol(&instance_1, asset_id_3, Some(symbol_3.clone())).await; assert_eq!( symbol(&instance_1, asset_id_3).await, - Some(String::from("FA3")) + Some(symbol_3.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_3, + symbol: Some(symbol_3), + sender: Identity::Address(owner_wallet.address().into()), + } ); - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); assert_eq!(symbol(&instance_1, asset_id_4).await, None); - set_symbol(&instance_1, asset_id_4, String::from("FA4")).await; + let response = set_symbol(&instance_1, asset_id_4, Some(symbol_4.clone())).await; assert_eq!( symbol(&instance_1, asset_id_4).await, - Some(String::from("FA4")) + Some(symbol_4.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_4, + symbol: Some(symbol_4), + sender: Identity::Address(owner_wallet.address().into()), + } ); - let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(symbol(&instance_1, asset_id_5).await, None); - set_symbol(&instance_1, asset_id_5, String::from("FA5")).await; + let response = set_symbol(&instance_1, asset_id_5, Some(symbol_5.clone())).await; assert_eq!( symbol(&instance_1, asset_id_5).await, - Some(String::from("FA5")) + Some(symbol_5.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_5, + symbol: Some(symbol_5), + sender: Identity::Address(owner_wallet.address().into()), + } ); } #[tokio::test] - async fn does_not_overwrite_other_symbols() { + async fn does_not_overwrite_other_assets() { 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) = - defaults(id, owner_wallet, other_wallet.clone()); + defaults(id, owner_wallet.clone(), other_wallet.clone()); + let symbol_1 = String::from("FA1"); + let symbol_2 = String::from("FA2"); assert_eq!(symbol(&instance_1, asset_id_1).await, None); - set_symbol(&instance_1, asset_id_1, String::from("FA1")).await; + let response = set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; assert_eq!( symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) + Some(symbol_1.clone()) + ); + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_1, + symbol: Some(symbol_1.clone()), + sender: Identity::Address(owner_wallet.address().into()), + } ); assert_eq!(symbol(&instance_1, asset_id_2).await, None); - set_symbol(&instance_1, asset_id_2, String::from("FA2")).await; + let response = set_symbol(&instance_1, asset_id_2, Some(symbol_2.clone())).await; + let log = response + .decode_logs_with_type::() + .unwrap(); + let event = log.first().unwrap(); + assert_eq!( + *event, + SetSymbolEvent { + asset: asset_id_2, + symbol: Some(symbol_2.clone()), + sender: Identity::Address(owner_wallet.address().into()), + } + ); assert_eq!( symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) + Some(symbol_1) ); assert_eq!( symbol(&instance_1, asset_id_2).await, - Some(String::from("FA2")) + Some(symbol_2) ); } } + +mod revert { + + use super::*; + + #[tokio::test] + #[should_panic(expected = "EmptyString")] + async fn when_empty_string() { + 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) = + defaults(id, owner_wallet, other_wallet); + let symbol_1 = String::from(""); + + set_symbol(&instance_1, asset_id_1, Some(symbol_1)).await; + } +} diff --git a/tests/src/native_asset/tests/functions/symbol.rs b/tests/src/native_asset/tests/functions/symbol.rs index b7a9eae8..5c6b3ecd 100644 --- a/tests/src/native_asset/tests/functions/symbol.rs +++ b/tests/src/native_asset/tests/functions/symbol.rs @@ -9,88 +9,76 @@ mod success { use super::*; #[tokio::test] - async fn one_asset() { + async fn get_none_symbol() { 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) = defaults(id, owner_wallet, other_wallet.clone()); assert_eq!(symbol(&instance_1, asset_id_1).await, None); - - set_symbol(&instance_1, asset_id_1, String::from("FA1")).await; - assert_eq!( - symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) - ); } #[tokio::test] - async fn multiple_assets() { + async fn get_one_asset_symbol() { 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) = + let (asset_id_1, _asset_id_2, _sub_id_1, _sub_id_2, _identity1, _other_identity) = defaults(id, owner_wallet, other_wallet.clone()); + let symbol_1 = String::from("FA1"); assert_eq!(symbol(&instance_1, asset_id_1).await, None); - set_symbol(&instance_1, asset_id_1, String::from("FA1")).await; - assert_eq!( - symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) - ); - assert_eq!(symbol(&instance_1, asset_id_2).await, None); - set_symbol(&instance_1, asset_id_2, String::from("FA2")).await; - assert_eq!( - symbol(&instance_1, asset_id_2).await, - Some(String::from("FA2")) - ); - let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); - assert_eq!(symbol(&instance_1, asset_id_3).await, None); - set_symbol(&instance_1, asset_id_3, String::from("FA3")).await; - assert_eq!( - symbol(&instance_1, asset_id_3).await, - Some(String::from("FA3")) - ); - - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); - assert_eq!(symbol(&instance_1, asset_id_4).await, None); - set_symbol(&instance_1, asset_id_4, String::from("FA4")).await; + set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; assert_eq!( - symbol(&instance_1, asset_id_4).await, - Some(String::from("FA4")) - ); - - let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); - assert_eq!(symbol(&instance_1, asset_id_5).await, None); - set_symbol(&instance_1, asset_id_5, String::from("FA5")).await; - assert_eq!( - symbol(&instance_1, asset_id_5).await, - Some(String::from("FA5")) + symbol(&instance_1, asset_id_1).await, + Some(symbol_1) ); } #[tokio::test] - async fn does_not_overwrite_other_symbols() { + async fn get_multiple_assets_symbols() { 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) = defaults(id, owner_wallet, other_wallet.clone()); + let symbol_1 = String::from("FA1"); + let symbol_2 = String::from("FA2"); + let symbol_3 = String::from("FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1FA1"); + let symbol_4 = String::from("F"); + let symbol_5 = String::from("FA1"); + let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(symbol(&instance_1, asset_id_1).await, None); - set_symbol(&instance_1, asset_id_1, String::from("FA1")).await; - assert_eq!( - symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) - ); - assert_eq!(symbol(&instance_1, asset_id_2).await, None); - set_symbol(&instance_1, asset_id_2, String::from("FA2")).await; + assert_eq!(symbol(&instance_1, asset_id_3).await, None); + assert_eq!(symbol(&instance_1, asset_id_4).await, None); + assert_eq!(symbol(&instance_1, asset_id_5).await, None); + + set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; + set_symbol(&instance_1, asset_id_2, Some(symbol_2.clone())).await; + set_symbol(&instance_1, asset_id_3, Some(symbol_3.clone())).await; + set_symbol(&instance_1, asset_id_4, Some(symbol_4.clone())).await; + set_symbol(&instance_1, asset_id_5, Some(symbol_5.clone())).await; assert_eq!( symbol(&instance_1, asset_id_1).await, - Some(String::from("FA1")) + Some(symbol_1) ); assert_eq!( symbol(&instance_1, asset_id_2).await, - Some(String::from("FA2")) + Some(symbol_2) + ); + assert_eq!( + symbol(&instance_1, asset_id_3).await, + Some(symbol_3) + ); + assert_eq!( + symbol(&instance_1, asset_id_4).await, + Some(symbol_4) + ); + assert_eq!( + symbol(&instance_1, asset_id_5).await, + Some(symbol_5) ); } } diff --git a/tests/src/native_asset/tests/functions/total_assets.rs b/tests/src/native_asset/tests/functions/total_assets.rs index a857da77..afde490f 100644 --- a/tests/src/native_asset/tests/functions/total_assets.rs +++ b/tests/src/native_asset/tests/functions/total_assets.rs @@ -16,7 +16,7 @@ mod success { assert_eq!(total_assets(&instance_1).await, 0); - mint(&instance_1, identity2, sub_id_1, 100).await; + mint(&instance_1, identity2, Some(sub_id_1), 100).await; assert_eq!(total_assets(&instance_1).await, 1); } @@ -28,19 +28,19 @@ mod success { assert_eq!(total_assets(&instance_1).await, 0); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_assets(&instance_1).await, 1); - mint(&instance_1, identity2.clone(), sub_id_2, 200).await; + mint(&instance_1, identity2.clone(), Some(sub_id_2), 200).await; assert_eq!(total_assets(&instance_1).await, 2); - mint(&instance_1, identity2.clone(), Bits256([3u8; 32]), 300).await; + mint(&instance_1, identity2.clone(), Some(Bits256([3u8; 32])), 300).await; assert_eq!(total_assets(&instance_1).await, 3); - mint(&instance_1, identity2.clone(), Bits256([4u8; 32]), 400).await; + mint(&instance_1, identity2.clone(), Some(Bits256([4u8; 32])), 400).await; assert_eq!(total_assets(&instance_1).await, 4); - mint(&instance_1, identity2, Bits256([5u8; 32]), 200).await; + mint(&instance_1, identity2, Some(Bits256([5u8; 32])), 200).await; assert_eq!(total_assets(&instance_1).await, 5); } @@ -52,13 +52,13 @@ mod success { assert_eq!(total_assets(&instance_1).await, 0); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_assets(&instance_1).await, 1); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_assets(&instance_1).await, 1); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_assets(&instance_1).await, 1); } } diff --git a/tests/src/native_asset/tests/functions/total_supply.rs b/tests/src/native_asset/tests/functions/total_supply.rs index d08cc37e..77d1c448 100644 --- a/tests/src/native_asset/tests/functions/total_supply.rs +++ b/tests/src/native_asset/tests/functions/total_supply.rs @@ -15,7 +15,7 @@ mod success { defaults(id, owner_wallet, other_wallet.clone()); assert_eq!(total_supply(&instance_1, asset_id_1).await, None); - mint(&instance_1, identity2, sub_id_1, 100).await; + mint(&instance_1, identity2, Some(sub_id_1), 100).await; assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); } @@ -26,26 +26,26 @@ mod success { defaults(id, owner_wallet, other_wallet.clone()); assert_eq!(total_supply(&instance_1, asset_id_1).await, None); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); assert_eq!(total_supply(&instance_1, asset_id_2).await, None); - mint(&instance_1, identity2.clone(), sub_id_2, 200).await; + mint(&instance_1, identity2.clone(), Some(sub_id_2), 200).await; assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(200)); let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(total_supply(&instance_1, asset_id_3).await, None); - mint(&instance_1, identity2.clone(), Bits256([3u8; 32]), 300).await; + mint(&instance_1, identity2.clone(), Some(Bits256([3u8; 32])), 300).await; assert_eq!(total_supply(&instance_1, asset_id_3).await, Some(300)); let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); assert_eq!(total_supply(&instance_1, asset_id_4).await, None); - mint(&instance_1, identity2.clone(), Bits256([4u8; 32]), 400).await; + mint(&instance_1, identity2.clone(), Some(Bits256([4u8; 32])), 400).await; assert_eq!(total_supply(&instance_1, asset_id_4).await, Some(400)); let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(total_supply(&instance_1, asset_id_5).await, None); - mint(&instance_1, identity2, Bits256([5u8; 32]), 500).await; + mint(&instance_1, identity2, Some(Bits256([5u8; 32])), 500).await; assert_eq!(total_supply(&instance_1, asset_id_5).await, Some(500)); } @@ -56,13 +56,13 @@ mod success { defaults(id, owner_wallet, other_wallet.clone()); assert_eq!(total_supply(&instance_1, asset_id_1).await, None); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); - mint(&instance_1, identity2.clone(), sub_id_2, 200).await; + mint(&instance_1, identity2.clone(), Some(sub_id_2), 200).await; assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); - mint(&instance_1, identity2.clone(), sub_id_2, 300).await; + mint(&instance_1, identity2.clone(), Some(sub_id_2), 300).await; assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); } @@ -73,7 +73,7 @@ mod success { defaults(id, owner_wallet, other_wallet.clone()); assert_eq!(total_supply(&instance_1, asset_id_1).await, None); - mint(&instance_1, identity2.clone(), sub_id_1, 100).await; + mint(&instance_1, identity2.clone(), Some(sub_id_1), 100).await; assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(100)); burn(&instance_2, asset_id_1, sub_id_1, 50).await; diff --git a/tests/src/native_asset/tests/utils/interface.rs b/tests/src/native_asset/tests/utils/interface.rs index d8dba435..4b680608 100644 --- a/tests/src/native_asset/tests/utils/interface.rs +++ b/tests/src/native_asset/tests/utils/interface.rs @@ -49,7 +49,7 @@ pub(crate) async fn decimals(contract: &AssetLib, asset: AssetId pub(crate) async fn mint( contract: &AssetLib, recipient: Identity, - sub_id: Bits256, + sub_id: Option, amount: u64, ) -> FuelCallResponse<()> { contract @@ -83,7 +83,7 @@ pub(crate) async fn burn( pub(crate) async fn set_name( contract: &AssetLib, asset: AssetId, - name: String, + name: Option, ) -> FuelCallResponse<()> { contract .methods() @@ -96,7 +96,7 @@ pub(crate) async fn set_name( pub(crate) async fn set_symbol( contract: &AssetLib, asset: AssetId, - name: String, + name: Option, ) -> FuelCallResponse<()> { contract .methods() @@ -137,11 +137,11 @@ pub(crate) async fn set_metadata( contract: &AssetLib, asset: AssetId, key: String, - metadata: Metadata, + metadata: Option, ) -> FuelCallResponse<()> { contract .methods() - .set_metadata(asset, key, metadata) + .set_metadata(asset, metadata, key) .call() .await .unwrap() diff --git a/tests/src/ownership/Forc.toml b/tests/src/ownership/Forc.toml index b24ee3f3..98c47417 100644 --- a/tests/src/ownership/Forc.toml +++ b/tests/src/ownership/Forc.toml @@ -5,5 +5,5 @@ license = "Apache-2.0" name = "ownership_test" [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" } diff --git a/tests/src/upgradability/Forc.toml b/tests/src/upgradability/Forc.toml index 02d03edb..d99cb1d1 100644 --- a/tests/src/upgradability/Forc.toml +++ b/tests/src/upgradability/Forc.toml @@ -5,5 +5,5 @@ license = "Apache-2.0" name = "upgradability_test" [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" } From 6f62c6fb9e59259fb912e5a64ebc658300ef3750 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:13:57 +0800 Subject: [PATCH 03/17] Update examples --- examples/Forc.lock | 2 +- examples/admin/Forc.toml | 2 +- examples/asset/base_docs/Forc.toml | 2 +- examples/asset/basic_src20/Forc.toml | 2 +- examples/asset/basic_src3/Forc.toml | 2 +- examples/asset/basic_src3/src/main.sw | 2 +- examples/asset/basic_src7/Forc.toml | 2 +- examples/asset/metadata_docs/Forc.toml | 2 +- examples/asset/metadata_docs/src/main.sw | 46 +++++++------------ .../asset/setting_src20_attributes/Forc.toml | 2 +- .../setting_src20_attributes/src/main.sw | 4 +- .../asset/setting_src7_attributes/Forc.toml | 2 +- .../asset/setting_src7_attributes/src/main.sw | 4 +- examples/asset/supply_docs/Forc.toml | 2 +- examples/asset/supply_docs/src/main.sw | 2 +- examples/ownership/Forc.toml | 2 +- examples/upgradability/Forc.toml | 2 +- 17 files changed, 34 insertions(+), 48 deletions(-) diff --git a/examples/Forc.lock b/examples/Forc.lock index b9587a52..d8fe4859 100644 --- a/examples/Forc.lock +++ b/examples/Forc.lock @@ -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]] diff --git a/examples/admin/Forc.toml b/examples/admin/Forc.toml index c01483cf..7cc2ca3c 100644 --- a/examples/admin/Forc.toml +++ b/examples/admin/Forc.toml @@ -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" } diff --git a/examples/asset/base_docs/Forc.toml b/examples/asset/base_docs/Forc.toml index 6dc4619a..2d6a05b3 100644 --- a/examples/asset/base_docs/Forc.toml +++ b/examples/asset/base_docs/Forc.toml @@ -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" } diff --git a/examples/asset/basic_src20/Forc.toml b/examples/asset/basic_src20/Forc.toml index b7659901..d7aac164 100644 --- a/examples/asset/basic_src20/Forc.toml +++ b/examples/asset/basic_src20/Forc.toml @@ -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" } diff --git a/examples/asset/basic_src3/Forc.toml b/examples/asset/basic_src3/Forc.toml index b4871c59..f40716f6 100644 --- a/examples/asset/basic_src3/Forc.toml +++ b/examples/asset/basic_src3/Forc.toml @@ -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" } diff --git a/examples/asset/basic_src3/src/main.sw b/examples/asset/basic_src3/src/main.sw index 2835b13c..3851570b 100644 --- a/examples/asset/basic_src3/src/main.sw +++ b/examples/asset/basic_src3/src/main.sw @@ -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, amount: u64) { // Pass the StorageKeys to the `_mint()` function from the Asset Library. _mint( storage diff --git a/examples/asset/basic_src7/Forc.toml b/examples/asset/basic_src7/Forc.toml index d6d7244a..ed59196f 100644 --- a/examples/asset/basic_src7/Forc.toml +++ b/examples/asset/basic_src7/Forc.toml @@ -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" } diff --git a/examples/asset/metadata_docs/Forc.toml b/examples/asset/metadata_docs/Forc.toml index f429e97a..ef132d46 100644 --- a/examples/asset/metadata_docs/Forc.toml +++ b/examples/asset/metadata_docs/Forc.toml @@ -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" } diff --git a/examples/asset/metadata_docs/src/main.sw b/examples/asset/metadata_docs/src/main.sw index 57eb7cc3..3964238a 100644 --- a/examples/asset/metadata_docs/src/main.sw +++ b/examples/asset/metadata_docs/src/main.sw @@ -3,7 +3,7 @@ contract; use std::{bytes::Bytes, string::String}; // ANCHOR: import -use sway_libs::asset::metadata::*; +use sway_libs::asset::metadata::{StorageMetadata, SetAssetMetadata, _set_metadata, _metadata}; use standards::src7::*; // ANCHOR_END: import @@ -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(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, key: String) { + _set_metadata(storage.metadata, asset, metadata, key); + } } -// ANCHOR_END: as_string +// ANCHOR_END: src7_set_metadata diff --git a/examples/asset/setting_src20_attributes/Forc.toml b/examples/asset/setting_src20_attributes/Forc.toml index 109673c5..cebfd6e3 100644 --- a/examples/asset/setting_src20_attributes/Forc.toml +++ b/examples/asset/setting_src20_attributes/Forc.toml @@ -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" } diff --git a/examples/asset/setting_src20_attributes/src/main.sw b/examples/asset/setting_src20_attributes/src/main.sw index 0ac8df9a..0023e984 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: String) { + fn set_name(asset: AssetId, name: Option) { _set_name(storage.name, asset, name); } #[storage(write)] - fn set_symbol(asset: AssetId, symbol: String) { + fn set_symbol(asset: AssetId, symbol: Option) { _set_symbol(storage.symbol, asset, symbol); } diff --git a/examples/asset/setting_src7_attributes/Forc.toml b/examples/asset/setting_src7_attributes/Forc.toml index f1dc4f94..57cacdfc 100644 --- a/examples/asset/setting_src7_attributes/Forc.toml +++ b/examples/asset/setting_src7_attributes/Forc.toml @@ -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" } diff --git a/examples/asset/setting_src7_attributes/src/main.sw b/examples/asset/setting_src7_attributes/src/main.sw index be17835f..1c20f771 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, key: String, metadata: Metadata) { - _set_metadata(storage.metadata, asset, key, metadata); + fn set_metadata(asset: AssetId, metadata: Option, key: String) { + _set_metadata(storage.metadata, asset, metadata, key); } } // ANCHOR_END: setting_src7_attributes diff --git a/examples/asset/supply_docs/Forc.toml b/examples/asset/supply_docs/Forc.toml index 2722843b..102751a2 100644 --- a/examples/asset/supply_docs/Forc.toml +++ b/examples/asset/supply_docs/Forc.toml @@ -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" } diff --git a/examples/asset/supply_docs/src/main.sw b/examples/asset/supply_docs/src/main.sw index 2575e5bc..30ede1dd 100644 --- a/examples/asset/supply_docs/src/main.sw +++ b/examples/asset/supply_docs/src/main.sw @@ -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, amount: u64); #[payable] #[storage(read, write)] fn burn(vault_sub_id: SubId, amount: u64); diff --git a/examples/ownership/Forc.toml b/examples/ownership/Forc.toml index 72adea04..93c78a21 100644 --- a/examples/ownership/Forc.toml +++ b/examples/ownership/Forc.toml @@ -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" } diff --git a/examples/upgradability/Forc.toml b/examples/upgradability/Forc.toml index c4364b2c..df6aa50d 100644 --- a/examples/upgradability/Forc.toml +++ b/examples/upgradability/Forc.toml @@ -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" } From cce9fe492d269bdfac5ef9588f7dd8782e8bc819 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:14:03 +0800 Subject: [PATCH 04/17] Update docs --- docs/book/src/asset/base.md | 2 + docs/book/src/asset/metadata.md | 70 +++------------------------------ docs/book/src/asset/supply.md | 4 +- 3 files changed, 10 insertions(+), 66 deletions(-) diff --git a/docs/book/src/asset/base.md b/docs/book/src/asset/base.md index 227779ba..054e51f9 100644 --- a/docs/book/src/asset/base.md +++ b/docs/book/src/asset/base.md @@ -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}} ``` diff --git a/docs/book/src/asset/metadata.md b/docs/book/src/asset/metadata.md index ccd442ff..c066f68a 100644 --- a/docs/book/src/asset/metadata.md +++ b/docs/book/src/asset/metadata.md @@ -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. @@ -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}} @@ -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}} ``` diff --git a/docs/book/src/asset/supply.md b/docs/book/src/asset/supply.md index 1d63c298..9057f994 100644 --- a/docs/book/src/asset/supply.md +++ b/docs/book/src/asset/supply.md @@ -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}} From 612b3c1c6ca8269668ed75b398295f2048e59abb Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:58:26 +0800 Subject: [PATCH 05/17] Update inline docs --- libs/src/asset/base.sw | 74 +++++++++++++++++++++++++++++++++----- libs/src/asset/metadata.sw | 58 ++++++++++++++++++++++++++++-- 2 files changed, 121 insertions(+), 11 deletions(-) diff --git a/libs/src/asset/base.sw b/libs/src/asset/base.sw index 7565ed22..656ca1c9 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`: [String] - The name of the asset. +/// * `name`: [Option] - The name of the asset. /// /// # Reverts /// @@ -223,8 +223,8 @@ pub fn _decimals( /// /// fn foo(asset: AssetId) { /// let name = String::from_ascii_str("Ether"); -/// _set_name(storage.name, asset, name); -/// assert(_name(storage.name, asset) == name); +/// _set_name(storage.name, asset, Some(name)); +/// assert(_name(storage.name, asset).unwrap() == name); /// } /// ``` #[storage(write)] @@ -262,7 +262,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`: [String] - The symbol of the asset. +/// * `symbol`: [Option] - The symbol of the asset. /// /// # Reverts /// @@ -284,8 +284,8 @@ pub fn _set_name( /// /// fn foo(asset: AssetId) { /// let symbol = String::from_ascii_str("ETH"); -/// _set_symbol(storage.symbol, asset, symbol); -/// assert(_symbol(storage.symbol, asset) == symbol); +/// _set_symbol(storage.symbol, asset, Some(symbol)); +/// assert(_symbol(storage.symbol, asset).unwrap() == symbol); /// } /// ``` #[storage(write)] @@ -333,7 +333,6 @@ pub fn _set_symbol( /// /// ```sway /// use sway_libs::asset::base::{_set_decimals, _decimals}; -/// use std::string::String; /// /// storage { /// decimals: StorageMap = StorageMap {}, @@ -342,7 +341,7 @@ pub fn _set_symbol( /// fn foo(asset: AssetId) { /// let decimals = 8u8; /// _set_decimals(storage.decimals, asset, decimals); -/// assert(_decimals(storage.decimals, asset) == decimals); +/// assert(_decimals(storage.decimals, asset).unwrap() == decimals); /// } /// ``` #[storage(write)] @@ -361,10 +360,69 @@ pub fn _set_decimals( } abi SetAssetAttributes { + /// Stores the name for a specific asset. + /// + /// # Arguments + /// + /// * `asset`: [AssetId] - The asset for the name to be stored. + /// * `name`: [Option] - The name which to be stored. + /// + /// # Example + /// + /// ```sway + /// use standards::src20::SRC20; + /// use sway_libs::asset::base::*; + /// use std::string::String; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, name: Option) { + /// 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); + /// 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. + /// + /// # Example + /// + /// ```sway + /// use standards::src20::SRC20; + /// use sway_libs::asset::base::*; + /// use std::string::String; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, symbol: Option) { + /// 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); + /// Stores the decimals for a specific asset. + /// + /// # Arguments + /// + /// * `asset`: [AssetId] - The asset for the symbol to be stored. + /// * `decimals`: [u8] - The decimals which to be stored. + /// + /// # Example + /// + /// ```sway + /// use standards::src20::SRC20; + /// use sway_libs::asset::base::*; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, decimals: u8) { + /// let contract_abi = abi(SetAssetAttributes, contract_id.bits()); + /// contract_abi.set_decimals(asset, decimals); + /// assert(contract_abi.decimals(asset).unwrap() == decimals); + /// } + /// ``` #[storage(write)] fn set_decimals(asset: AssetId, decimals: u8); } diff --git a/libs/src/asset/metadata.sw b/libs/src/asset/metadata.sw index 29d1ae93..4d098b6c 100644 --- a/libs/src/asset/metadata.sw +++ b/libs/src/asset/metadata.sw @@ -50,6 +50,7 @@ impl StorageKey { /// ```sway /// use standards::src7::Metadata; /// use sway_libs::asset::metadata::*; + /// use std::string::String; /// /// storage { /// metadata: StorageMetadata = StorageMetadata {} @@ -119,6 +120,7 @@ impl StorageKey { /// ```sway /// use standards::src7::Metadata; /// use sway_libs::asset::metadata::*; + /// use std::string::String; /// /// storage { /// metadata: StorageMetadata = StorageMetadata {} @@ -159,8 +161,8 @@ impl StorageKey { /// /// * `metadata_key`: [StorageKey] - The storage location for metadata. /// * `asset`: [AssetId] - The asset for the metadata to be stored. +/// * `metadata`: [Option] - The metadata which to be stored. /// * `key`: [String] - The key for the metadata to be stored. -/// * `metadata`: [Metadata] - The metadata which to be stored. /// /// # Number of Storage Accesses /// @@ -171,13 +173,14 @@ impl StorageKey { /// ```sway /// use standards::src7::Metadata; /// use sway_libs::asset::metadata::*; +/// use std::string::String; /// /// storage { /// metadata: StorageMetadata = StorageMetadata {} /// } /// -/// fn foo(asset: AssetId, key: String, metadata: Metadata) { -/// _set_metadata(storage.metadata, asset, key, metadata); +/// fn foo(asset: AssetId, key: String, metadata: Option) { +/// _set_metadata(storage.metadata, asset, metadata, key); /// } /// ``` #[storage(read, write)] @@ -190,6 +193,34 @@ pub fn _set_metadata( metadata_key.insert(asset, metadata, key); } +/// Returns metadata for a specific asset and key pair. +/// +/// # Arguments +/// +/// * `metadata_key`: [StorageKey] - The storage location for metadata. +/// * `asset`: [AssetId] - The asset for the metadata to be read. +/// * `metadata`: [Option] - The metadata which to be read. +/// * `key`: [String] - The key for the metadata to be read. +/// +/// # Number of Storage Accesses +/// +/// * Reads: `2` +/// +/// # Example +/// +/// ```sway +/// use standards::src7::Metadata; +/// use sway_libs::asset::metadata::*; +/// use std::string::String; +/// +/// storage { +/// metadata: StorageMetadata = StorageMetadata {} +/// } +/// +/// fn foo(asset: AssetId, key: String) { +/// let result: Option = _metadata(storage.metadata, asset, key); +/// } +/// ``` #[storage(read)] pub fn _metadata( metadata_key: StorageKey, @@ -200,6 +231,27 @@ pub fn _metadata( } abi SetAssetMetadata { + /// Stores metadata for a specific asset and key pair. + /// + /// # Arguments + /// + /// * `asset`: [AssetId] - The asset for the metadata to be stored. + /// * `metadata`: [Option] - The metadata which to be stored. + /// * `key`: [String] - The key for the metadata to be stored. + /// + /// # Example + /// + /// ```sway + /// use standards::src7::{SRC7, Metadata}; + /// use sway_libs::asset::metadata::*; + /// use std::string::String; + /// + /// fn foo(contract_id: ContractId, asset: AssetId, metadata: Option, key: String) { + /// 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); } From bc1c0c9a4c4ebebf0846bef5b55d5dbc4617a3c9 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:59:17 +0800 Subject: [PATCH 06/17] Run formttaer --- examples/asset/metadata_docs/src/main.sw | 2 +- libs/src/asset/metadata.sw | 16 +- .../src/native_asset/tests/functions/burn.rs | 138 ++++++++++++++---- .../native_asset/tests/functions/decimals.rs | 4 +- .../native_asset/tests/functions/metadata.rs | 66 +++++++-- .../src/native_asset/tests/functions/mint.rs | 83 +++++++++-- .../src/native_asset/tests/functions/name.rs | 31 +--- .../tests/functions/set_metadata.rs | 119 ++++++++++++--- .../native_asset/tests/functions/set_name.rs | 106 +++----------- .../tests/functions/set_symbol.rs | 55 ++----- .../native_asset/tests/functions/symbol.rs | 31 +--- .../tests/functions/total_assets.rs | 16 +- .../tests/functions/total_supply.rs | 16 +- 13 files changed, 425 insertions(+), 258 deletions(-) diff --git a/examples/asset/metadata_docs/src/main.sw b/examples/asset/metadata_docs/src/main.sw index 3964238a..0150f09a 100644 --- a/examples/asset/metadata_docs/src/main.sw +++ b/examples/asset/metadata_docs/src/main.sw @@ -3,7 +3,7 @@ contract; use std::{bytes::Bytes, string::String}; // ANCHOR: import -use sway_libs::asset::metadata::{StorageMetadata, SetAssetMetadata, _set_metadata, _metadata}; +use sway_libs::asset::metadata::{_metadata, _set_metadata, SetAssetMetadata, StorageMetadata}; use standards::src7::*; // ANCHOR_END: import diff --git a/libs/src/asset/metadata.sw b/libs/src/asset/metadata.sw index 4d098b6c..fac9a69b 100644 --- a/libs/src/asset/metadata.sw +++ b/libs/src/asset/metadata.sw @@ -61,7 +61,12 @@ impl StorageKey { /// } /// ``` #[storage(read, write)] - pub fn insert(self, asset: AssetId, metadata: Option, key: String) { + pub fn insert( + self, + asset: AssetId, + metadata: Option, + key: String, +) { let hashed_key = sha256((asset, key)); match metadata { @@ -223,13 +228,12 @@ pub fn _set_metadata( /// ``` #[storage(read)] pub fn _metadata( - metadata_key: StorageKey, - asset: AssetId, - key: String + metadata_key: StorageKey, + asset: AssetId, + key: String, ) -> Option { metadata_key.get(asset, key) -} - +} abi SetAssetMetadata { /// Stores metadata for a specific asset and key pair. /// diff --git a/tests/src/native_asset/tests/functions/burn.rs b/tests/src/native_asset/tests/functions/burn.rs index f0bcdd3c..a6107259 100644 --- a/tests/src/native_asset/tests/functions/burn.rs +++ b/tests/src/native_asset/tests/functions/burn.rs @@ -20,14 +20,26 @@ mod success { mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); assert_eq!(total_assets(&instance_1).await, 1); let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 - burn_amount_1 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 - burn_amount_1) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -57,13 +69,25 @@ mod success { mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); assert_eq!(total_assets(&instance_1).await, 1); let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 - burn_amount_1 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 - burn_amount_1) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -79,8 +103,14 @@ mod success { ); let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_2).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1 - burn_amount_2); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1 - burn_amount_2)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 - burn_amount_1 - burn_amount_2 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 - burn_amount_1 - burn_amount_2) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -96,8 +126,14 @@ mod success { ); let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_3).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 - burn_amount_1 - burn_amount_2 - burn_amount_3) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -126,21 +162,51 @@ mod success { assert!(mint_amount_1 >= burn_amount_1); assert!(mint_amount_2 >= burn_amount_2); - mint(&instance_1, identity2.clone(), Some(sub_id_1), mint_amount_1).await; + mint( + &instance_1, + identity2.clone(), + Some(sub_id_1), + mint_amount_1, + ) + .await; mint(&instance_1, identity2, Some(sub_id_2), mint_amount_2).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_2).await, + mint_amount_2 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); + assert_eq!( + total_supply(&instance_1, asset_id_2).await, + Some(mint_amount_2) + ); assert_eq!(total_assets(&instance_1).await, 2); let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 - burn_amount_1 + ); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_2).await, + mint_amount_2 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 - burn_amount_1) + ); + assert_eq!( + total_supply(&instance_1, asset_id_2).await, + Some(mint_amount_2) + ); assert_eq!(total_assets(&instance_1).await, 2); let log = response .decode_logs_with_type::() @@ -157,10 +223,22 @@ mod success { let response = burn(&instance_2, asset_id_2, sub_id_2, burn_amount_2).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 - burn_amount_1); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2 - burn_amount_2); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 - burn_amount_1)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2 - burn_amount_2)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 - burn_amount_1 + ); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_2).await, + mint_amount_2 - burn_amount_2 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 - burn_amount_1) + ); + assert_eq!( + total_supply(&instance_1, asset_id_2).await, + Some(mint_amount_2 - burn_amount_2) + ); assert_eq!(total_assets(&instance_1).await, 2); let log = response .decode_logs_with_type::() @@ -188,8 +266,14 @@ mod success { mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); assert_eq!(total_assets(&instance_1).await, 1); let response = burn(&instance_2, asset_id_1, sub_id_1, burn_amount_1).await; diff --git a/tests/src/native_asset/tests/functions/decimals.rs b/tests/src/native_asset/tests/functions/decimals.rs index a87c8a19..397258be 100644 --- a/tests/src/native_asset/tests/functions/decimals.rs +++ b/tests/src/native_asset/tests/functions/decimals.rs @@ -32,7 +32,7 @@ mod success { let decimals_4 = 16u8; let decimals_5 = 9u8; let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); - let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); + let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); assert_eq!(decimals(&instance_1, asset_id_1).await, None); @@ -46,7 +46,7 @@ mod success { set_decimals(&instance_1, asset_id_3, decimals_3).await; set_decimals(&instance_1, asset_id_4, decimals_4).await; set_decimals(&instance_1, asset_id_5, decimals_5).await; - + assert_eq!(decimals(&instance_1, asset_id_1).await, Some(decimals_1)); assert_eq!(decimals(&instance_1, asset_id_2).await, Some(decimals_2)); assert_eq!(decimals(&instance_1, asset_id_3).await, Some(decimals_3)); diff --git a/tests/src/native_asset/tests/functions/metadata.rs b/tests/src/native_asset/tests/functions/metadata.rs index b06a4c17..ca79648f 100644 --- a/tests/src/native_asset/tests/functions/metadata.rs +++ b/tests/src/native_asset/tests/functions/metadata.rs @@ -28,7 +28,13 @@ mod success { assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key).await, Some(metadata1) @@ -49,10 +55,28 @@ mod success { assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); - - set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; - set_metadata(&instance_1, asset_id_2, key.clone(), Some(metadata2.clone())).await; - set_metadata(&instance_1, asset_id_3, key.clone(), Some(metadata3.clone())).await; + + set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; + set_metadata( + &instance_1, + asset_id_2, + key.clone(), + Some(metadata2.clone()), + ) + .await; + set_metadata( + &instance_1, + asset_id_3, + key.clone(), + Some(metadata3.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, @@ -90,10 +114,34 @@ mod success { assert_eq!(metadata(&instance_1, asset_id_1, key3.clone()).await, None); assert_eq!(metadata(&instance_1, asset_id_1, key4.clone()).await, None); - set_metadata(&instance_1, asset_id_1, key1.clone(), Some(metadata1.clone())).await; - set_metadata(&instance_1, asset_id_1, key2.clone(), Some(metadata2.clone())).await; - set_metadata(&instance_1, asset_id_1, key3.clone(), Some(metadata3.clone())).await; - set_metadata(&instance_1, asset_id_1, key4.clone(), Some(metadata4.clone())).await; + set_metadata( + &instance_1, + asset_id_1, + key1.clone(), + Some(metadata1.clone()), + ) + .await; + set_metadata( + &instance_1, + asset_id_1, + key2.clone(), + Some(metadata2.clone()), + ) + .await; + set_metadata( + &instance_1, + asset_id_1, + key3.clone(), + Some(metadata3.clone()), + ) + .await; + set_metadata( + &instance_1, + asset_id_1, + key4.clone(), + Some(metadata4.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key1.clone()).await, diff --git a/tests/src/native_asset/tests/functions/mint.rs b/tests/src/native_asset/tests/functions/mint.rs index 4a25bcb0..2669c4aa 100644 --- a/tests/src/native_asset/tests/functions/mint.rs +++ b/tests/src/native_asset/tests/functions/mint.rs @@ -21,8 +21,14 @@ mod success { let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -48,8 +54,14 @@ mod success { let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount_1).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -65,8 +77,14 @@ mod success { ); let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount_2).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1 + mint_amount_2); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1 + mint_amount_2)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + mint_amount_2 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1 + mint_amount_2) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -95,8 +113,14 @@ mod success { let response = mint(&instance_1, identity2, Some(sub_id_1), mint_amount).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount) + ); assert_eq!(total_assets(&instance_1).await, 1); let log = response .decode_logs_with_type::() @@ -126,7 +150,10 @@ mod success { let response = mint(&instance_1, identity2, None, mint_amount).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id).await, mint_amount); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id).await, + mint_amount + ); assert_eq!(total_supply(&instance_1, asset_id).await, Some(mint_amount)); assert_eq!(total_assets(&instance_1).await, 1); let log = response @@ -151,11 +178,23 @@ mod success { let mint_amount_1 = 100; let mint_amount_2 = 200; - let response = mint(&instance_1, identity2.clone(), Some(sub_id_1), mint_amount_1).await; + let response = mint( + &instance_1, + identity2.clone(), + Some(sub_id_1), + mint_amount_1, + ) + .await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, 0); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); assert_eq!(total_supply(&instance_1, asset_id_2).await, None); assert_eq!(total_assets(&instance_1).await, 1); let log = response @@ -173,10 +212,22 @@ mod success { let response = mint(&instance_1, identity2, Some(sub_id_2), mint_amount_2).await; - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_1).await, mint_amount_1); - assert_eq!(get_wallet_balance(&other_wallet, &asset_id_2).await, mint_amount_2); - assert_eq!(total_supply(&instance_1, asset_id_1).await, Some(mint_amount_1)); - assert_eq!(total_supply(&instance_1, asset_id_2).await, Some(mint_amount_2)); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_1).await, + mint_amount_1 + ); + assert_eq!( + get_wallet_balance(&other_wallet, &asset_id_2).await, + mint_amount_2 + ); + assert_eq!( + total_supply(&instance_1, asset_id_1).await, + Some(mint_amount_1) + ); + assert_eq!( + total_supply(&instance_1, asset_id_2).await, + Some(mint_amount_2) + ); assert_eq!(total_assets(&instance_1).await, 2); let log = response .decode_logs_with_type::() diff --git a/tests/src/native_asset/tests/functions/name.rs b/tests/src/native_asset/tests/functions/name.rs index f6717fc3..d7bf607d 100644 --- a/tests/src/native_asset/tests/functions/name.rs +++ b/tests/src/native_asset/tests/functions/name.rs @@ -27,10 +27,7 @@ mod success { assert_eq!(name(&instance_1, asset_id_1).await, None); set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1) - ); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1)); } #[tokio::test] @@ -47,7 +44,6 @@ mod success { let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); - assert_eq!(name(&instance_1, asset_id_1).await, None); assert_eq!(name(&instance_1, asset_id_2).await, None); assert_eq!(name(&instance_1, asset_id_3).await, None); @@ -60,25 +56,10 @@ mod success { set_name(&instance_1, asset_id_4, Some(name_4.clone())).await; set_name(&instance_1, asset_id_5, Some(name_5.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1) - ); - assert_eq!( - name(&instance_1, asset_id_2).await, - Some(name_2) - ); - assert_eq!( - name(&instance_1, asset_id_3).await, - Some(name_3) - ); - assert_eq!( - name(&instance_1, asset_id_4).await, - Some(name_4) - ); - assert_eq!( - name(&instance_1, asset_id_5).await, - Some(name_5) - ); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1)); + assert_eq!(name(&instance_1, asset_id_2).await, Some(name_2)); + assert_eq!(name(&instance_1, asset_id_3).await, Some(name_3)); + assert_eq!(name(&instance_1, asset_id_4).await, Some(name_4)); + assert_eq!(name(&instance_1, asset_id_5).await, Some(name_5)); } } diff --git a/tests/src/native_asset/tests/functions/set_metadata.rs b/tests/src/native_asset/tests/functions/set_metadata.rs index b0e29289..84d20f45 100644 --- a/tests/src/native_asset/tests/functions/set_metadata.rs +++ b/tests/src/native_asset/tests/functions/set_metadata.rs @@ -18,7 +18,13 @@ mod success { assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -48,7 +54,13 @@ mod success { let metadata1 = Metadata::String(String::from("Fuel NFT Metadata")); let key = String::from("key1"); - set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -83,7 +95,13 @@ mod success { let key = String::from("key1"); assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -105,7 +123,13 @@ mod success { ); assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_2, key.clone(), Some(metadata2.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_2, + key.clone(), + Some(metadata2.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_2, key.clone()).await, Some(metadata2.clone()) @@ -128,7 +152,13 @@ mod success { let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_3, key.clone(), Some(metadata3.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_3, + key.clone(), + Some(metadata3.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_3, key.clone()).await, Some(metadata3.clone()) @@ -162,7 +192,13 @@ mod success { let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(metadata(&instance_1, asset_id_1, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1.clone(), key.clone()).await, Some(metadata1.clone()) @@ -182,7 +218,13 @@ mod success { ); assert_eq!(metadata(&instance_1, asset_id_2, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_2, key.clone(), Some(metadata2.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_2, + key.clone(), + Some(metadata2.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1.clone()) @@ -206,7 +248,13 @@ mod success { ); assert_eq!(metadata(&instance_1, asset_id_3, key.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_3, key.clone(), Some(metadata3.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_3, + key.clone(), + Some(metadata3.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key.clone()).await, Some(metadata1) @@ -252,7 +300,13 @@ mod success { let key4 = String::from("key4"); assert_eq!(metadata(&instance_1, asset_id_1, key1.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key1.clone(), Some(metadata1.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key1.clone(), + Some(metadata1.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key1.clone()).await, Some(metadata1.clone()) @@ -272,7 +326,13 @@ mod success { ); assert_eq!(metadata(&instance_1, asset_id_1, key2.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key2.clone(), Some(metadata2.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key2.clone(), + Some(metadata2.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key2.clone()).await, Some(metadata2.clone()) @@ -296,7 +356,13 @@ mod success { ); assert_eq!(metadata(&instance_1, asset_id_1, key3.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key3.clone(), Some(metadata3.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key3.clone(), + Some(metadata3.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key3.clone()).await, Some(metadata3.clone()) @@ -324,7 +390,13 @@ mod success { ); assert_eq!(metadata(&instance_1, asset_id_1, key4.clone()).await, None); - let response = set_metadata(&instance_1, asset_id_1, key4.clone(), Some(metadata4.clone())).await; + let response = set_metadata( + &instance_1, + asset_id_1, + key4.clone(), + Some(metadata4.clone()), + ) + .await; assert_eq!( metadata(&instance_1, asset_id_1, key4.clone()).await, Some(metadata4.clone()) @@ -370,7 +442,13 @@ mod revert { let metadata1 = Metadata::String(String::from("")); let key = String::from("key1"); - set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; } #[tokio::test] @@ -379,13 +457,16 @@ mod revert { 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) = defaults(id, owner_wallet.clone(), other_wallet.clone()); - let metadata1 = Metadata::Bytes( - Bytes::from_hex_str("") - .expect("failed to convert to bytes"), - ); + let metadata1 = + Metadata::Bytes(Bytes::from_hex_str("").expect("failed to convert to bytes")); let key = String::from("key1"); - set_metadata(&instance_1, asset_id_1, key.clone(), Some(metadata1.clone())).await; + set_metadata( + &instance_1, + asset_id_1, + key.clone(), + Some(metadata1.clone()), + ) + .await; } - } diff --git a/tests/src/native_asset/tests/functions/set_name.rs b/tests/src/native_asset/tests/functions/set_name.rs index 3453cb58..03246153 100644 --- a/tests/src/native_asset/tests/functions/set_name.rs +++ b/tests/src/native_asset/tests/functions/set_name.rs @@ -18,13 +18,8 @@ mod success { assert_eq!(name(&instance_1, asset_id_1).await, None); let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -44,16 +39,11 @@ mod success { let name_1 = String::from("Fuel Asset 1"); set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1.clone()) - ); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1.clone())); let response = set_name(&instance_1, asset_id_1, None).await; assert_eq!(name(&instance_1, asset_id_1).await, None); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -74,13 +64,8 @@ mod success { let name_2 = String::from("Fuel Asset 2"); let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -92,13 +77,8 @@ mod success { ); let response = set_name(&instance_1, asset_id_1, Some(name_2.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_2.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_2.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -126,13 +106,8 @@ mod success { assert_eq!(name(&instance_1, asset_id_1).await, None); let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -145,13 +120,8 @@ mod success { assert_eq!(name(&instance_1, asset_id_2).await, None); let response = set_name(&instance_1, asset_id_2, Some(name_2.clone())).await; - assert_eq!( - name(&instance_1, asset_id_2).await, - Some(name_2.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_2).await, Some(name_2.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -164,13 +134,8 @@ mod success { assert_eq!(name(&instance_1, asset_id_3).await, None); let response = set_name(&instance_1, asset_id_3, Some(name_3.clone())).await; - assert_eq!( - name(&instance_1, asset_id_3).await, - Some(name_3.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_3).await, Some(name_3.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -183,13 +148,8 @@ mod success { assert_eq!(name(&instance_1, asset_id_4).await, None); let response = set_name(&instance_1, asset_id_4, Some(name_4.clone())).await; - assert_eq!( - name(&instance_1, asset_id_4).await, - Some(name_4.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_4).await, Some(name_4.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -202,13 +162,8 @@ mod success { assert_eq!(name(&instance_1, asset_id_5).await, None); let response = set_name(&instance_1, asset_id_5, Some(name_5.clone())).await; - assert_eq!( - name(&instance_1, asset_id_5).await, - Some(name_5.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_5).await, Some(name_5.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -227,16 +182,11 @@ mod success { defaults(id, owner_wallet.clone(), other_wallet.clone()); let name_1 = String::from("Fuel Asset 1"); let name_2 = String::from("Fuel Asset 2"); - + assert_eq!(name(&instance_1, asset_id_1).await, None); let response = set_name(&instance_1, asset_id_1, Some(name_1.clone())).await; - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1.clone()) - ); - let log = response - .decode_logs_with_type::() - .unwrap(); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1.clone())); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -249,9 +199,7 @@ mod success { assert_eq!(name(&instance_1, asset_id_2).await, None); let response = set_name(&instance_1, asset_id_2, Some(name_2.clone())).await; - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -262,14 +210,8 @@ mod success { } ); - assert_eq!( - name(&instance_1, asset_id_1).await, - Some(name_1) - ); - assert_eq!( - name(&instance_1, asset_id_2).await, - Some(name_2) - ); + assert_eq!(name(&instance_1, asset_id_1).await, Some(name_1)); + assert_eq!(name(&instance_1, asset_id_2).await, Some(name_2)); } } diff --git a/tests/src/native_asset/tests/functions/set_symbol.rs b/tests/src/native_asset/tests/functions/set_symbol.rs index 38aa6ec8..bd18cc43 100644 --- a/tests/src/native_asset/tests/functions/set_symbol.rs +++ b/tests/src/native_asset/tests/functions/set_symbol.rs @@ -22,9 +22,7 @@ mod success { symbol(&instance_1, asset_id_1).await, Some(symbol_1.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -51,9 +49,7 @@ mod success { let response = set_symbol(&instance_1, asset_id_1, None).await; assert_eq!(symbol(&instance_1, asset_id_1).await, None); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -73,15 +69,12 @@ mod success { let symbol_1 = String::from("FA1"); let symbol_2 = String::from("FA2"); - let response = set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; assert_eq!( symbol(&instance_1, asset_id_1).await, Some(symbol_1.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -97,9 +90,7 @@ mod success { symbol(&instance_1, asset_id_1).await, Some(symbol_2.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -131,9 +122,7 @@ mod success { symbol(&instance_1, asset_id_1).await, Some(symbol_1.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -150,9 +139,7 @@ mod success { symbol(&instance_1, asset_id_2).await, Some(symbol_2.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -169,9 +156,7 @@ mod success { symbol(&instance_1, asset_id_3).await, Some(symbol_3.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -188,9 +173,7 @@ mod success { symbol(&instance_1, asset_id_4).await, Some(symbol_4.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -207,9 +190,7 @@ mod success { symbol(&instance_1, asset_id_5).await, Some(symbol_5.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -235,9 +216,7 @@ mod success { symbol(&instance_1, asset_id_1).await, Some(symbol_1.clone()) ); - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -250,9 +229,7 @@ mod success { assert_eq!(symbol(&instance_1, asset_id_2).await, None); let response = set_symbol(&instance_1, asset_id_2, Some(symbol_2.clone())).await; - let log = response - .decode_logs_with_type::() - .unwrap(); + let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( *event, @@ -263,14 +240,8 @@ mod success { } ); - assert_eq!( - symbol(&instance_1, asset_id_1).await, - Some(symbol_1) - ); - assert_eq!( - symbol(&instance_1, asset_id_2).await, - Some(symbol_2) - ); + assert_eq!(symbol(&instance_1, asset_id_1).await, Some(symbol_1)); + assert_eq!(symbol(&instance_1, asset_id_2).await, Some(symbol_2)); } } diff --git a/tests/src/native_asset/tests/functions/symbol.rs b/tests/src/native_asset/tests/functions/symbol.rs index 5c6b3ecd..1b27e473 100644 --- a/tests/src/native_asset/tests/functions/symbol.rs +++ b/tests/src/native_asset/tests/functions/symbol.rs @@ -26,12 +26,8 @@ mod success { assert_eq!(symbol(&instance_1, asset_id_1).await, None); - set_symbol(&instance_1, asset_id_1, Some(symbol_1.clone())).await; - assert_eq!( - symbol(&instance_1, asset_id_1).await, - Some(symbol_1) - ); + assert_eq!(symbol(&instance_1, asset_id_1).await, Some(symbol_1)); } #[tokio::test] @@ -60,25 +56,10 @@ mod success { set_symbol(&instance_1, asset_id_4, Some(symbol_4.clone())).await; set_symbol(&instance_1, asset_id_5, Some(symbol_5.clone())).await; - assert_eq!( - symbol(&instance_1, asset_id_1).await, - Some(symbol_1) - ); - assert_eq!( - symbol(&instance_1, asset_id_2).await, - Some(symbol_2) - ); - assert_eq!( - symbol(&instance_1, asset_id_3).await, - Some(symbol_3) - ); - assert_eq!( - symbol(&instance_1, asset_id_4).await, - Some(symbol_4) - ); - assert_eq!( - symbol(&instance_1, asset_id_5).await, - Some(symbol_5) - ); + assert_eq!(symbol(&instance_1, asset_id_1).await, Some(symbol_1)); + assert_eq!(symbol(&instance_1, asset_id_2).await, Some(symbol_2)); + assert_eq!(symbol(&instance_1, asset_id_3).await, Some(symbol_3)); + assert_eq!(symbol(&instance_1, asset_id_4).await, Some(symbol_4)); + assert_eq!(symbol(&instance_1, asset_id_5).await, Some(symbol_5)); } } diff --git a/tests/src/native_asset/tests/functions/total_assets.rs b/tests/src/native_asset/tests/functions/total_assets.rs index afde490f..d3063348 100644 --- a/tests/src/native_asset/tests/functions/total_assets.rs +++ b/tests/src/native_asset/tests/functions/total_assets.rs @@ -34,10 +34,22 @@ mod success { mint(&instance_1, identity2.clone(), Some(sub_id_2), 200).await; assert_eq!(total_assets(&instance_1).await, 2); - mint(&instance_1, identity2.clone(), Some(Bits256([3u8; 32])), 300).await; + mint( + &instance_1, + identity2.clone(), + Some(Bits256([3u8; 32])), + 300, + ) + .await; assert_eq!(total_assets(&instance_1).await, 3); - mint(&instance_1, identity2.clone(), Some(Bits256([4u8; 32])), 400).await; + mint( + &instance_1, + identity2.clone(), + Some(Bits256([4u8; 32])), + 400, + ) + .await; assert_eq!(total_assets(&instance_1).await, 4); mint(&instance_1, identity2, Some(Bits256([5u8; 32])), 200).await; diff --git a/tests/src/native_asset/tests/functions/total_supply.rs b/tests/src/native_asset/tests/functions/total_supply.rs index 77d1c448..70bc703c 100644 --- a/tests/src/native_asset/tests/functions/total_supply.rs +++ b/tests/src/native_asset/tests/functions/total_supply.rs @@ -35,12 +35,24 @@ mod success { let asset_id_3 = get_asset_id(Bytes32::from([3u8; 32]), id); assert_eq!(total_supply(&instance_1, asset_id_3).await, None); - mint(&instance_1, identity2.clone(), Some(Bits256([3u8; 32])), 300).await; + mint( + &instance_1, + identity2.clone(), + Some(Bits256([3u8; 32])), + 300, + ) + .await; assert_eq!(total_supply(&instance_1, asset_id_3).await, Some(300)); let asset_id_4 = get_asset_id(Bytes32::from([4u8; 32]), id); assert_eq!(total_supply(&instance_1, asset_id_4).await, None); - mint(&instance_1, identity2.clone(), Some(Bits256([4u8; 32])), 400).await; + mint( + &instance_1, + identity2.clone(), + Some(Bits256([4u8; 32])), + 400, + ) + .await; assert_eq!(total_supply(&instance_1, asset_id_4).await, Some(400)); let asset_id_5 = get_asset_id(Bytes32::from([5u8; 32]), id); From 21ba44d0590d85aa757b15e028b6d3d27d820ff4 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 12:59:24 +0800 Subject: [PATCH 07/17] Update CHANGELOG --- CHANGELOG.md | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fe77ed0..6499995a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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` 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); +} +``` + ## [v0.23.1] ### Added v0.23.1 From 1e909dd44f9b55829bd94411ecfc2337e8d78ca4 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Thu, 29 Aug 2024 13:00:55 +0800 Subject: [PATCH 08/17] Fix markdown --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6499995a..0de9f403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,7 +85,7 @@ fn foo(my_metadata: Metadata) { } ``` -After: +After: ```sway use standards::src7::*; @@ -120,7 +120,7 @@ abi SetAssetMetadata { - [#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: +Before: ```sway fn foo(asset: AssetId, recipient: Identity, amount: u64, key: String, metadata: Metadata) { @@ -131,7 +131,7 @@ fn foo(asset: AssetId, recipient: Identity, amount: u64, key: String, metadata: } ``` -After: +After: ```sway fn foo(asset: AssetId, recipient: Identity, amount: u64, metadata: Metadata, key: String) { From c268ede37ea6dce961979eeae481f3a1a59b41d7 Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 02:56:15 +0100 Subject: [PATCH 09/17] updated CI --- .github/workflows/ci.yml | 6 +++--- .github/workflows/gh-pages.yml | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9fedbb14..a1d67ee5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,9 +15,9 @@ concurrency: env: CARGO_TERM_COLOR: always REGISTRY: ghcr.io - RUST_VERSION: 1.77.0 - FORC_VERSION: 0.60.0 - CORE_VERSION: 0.26.0 + RUST_VERSION: 1.80.1 + FORC_VERSION: 0.63.3 + CORE_VERSION: 0.34.0 jobs: build-sway-lib: diff --git a/.github/workflows/gh-pages.yml b/.github/workflows/gh-pages.yml index ef0b3769..1ad288bd 100644 --- a/.github/workflows/gh-pages.yml +++ b/.github/workflows/gh-pages.yml @@ -8,9 +8,9 @@ on: - v* env: - RUST_VERSION: 1.77.0 - FORC_VERSION: 0.60.0 - CORE_VERSION: 0.26.0 + RUST_VERSION: 1.80.1 + FORC_VERSION: 0.63.3 + CORE_VERSION: 0.34.0 jobs: deploy-contributing: From 61f70cb1893f14798c5f1160eec1b38fe27ff6f8 Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 02:56:34 +0100 Subject: [PATCH 10/17] updated docs --- README.md | 8 ++++---- docs/book/src/getting_started/index.md | 2 +- docs/book/src/getting_started/running_tests.md | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index da12f023..c114edf7 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ - - + + @@ -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.60.0` and `fuel-core v0.26.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.63.3` and `fuel-core v0.34.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/`. @@ -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.60.0`, `fuels-rs v0.62.0` and `fuel-core v0.26.0`. +> All projects currently use `forc v0.63.3`, `fuels-rs v0.66.2` and `fuel-core v0.34.0`. ## Contributing diff --git a/docs/book/src/getting_started/index.md b/docs/book/src/getting_started/index.md index 39d89f84..7746e199 100644 --- a/docs/book/src/getting_started/index.md +++ b/docs/book/src/getting_started/index.md @@ -38,7 +38,7 @@ use sway_libs::ownership::only_owner; ``` > **NOTE:** -> All projects currently use `forc v0.60.0`, `fuels-rs v0.62.0` and `fuel-core 0.26.0`. +> All projects currently use `forc 0.63.3`, `fuels-rs v0.66.2` and `fuel-core 0.34.0`. ## Using Sway Libs diff --git a/docs/book/src/getting_started/running_tests.md b/docs/book/src/getting_started/running_tests.md index 5bea15e2..c1b110d5 100644 --- a/docs/book/src/getting_started/running_tests.md +++ b/docs/book/src/getting_started/running_tests.md @@ -1,6 +1,6 @@ # 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.60.0` and `fuel-core v0.26.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.63.3` and `fuel-core v0.34.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/`. From 5a1820ae483334fc9131f725df881c49e312d656 Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 02:56:51 +0100 Subject: [PATCH 11/17] updated libs --- libs/Forc.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/Forc.lock b/libs/Forc.lock index 8710191c..3eb119c0 100644 --- a/libs/Forc.lock +++ b/libs/Forc.lock @@ -1,6 +1,6 @@ [[package]] name = "core" -source = "path+from-root-E19CE48B3E858B72" +source = "path+from-root-EF1196EF955CE54B" [[package]] name = "standards" @@ -9,7 +9,7 @@ dependencies = ["std"] [[package]] name = "std" -source = "git+https://github.com/fuellabs/sway?tag=v0.60.0#2f0392ee35a1e4dd80bd8034962d5b4083dfb8b6" +source = "git+https://github.com/fuellabs/sway?tag=v0.63.3#f55c81cce61aac31913ac0e87306cbaed7da679a" dependencies = ["core"] [[package]] From c70569c0e8fbfe032d353c4e1417fdc58b45c214 Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 02:57:03 +0100 Subject: [PATCH 12/17] updated examples --- examples/Cargo.toml | 2 +- examples/Forc.lock | 4 ++-- examples/merkle/mod.rs | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index be271caf..31b8ad4f 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -8,7 +8,7 @@ license = "Apache-2.0" [dependencies] fuel-merkle = { version = "0.50.0" } sha2 = { version = "0.10" } -fuels = { version = "0.62.0", features = ["fuel-core-lib"] } +fuels = { version = "0.66.2", features = ["fuel-core-lib"] } tokio = { version = "1.12", features = ["rt", "macros"] } [[test]] diff --git a/examples/Forc.lock b/examples/Forc.lock index d8fe4859..f859c0f1 100644 --- a/examples/Forc.lock +++ b/examples/Forc.lock @@ -53,7 +53,7 @@ dependencies = [ [[package]] name = "core" -source = "path+from-root-E19CE48B3E858B72" +source = "path+from-root-EF1196EF955CE54B" [[package]] name = "merkle_examples" @@ -146,7 +146,7 @@ dependencies = ["std"] [[package]] name = "std" -source = "git+https://github.com/fuellabs/sway?tag=v0.60.0#2f0392ee35a1e4dd80bd8034962d5b4083dfb8b6" +source = "git+https://github.com/fuellabs/sway?tag=v0.63.3#f55c81cce61aac31913ac0e87306cbaed7da679a" dependencies = ["core"] [[package]] diff --git a/examples/merkle/mod.rs b/examples/merkle/mod.rs index b889c3f0..e225d337 100644 --- a/examples/merkle/mod.rs +++ b/examples/merkle/mod.rs @@ -36,7 +36,7 @@ async fn get_contract_instance() -> (MerkleExample, WalletUnlock .await .unwrap(); - let instance = MerkleExample::new(id.clone(), wallet.clone()); + let instance = MerkleExample::new(id, wallet.clone()); (instance, wallet) } @@ -48,12 +48,12 @@ async fn rust_setup_example() { // ANCHOR: generating_a_tree // Create a new Merkle Tree and define leaves let mut tree = MerkleTree::new(); - let leaves = ["A".as_bytes(), "B".as_bytes(), "C".as_bytes()].to_vec(); + let leaves = [b"A", b"B", b"C"].to_vec(); // Hash the leaves and then push to the merkle tree - for datum in leaves.iter() { + for datum in &leaves { let mut hasher = Sha256::new(); - hasher.update(&datum); + hasher.update(datum); let hash = hasher.finalize(); tree.push(&hash); } @@ -69,14 +69,14 @@ async fn rust_setup_example() { // Convert the proof set from Vec to Vec let mut bits256_proof: Vec = Vec::new(); for itterator in proof_set { - bits256_proof.push(Bits256(itterator.clone())); + bits256_proof.push(Bits256(itterator)); } // ANCHOR_END: generating_proof // ANCHOR: verify_proof // Create the merkle leaf let mut leaf_hasher = Sha256::new(); - leaf_hasher.update(&leaves[key as usize]); + leaf_hasher.update(leaves[key as usize]); let hashed_leaf_data = leaf_hasher.finalize(); let merkle_leaf = leaf_sum(&hashed_leaf_data); @@ -104,7 +104,7 @@ async fn rust_setup_example() { pub fn leaf_sum(data: &[u8]) -> [u8; 32] { let mut hash = Sha256::new(); - hash.update(&[LEAF]); + hash.update([LEAF]); hash.update(data); hash.finalize().into() From 37e37955d98ca9e31fbb7911fa366350dde76745 Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 02:57:27 +0100 Subject: [PATCH 13/17] updated tests - bytecode lib tests fail --- tests/Cargo.toml | 6 ++--- tests/Forc.lock | 4 ++-- tests/src/admin/tests/utils/mod.rs | 12 +++++----- tests/src/bytecode/tests/utils/mod.rs | 12 +++++----- .../src/native_asset/tests/utils/interface.rs | 18 +++++++------- tests/src/ownership/tests/functions/owner.rs | 2 +- .../tests/functions/renounce_ownership.rs | 2 +- .../tests/functions/set_ownership.rs | 2 +- .../tests/functions/transfer_ownership.rs | 2 +- tests/src/ownership/tests/utils/mod.rs | 12 ++++------ tests/src/upgradability/src/main.sw | 24 ++++++++++++------- tests/src/upgradability/tests/utils/mod.rs | 20 ++++++---------- 12 files changed, 57 insertions(+), 59 deletions(-) diff --git a/tests/Cargo.toml b/tests/Cargo.toml index fcdee262..ac09f464 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -6,15 +6,15 @@ edition = "2021" license = "Apache-2.0" [dependencies] -fuel-merkle = { version = "0.49.0" } -fuels = { version = "0.62.0", features = ["fuel-core-lib"] } +fuel-merkle = { version = "0.56.0" } +fuels = { version = "0.66.2", features = ["fuel-core-lib"] } sha2 = { version = "0.10" } tokio = { version = "1.12", features = ["rt", "macros"] } rand = { version = "0.8.5", default-features = false, features = [ "std_rng", "getrandom", ] } -fuel-tx = { version = "0.43.1" } +fuel-tx = { version = "0.56.0" } [[test]] harness = true diff --git a/tests/Forc.lock b/tests/Forc.lock index 88243c18..6bd6ac1a 100644 --- a/tests/Forc.lock +++ b/tests/Forc.lock @@ -22,7 +22,7 @@ dependencies = ["std"] [[package]] name = "core" -source = "path+from-root-E19CE48B3E858B72" +source = "path+from-root-EF1196EF955CE54B" [[package]] name = "i128_test" @@ -215,7 +215,7 @@ dependencies = ["std"] [[package]] name = "std" -source = "git+https://github.com/fuellabs/sway?tag=v0.60.0#2f0392ee35a1e4dd80bd8034962d5b4083dfb8b6" +source = "git+https://github.com/fuellabs/sway?tag=v0.63.3#f55c81cce61aac31913ac0e87306cbaed7da679a" dependencies = ["core"] [[package]] diff --git a/tests/src/admin/tests/utils/mod.rs b/tests/src/admin/tests/utils/mod.rs index 214021e0..53a4aa8e 100644 --- a/tests/src/admin/tests/utils/mod.rs +++ b/tests/src/admin/tests/utils/mod.rs @@ -3,7 +3,7 @@ use fuels::{ abigen, launch_custom_provider_and_get_wallets, Contract, LoadConfiguration, StorageConfiguration, TxPolicies, WalletUnlocked, WalletsConfig, }, - programs::call_response::FuelCallResponse, + programs::responses::CallResponse, types::Identity, }; @@ -25,7 +25,7 @@ pub mod abi_calls { pub async fn add_admin( contract: &AdminLib, new_admin: Identity, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .add_admin(new_admin) @@ -37,7 +37,7 @@ pub mod abi_calls { pub async fn remove_admin( contract: &AdminLib, old_admin: Identity, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .remove_admin(old_admin) @@ -56,11 +56,11 @@ pub mod abi_calls { .value } - pub async fn only_admin(contract: &AdminLib) -> FuelCallResponse<()> { + pub async fn only_admin(contract: &AdminLib) -> CallResponse<()> { contract.methods().only_admin().call().await.unwrap() } - pub async fn only_owner_or_admin(contract: &AdminLib) -> FuelCallResponse<()> { + pub async fn only_owner_or_admin(contract: &AdminLib) -> CallResponse<()> { contract .methods() .only_owner_or_admin() @@ -72,7 +72,7 @@ pub mod abi_calls { pub async fn set_ownership( contract: &AdminLib, new_owner: Identity, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .set_ownership(new_owner) diff --git a/tests/src/bytecode/tests/utils/mod.rs b/tests/src/bytecode/tests/utils/mod.rs index bf05533f..5512f875 100644 --- a/tests/src/bytecode/tests/utils/mod.rs +++ b/tests/src/bytecode/tests/utils/mod.rs @@ -2,7 +2,7 @@ use fuels::{ accounts::predicate::Predicate, core::codec::{DecoderConfig, EncoderConfig}, prelude::*, - programs::call_response::FuelCallResponse, + programs::responses::CallResponse, tx::StorageSlot, types::{Bits256, Bytes32}, }; @@ -147,7 +147,7 @@ pub mod abi_calls { configurables: Option)>>, contract_id: ContractId, simple_contract_instance: SimpleContract, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .verify_contract_bytecode(contract_id, bytecode, configurables) @@ -163,7 +163,7 @@ pub mod abi_calls { configurables: Option)>>, contract_id: ContractId, complex_contract_instance: ComplexContract, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .clone() .with_encoder_config(EncoderConfig { @@ -183,7 +183,7 @@ pub mod abi_calls { bytecode: Vec, configurables: Option)>>, predicate_id: Address, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .verify_predicate_address(predicate_id, bytecode, configurables) @@ -262,7 +262,7 @@ pub mod test_helpers { let rng = &mut StdRng::seed_from_u64(2322u64); let salt: [u8; 32] = rng.gen(); let storage_vec = Vec::::new(); - let result_id = Contract::new(bytecode, salt.into(), storage_vec) + let result_id = Contract::regular(bytecode, salt.into(), storage_vec) .deploy(&wallet, TxPolicies::default()) .await .unwrap(); @@ -347,7 +347,7 @@ pub mod test_helpers { let rng = &mut StdRng::seed_from_u64(2323u64); let salt: [u8; 32] = rng.gen(); let storage_vec = Vec::::new(); - let result_id = Contract::new(bytecode, salt.into(), storage_vec) + let result_id = Contract::regular(bytecode, salt.into(), storage_vec) .deploy(&wallet, TxPolicies::default()) .await .unwrap(); diff --git a/tests/src/native_asset/tests/utils/interface.rs b/tests/src/native_asset/tests/utils/interface.rs index 4b680608..7294ea43 100644 --- a/tests/src/native_asset/tests/utils/interface.rs +++ b/tests/src/native_asset/tests/utils/interface.rs @@ -1,8 +1,8 @@ use crate::native_asset::tests::utils::setup::{AssetLib, Metadata}; use fuels::{ prelude::{AssetId, CallParameters, TxPolicies, WalletUnlocked}, - programs::{call_response::FuelCallResponse, call_utils::TxDependencyExtension}, - types::{Bits256, Identity}, + programs::responses::CallResponse, + types::{transaction_builders::VariableOutputPolicy, Bits256, Identity}, }; pub(crate) async fn total_assets(contract: &AssetLib) -> u64 { @@ -51,11 +51,11 @@ pub(crate) async fn mint( recipient: Identity, sub_id: Option, amount: u64, -) -> FuelCallResponse<()> { +) -> CallResponse<()> { contract .methods() .mint(recipient, sub_id, amount) - .append_variable_outputs(1) + .with_variable_output_policy(VariableOutputPolicy::Exactly(1)) .call() .await .unwrap() @@ -66,7 +66,7 @@ pub(crate) async fn burn( asset_id: AssetId, sub_id: Bits256, amount: u64, -) -> FuelCallResponse<()> { +) -> CallResponse<()> { let call_params = CallParameters::new(amount, asset_id, 1_000_000); contract @@ -84,7 +84,7 @@ pub(crate) async fn set_name( contract: &AssetLib, asset: AssetId, name: Option, -) -> FuelCallResponse<()> { +) -> CallResponse<()> { contract .methods() .set_name(asset, name) @@ -97,7 +97,7 @@ pub(crate) async fn set_symbol( contract: &AssetLib, asset: AssetId, name: Option, -) -> FuelCallResponse<()> { +) -> CallResponse<()> { contract .methods() .set_symbol(asset, name) @@ -110,7 +110,7 @@ pub(crate) async fn set_decimals( contract: &AssetLib, asset: AssetId, decimals: u8, -) -> FuelCallResponse<()> { +) -> CallResponse<()> { contract .methods() .set_decimals(asset, decimals) @@ -138,7 +138,7 @@ pub(crate) async fn set_metadata( asset: AssetId, key: String, metadata: Option, -) -> FuelCallResponse<()> { +) -> CallResponse<()> { contract .methods() .set_metadata(asset, metadata, key) diff --git a/tests/src/ownership/tests/functions/owner.rs b/tests/src/ownership/tests/functions/owner.rs index 19062760..b5c84830 100644 --- a/tests/src/ownership/tests/functions/owner.rs +++ b/tests/src/ownership/tests/functions/owner.rs @@ -1,7 +1,7 @@ use crate::ownership::tests::utils::{ abi_calls::{owner, renounce_ownership, set_ownership}, - abigen_bindings::ownership_lib_mod::State, test_helpers::setup, + State, }; use fuels::types::Identity; diff --git a/tests/src/ownership/tests/functions/renounce_ownership.rs b/tests/src/ownership/tests/functions/renounce_ownership.rs index adf35a77..2d266f7d 100644 --- a/tests/src/ownership/tests/functions/renounce_ownership.rs +++ b/tests/src/ownership/tests/functions/renounce_ownership.rs @@ -1,7 +1,7 @@ use crate::ownership::tests::utils::{ abi_calls::{owner, renounce_ownership, set_ownership}, - abigen_bindings::ownership_lib_mod::State, test_helpers::setup, + State, }; use fuels::types::Identity; diff --git a/tests/src/ownership/tests/functions/set_ownership.rs b/tests/src/ownership/tests/functions/set_ownership.rs index dd1dbb86..c8316e77 100644 --- a/tests/src/ownership/tests/functions/set_ownership.rs +++ b/tests/src/ownership/tests/functions/set_ownership.rs @@ -1,7 +1,7 @@ use crate::ownership::tests::utils::{ abi_calls::{owner, renounce_ownership, set_ownership}, - abigen_bindings::ownership_lib_mod::State, test_helpers::setup, + State, }; use fuels::types::Identity; diff --git a/tests/src/ownership/tests/functions/transfer_ownership.rs b/tests/src/ownership/tests/functions/transfer_ownership.rs index 053f9a2f..cb5479eb 100644 --- a/tests/src/ownership/tests/functions/transfer_ownership.rs +++ b/tests/src/ownership/tests/functions/transfer_ownership.rs @@ -1,7 +1,7 @@ use crate::ownership::tests::utils::{ abi_calls::{owner, set_ownership, transfer_ownership}, - abigen_bindings::ownership_lib_mod::State, test_helpers::setup, + State, }; use fuels::types::Identity; diff --git a/tests/src/ownership/tests/utils/mod.rs b/tests/src/ownership/tests/utils/mod.rs index 924097fd..48063914 100644 --- a/tests/src/ownership/tests/utils/mod.rs +++ b/tests/src/ownership/tests/utils/mod.rs @@ -3,7 +3,7 @@ use fuels::{ abigen, launch_custom_provider_and_get_wallets, Contract, LoadConfiguration, StorageConfiguration, TxPolicies, WalletUnlocked, WalletsConfig, }, - programs::call_response::FuelCallResponse, + programs::responses::CallResponse, types::Identity, }; @@ -22,7 +22,7 @@ pub mod abi_calls { use super::*; - pub async fn only_owner(contract: &OwnershipLib) -> FuelCallResponse<()> { + pub async fn only_owner(contract: &OwnershipLib) -> CallResponse<()> { contract.methods().only_owner().call().await.unwrap() } @@ -30,9 +30,7 @@ pub mod abi_calls { contract.methods().owner().call().await.unwrap().value } - pub async fn renounce_ownership( - contract: &OwnershipLib, - ) -> FuelCallResponse<()> { + pub async fn renounce_ownership(contract: &OwnershipLib) -> CallResponse<()> { contract .methods() .renounce_ownership() @@ -44,7 +42,7 @@ pub mod abi_calls { pub async fn set_ownership( contract: &OwnershipLib, new_owner: Identity, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .set_ownership(new_owner) @@ -56,7 +54,7 @@ pub mod abi_calls { pub async fn transfer_ownership( contract: &OwnershipLib, new_owner: Identity, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .transfer_ownership(new_owner) diff --git a/tests/src/upgradability/src/main.sw b/tests/src/upgradability/src/main.sw index 275fb8ad..af6f04e0 100644 --- a/tests/src/upgradability/src/main.sw +++ b/tests/src/upgradability/src/main.sw @@ -14,11 +14,17 @@ configurable { INITIAL_OWNER: State = State::Uninitialized, } -#[namespace(SRC14)] storage { - // target is at sha256("storage_SRC14_0") - target: Option = None, - proxy_owner: State = State::Uninitialized, + SRC14 { + /// The [ContractId] of the target contract. + /// + /// # Additional Information + /// + /// `target` is stored at sha256("storage_SRC14_0") + target in 0x7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd55: Option = None, + /// The [State] of the proxy owner. + proxy_owner: State = State::Uninitialized, + }, } abi UpgradableTest { @@ -47,25 +53,25 @@ impl SRC14 for Contract { impl SRC14Extension for Contract { #[storage(read)] fn proxy_owner() -> State { - _proxy_owner(storage.proxy_owner) + _proxy_owner(storage::SRC14.proxy_owner) } } impl UpgradableTest for Contract { #[storage(read)] fn only_proxy_owner() { - only_proxy_owner(storage.proxy_owner); + only_proxy_owner(storage::SRC14.proxy_owner); } #[storage(write)] fn set_proxy_owner(new_proxy_owner: State) { - _set_proxy_owner(new_proxy_owner, storage.proxy_owner); + _set_proxy_owner(new_proxy_owner, storage::SRC14.proxy_owner); } // Used to immediately set the storage variables as the configured constants #[storage(write)] fn initialize_proxy() { - storage.target.write(INITIAL_TARGET); - storage.proxy_owner.write(INITIAL_OWNER); + storage::SRC14.target.write(INITIAL_TARGET); + storage::SRC14.proxy_owner.write(INITIAL_OWNER); } } diff --git a/tests/src/upgradability/tests/utils/mod.rs b/tests/src/upgradability/tests/utils/mod.rs index 4b52c7fa..556364c1 100644 --- a/tests/src/upgradability/tests/utils/mod.rs +++ b/tests/src/upgradability/tests/utils/mod.rs @@ -3,7 +3,7 @@ use fuels::{ abigen, launch_custom_provider_and_get_wallets, Contract, ContractId, LoadConfiguration, StorageConfiguration, TxPolicies, WalletUnlocked, WalletsConfig, }, - programs::call_response::FuelCallResponse, + programs::responses::CallResponse, }; // Load abi from json @@ -24,7 +24,7 @@ pub mod abi_calls { pub async fn set_proxy_target( contract: &UpgradabilityLib, new_target: ContractId, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .set_proxy_target(new_target) @@ -35,26 +35,22 @@ pub mod abi_calls { pub async fn proxy_target( contract: &UpgradabilityLib, - ) -> FuelCallResponse> { + ) -> CallResponse> { contract.methods().proxy_target().call().await.unwrap() } - pub async fn proxy_owner( - contract: &UpgradabilityLib, - ) -> FuelCallResponse { + pub async fn proxy_owner(contract: &UpgradabilityLib) -> CallResponse { contract.methods().proxy_owner().call().await.unwrap() } - pub async fn only_proxy_owner( - contract: &UpgradabilityLib, - ) -> FuelCallResponse<()> { + pub async fn only_proxy_owner(contract: &UpgradabilityLib) -> CallResponse<()> { contract.methods().only_proxy_owner().call().await.unwrap() } pub async fn set_proxy_owner( contract: &UpgradabilityLib, new_proxy_owner: State, - ) -> FuelCallResponse<()> { + ) -> CallResponse<()> { contract .methods() .set_proxy_owner(new_proxy_owner) @@ -63,9 +59,7 @@ pub mod abi_calls { .unwrap() } - pub async fn initialize_proxy( - contract: &UpgradabilityLib, - ) -> FuelCallResponse<()> { + pub async fn initialize_proxy(contract: &UpgradabilityLib) -> CallResponse<()> { contract.methods().initialize_proxy().call().await.unwrap() } } From 032285335a01fcd506a98f59467f1127798a688f Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 04:31:59 +0100 Subject: [PATCH 14/17] updated upgradability lib with pinned owner slot --- libs/src/upgradability.sw | 55 ++++++++++++++------------------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/libs/src/upgradability.sw b/libs/src/upgradability.sw index 829ed7f6..23c31b49 100644 --- a/libs/src/upgradability.sw +++ b/libs/src/upgradability.sw @@ -7,6 +7,11 @@ use ::upgradability::{errors::SetProxyOwnerError, events::{ProxyOwnerSet, ProxyT use std::{auth::msg_sender, storage::storage_api::{read, write}}; use standards::{src14::SRC14_TARGET_STORAGE, src5::{AccessError, State}}; +/// The storage slot to store the proxy owner State. +/// +/// Value is `sha256("storage_SRC14_1")`. +pub const PROXY_OWNER_STORAGE: b256 = 0xbb79927b15d9259ea316f2ecb2297d6cc8851888a98278c0a2e03e1a091ea754; + /// Returns the proxy target. /// /// # Returns @@ -66,10 +71,6 @@ pub fn _set_proxy_target(new_target: ContractId) { /// Returns the owner of the proxy. /// -/// # Arguments -/// -/// * `proxy_owner_storage_key`: [StorageKey] - The storage key of the stored proxy owner. -/// /// # Returns /// /// * [State] - The state of the proxy ownership. @@ -83,25 +84,19 @@ pub fn _set_proxy_target(new_target: ContractId) { /// ```sway /// use sway_libs::upgradability::_proxy_owner; /// -/// storage { -/// proxy_owner: State = State::Uninitialized, -/// } /// /// fn foo() { -/// let stored_proxy_owner = _proxy_owner(storage.proxy_owner); +/// let stored_proxy_owner = _proxy_owner(); /// } /// ``` #[storage(read)] -pub fn _proxy_owner(proxy_owner_storage_key: StorageKey) -> State { - proxy_owner_storage_key.read() +pub fn _proxy_owner() -> State { + let proxy_owner_key = StorageKey::new(PROXY_OWNER_STORAGE, 0, PROXY_OWNER_STORAGE); + proxy_owner_key.read() } /// Ensures that the sender is the proxy owner. /// -/// # Arguments -/// -/// * `proxy_owner_storage_key`: [StorageKey] - The storage key of the stored proxy owner. -/// /// # Reverts /// /// * When the sender is not the proxy owner. @@ -115,19 +110,15 @@ pub fn _proxy_owner(proxy_owner_storage_key: StorageKey) -> State { /// ```sway /// use sway_libs::ownership::only_proxy_owner; /// -/// storage { -/// proxy_owner: State = State::Uninitialized, -/// } -/// /// fn foo() { -/// only_proxy_owner(storage.proxy_owner); +/// only_proxy_owner(); /// // Do stuff here if the sender is the proxy owner /// } /// ``` #[storage(read)] -pub fn only_proxy_owner(proxy_owner_storage_key: StorageKey) { +pub fn only_proxy_owner() { require( - _proxy_owner(proxy_owner_storage_key) == State::Initialized(msg_sender().unwrap()), + _proxy_owner() == State::Initialized(msg_sender().unwrap()), AccessError::NotOwner, ); } @@ -141,7 +132,6 @@ pub fn only_proxy_owner(proxy_owner_storage_key: StorageKey) { /// # Arguments /// /// * `new_proxy_owner`: [State] - The new state of the proxy ownership. -/// * `proxy_owner_storage_key`: [StorageKey] - The storage key of the stored proxy owner. /// /// # Reverts /// @@ -157,31 +147,26 @@ pub fn only_proxy_owner(proxy_owner_storage_key: StorageKey) { /// ```sway /// use sway_libs::upgradability::{_proxy_owner, _set_proxy_owner}; /// -/// storage { -/// proxy_owner: State = State::Uninitialized, -/// } -/// /// fn foo(new_owner: Identity) { -/// assert(_proxy_owner(storage.proxy_owner) == State::Initialized(Identity::Address(Address::zero())); +/// assert(_proxy_owner() == State::Initialized(Identity::Address(Address::zero())); /// /// let new_proxy_owner = State::Initialized(new_owner); -/// _set_proxy_owner(new_proxy_owner, storage.proxy_owner); +/// _set_proxy_owner(new_proxy_owner); /// -/// assert(_proxy_owner(storage.proxy_owner) == State::Initialized(new_owner)); +/// assert(_proxy_owner() == State::Initialized(new_owner)); /// } /// ``` #[storage(write)] -pub fn _set_proxy_owner( - new_proxy_owner: State, - proxy_owner_storage_key: StorageKey, -) { - only_proxy_owner(proxy_owner_storage_key); +pub fn _set_proxy_owner(new_proxy_owner: State) { + only_proxy_owner(); + require( new_proxy_owner != State::Uninitialized, SetProxyOwnerError::CannotUninitialize, ); - proxy_owner_storage_key.write(new_proxy_owner); + let proxy_owner_key = StorageKey::new(PROXY_OWNER_STORAGE, 0, PROXY_OWNER_STORAGE); + proxy_owner_key.write(new_proxy_owner); log(ProxyOwnerSet { new_proxy_owner, From d791e0641562dc98e70627c650583fb60d932d7a Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 04:32:18 +0100 Subject: [PATCH 15/17] updated upgradability example with pinned owner slot --- examples/upgradability/src/main.sw | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/upgradability/src/main.sw b/examples/upgradability/src/main.sw index 26ebf40f..f7ec5a32 100644 --- a/examples/upgradability/src/main.sw +++ b/examples/upgradability/src/main.sw @@ -9,11 +9,21 @@ use standards::{src14::*, src5::*}; use sway_libs::upgradability::{_proxy_owner, _proxy_target, _set_proxy_target}; use standards::{src14::{SRC14, SRC14Extension}, src5::State}; -#[namespace(SRC14)] storage { - // target is at sha256("storage_SRC14_0") - target: Option = None, - proxy_owner: State = State::Uninitialized, + SRC14 { + /// The [ContractId] of the target contract. + /// + /// # Additional Information + /// + /// `target` is stored at sha256("storage_SRC14_0") + target in 0x7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd55: Option = None, + /// The [State] of the proxy owner. + /// + /// # Additional Information + /// + /// `proxy_owner` is stored at sha256("storage_SRC14_1") + proxy_owner in 0xbb79927b15d9259ea316f2ecb2297d6cc8851888a98278c0a2e03e1a091ea754: State = State::Uninitialized, + }, } impl SRC14 for Contract { @@ -31,7 +41,7 @@ impl SRC14 for Contract { impl SRC14Extension for Contract { #[storage(read)] fn proxy_owner() -> State { - _proxy_owner(storage.proxy_owner) + _proxy_owner() } } // ANCHOR_END: integrate_with_src14 @@ -53,21 +63,21 @@ fn proxy_target() -> Option { // ANCHOR: set_proxy_owner #[storage(write)] fn set_proxy_owner(new_proxy_owner: State) { - _set_proxy_owner(new_proxy_owner, storage.proxy_owner); + _set_proxy_owner(new_proxy_owner); } // ANCHOR_END: set_proxy_owner // ANCHOR: proxy_owner #[storage(read)] fn proxy_owner() -> State { - _proxy_owner(storage.proxy_owner) + _proxy_owner() } // ANCHOR_END: proxy_owner // ANCHOR: only_proxy_owner #[storage(read)] fn only_proxy_owner_may_call() { - only_proxy_owner(storage.proxy_owner); + only_proxy_owner(); // Only the proxy's owner may reach this line. } // ANCHOR_END: only_proxy_owner From 558e277c940c18340c37f36d1ad162a800d84a91 Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 04:32:40 +0100 Subject: [PATCH 16/17] updated upgradability testing with pinned owner slot --- tests/src/upgradability/src/main.sw | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/src/upgradability/src/main.sw b/tests/src/upgradability/src/main.sw index af6f04e0..313b8b68 100644 --- a/tests/src/upgradability/src/main.sw +++ b/tests/src/upgradability/src/main.sw @@ -23,7 +23,11 @@ storage { /// `target` is stored at sha256("storage_SRC14_0") target in 0x7bb458adc1d118713319a5baa00a2d049dd64d2916477d2688d76970c898cd55: Option = None, /// The [State] of the proxy owner. - proxy_owner: State = State::Uninitialized, + /// + /// # Additional Information + /// + /// `proxy_owner` is stored at sha256("storage_SRC14_1") + proxy_owner in 0xbb79927b15d9259ea316f2ecb2297d6cc8851888a98278c0a2e03e1a091ea754: State = State::Uninitialized, }, } @@ -53,19 +57,19 @@ impl SRC14 for Contract { impl SRC14Extension for Contract { #[storage(read)] fn proxy_owner() -> State { - _proxy_owner(storage::SRC14.proxy_owner) + _proxy_owner() } } impl UpgradableTest for Contract { #[storage(read)] fn only_proxy_owner() { - only_proxy_owner(storage::SRC14.proxy_owner); + only_proxy_owner(); } #[storage(write)] fn set_proxy_owner(new_proxy_owner: State) { - _set_proxy_owner(new_proxy_owner, storage::SRC14.proxy_owner); + _set_proxy_owner(new_proxy_owner); } // Used to immediately set the storage variables as the configured constants From 629e82289596bac77b5228ec0d9226993fd684cc Mon Sep 17 00:00:00 2001 From: K1-R1 Date: Fri, 30 Aug 2024 04:44:58 +0100 Subject: [PATCH 17/17] changelog --- CHANGELOG.md | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0de9f403..a0db38f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [#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. +- [#290](https://github.com/FuelLabs/sway-libs/pull/290) Update the Upgradeability library to use a specific storage slot for owner functionality. ### Fixed @@ -78,9 +79,9 @@ Before: 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_b256(); + let res: bool = my_metadata.is_string(); + let res: bool = my_metadata.is_bytes(); let res: bool = my_metadata.is_uint(); } ``` @@ -91,9 +92,9 @@ After: 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_b256(); + let res: bool = my_metadata.is_string(); + let res: bool = my_metadata.is_bytes(); let res: bool = my_metadata.is_uint(); } ``` @@ -143,6 +144,28 @@ fn foo(asset: AssetId, recipient: Identity, amount: u64, metadata: 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: + +```sway +fn foo() { + let stored_proxy_owner = _proxy_owner(storage.proxy_owner); + only_proxy_owner(storage.proxy_owner); + _set_proxy_owner(new_proxy_owner, storage.proxy_owner); +} +``` + +After: + +```sway +fn foo() { + let stored_proxy_owner = _proxy_owner(); + only_proxy_owner(); + _set_proxy_owner(new_proxy_owner); +} +``` + ## [v0.23.1] ### Added v0.23.1