Skip to content

Commit

Permalink
test(api/nonfungibles): encoding_read_result
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Oct 18, 2024
1 parent 2653a05 commit c41134f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 21 deletions.
10 changes: 5 additions & 5 deletions pallets/api/src/nonfungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ pub mod pallet {
from: Option<AccountIdOf<T>>,
/// The recipient of the transfer. `None` when burning.
to: Option<AccountIdOf<T>>,
/// The amount minted.
value: Option<BalanceOf<T>>,
/// The price of the collection item.
price: Option<BalanceOf<T>>,
},
}

Expand Down Expand Up @@ -167,7 +167,7 @@ pub mod pallet {
item,
from: None,
to: Some(account),
value: mint_price,
price: mint_price,
});
Ok(())
}
Expand All @@ -186,7 +186,7 @@ pub mod pallet {
item,
from: Some(account),
to: None,
value: None,
price: None,
});
Ok(())
}
Expand All @@ -206,7 +206,7 @@ pub mod pallet {
item,
from: Some(from),
to: Some(to),
value: None,
price: None,
});
Ok(())
}
Expand Down
109 changes: 104 additions & 5 deletions pallets/api/src/nonfungibles/tests.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,131 @@
use codec::Encode;
use frame_support::{assert_ok, traits::nonfungibles_v2::InspectEnumerable};
use frame_support::assert_ok;
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_nfts::{AccountBalance, CollectionConfig, CollectionSettings, MintSettings};

use super::types::{AccountIdOf, CollectionIdOf, ItemIdOf};
use crate::{
mock::*,
nonfungibles::{Event, Read::*},
nonfungibles::{Event, Read::*, ReadResult},
Read,
};

const ITEM: u32 = 1;

mod encoding_read_result {
use pallet_nfts::{CollectionDetails, ItemDeposit, ItemDetails};
use sp_runtime::{BoundedBTreeMap, BoundedVec};

use super::*;

#[test]
fn total_supply() {
let total_supply: u32 = 1_000_000;
assert_eq!(ReadResult::TotalSupply::<Test>(total_supply).encode(), total_supply.encode());
}

#[test]
fn balance_of() {
let balance: u32 = 100;
assert_eq!(ReadResult::BalanceOf::<Test>(balance).encode(), balance.encode());
}

#[test]
fn allowance() {
let allowance = false;
assert_eq!(ReadResult::Allowance::<Test>(allowance).encode(), allowance.encode());
}

#[test]
fn owner_of() {
let mut owner = Some(account(ALICE));
assert_eq!(ReadResult::OwnerOf::<Test>(owner.clone()).encode(), owner.encode());
owner = None;
assert_eq!(ReadResult::OwnerOf::<Test>(owner.clone()).encode(), owner.encode());
}

#[test]
fn get_attribute() {
let mut attribute = Some(BoundedVec::truncate_from("some attribute".as_bytes().to_vec()));
assert_eq!(
ReadResult::GetAttribute::<Test>(attribute.clone()).encode(),
attribute.encode()
);
attribute = None;
assert_eq!(
ReadResult::GetAttribute::<Test>(attribute.clone()).encode(),
attribute.encode()
);
}

#[test]
fn collection_owner() {
let mut collection_owner = Some(account(ALICE));
assert_eq!(
ReadResult::CollectionOwner::<Test>(collection_owner.clone()).encode(),
collection_owner.encode()
);
collection_owner = None;
assert_eq!(
ReadResult::CollectionOwner::<Test>(collection_owner.clone()).encode(),
collection_owner.encode()
);
}

#[test]
fn collection() {
let mut collection_details = Some(CollectionDetails {
owner: account(ALICE),
owner_deposit: 0,
items: 0,
item_metadatas: 0,
item_configs: 0,
attributes: 0,
});
assert_eq!(
ReadResult::Collection::<Test>(collection_details.clone()).encode(),
collection_details.encode()
);
collection_details = None;
assert_eq!(
ReadResult::Collection::<Test>(collection_details.clone()).encode(),
collection_details.encode()
);
}

#[test]
fn item() {
let mut item_details = Some(ItemDetails {
owner: account(ALICE),
approvals: BoundedBTreeMap::default(),
deposit: ItemDeposit { amount: 0, account: account(BOB) },
});
assert_eq!(ReadResult::Item::<Test>(item_details.clone()).encode(), item_details.encode());
item_details = None;
assert_eq!(ReadResult::Item::<Test>(item_details.clone()).encode(), item_details.encode());
}
}

#[test]
fn mint_works() {
new_test_ext().execute_with(|| {
let owner = account(ALICE);
let collection = create_collection(owner.clone());
// Successfully mint a new collection item.
let balance_before_mint = AccountBalance::<Test>::get(collection.clone(), owner.clone());
//
assert_ok!(NonFungibles::mint(
signed(owner.clone()),
owner.clone(),
collection,
ITEM,
None
));
let balance_after_mint = AccountBalance::<Test>::get(collection.clone(), owner.clone());
assert_eq!(balance_after_mint, 1);
assert_eq!(balance_after_mint - balance_before_mint, 1);
System::assert_last_event(
Event::Transfer { collection, item: ITEM, from: None, to: Some(owner), value: None }
Event::Transfer { collection, item: ITEM, from: None, to: Some(owner), price: None }
.into(),
);
});
Expand All @@ -40,7 +139,7 @@ fn burn_works() {
// Successfully burn an existing new collection item.
assert_ok!(NonFungibles::burn(signed(owner.clone()), collection, ITEM));
System::assert_last_event(
Event::Transfer { collection, item, from: Some(owner), to: None, value: None }.into(),
Event::Transfer { collection, item, from: Some(owner), to: None, price: None }.into(),
);
});
}
Expand All @@ -54,7 +153,7 @@ fn transfer() {
// Successfully burn an existing new collection item.
assert_ok!(NonFungibles::transfer(signed(owner.clone()), collection, ITEM, dest.clone()));
System::assert_last_event(
Event::Transfer { collection, item, from: Some(owner), to: Some(dest), value: None }
Event::Transfer { collection, item, from: Some(owner), to: Some(dest), price: None }
.into(),
);
});
Expand Down
22 changes: 11 additions & 11 deletions pallets/nfts/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ pub(super) type PreSignedAttributesOf<T, I = ()> = PreSignedAttributes<
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct CollectionDetails<AccountId, DepositBalance> {
/// Collection's owner.
pub(super) owner: AccountId,
pub owner: AccountId,
/// The total balance deposited by the owner for all the storage data associated with this
/// collection. Used by `destroy`.
pub(super) owner_deposit: DepositBalance,
pub owner_deposit: DepositBalance,
/// The total number of outstanding items of this collection.
pub(super) items: u32,
pub items: u32,
/// The total number of outstanding item metadata of this collection.
pub(super) item_metadatas: u32,
pub item_metadatas: u32,
/// The total number of outstanding item configs of this collection.
pub(super) item_configs: u32,
pub item_configs: u32,
/// The total number of attributes for this collection.
pub(super) attributes: u32,
pub attributes: u32,
}

/// Witness data for the destroy transactions.
Expand Down Expand Up @@ -145,21 +145,21 @@ pub struct MintWitness<ItemId, Balance> {
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
pub struct ItemDetails<AccountId, Deposit, Approvals> {
/// The owner of this item.
pub(super) owner: AccountId,
pub owner: AccountId,
/// The approved transferrer of this item, if one is set.
pub(super) approvals: Approvals,
pub approvals: Approvals,
/// The amount held in the pallet's default account for this item. Free-hold items will have
/// this as zero.
pub(super) deposit: Deposit,
pub deposit: Deposit,
}

/// Information about the reserved item deposit.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ItemDeposit<DepositBalance, AccountId> {
/// A depositor account.
pub(super) account: AccountId,
pub account: AccountId,
/// An amount that gets reserved.
pub(super) amount: DepositBalance,
pub amount: DepositBalance,
}

/// Information about the collection's metadata.
Expand Down

0 comments on commit c41134f

Please sign in to comment.