From 80fe5f3dba31c62a9c3313cfacf99cf35d25e401 Mon Sep 17 00:00:00 2001 From: chungquantin <56880684+chungquantin@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:07:35 +0700 Subject: [PATCH] chore: rebase account balance deposit branch --- pallets/api/src/mock.rs | 1 + pallets/api/src/nonfungibles/mod.rs | 7 +++++-- pallets/api/src/nonfungibles/tests.rs | 30 ++++++++++++++++----------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pallets/api/src/mock.rs b/pallets/api/src/mock.rs index ef492b26..a721ee36 100644 --- a/pallets/api/src/mock.rs +++ b/pallets/api/src/mock.rs @@ -165,6 +165,7 @@ type NftsInstance = pallet_nfts::Instance1; impl pallet_nfts::Config for Test { type ApprovalsLimit = ConstU32<10>; type AttributeDepositBase = ConstU128<1>; + type BalanceDeposit = ConstU128<1>; type CollectionApprovalDeposit = ConstU128<1>; type CollectionDeposit = ConstU128<2>; type CollectionId = u32; diff --git a/pallets/api/src/nonfungibles/mod.rs b/pallets/api/src/nonfungibles/mod.rs index d10d9770..19c03198 100644 --- a/pallets/api/src/nonfungibles/mod.rs +++ b/pallets/api/src/nonfungibles/mod.rs @@ -585,8 +585,11 @@ pub mod pallet { TotalSupply(collection) => ReadResult::TotalSupply( NftsOf::::collection_items(collection).unwrap_or_default() as u128, ), - BalanceOf { collection, owner } => - ReadResult::BalanceOf(AccountBalanceOf::::get(collection, owner)), + BalanceOf { collection, owner } => ReadResult::BalanceOf( + AccountBalanceOf::::get(collection, owner) + .map(|(balance, _)| balance) + .unwrap_or_default(), + ), Allowance { collection, owner, operator, item } => ReadResult::Allowance( NftsOf::::check_approval(&collection, &item, &owner, &operator).is_ok(), ), diff --git a/pallets/api/src/nonfungibles/tests.rs b/pallets/api/src/nonfungibles/tests.rs index d6458476..aec7c9dc 100644 --- a/pallets/api/src/nonfungibles/tests.rs +++ b/pallets/api/src/nonfungibles/tests.rs @@ -109,10 +109,10 @@ fn transfer_works() { ); // Successfully transfer a collection item. nfts::create_collection_mint(owner, owner, item); - let balance_before_transfer = AccountBalanceOf::::get(collection, &dest); + let balance_before_transfer = nfts::balance_of(collection, &dest); assert_ok!(NonFungibles::transfer(signed(owner), collection, item, dest)); - let balance_after_transfer = AccountBalanceOf::::get(collection, &dest); - assert_eq!(AccountBalanceOf::::get(collection, &owner), 0); + let balance_after_transfer = nfts::balance_of(collection, &dest); + assert!(nfts::balance_of(collection, &owner).is_zero()); assert_eq!(balance_after_transfer - balance_before_transfer, 1); System::assert_last_event( Event::Transfer { collection, item, from: Some(owner), to: Some(dest), price: None } @@ -134,11 +134,11 @@ fn approved_transfer_works() { // Approve `operator` to transfer `collection` items owned by the `owner`. assert_ok!(Nfts::approve_collection_transfer(signed(owner), collection, operator, None)); // Successfully transfers a collection item. - let from_balance_before_transfer = AccountBalanceOf::::get(collection, &owner); - let to_balance_before_transfer = AccountBalanceOf::::get(collection, &dest); + let from_balance_before_transfer = nfts::balance_of(collection, &owner); + let to_balance_before_transfer = nfts::balance_of(collection, &dest); assert_ok!(NonFungibles::transfer(signed(operator), collection, item, dest)); - let from_balance_after_transfer = AccountBalanceOf::::get(collection, &owner); - let to_balance_after_transfer = AccountBalanceOf::::get(collection, &dest); + let from_balance_after_transfer = nfts::balance_of(collection, &owner); + let to_balance_after_transfer = nfts::balance_of(collection, &dest); // Check that `to` has received the `value` tokens from `from`. assert_eq!(to_balance_after_transfer, to_balance_before_transfer + 1); assert_eq!(from_balance_after_transfer, from_balance_before_transfer - 1); @@ -171,9 +171,9 @@ fn mint_works() { ); // Successfully mints a new collection item. nfts::create_collection(owner); - let balance_before_mint = AccountBalanceOf::::get(collection, owner); + let balance_before_mint = nfts::balance_of(collection, &owner); assert_ok!(NonFungibles::mint(signed(owner), owner, collection, item, witness)); - let balance_after_mint = AccountBalanceOf::::get(collection, owner); + let balance_after_mint = nfts::balance_of(collection, &owner); assert_eq!(balance_after_mint, balance_before_mint + 1); System::assert_last_event( Event::Transfer { collection, item, from: None, to: Some(owner), price: None }.into(), @@ -196,9 +196,9 @@ fn burn_works() { assert_noop!(NonFungibles::burn(signed(owner), collection, item), NftsError::UnknownItem); // Successfully burns an existing new collection item. nfts::create_collection_mint(owner, owner, ITEM); - let balance_before_burn = AccountBalanceOf::::get(collection, owner); + let balance_before_burn = nfts::balance_of(collection, &owner); assert_ok!(NonFungibles::burn(signed(owner), collection, item)); - let balance_after_burn = AccountBalanceOf::::get(collection, owner); + let balance_after_burn = nfts::balance_of(collection, &owner); assert_eq!(balance_after_burn, balance_before_burn - 1); System::assert_last_event( Event::Transfer { collection, item, from: Some(owner), to: None, price: None }.into(), @@ -718,7 +718,7 @@ fn balance_of_works() { ); assert_eq!( NonFungibles::read(BalanceOf { collection, owner }).encode(), - AccountBalanceOf::::get(collection, owner).encode() + nfts::balance_of(collection, &owner).encode() ); }); }); @@ -907,6 +907,12 @@ mod nfts { use super::*; use crate::nonfungibles::AttributeNamespaceOf; + pub(super) fn balance_of(collection: CollectionIdOf, owner: &AccountId) -> u32 { + AccountBalanceOf::::get(collection, &owner) + .map(|(balance, _)| balance) + .unwrap_or_default() + } + pub(super) fn create_collection_mint_and_approve( owner: AccountId, mint_to: AccountId,