From dff52ad8606b60c9d200e7690a5fcba59d5f7c81 Mon Sep 17 00:00:00 2001 From: chexware Date: Sat, 20 Jul 2024 19:12:02 +0100 Subject: [PATCH] fix: working unsigned creation of collections and tokens; updated get_nft_detail to return relevant NftAssetData instead of NftClassData --- modules/bridge/src/mock.rs | 11 +++-------- pallets/auction/src/lib.rs | 4 ++-- pallets/estate/src/mock.rs | 9 ++------- pallets/governance/src/mock.rs | 14 +++----------- pallets/metaverse/src/mock.rs | 9 ++------- pallets/nft-migration/src/lib.rs | 2 +- pallets/nft-migration/src/mock.rs | 4 ++-- pallets/nft-migration/src/tests.rs | 20 ++++++++++++++++---- pallets/nft/src/lib.rs | 8 ++++---- traits/core-primitives/src/lib.rs | 2 +- 10 files changed, 36 insertions(+), 47 deletions(-) diff --git a/modules/bridge/src/mock.rs b/modules/bridge/src/mock.rs index 2fc1f811..b70f392e 100644 --- a/modules/bridge/src/mock.rs +++ b/modules/bridge/src/mock.rs @@ -11,7 +11,7 @@ use sp_core::H256; use sp_runtime::traits::AccountIdConversion; use sp_runtime::{traits::IdentityLookup, ModuleError, Perbill}; -use core_primitives::{Attributes, CollectionType, NFTTrait, NftClassData, NftMetadata, TokenType}; +use core_primitives::{Attributes, CollectionType, NFTTrait, NftClassData, NftMetadata, TokenType, NftAssetData}; use primitives::{ continuum::MapTrait, Amount, ClassId, EstateId, FungibleTokenId, GroupCollectionId, MapSpotId, TokenId, UndeployedLandBlockId, @@ -383,16 +383,11 @@ impl NFTTrait for MockNFTHandler { CLASS_FUND_ID } - fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { - let new_data = NftClassData { + fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { + let new_data = NftAssetData { deposit: 0, attributes: test_attributes(1), - token_type: TokenType::Transferable, - collection_type: CollectionType::Collectable, is_locked: false, - royalty_fee: Perbill::from_percent(0u32), - mint_limit: None, - total_minted_tokens: 0u32, }; Ok(new_data) } diff --git a/pallets/auction/src/lib.rs b/pallets/auction/src/lib.rs index c49d9b7a..fd94175e 100644 --- a/pallets/auction/src/lib.rs +++ b/pallets/auction/src/lib.rs @@ -1362,8 +1362,8 @@ pub mod pallet { social_currency_id: FungibleTokenId, ) -> DispatchResult { // Get royalty fee - let nft_details = T::NFTHandler::get_nft_detail((asset_id.0, asset_id.1))?; - let royalty_fee: Self::Balance = nft_details.royalty_fee * *high_bid_price; + let class_details = T::NFTHandler::get_nft_class_detail(asset_id.0)?; + let royalty_fee: Self::Balance = class_details.royalty_fee * *high_bid_price; let class_fund = T::NFTHandler::get_class_fund(&asset_id.0); // Transfer loyalty fee from winner to class fund pot diff --git a/pallets/estate/src/mock.rs b/pallets/estate/src/mock.rs index 718a6870..3f3f566a 100644 --- a/pallets/estate/src/mock.rs +++ b/pallets/estate/src/mock.rs @@ -425,16 +425,11 @@ impl NFTTrait for MockNFTHandler { CLASS_FUND_ID } - fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { - let new_data = NftClassData { + fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { + let new_data = NftAssetData { deposit: 0, attributes: test_attributes(1), - token_type: TokenType::Transferable, - collection_type: CollectionType::Collectable, is_locked: false, - royalty_fee: Perbill::from_percent(0u32), - mint_limit: None, - total_minted_tokens: 0u32, }; Ok(new_data) } diff --git a/pallets/governance/src/mock.rs b/pallets/governance/src/mock.rs index 0aa998f7..22713a06 100644 --- a/pallets/governance/src/mock.rs +++ b/pallets/governance/src/mock.rs @@ -14,10 +14,7 @@ use sp_runtime::{ }; use sp_std::collections::btree_map::BTreeMap; -use metaverse_primitive::{ - Attributes, CollectionType, MetaverseInfo as MetaversePrimitiveInfo, MetaverseLandTrait, MetaverseMetadata, - MetaverseTrait, NFTTrait, NftClassData, NftMetadata, TokenType, -}; +use metaverse_primitive::{Attributes, CollectionType, MetaverseInfo as MetaversePrimitiveInfo, MetaverseLandTrait, MetaverseMetadata, MetaverseTrait, NFTTrait, NftClassData, NftMetadata, TokenType, NftAssetData}; use primitives::{Amount, ClassId, FungibleTokenId, GroupCollectionId, TokenId}; use sp_runtime::BuildStorage; @@ -351,16 +348,11 @@ impl NFTTrait for MockNFTHandler { CLASS_FUND_ID } - fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { - let new_data = NftClassData { + fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { + let new_data = NftAssetData { deposit: 0, attributes: test_attributes(1), - token_type: TokenType::Transferable, - collection_type: CollectionType::Collectable, is_locked: false, - royalty_fee: Perbill::from_percent(0u32), - mint_limit: None, - total_minted_tokens: 0u32, }; Ok(new_data) } diff --git a/pallets/metaverse/src/mock.rs b/pallets/metaverse/src/mock.rs index f125bbd4..4f18a43c 100644 --- a/pallets/metaverse/src/mock.rs +++ b/pallets/metaverse/src/mock.rs @@ -211,16 +211,11 @@ impl NFTTrait for MockNFTHandler { CLASS_FUND_ID } - fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { - let new_data = NftClassData { + fn get_nft_detail(_asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError> { + let new_data = NftAssetData { deposit: 0, attributes: test_attributes(1), - token_type: TokenType::Transferable, - collection_type: CollectionType::Collectable, is_locked: false, - royalty_fee: Perbill::from_percent(0u32), - mint_limit: None, - total_minted_tokens: 0u32, }; Ok(new_data) } diff --git a/pallets/nft-migration/src/lib.rs b/pallets/nft-migration/src/lib.rs index cf81636f..7a1d4fde 100644 --- a/pallets/nft-migration/src/lib.rs +++ b/pallets/nft-migration/src/lib.rs @@ -138,7 +138,7 @@ pub mod pallet { T::NFTSource::get_next_collection_id() == collection_id, Error::::InconsistentMigrationData ); - T::NFTSource::migrate_collection(collection_data); + T::NFTSource::migrate_collection(collection_data)?; Ok(()) } diff --git a/pallets/nft-migration/src/mock.rs b/pallets/nft-migration/src/mock.rs index 82400af9..683c339d 100644 --- a/pallets/nft-migration/src/mock.rs +++ b/pallets/nft-migration/src/mock.rs @@ -36,7 +36,7 @@ pub type Extrinsic = TestXt; pub const ALICE: AccountId = AccountId32::new([1; 32]); pub const BOB: AccountId = AccountId32::new([5; 32]); pub const COLLECTION_ID: GroupCollectionId = 0; -pub const CLASS_ID: ClassId = 1; +pub const CLASS_ID: ClassId = 0; fn test_attributes(x: u8) -> Attributes { let mut attr: Attributes = BTreeMap::new(); @@ -290,7 +290,7 @@ parameter_types! { pub const StorageDepositFee: Balance = 1; pub MaxBatchTransfer: u32 = 3; pub MaxBatchMinting: u32 = 12; - pub MaxMetadata: u32 = 10; + pub MaxMetadata: u32 = 1024; pub const MiningCurrencyId: FungibleTokenId = FungibleTokenId::MiningResource(0); pub const MetaverseNetworkTreasuryPalletId: PalletId = PalletId(*b"bit/trsy"); pub NftPalletId: PalletId = PalletId(*b"bit/bNFT"); diff --git a/pallets/nft-migration/src/tests.rs b/pallets/nft-migration/src/tests.rs index da5de1fe..73ee6424 100644 --- a/pallets/nft-migration/src/tests.rs +++ b/pallets/nft-migration/src/tests.rs @@ -1,5 +1,6 @@ #![cfg(test)] +use core_primitives::NFTTrait; use frame_support::{assert_err, assert_noop, assert_ok}; use sp_runtime::{traits::BadOrigin, Perbill}; use std::collections::BTreeMap; @@ -45,7 +46,7 @@ fn test_attributes(x: u8) -> core_primitives::Attributes { fn get_test_class_data() -> NftClassData { NftClassData { - deposit: 1, + deposit: 5, attributes: test_attributes(10), token_type: TokenType::Transferable, collection_type: CollectionType::Collectable, @@ -58,7 +59,7 @@ fn get_test_class_data() -> NftClassData { fn get_test_token_data() -> NftAssetData { NftAssetData { - deposit: 1, + deposit: 2, attributes: test_attributes(20), is_locked: false, } @@ -84,14 +85,18 @@ fn start_migration_should_work() { #[test] fn migrating_single_collection_should_work() { ExtBuilder::default().build().execute_with(|| { + init_test_collection(); let collection_id = Nft::get_next_collection_id(); assert_ok!(NftMigration::migrate_collection_unsigned( RuntimeOrigin::none(), collection_id, get_test_collection_data() )); - //assert_eq!(Nft::get_group_collection(collection_id), Some(get_test_collection_data())); - //assert_eq!(Nft::get_next_collection_id(), collection_id + 1); + assert_eq!(Nft::get_next_collection_id(), collection_id + 1); + assert_eq!( + Nft::get_group_collection(collection_id), + Some(get_test_collection_data()) + ); }); } @@ -119,6 +124,10 @@ fn migrating_single_class_should_work() { get_test_metadata(), get_test_class_data(), )); + assert_eq!(Nft::get_next_class_id(), class_id + 1); + assert_eq!(Nft::get_nft_group_collection(&class_id), Ok(COLLECTION_ID)); + assert_eq!(Nft::get_nft_class_detail(class_id), Ok(get_test_class_data())); + assert_eq!(Nft::get_total_issuance(class_id), Ok(0)); }); } @@ -154,6 +163,9 @@ fn migrating_single_token_should_work() { get_test_metadata(), get_test_token_data(), )); + assert_eq!(Nft::get_next_token_id(CLASS_ID), token_id + 1); + assert_eq!(Nft::get_nft_detail((CLASS_ID, token_id)), Ok(get_test_token_data())); + assert_eq!(Nft::check_ownership(&ALICE, &(CLASS_ID, token_id)), Ok(true)); }); } diff --git a/pallets/nft/src/lib.rs b/pallets/nft/src/lib.rs index 4bb52046..6e82e25a 100644 --- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -1550,8 +1550,8 @@ impl NFTTrait> for Pallet { Ok(who == &asset_info.owner) } - fn get_nft_detail(asset_id: (Self::ClassId, Self::TokenId)) -> Result>, DispatchError> { - let asset_info = NftModule::::classes(asset_id.0).ok_or(Error::::AssetInfoNotFound)?; + fn get_nft_detail(asset_id: (Self::ClassId, Self::TokenId)) -> Result>, DispatchError> { + let asset_info = NftModule::::tokens(asset_id.0, asset_id.1).ok_or(Error::::AssetInfoNotFound)?; Ok(asset_info.data) } @@ -1820,12 +1820,12 @@ impl NFTMigrationTrait> for Pallet { owner, owner, class_id, - Some(token_id), + None, token_metadata, token_data.attributes, token_data.is_locked, false, - ); + )?; // TODO: Transfer token minting fees back to owner Ok(()) } diff --git a/traits/core-primitives/src/lib.rs b/traits/core-primitives/src/lib.rs index 51699d7d..05779615 100644 --- a/traits/core-primitives/src/lib.rs +++ b/traits/core-primitives/src/lib.rs @@ -247,7 +247,7 @@ pub trait NFTTrait { /// Check the ownership of this nft asset fn check_ownership(who: &AccountId, asset_id: &(Self::ClassId, Self::TokenId)) -> Result; /// Get the detail of this nft - fn get_nft_detail(asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError>; + fn get_nft_detail(asset_id: (Self::ClassId, Self::TokenId)) -> Result, DispatchError>; /// Get the detail of this nft fn get_nft_group_collection(nft_collection: &Self::ClassId) -> Result; /// Check if the asset is a stackable NFT