Skip to content

Commit

Permalink
chore: rebase account balance deposit branch
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Dec 23, 2024
1 parent 46887f8 commit 80fe5f3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions pallets/api/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ type NftsInstance = pallet_nfts::Instance1;
impl pallet_nfts::Config<NftsInstance> 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;
Expand Down
7 changes: 5 additions & 2 deletions pallets/api/src/nonfungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,11 @@ pub mod pallet {
TotalSupply(collection) => ReadResult::TotalSupply(
NftsOf::<T>::collection_items(collection).unwrap_or_default() as u128,
),
BalanceOf { collection, owner } =>
ReadResult::BalanceOf(AccountBalanceOf::<T>::get(collection, owner)),
BalanceOf { collection, owner } => ReadResult::BalanceOf(
AccountBalanceOf::<T>::get(collection, owner)
.map(|(balance, _)| balance)
.unwrap_or_default(),
),
Allowance { collection, owner, operator, item } => ReadResult::Allowance(
NftsOf::<T>::check_approval(&collection, &item, &owner, &operator).is_ok(),
),
Expand Down
30 changes: 18 additions & 12 deletions pallets/api/src/nonfungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ fn transfer_works() {
);
// Successfully transfer a collection item.
nfts::create_collection_mint(owner, owner, item);
let balance_before_transfer = AccountBalanceOf::<Test>::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::<Test>::get(collection, &dest);
assert_eq!(AccountBalanceOf::<Test>::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 }
Expand All @@ -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::<Test>::get(collection, &owner);
let to_balance_before_transfer = AccountBalanceOf::<Test>::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::<Test>::get(collection, &owner);
let to_balance_after_transfer = AccountBalanceOf::<Test>::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);
Expand Down Expand Up @@ -171,9 +171,9 @@ fn mint_works() {
);
// Successfully mints a new collection item.
nfts::create_collection(owner);
let balance_before_mint = AccountBalanceOf::<Test>::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::<Test>::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(),
Expand All @@ -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::<Test>::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::<Test>::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(),
Expand Down Expand Up @@ -718,7 +718,7 @@ fn balance_of_works() {
);
assert_eq!(
NonFungibles::read(BalanceOf { collection, owner }).encode(),
AccountBalanceOf::<Test>::get(collection, owner).encode()
nfts::balance_of(collection, &owner).encode()
);
});
});
Expand Down Expand Up @@ -907,6 +907,12 @@ mod nfts {
use super::*;
use crate::nonfungibles::AttributeNamespaceOf;

pub(super) fn balance_of(collection: CollectionIdOf<Test>, owner: &AccountId) -> u32 {
AccountBalanceOf::<Test>::get(collection, &owner)
.map(|(balance, _)| balance)
.unwrap_or_default()
}

pub(super) fn create_collection_mint_and_approve(
owner: AccountId,
mint_to: AccountId,
Expand Down

0 comments on commit 80fe5f3

Please sign in to comment.