difftreelog
Merge branch 'develop' into feature/NFTPAR-288
in: master
25 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -323,6 +323,8 @@
CollectionNotFound,
/// Item not exists.
TokenNotFound,
+ /// Admin not found
+ AdminNotFound,
/// Arithmetic calculation overflow.
NumOverflow,
/// Account already has admin role.
@@ -426,7 +428,7 @@
pub AddressTokens get(fn address_tokens): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => Vec<TokenId>;
/// Tokens transfer baskets
- pub CreateItemBasket get(fn create_item_basket): map hasher(identity) CollectionId => T::BlockNumber;
+ pub CreateItemBasket get(fn create_item_basket): map hasher(twox_64_concat) (CollectionId, T::AccountId) => T::BlockNumber;
pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => T::BlockNumber;
pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(twox_64_concat) T::AccountId => T::BlockNumber;
pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(identity) CollectionId, hasher(identity) TokenId => T::BlockNumber;
@@ -812,13 +814,11 @@
let sender = ensure_signed(origin)?;
Self::check_owner_or_admin_permissions(collection_id, sender)?;
+ ensure!(<AdminList<T>>::contains_key(collection_id), Error::<T>::AdminNotFound);
- if <AdminList<T>>::contains_key(collection_id)
- {
- let mut admin_arr = <AdminList<T>>::get(collection_id);
- admin_arr.retain(|i| *i != account_id);
- <AdminList<T>>::insert(collection_id, admin_arr);
- }
+ let mut admin_arr = <AdminList<T>>::get(collection_id);
+ admin_arr.retain(|i| *i != account_id);
+ <AdminList<T>>::insert(collection_id, admin_arr);
Ok(())
}
@@ -1009,7 +1009,7 @@
{
CollectionMode::NFT => Self::burn_nft_item(collection_id, item_id)?,
CollectionMode::Fungible(_) => Self::burn_fungible_item(&sender, collection_id, value)?,
- CollectionMode::ReFungible(_) => Self::burn_refungible_item(collection_id, item_id, sender.clone())?,
+ CollectionMode::ReFungible(_) => Self::burn_refungible_item(collection_id, item_id, &sender)?,
_ => ()
};
@@ -1092,6 +1092,9 @@
let sender = ensure_signed(origin)?;
+ Self::collection_exists(collection_id)?;
+ Self::token_exists(collection_id, item_id, &sender)?;
+
// Transfer permissions check
let target_collection = <Collection<T>>::get(collection_id);
ensure!(Self::is_item_owner(sender.clone(), collection_id, item_id) ||
@@ -1161,7 +1164,7 @@
}
// Reduce approval by transferred amount or remove if remaining approval drops to 0
- if approval - value > 0 {
+ if approval.checked_sub(value).unwrap_or(0) > 0 {
<Allowances<T>>::insert(collection_id, (item_id, &from, &recipient), approval - value);
}
else {
@@ -1216,7 +1219,8 @@
let sender = ensure_signed(origin)?;
Self::collection_exists(collection_id)?;
-
+ Self::token_exists(collection_id, item_id, &sender)?;
+
ensure!(ChainLimit::get().custom_data_limit >= data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);
// Modify permissions check
@@ -1224,8 +1228,6 @@
ensure!(Self::is_item_owner(sender.clone(), collection_id, item_id) ||
Self::is_owner_or_admin_permissions(collection_id, sender.clone()),
Error::<T>::NoPermission);
-
- Self::item_exists(collection_id, item_id, &target_collection.mode)?;
match target_collection.mode
{
@@ -1320,7 +1322,7 @@
Self::check_owner_or_admin_permissions(collection_id, sender.clone())?;
// check schema limit
- ensure!(schema.len() as u32 > ChainLimit::get().const_on_chain_schema_limit, "");
+ ensure!(schema.len() as u32 <= ChainLimit::get().const_on_chain_schema_limit, "");
let mut target_collection = <Collection<T>>::get(collection_id);
target_collection.const_on_chain_schema = schema;
@@ -1351,7 +1353,7 @@
Self::check_owner_or_admin_permissions(collection_id, sender.clone())?;
// check schema limit
- ensure!(schema.len() as u32 > ChainLimit::get().variable_on_chain_schema_limit, "");
+ ensure!(schema.len() as u32 <= ChainLimit::get().variable_on_chain_schema_limit, "");
let mut target_collection = <Collection<T>>::get(collection_id);
target_collection.variable_on_chain_schema = schema;
@@ -1677,13 +1679,13 @@
let value = item.owner.first().unwrap().fraction;
let owner = item.owner.first().unwrap().owner.clone();
- Self::add_token_index(collection_id, current_index, owner.clone())?;
+ Self::add_token_index(collection_id, current_index, &owner)?;
<ItemListIndex>::insert(collection_id, current_index);
<ReFungibleItemList<T>>::insert(collection_id, current_index, itemcopy);
// Update balance
- let new_balance = <Balance<T>>::get(collection_id, owner.clone())
+ let new_balance = <Balance<T>>::get(collection_id, &owner)
.checked_add(value)
.ok_or(Error::<T>::NumOverflow)?;
<Balance<T>>::insert(collection_id, owner.clone(), new_balance);
@@ -1697,7 +1699,7 @@
.ok_or(Error::<T>::NumOverflow)?;
let item_owner = item.owner.clone();
- Self::add_token_index(collection_id, current_index, item.owner.clone())?;
+ Self::add_token_index(collection_id, current_index, &item.owner)?;
<ItemListIndex>::insert(collection_id, current_index);
<NftItemList<T>>::insert(collection_id, current_index, item);
@@ -1714,29 +1716,44 @@
fn burn_refungible_item(
collection_id: CollectionId,
item_id: TokenId,
- owner: T::AccountId,
+ owner: &T::AccountId,
) -> DispatchResult {
ensure!(
<ReFungibleItemList<T>>::contains_key(collection_id, item_id),
Error::<T>::TokenNotFound
);
- let collection = <ReFungibleItemList<T>>::get(collection_id, item_id);
- let item = collection
+ let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id);
+ let rft_balance = token
.owner
.iter()
- .filter(|&i| i.owner == owner)
+ .filter(|&i| i.owner == *owner)
.next()
.unwrap();
- Self::remove_token_index(collection_id, item_id, owner.clone())?;
+ Self::remove_token_index(collection_id, item_id, owner)?;
// update balance
- let new_balance = <Balance<T>>::get(collection_id, item.owner.clone())
- .checked_sub(item.fraction)
+ let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.clone())
+ .checked_sub(rft_balance.fraction)
.ok_or(Error::<T>::NumOverflow)?;
- <Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);
+ <Balance<T>>::insert(collection_id, rft_balance.owner.clone(), new_balance);
- <ReFungibleItemList<T>>::remove(collection_id, item_id);
+ // Re-create owners list with sender removed
+ let index = token
+ .owner
+ .iter()
+ .position(|i| i.owner == *owner)
+ .unwrap();
+ token.owner.remove(index);
+ let owner_count = token.owner.len();
+ // Burn the token completely if this was the last (only) owner
+ if owner_count == 0 {
+ <ReFungibleItemList<T>>::remove(collection_id, item_id);
+ }
+ else {
+ <ReFungibleItemList<T>>::insert(collection_id, item_id, token);
+ }
+
Ok(())
}
@@ -1746,10 +1763,10 @@
Error::<T>::TokenNotFound
);
let item = <NftItemList<T>>::get(collection_id, item_id);
- Self::remove_token_index(collection_id, item_id, item.owner.clone())?;
+ Self::remove_token_index(collection_id, item_id, &item.owner)?;
// update balance
- let new_balance = <Balance<T>>::get(collection_id, item.owner.clone())
+ let new_balance = <Balance<T>>::get(collection_id, &item.owner)
.checked_sub(1)
.ok_or(Error::<T>::NumOverflow)?;
<Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);
@@ -1858,16 +1875,33 @@
Ok(())
}
+ /// Check if token exists. In case of Fungible, check if there is an entry for
+ /// the owner in fungible balances double map
+ fn token_exists(
+ collection_id: CollectionId,
+ item_id: TokenId,
+ owner: &T::AccountId
+ ) -> DispatchResult {
+ let target_collection = <Collection<T>>::get(collection_id);
+ let exists = match target_collection.mode
+ {
+ CollectionMode::NFT => <NftItemList<T>>::contains_key(collection_id, item_id),
+ CollectionMode::Fungible(_) => <FungibleItemList<T>>::contains_key(collection_id, owner),
+ CollectionMode::ReFungible(_) => <ReFungibleItemList<T>>::contains_key(collection_id, item_id),
+ _ => false
+ };
+
+ ensure!(exists == true, Error::<T>::TokenNotFound);
+ Ok(())
+ }
+
fn transfer_fungible(
collection_id: CollectionId,
value: u128,
owner: &T::AccountId,
recipient: &T::AccountId,
) -> DispatchResult {
- ensure!(
- <FungibleItemList<T>>::contains_key(collection_id, owner),
- Error::<T>::TokenNotFound
- );
+ Self::token_exists(collection_id, 0, owner)?;
let mut balance = <FungibleItemList<T>>::get(collection_id, owner);
ensure!(balance.value >= value, Error::<T>::TokenValueTooLow);
@@ -1897,10 +1931,7 @@
owner: T::AccountId,
new_owner: T::AccountId,
) -> DispatchResult {
- ensure!(
- <ReFungibleItemList<T>>::contains_key(collection_id, item_id),
- Error::<T>::TokenNotFound
- );
+ Self::token_exists(collection_id, item_id, &owner)?;
let full_item = <ReFungibleItemList<T>>::get(collection_id, item_id);
let item = full_item
@@ -1941,7 +1972,7 @@
<ReFungibleItemList<T>>::insert(collection_id, item_id, new_full_item);
// update index collection
- Self::move_token_index(collection_id, item_id, old_owner.clone(), new_owner.clone())?;
+ Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;
} else {
let mut new_full_item = full_item.clone();
new_full_item
@@ -1966,7 +1997,7 @@
owner: new_owner.clone(),
fraction: value,
});
- Self::add_token_index(collection_id, item_id, new_owner.clone())?;
+ Self::add_token_index(collection_id, item_id, &new_owner)?;
}
<ReFungibleItemList<T>>::insert(collection_id, item_id, new_full_item);
@@ -1981,10 +2012,7 @@
sender: T::AccountId,
new_owner: T::AccountId,
) -> DispatchResult {
- ensure!(
- <NftItemList<T>>::contains_key(collection_id, item_id),
- Error::<T>::TokenNotFound
- );
+ Self::token_exists(collection_id, item_id, &sender)?;
let mut item = <NftItemList<T>>::get(collection_id, item_id);
@@ -2010,25 +2038,11 @@
<NftItemList<T>>::insert(collection_id, item_id, item);
// update index collection
- Self::move_token_index(collection_id, item_id, old_owner.clone(), new_owner.clone())?;
+ Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;
Ok(())
}
- fn item_exists(
- collection_id: CollectionId,
- item_id: TokenId,
- mode: &CollectionMode
- ) -> DispatchResult {
- match mode {
- CollectionMode::NFT => ensure!(<NftItemList<T>>::contains_key(collection_id, item_id), Error::<T>::TokenNotFound),
- CollectionMode::ReFungible(_) => ensure!(<ReFungibleItemList<T>>::contains_key(collection_id, item_id), Error::<T>::TokenNotFound),
- _ => ()
- };
-
- Ok(())
- }
-
fn set_re_fungible_variable_data(
collection_id: CollectionId,
item_id: TokenId,
@@ -2090,12 +2104,12 @@
.unwrap();
let item_owner = item.owner.clone();
- Self::add_token_index(collection_id, current_index, item.owner.clone()).unwrap();
+ Self::add_token_index(collection_id, current_index, &item.owner).unwrap();
<ItemListIndex>::insert(collection_id, current_index);
// Update balance
- let new_balance = <Balance<T>>::get(collection_id, item_owner.clone())
+ let new_balance = <Balance<T>>::get(collection_id, &item_owner)
.checked_add(1)
.unwrap();
<Balance<T>>::insert(collection_id, item_owner.clone(), new_balance);
@@ -2106,7 +2120,7 @@
.checked_add(1)
.unwrap();
- Self::add_token_index(collection_id, current_index, (*owner).clone()).unwrap();
+ Self::add_token_index(collection_id, current_index, owner).unwrap();
<ItemListIndex>::insert(collection_id, current_index);
@@ -2125,24 +2139,24 @@
let value = item.owner.first().unwrap().fraction;
let owner = item.owner.first().unwrap().owner.clone();
- Self::add_token_index(collection_id, current_index, owner.clone()).unwrap();
+ Self::add_token_index(collection_id, current_index, &owner).unwrap();
<ItemListIndex>::insert(collection_id, current_index);
// Update balance
- let new_balance = <Balance<T>>::get(collection_id, owner.clone())
+ let new_balance = <Balance<T>>::get(collection_id, &owner)
.checked_add(value)
.unwrap();
<Balance<T>>::insert(collection_id, owner.clone(), new_balance);
}
- fn add_token_index(collection_id: CollectionId, item_index: TokenId, owner: T::AccountId) -> DispatchResult {
+ fn add_token_index(collection_id: CollectionId, item_index: TokenId, owner: &T::AccountId) -> DispatchResult {
// add to account limit
- if <AccountItemCount<T>>::contains_key(owner.clone()) {
+ if <AccountItemCount<T>>::contains_key(owner) {
// bound Owned tokens by a single address
- let count = <AccountItemCount<T>>::get(owner.clone());
+ let count = <AccountItemCount<T>>::get(owner);
ensure!(count < ChainLimit::get().account_token_ownership_limit, Error::<T>::AddressOwnershipLimitExceeded);
<AccountItemCount<T>>::insert(owner.clone(), count
@@ -2153,9 +2167,9 @@
<AccountItemCount<T>>::insert(owner.clone(), 1);
}
- let list_exists = <AddressTokens<T>>::contains_key(collection_id, owner.clone());
+ let list_exists = <AddressTokens<T>>::contains_key(collection_id, owner);
if list_exists {
- let mut list = <AddressTokens<T>>::get(collection_id, owner.clone());
+ let mut list = <AddressTokens<T>>::get(collection_id, owner);
let item_contains = list.contains(&item_index.clone());
if !item_contains {
@@ -2166,8 +2180,7 @@
} else {
let mut itm = Vec::new();
itm.push(item_index.clone());
- <AddressTokens<T>>::insert(collection_id, owner, itm);
-
+ <AddressTokens<T>>::insert(collection_id, owner.clone(), itm);
}
Ok(())
@@ -2176,24 +2189,24 @@
fn remove_token_index(
collection_id: CollectionId,
item_index: TokenId,
- owner: T::AccountId,
+ owner: &T::AccountId,
) -> DispatchResult {
// update counter
<AccountItemCount<T>>::insert(owner.clone(),
- <AccountItemCount<T>>::get(owner.clone())
+ <AccountItemCount<T>>::get(owner)
.checked_sub(1)
.ok_or(Error::<T>::NumOverflow)?);
- let list_exists = <AddressTokens<T>>::contains_key(collection_id, owner.clone());
+ let list_exists = <AddressTokens<T>>::contains_key(collection_id, owner);
if list_exists {
- let mut list = <AddressTokens<T>>::get(collection_id, owner.clone());
+ let mut list = <AddressTokens<T>>::get(collection_id, owner);
let item_contains = list.contains(&item_index.clone());
if item_contains {
list.retain(|&item| item != item_index);
- <AddressTokens<T>>::insert(collection_id, owner, list);
+ <AddressTokens<T>>::insert(collection_id, owner.clone(), list);
}
}
@@ -2203,8 +2216,8 @@
fn move_token_index(
collection_id: CollectionId,
item_index: TokenId,
- old_owner: T::AccountId,
- new_owner: T::AccountId,
+ old_owner: &T::AccountId,
+ new_owner: &T::AccountId,
) -> DispatchResult {
Self::remove_token_index(collection_id, item_index, old_owner)?;
Self::add_token_index(collection_id, item_index, new_owner)?;
@@ -2319,15 +2332,15 @@
let limit = <Collection<T>>::get(collection_id).limits.sponsor_transfer_timeout;
let mut sponsored = true;
- if <CreateItemBasket<T>>::contains_key(collection_id) {
- let last_tx_block = <CreateItemBasket<T>>::get(collection_id);
+ if <CreateItemBasket<T>>::contains_key((collection_id, &who)) {
+ let last_tx_block = <CreateItemBasket<T>>::get((collection_id, &who));
let limit_time = last_tx_block + limit.into();
if block_number <= limit_time {
sponsored = false;
}
}
if sponsored {
- <CreateItemBasket<T>>::insert(collection_id, block_number);
+ <CreateItemBasket<T>>::insert((collection_id, who.clone()), block_number);
}
// check free create limit
pallets/nft/src/tests.rsdiffbeforeafterboth1// Tests to be written here2use super::*;3use crate::mock::*;4use crate::{AccessMode, ApprovePermissions, CollectionMode,5 Ownership, ChainLimits, CreateItemData, CreateNftData, CreateFungibleData, CreateReFungibleData,6 CollectionId, TokenId, MAX_DECIMAL_POINTS};7use frame_support::{assert_noop, assert_ok};8use frame_system::{ RawOrigin };910fn default_collection_numbers_limit() -> u32 {11 1012}1314fn default_limits() {15 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 16 collection_numbers_limit: default_collection_numbers_limit(),17 account_token_ownership_limit: 10,18 collections_admins_limit: 5,19 custom_data_limit: 2048,20 nft_sponsor_transfer_timeout: 15,21 fungible_sponsor_transfer_timeout: 15,22 refungible_sponsor_transfer_timeout: 15, 23 }));24}2526fn default_nft_data() -> CreateNftData {27 CreateNftData { const_data: vec![1, 2, 3], variable_data: vec![3, 2, 1] }28}2930fn default_fungible_data () -> CreateFungibleData {31 CreateFungibleData { }32}3334fn default_re_fungible_data () -> CreateReFungibleData {35 CreateReFungibleData { const_data: vec![1, 2, 3], variable_data: vec![3, 2, 1] }36}3738fn create_test_collection_for_owner(mode: &CollectionMode, owner: u64, id: CollectionId) -> CollectionId {39 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();40 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();41 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();4243 let origin1 = Origin::signed(owner);44 assert_ok!(TemplateModule::create_collection(45 origin1.clone(),46 col_name1.clone(),47 col_desc1.clone(),48 token_prefix1.clone(),49 mode.clone()50 ));5152 let saved_col_name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();53 let saved_description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();54 let saved_prefix: Vec<u8> = b"token_prefix1\0".to_vec();55 assert_eq!(TemplateModule::collection(id).owner, owner);56 assert_eq!(TemplateModule::collection(id).name, saved_col_name);57 assert_eq!(TemplateModule::collection(id).mode, *mode);58 assert_eq!(TemplateModule::collection(id).description, saved_description);59 assert_eq!(TemplateModule::collection(id).token_prefix, saved_prefix);60 id61}6263fn create_test_collection(mode: &CollectionMode, id: CollectionId) -> CollectionId {64 create_test_collection_for_owner(&mode, 1, id)65}6667fn create_test_item(collection_id: CollectionId, data: &CreateItemData) {68 let origin1 = Origin::signed(1);69 assert_ok!(TemplateModule::create_item(70 origin1.clone(),71 collection_id,72 1,73 data.clone()74 ));7576}7778// Use cases tests region79// #region8081#[test]82fn set_version_schema() {83 new_test_ext().execute_with(|| {84 default_limits();85 let origin1 = Origin::signed(1);86 let collection_id = create_test_collection(&CollectionMode::NFT, 1);87 88 assert_ok!(TemplateModule::set_schema_version(origin1, collection_id, SchemaVersion::Unique));89 assert_eq!(TemplateModule::collection(collection_id).schema_version, SchemaVersion::Unique);90 });91}9293#[test]94fn create_fungible_collection_fails_with_large_decimal_numbers() {95 new_test_ext().execute_with(|| {96 default_limits();9798 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();99 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();100 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();101102 let origin1 = Origin::signed(1);103 assert_noop!(TemplateModule::create_collection(104 origin1,105 col_name1,106 col_desc1,107 token_prefix1,108 CollectionMode::Fungible(MAX_DECIMAL_POINTS + 1)109 ), Error::<Test>::CollectionDecimalPointLimitExceeded);110 }); 111}112113#[test]114fn create_re_fungible_collection_fails_with_large_decimal_numbers() {115 new_test_ext().execute_with(|| {116 default_limits();117118 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();119 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();120 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();121122 let origin1 = Origin::signed(1);123 assert_noop!(TemplateModule::create_collection(124 origin1,125 col_name1,126 col_desc1,127 token_prefix1,128 CollectionMode::ReFungible(MAX_DECIMAL_POINTS + 1)129 ), Error::<Test>::CollectionDecimalPointLimitExceeded);130 });131}132133#[test]134fn create_nft_item() {135 new_test_ext().execute_with(|| {136 default_limits();137 let collection_id = create_test_collection(&CollectionMode::NFT, 1);138 139 let data = default_nft_data();140 create_test_item(collection_id, &data.clone().into());141 assert_eq!(TemplateModule::nft_item_id(collection_id, 1).const_data, data.const_data);142 assert_eq!(TemplateModule::nft_item_id(collection_id, 1).variable_data, data.variable_data);143 });144}145146// Use cases tests region147// #region148#[test]149fn create_nft_multiple_items() {150 new_test_ext().execute_with(|| {151 default_limits();152 153 create_test_collection(&CollectionMode::NFT, 1);154155 let origin1 = Origin::signed(1);156157 let items_data = vec![default_nft_data(), default_nft_data(), default_nft_data()];158159 assert_ok!(TemplateModule::create_multiple_items(160 origin1.clone(),161 1,162 1,163 items_data.clone().into_iter().map(|d| { d.into() }).collect()164 ));165 for (index, data) in items_data.iter().enumerate() {166 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).const_data.to_vec(), data.const_data);167 assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).variable_data.to_vec(), data.variable_data);168 }169 });170}171172#[test]173fn create_refungible_item() {174 new_test_ext().execute_with(|| {175 default_limits();176 let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1);177178 let data = default_re_fungible_data();179 create_test_item(collection_id, &data.clone().into());180 assert_eq!(181 TemplateModule::refungible_item_id(collection_id, 1).const_data,182 data.const_data183 );184 assert_eq!(185 TemplateModule::refungible_item_id(collection_id, 1).variable_data,186 data.variable_data187 );188 assert_eq!(189 TemplateModule::refungible_item_id(collection_id, 1).owner[0],190 Ownership {191 owner: 1,192 fraction: 1000193 }194 );195 });196}197198#[test]199fn create_multiple_refungible_items() {200 new_test_ext().execute_with(|| {201 default_limits();202 203 create_test_collection(&CollectionMode::ReFungible(3), 1);204205 let origin1 = Origin::signed(1);206207 let items_data = vec![default_re_fungible_data(), default_re_fungible_data(), default_re_fungible_data()];208209 assert_ok!(TemplateModule::create_multiple_items(210 origin1.clone(),211 1,212 1,213 items_data.clone().into_iter().map(|d| { d.into() }).collect()214 ));215 for (index, data) in items_data.iter().enumerate() {216217 let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId);218 assert_eq!(item.const_data.to_vec(), data.const_data);219 assert_eq!(item.variable_data.to_vec(), data.variable_data);220 assert_eq!(221 item.owner[0],222 Ownership {223 owner: 1,224 fraction: 1000225 }226 );227 }228 });229}230231#[test]232fn create_fungible_item() {233 new_test_ext().execute_with(|| {234 default_limits();235 236 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);237238 let data = default_fungible_data();239 create_test_item(collection_id, &data.into());240241 assert_eq!(TemplateModule::fungible_item_id(collection_id, 1).owner, 1);242 });243}244245#[test]246fn create_multiple_fungible_items() {247 new_test_ext().execute_with(|| {248 default_limits();249250 create_test_collection(&CollectionMode::Fungible(3), 1);251252 let origin1 = Origin::signed(1);253254 let items_data = vec![default_fungible_data(), default_fungible_data(), default_fungible_data()];255256 assert_ok!(TemplateModule::create_multiple_items(257 origin1.clone(),258 1,259 1,260 items_data.clone().into_iter().map(|d| { d.into() }).collect()261 ));262 263 for (index, _) in items_data.iter().enumerate() {264 assert_eq!(TemplateModule::fungible_item_id(1, (index + 1) as TokenId).owner, 1);265 }266 assert_eq!(TemplateModule::balance_count(1, 1), 3000);267 assert_eq!(TemplateModule::address_tokens(1, 1), [1, 2, 3]);268 });269}270271#[test]272fn transfer_fungible_item() {273 new_test_ext().execute_with(|| {274 default_limits();275 276 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);277278 let origin1 = Origin::signed(1);279 let origin2 = Origin::signed(2);280281 let data = default_fungible_data();282 create_test_item(collection_id, &data.into());283284 assert_eq!(TemplateModule::fungible_item_id(1, 1).owner, 1);285 assert_eq!(TemplateModule::balance_count(1, 1), 1000);286 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);287288 // change owner scenario289 assert_ok!(TemplateModule::transfer(origin1.clone(), 2, 1, 1, 1000));290 assert_eq!(TemplateModule::fungible_item_id(1, 1).owner, 2);291 assert_eq!(TemplateModule::fungible_item_id(1, 1).value, 1000);292 assert_eq!(TemplateModule::balance_count(1, 1), 0);293 assert_eq!(TemplateModule::balance_count(1, 2), 1000);294 // assert_eq!(TemplateModule::address_tokens(1, 1), []);295 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);296297 // split item scenario298 assert_ok!(TemplateModule::transfer(origin2.clone(), 3, 1, 1, 500));299 assert_eq!(TemplateModule::fungible_item_id(1, 1).owner, 2);300 assert_eq!(TemplateModule::fungible_item_id(1, 2).owner, 3);301 assert_eq!(TemplateModule::balance_count(1, 2), 500);302 assert_eq!(TemplateModule::balance_count(1, 3), 500);303 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);304 assert_eq!(TemplateModule::address_tokens(1, 3), [2]);305306 // split item and new owner has account scenario307 assert_ok!(TemplateModule::transfer(origin2.clone(), 3, 1, 1, 200));308 assert_eq!(TemplateModule::fungible_item_id(1, 1).value, 300);309 assert_eq!(TemplateModule::fungible_item_id(1, 2).value, 700);310 assert_eq!(TemplateModule::balance_count(1, 2), 300);311 assert_eq!(TemplateModule::balance_count(1, 3), 700);312 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);313 assert_eq!(TemplateModule::address_tokens(1, 3), [2]);314 });315}316317#[test]318fn transfer_refungible_item() {319 new_test_ext().execute_with(|| {320 default_limits();321 322 let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1);323324 let data = default_re_fungible_data();325 create_test_item(collection_id, &data.clone().into());326327 let origin1 = Origin::signed(1);328 let origin2 = Origin::signed(2);329 assert_eq!(330 TemplateModule::refungible_item_id(collection_id, 1).const_data,331 data.const_data332 );333 assert_eq!(334 TemplateModule::refungible_item_id(collection_id, 1).variable_data,335 data.variable_data336 );337 assert_eq!(338 TemplateModule::refungible_item_id(collection_id, 1).owner[0],339 Ownership {340 owner: 1,341 fraction: 1000342 }343 );344 assert_eq!(TemplateModule::balance_count(1, 1), 1000);345 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);346347 // change owner scenario348 assert_ok!(TemplateModule::transfer(origin1.clone(), 2, 1, 1, 1000));349 assert_eq!(350 TemplateModule::refungible_item_id(1, 1).owner[0],351 Ownership {352 owner: 2,353 fraction: 1000354 }355 );356 assert_eq!(TemplateModule::balance_count(1, 1), 0);357 assert_eq!(TemplateModule::balance_count(1, 2), 1000);358 // assert_eq!(TemplateModule::address_tokens(1, 1), []);359 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);360361 // split item scenario362 assert_ok!(TemplateModule::transfer(origin2.clone(), 3, 1, 1, 500));363 assert_eq!(364 TemplateModule::refungible_item_id(1, 1).owner[0],365 Ownership {366 owner: 2,367 fraction: 500368 }369 );370 assert_eq!(371 TemplateModule::refungible_item_id(1, 1).owner[1],372 Ownership {373 owner: 3,374 fraction: 500375 }376 );377 assert_eq!(TemplateModule::balance_count(1, 2), 500);378 assert_eq!(TemplateModule::balance_count(1, 3), 500);379 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);380 assert_eq!(TemplateModule::address_tokens(1, 3), [1]);381382 // split item and new owner has account scenario383 assert_ok!(TemplateModule::transfer(origin2.clone(), 3, 1, 1, 200));384 assert_eq!(385 TemplateModule::refungible_item_id(1, 1).owner[0],386 Ownership {387 owner: 2,388 fraction: 300389 }390 );391 assert_eq!(392 TemplateModule::refungible_item_id(1, 1).owner[1],393 Ownership {394 owner: 3,395 fraction: 700396 }397 );398 assert_eq!(TemplateModule::balance_count(1, 2), 300);399 assert_eq!(TemplateModule::balance_count(1, 3), 700);400 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);401 assert_eq!(TemplateModule::address_tokens(1, 3), [1]);402 });403}404405#[test]406fn transfer_nft_item() {407 new_test_ext().execute_with(|| {408 default_limits();409 410 let collection_id = create_test_collection(&CollectionMode::NFT, 1);411412 let data = default_nft_data();413 create_test_item(collection_id, &data.into());414 assert_eq!(TemplateModule::balance_count(1, 1), 1);415 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);416417 let origin1 = Origin::signed(1);418 // default scenario419 assert_ok!(TemplateModule::transfer(origin1.clone(), 2, 1, 1, 1000));420 assert_eq!(TemplateModule::nft_item_id(1, 1).owner, 2);421 assert_eq!(TemplateModule::balance_count(1, 1), 0);422 assert_eq!(TemplateModule::balance_count(1, 2), 1);423 // assert_eq!(TemplateModule::address_tokens(1, 1), []);424 assert_eq!(TemplateModule::address_tokens(1, 2), [1]);425 });426}427428#[test]429fn nft_approve_and_transfer_from() {430 new_test_ext().execute_with(|| {431 default_limits();432 433 let collection_id = create_test_collection(&CollectionMode::NFT, 1);434435 let data = default_nft_data();436 create_test_item(collection_id, &data.into());437438 let origin1 = Origin::signed(1);439 let origin2 = Origin::signed(2);440441 assert_eq!(TemplateModule::balance_count(1, 1), 1);442 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);443444 // neg transfer445 assert_noop!(TemplateModule::transfer_from(446 origin2.clone(),447 1,448 2,449 1,450 1,451 1), Error::<Test>::NoPermission);452453 // do approve454 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));455 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);456 assert_eq!(457 TemplateModule::approved(1, (1, 1))[0],458 ApprovePermissions {459 approved: 2,460 amount: 100000000461 }462 );463464 assert_ok!(TemplateModule::transfer_from(465 origin2.clone(),466 1,467 2,468 1,469 1,470 1471 ));472 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 0);473 });474}475476#[test]477fn nft_approve_and_transfer_from_white_list() {478 new_test_ext().execute_with(|| {479 default_limits();480 481 let collection_id = create_test_collection(&CollectionMode::NFT, 1);482483 let origin1 = Origin::signed(1);484 let origin2 = Origin::signed(2);485486 let data = default_nft_data();487 create_test_item(collection_id, &data.clone().into());488489 assert_eq!(TemplateModule::nft_item_id(1, 1).const_data, data.const_data);490 assert_eq!(TemplateModule::balance_count(1, 1), 1);491 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);492493 assert_ok!(TemplateModule::set_mint_permission(494 origin1.clone(),495 1,496 true497 ));498 assert_ok!(TemplateModule::set_public_access_mode(499 origin1.clone(),500 1,501 AccessMode::WhiteList502 ));503 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));504 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 2));505 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 3));506507 // do approve508 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));509 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);510 assert_ok!(TemplateModule::approve(origin1.clone(), 3, 1, 1));511 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 2);512 assert_eq!(513 TemplateModule::approved(1, (1, 1))[0],514 ApprovePermissions {515 approved: 2,516 amount: 100000000517 }518 );519520 assert_ok!(TemplateModule::transfer_from(521 origin2.clone(),522 1,523 3,524 1,525 1,526 1527 ));528 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 0);529 });530}531532#[test]533fn refungible_approve_and_transfer_from() {534 new_test_ext().execute_with(|| {535 default_limits();536 537 let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1);538 539 let origin1 = Origin::signed(1);540 let origin2 = Origin::signed(2);541542 let data = default_re_fungible_data();543 create_test_item(collection_id, &data.into());544545 assert_eq!(TemplateModule::balance_count(1, 1), 1000);546 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);547548 assert_ok!(TemplateModule::set_mint_permission(549 origin1.clone(),550 1,551 true552 ));553 assert_ok!(TemplateModule::set_public_access_mode(554 origin1.clone(),555 1,556 AccessMode::WhiteList557 ));558 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));559 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 2));560 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 3));561562 // do approve563 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));564 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);565 assert_ok!(TemplateModule::approve(origin1.clone(), 3, 1, 1));566 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 2);567 assert_eq!(568 TemplateModule::approved(1, (1, 1))[0],569 ApprovePermissions {570 approved: 2,571 amount: 100000000572 }573 );574575 assert_ok!(TemplateModule::transfer_from(576 origin2.clone(),577 1,578 3,579 1,580 1,581 100582 ));583 assert_eq!(TemplateModule::balance_count(1, 1), 900);584 assert_eq!(TemplateModule::balance_count(1, 3), 100);585 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);586 assert_eq!(TemplateModule::address_tokens(1, 3), [1]);587588 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);589 assert_eq!(590 TemplateModule::approved(1, (1, 1))[0],591 ApprovePermissions {592 approved: 3,593 amount: 100000000594 }595 );596 });597}598599#[test]600fn fungible_approve_and_transfer_from() {601 new_test_ext().execute_with(|| {602 default_limits();603 604 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);605 606 let data = default_fungible_data();607 create_test_item(collection_id, &data.into());608609 let origin1 = Origin::signed(1);610 let origin2 = Origin::signed(2);611612 assert_eq!(TemplateModule::balance_count(1, 1), 1000);613 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);614615 assert_ok!(TemplateModule::set_mint_permission(616 origin1.clone(),617 1,618 true619 ));620 assert_ok!(TemplateModule::set_public_access_mode(621 origin1.clone(),622 1,623 AccessMode::WhiteList624 ));625 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));626 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 2));627 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 3));628629 // do approve630 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));631 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);632 assert_ok!(TemplateModule::approve(origin1.clone(), 3, 1, 1));633 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 2);634 assert_eq!(635 TemplateModule::approved(1, (1, 1))[0],636 ApprovePermissions {637 approved: 2,638 amount: 100000000639 }640 );641642 assert_ok!(TemplateModule::transfer_from(643 origin2.clone(),644 1,645 3,646 1,647 1,648 100649 ));650 assert_eq!(TemplateModule::balance_count(1, 1), 900);651 assert_eq!(TemplateModule::balance_count(1, 3), 100);652 assert_eq!(TemplateModule::address_tokens(1, 1), [1]);653 assert_eq!(TemplateModule::address_tokens(1, 3), [2]);654655 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);656 assert_eq!(657 TemplateModule::approved(1, (1, 1))[0],658 ApprovePermissions {659 approved: 3,660 amount: 100000000661 }662 );663664 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));665 assert_ok!(TemplateModule::transfer_from(666 origin2.clone(),667 1,668 3,669 1,670 1,671 900672 ));673 assert_eq!(TemplateModule::balance_count(1, 1), 0);674 assert_eq!(TemplateModule::balance_count(1, 3), 1000);675 // assert_eq!(TemplateModule::address_tokens(1, 1), []);676 assert_eq!(TemplateModule::address_tokens(1, 3), [2]);677678 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 0);679 });680}681682#[test]683fn change_collection_owner() {684 new_test_ext().execute_with(|| {685 default_limits();686 687 let collection_id = create_test_collection(&CollectionMode::NFT, 1);688 689 let origin1 = Origin::signed(1);690 assert_ok!(TemplateModule::change_collection_owner(691 origin1.clone(),692 collection_id,693 2694 ));695 assert_eq!(TemplateModule::collection(collection_id).owner, 2);696 });697}698699#[test]700fn destroy_collection() {701 new_test_ext().execute_with(|| {702 default_limits();703 704 let collection_id = create_test_collection(&CollectionMode::NFT, 1);705 706 let origin1 = Origin::signed(1);707 assert_ok!(TemplateModule::destroy_collection(origin1.clone(), collection_id));708 });709}710711#[test]712fn burn_nft_item() {713 new_test_ext().execute_with(|| {714 default_limits();715 716 let collection_id = create_test_collection(&CollectionMode::NFT, 1);717718 let origin1 = Origin::signed(1);719 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));720 721 let data = default_nft_data();722 create_test_item(collection_id, &data.into());723724 // check balance (collection with id = 1, user id = 1)725 assert_eq!(TemplateModule::balance_count(1, 1), 1);726727 // burn item728 assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1));729 assert_noop!(730 TemplateModule::burn_item(origin1.clone(), 1, 1),731 Error::<Test>::TokenNotFound732 );733734 assert_eq!(TemplateModule::balance_count(1, 1), 0);735 });736}737738#[test]739fn burn_fungible_item() {740 new_test_ext().execute_with(|| {741 default_limits();742 743 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);744 745 let origin1 = Origin::signed(1);746 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));747 748 let data = default_fungible_data();749 create_test_item(collection_id, &data.into());750751 // check balance (collection with id = 1, user id = 1)752 assert_eq!(TemplateModule::balance_count(1, 1), 1000);753754 // burn item755 assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1));756 assert_noop!(757 TemplateModule::burn_item(origin1.clone(), 1, 1),758 Error::<Test>::TokenNotFound759 );760761 assert_eq!(TemplateModule::balance_count(1, 1), 0);762 });763}764765#[test]766fn burn_refungible_item() {767 new_test_ext().execute_with(|| {768 default_limits();769 770 let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1);771 let origin1 = Origin::signed(1);772773 assert_ok!(TemplateModule::set_mint_permission(774 origin1.clone(),775 collection_id,776 true777 ));778 assert_ok!(TemplateModule::set_public_access_mode(779 origin1.clone(),780 collection_id,781 AccessMode::WhiteList782 ));783 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));784785 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2));786 787 let data = default_re_fungible_data();788 create_test_item(collection_id, &data.into());789790 // check balance (collection with id = 1, user id = 2)791 assert_eq!(TemplateModule::balance_count(1, 1), 1000);792793 // burn item794 assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1));795 assert_noop!(796 TemplateModule::burn_item(origin1.clone(), 1, 1),797 Error::<Test>::TokenNotFound798 );799800 assert_eq!(TemplateModule::balance_count(1, 1), 0);801 });802}803804#[test]805fn add_collection_admin() {806 new_test_ext().execute_with(|| {807 default_limits();808 809 let collection1_id = create_test_collection_for_owner(&CollectionMode::NFT, 1, 1);810 create_test_collection_for_owner(&CollectionMode::NFT, 2, 2);811 create_test_collection_for_owner(&CollectionMode::NFT, 3, 3);812 813 let origin1 = Origin::signed(1);814815 // collection admin816 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection1_id, 2));817 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection1_id, 3));818819 assert_eq!(TemplateModule::admin_list_collection(collection1_id).contains(&2), true);820 assert_eq!(TemplateModule::admin_list_collection(collection1_id).contains(&3), true);821 });822}823824#[test]825fn remove_collection_admin() {826 new_test_ext().execute_with(|| {827 default_limits();828 829 let collection1_id = create_test_collection_for_owner(&CollectionMode::NFT, 1, 1);830 create_test_collection_for_owner(&CollectionMode::NFT, 2, 2);831 create_test_collection_for_owner(&CollectionMode::NFT, 3, 3);832833 let origin1 = Origin::signed(1);834 let origin2 = Origin::signed(2);835836 // collection admin837 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection1_id, 2));838 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection1_id, 3));839840 assert_eq!(TemplateModule::admin_list_collection(1).contains(&2), true);841 assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), true);842843 // remove admin844 assert_ok!(TemplateModule::remove_collection_admin(845 origin2.clone(),846 1,847 3848 ));849 assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), false);850 });851}852853#[test]854fn balance_of() {855 new_test_ext().execute_with(|| {856 default_limits();857 858 let nft_collection_id = create_test_collection(&CollectionMode::NFT, 1);859 let fungible_collection_id = create_test_collection(&CollectionMode::Fungible(3), 2);860 let re_fungible_collection_id = create_test_collection(&CollectionMode::ReFungible(3), 3);861 862 // check balance before863 assert_eq!(TemplateModule::balance_count(nft_collection_id, 1), 0);864 assert_eq!(TemplateModule::balance_count(fungible_collection_id, 1), 0);865 assert_eq!(TemplateModule::balance_count(re_fungible_collection_id, 1), 0);866867 let nft_data = default_nft_data();868 create_test_item(nft_collection_id, &nft_data.into());869 870 let fungible_data = default_fungible_data();871 create_test_item(fungible_collection_id, &fungible_data.into());872 873 let re_fungible_data = default_re_fungible_data();874 create_test_item(re_fungible_collection_id, &re_fungible_data.into());875876 // check balance (collection with id = 1, user id = 1)877 assert_eq!(TemplateModule::balance_count(nft_collection_id, 1), 1);878 assert_eq!(TemplateModule::balance_count(fungible_collection_id, 1), 1000);879 assert_eq!(TemplateModule::balance_count(re_fungible_collection_id, 1), 1000);880 assert_eq!(TemplateModule::nft_item_id(nft_collection_id, 1).owner, 1);881 assert_eq!(TemplateModule::fungible_item_id(fungible_collection_id, 1).owner, 1);882 assert_eq!(TemplateModule::refungible_item_id(re_fungible_collection_id, 1).owner[0].owner, 1);883 });884}885886#[test]887fn approve() {888 new_test_ext().execute_with(|| {889 default_limits();890 891 let collection_id = create_test_collection(&CollectionMode::NFT, 1);892 893 let data = default_nft_data();894 create_test_item(collection_id, &data.into());895896 let origin1 = Origin::signed(1);897 898 // approve899 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));900 assert_eq!(TemplateModule::approved(1, (1, 1))[0].approved, 2);901 });902}903904#[test]905fn transfer_from() {906 new_test_ext().execute_with(|| {907 default_limits();908 909 let collection_id = create_test_collection(&CollectionMode::NFT, 1);910 let origin1 = Origin::signed(1);911 let origin2 = Origin::signed(2);912913 let data = default_nft_data();914 create_test_item(collection_id, &data.into());915916 // approve917 assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));918 assert_eq!(TemplateModule::approved(1, (1, 1))[0].approved, 2);919920 assert_ok!(TemplateModule::set_mint_permission(921 origin1.clone(),922 1,923 true924 ));925 assert_ok!(TemplateModule::set_public_access_mode(926 origin1.clone(),927 1,928 AccessMode::WhiteList929 ));930 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));931 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 2));932 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 3));933934 assert_ok!(TemplateModule::transfer_from(935 origin2.clone(),936 1,937 2,938 1,939 1,940 1941 ));942943 // after transfer944 assert_eq!(TemplateModule::balance_count(1, 1), 0);945 assert_eq!(TemplateModule::balance_count(1, 2), 1);946 });947}948949// #endregion950951// Coverage tests region952// #region953954#[test]955fn owner_can_add_address_to_white_list() {956 new_test_ext().execute_with(|| {957 default_limits();958 959 let collection_id = create_test_collection(&CollectionMode::NFT, 1);960961 let origin1 = Origin::signed(1);962 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));963 assert_eq!(TemplateModule::white_list(collection_id, 2), true);964 });965}966967#[test]968fn admin_can_add_address_to_white_list() {969 new_test_ext().execute_with(|| {970 default_limits();971 972 let collection_id = create_test_collection(&CollectionMode::NFT, 1);973 let origin1 = Origin::signed(1);974 let origin2 = Origin::signed(2);975976 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));977 assert_ok!(TemplateModule::add_to_white_list(origin2.clone(), collection_id, 3));978 assert_eq!(TemplateModule::white_list(collection_id, 3), true);979 });980}981982#[test]983fn nonprivileged_user_cannot_add_address_to_white_list() {984 new_test_ext().execute_with(|| {985 default_limits();986 987 let collection_id = create_test_collection(&CollectionMode::NFT, 1);988989 let origin2 = Origin::signed(2);990 assert_noop!(991 TemplateModule::add_to_white_list(origin2.clone(), collection_id, 3),992 Error::<Test>::NoPermission993 );994 });995}996997#[test]998fn nobody_can_add_address_to_white_list_of_nonexisting_collection() {999 new_test_ext().execute_with(|| {1000 default_limits();10011002 let origin1 = Origin::signed(1);10031004 assert_noop!(1005 TemplateModule::add_to_white_list(origin1.clone(), 1, 2),1006 Error::<Test>::CollectionNotFound1007 );1008 });1009}10101011#[test]1012fn nobody_can_add_address_to_white_list_of_deleted_collection() {1013 new_test_ext().execute_with(|| {1014 default_limits();1015 1016 let collection_id = create_test_collection(&CollectionMode::NFT, 1);10171018 let origin1 = Origin::signed(1);1019 assert_ok!(TemplateModule::destroy_collection(origin1.clone(), collection_id));1020 assert_noop!(1021 TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2),1022 Error::<Test>::CollectionNotFound1023 );1024 });1025}10261027// If address is already added to white list, nothing happens1028#[test]1029fn address_is_already_added_to_white_list() {1030 new_test_ext().execute_with(|| {1031 default_limits();1032 1033 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1034 let origin1 = Origin::signed(1);1035 1036 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1037 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1038 assert_eq!(TemplateModule::white_list(collection_id, 2), true);1039 });1040}10411042#[test]1043fn owner_can_remove_address_from_white_list() {1044 new_test_ext().execute_with(|| {1045 default_limits();1046 1047 let collection_id = create_test_collection(&CollectionMode::NFT, 1);10481049 let origin1 = Origin::signed(1);1050 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1051 assert_ok!(TemplateModule::remove_from_white_list(1052 origin1.clone(),1053 collection_id,1054 21055 ));1056 assert_eq!(TemplateModule::white_list(collection_id, 2), false);1057 });1058}10591060#[test]1061fn admin_can_remove_address_from_white_list() {1062 new_test_ext().execute_with(|| {1063 default_limits();1064 1065 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1066 let origin1 = Origin::signed(1);1067 let origin2 = Origin::signed(2);10681069 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));10701071 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 3));1072 assert_ok!(TemplateModule::remove_from_white_list(1073 origin2.clone(),1074 collection_id,1075 31076 ));1077 assert_eq!(TemplateModule::white_list(collection_id, 3), false);1078 });1079}10801081#[test]1082fn nonprivileged_user_cannot_remove_address_from_white_list() {1083 new_test_ext().execute_with(|| {1084 default_limits();1085 1086 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1087 let origin1 = Origin::signed(1);1088 let origin2 = Origin::signed(2);10891090 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1091 assert_noop!(1092 TemplateModule::remove_from_white_list(origin2.clone(), collection_id, 2),1093 Error::<Test>::NoPermission1094 );1095 assert_eq!(TemplateModule::white_list(collection_id, 2), true);1096 });1097}10981099#[test]1100fn nobody_can_remove_address_from_white_list_of_nonexisting_collection() {1101 new_test_ext().execute_with(|| {1102 default_limits();1103 let origin1 = Origin::signed(1);11041105 assert_noop!(1106 TemplateModule::remove_from_white_list(origin1.clone(), 1, 2),1107 Error::<Test>::CollectionNotFound1108 );1109 });1110}11111112#[test]1113fn nobody_can_remove_address_from_white_list_of_deleted_collection() {1114 new_test_ext().execute_with(|| {1115 default_limits();1116 1117 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1118 let origin1 = Origin::signed(1);1119 let origin2 = Origin::signed(2);11201121 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1122 assert_ok!(TemplateModule::destroy_collection(origin1.clone(), collection_id));1123 assert_noop!(1124 TemplateModule::remove_from_white_list(origin2.clone(), collection_id, 2),1125 Error::<Test>::CollectionNotFound1126 );1127 assert_eq!(TemplateModule::white_list(collection_id, 2), false);1128 });1129}11301131// If address is already removed from white list, nothing happens1132#[test]1133fn address_is_already_removed_from_white_list() {1134 new_test_ext().execute_with(|| {1135 default_limits();1136 1137 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1138 let origin1 = Origin::signed(1);11391140 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));1141 assert_ok!(TemplateModule::remove_from_white_list(1142 origin1.clone(),1143 collection_id,1144 21145 ));1146 assert_ok!(TemplateModule::remove_from_white_list(1147 origin1.clone(),1148 collection_id,1149 21150 ));1151 assert_eq!(TemplateModule::white_list(collection_id, 2), false);1152 });1153}11541155// If Public Access mode is set to WhiteList, tokens can’t be transferred from a non-whitelisted address with transfer or transferFrom (2 tests)1156#[test]1157fn white_list_test_1() {1158 new_test_ext().execute_with(|| {1159 default_limits();1160 1161 let collection_id = create_test_collection(&CollectionMode::NFT, 1);11621163 let origin1 = Origin::signed(1);1164 1165 let data = default_nft_data();1166 create_test_item(collection_id, &data.into());11671168 assert_ok!(TemplateModule::set_public_access_mode(1169 origin1.clone(),1170 collection_id,1171 AccessMode::WhiteList1172 ));1173 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));11741175 assert_noop!(1176 TemplateModule::transfer(origin1.clone(), 3, 1, 1, 1),1177 Error::<Test>::AddresNotInWhiteList1178 );1179 });1180}11811182#[test]1183fn white_list_test_2() {1184 new_test_ext().execute_with(|| {1185 default_limits();1186 1187 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1188 let origin1 = Origin::signed(1);1189 1190 let data = default_nft_data();1191 create_test_item(collection_id, &data.into());11921193 assert_ok!(TemplateModule::set_public_access_mode(1194 origin1.clone(),1195 collection_id,1196 AccessMode::WhiteList1197 ));1198 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));1199 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 2));12001201 // do approve1202 assert_ok!(TemplateModule::approve(origin1.clone(), 1, 1, 1));1203 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);12041205 assert_ok!(TemplateModule::remove_from_white_list(1206 origin1.clone(),1207 1,1208 11209 ));12101211 assert_noop!(1212 TemplateModule::transfer_from(origin1.clone(), 1, 3, 1, 1, 1),1213 Error::<Test>::AddresNotInWhiteList1214 );1215 });1216}12171218// If Public Access mode is set to WhiteList, tokens can’t be transferred to a non-whitelisted address with transfer or transferFrom (2 tests)1219#[test]1220fn white_list_test_3() {1221 new_test_ext().execute_with(|| {1222 default_limits();1223 1224 let collection_id = create_test_collection(&CollectionMode::NFT, 1);12251226 let origin1 = Origin::signed(1);1227 1228 let data = default_nft_data();1229 create_test_item(collection_id, &data.into());12301231 assert_ok!(TemplateModule::set_public_access_mode(1232 origin1.clone(),1233 collection_id,1234 AccessMode::WhiteList1235 ));1236 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), 1, 1));12371238 assert_noop!(1239 TemplateModule::transfer(origin1.clone(), 3, 1, 1, 1),1240 Error::<Test>::AddresNotInWhiteList1241 );1242 });1243}12441245#[test]1246fn white_list_test_4() {1247 new_test_ext().execute_with(|| {1248 default_limits();1249 1250 let collection_id = create_test_collection(&CollectionMode::NFT, 1);12511252 let origin1 = Origin::signed(1);12531254 let data = default_nft_data();1255 create_test_item(collection_id, &data.into());12561257 assert_ok!(TemplateModule::set_public_access_mode(1258 origin1.clone(),1259 collection_id,1260 AccessMode::WhiteList1261 ));1262 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 1));1263 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));12641265 // do approve1266 assert_ok!(TemplateModule::approve(origin1.clone(), 1, 1, 1));1267 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);12681269 assert_ok!(TemplateModule::remove_from_white_list(1270 origin1.clone(),1271 collection_id,1272 21273 ));12741275 assert_noop!(1276 TemplateModule::transfer_from(origin1.clone(), 1, 3, 1, 1, 1),1277 Error::<Test>::AddresNotInWhiteList1278 );1279 });1280}12811282// If Public Access mode is set to WhiteList, tokens can’t be destroyed by a non-whitelisted address (even if it owned them before enabling WhiteList mode)1283#[test]1284fn white_list_test_5() {1285 new_test_ext().execute_with(|| {1286 default_limits();1287 1288 let collection_id = create_test_collection(&CollectionMode::NFT, 1);12891290 let origin1 = Origin::signed(1);12911292 let data = default_nft_data();1293 create_test_item(collection_id, &data.into());12941295 assert_ok!(TemplateModule::set_public_access_mode(1296 origin1.clone(),1297 collection_id,1298 AccessMode::WhiteList1299 ));1300 assert_noop!(1301 TemplateModule::burn_item(origin1.clone(), 1, 1),1302 Error::<Test>::AddresNotInWhiteList1303 );1304 });1305}13061307// If Public Access mode is set to WhiteList, oken transfers can’t be Approved by a non-whitelisted address (see Approve method).1308#[test]1309fn white_list_test_6() {1310 new_test_ext().execute_with(|| {1311 default_limits();1312 1313 let collection_id = create_test_collection(&CollectionMode::NFT, 1);13141315 let origin1 = Origin::signed(1);1316 assert_ok!(TemplateModule::set_public_access_mode(1317 origin1.clone(),1318 collection_id,1319 AccessMode::WhiteList1320 ));13211322 // do approve1323 assert_noop!(1324 TemplateModule::approve(origin1.clone(), 1, 1, 1),1325 Error::<Test>::AddresNotInWhiteList1326 );1327 });1328}13291330// If Public Access mode is set to WhiteList, tokens can be transferred from a whitelisted address with transfer or transferFrom (2 tests) and1331// tokens can be transferred from a whitelisted address with transfer or transferFrom (2 tests)1332#[test]1333fn white_list_test_7() {1334 new_test_ext().execute_with(|| {1335 default_limits();1336 1337 let collection_id = create_test_collection(&CollectionMode::NFT, 1);13381339 let data = default_nft_data();1340 create_test_item(collection_id, &data.into());1341 1342 let origin1 = Origin::signed(1);13431344 assert_ok!(TemplateModule::set_public_access_mode(1345 origin1.clone(),1346 collection_id,1347 AccessMode::WhiteList1348 ));1349 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 1));1350 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));13511352 assert_ok!(TemplateModule::transfer(origin1.clone(), 2, 1, 1, 1));1353 });1354}13551356#[test]1357fn white_list_test_8() {1358 new_test_ext().execute_with(|| {1359 default_limits();1360 1361 let collection_id = create_test_collection(&CollectionMode::NFT, 1);13621363 let data = default_nft_data();1364 create_test_item(collection_id, &data.into());1365 1366 let origin1 = Origin::signed(1);13671368 assert_ok!(TemplateModule::set_public_access_mode(1369 origin1.clone(),1370 collection_id,1371 AccessMode::WhiteList1372 ));1373 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 1));1374 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));13751376 // do approve1377 assert_ok!(TemplateModule::approve(origin1.clone(), 1, 1, 1));1378 assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1);13791380 assert_ok!(TemplateModule::transfer_from(1381 origin1.clone(),1382 1,1383 2,1384 1,1385 1,1386 11387 ));1388 });1389}13901391// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by owner.1392#[test]1393fn white_list_test_9() {1394 new_test_ext().execute_with(|| {1395 default_limits();1396 1397 let collection_id = create_test_collection(&CollectionMode::NFT, 1);1398 let origin1 = Origin::signed(1);13991400 assert_ok!(TemplateModule::set_public_access_mode(1401 origin1.clone(),1402 collection_id,1403 AccessMode::WhiteList1404 ));1405 assert_ok!(TemplateModule::set_mint_permission(1406 origin1.clone(),1407 collection_id,1408 false1409 ));14101411 let data = default_nft_data();1412 create_test_item(collection_id, &data.into());1413 });1414}14151416// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens can be created by admin.1417#[test]1418fn white_list_test_10() {1419 new_test_ext().execute_with(|| {1420 default_limits();1421 1422 let collection_id = create_test_collection(&CollectionMode::NFT, 1);14231424 let origin1 = Origin::signed(1);1425 let origin2 = Origin::signed(2);14261427 assert_ok!(TemplateModule::set_public_access_mode(1428 origin1.clone(),1429 collection_id,1430 AccessMode::WhiteList1431 ));1432 assert_ok!(TemplateModule::set_mint_permission(1433 origin1.clone(),1434 collection_id,1435 false1436 ));14371438 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));14391440 assert_ok!(TemplateModule::create_item(1441 origin2.clone(),1442 collection_id,1443 2,1444 default_nft_data().into()1445 ));1446 });1447}14481449// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and white listed address.1450#[test]1451fn white_list_test_11() {1452 new_test_ext().execute_with(|| {1453 default_limits();1454 1455 let collection_id = create_test_collection(&CollectionMode::NFT, 1);14561457 let origin1 = Origin::signed(1);1458 let origin2 = Origin::signed(2);14591460 assert_ok!(TemplateModule::set_public_access_mode(1461 origin1.clone(),1462 collection_id,1463 AccessMode::WhiteList1464 ));1465 assert_ok!(TemplateModule::set_mint_permission(1466 origin1.clone(),1467 collection_id,1468 false1469 ));1470 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));14711472 assert_noop!(1473 TemplateModule::create_item(origin2.clone(), 1, 2, default_nft_data().into()),1474 Error::<Test>::PublicMintingNotAllowed1475 );1476 });1477}14781479// If Public Access mode is set to WhiteList, and Mint Permission is set to false, tokens cannot be created by non-privileged and non-white listed address.1480#[test]1481fn white_list_test_12() {1482 new_test_ext().execute_with(|| {1483 default_limits();1484 1485 let collection_id = create_test_collection(&CollectionMode::NFT, 1);14861487 let origin1 = Origin::signed(1);1488 let origin2 = Origin::signed(2);14891490 assert_ok!(TemplateModule::set_public_access_mode(1491 origin1.clone(),1492 collection_id,1493 AccessMode::WhiteList1494 ));1495 assert_ok!(TemplateModule::set_mint_permission(1496 origin1.clone(),1497 collection_id,1498 false1499 ));15001501 assert_noop!(1502 TemplateModule::create_item(origin2.clone(), 1, 2, default_nft_data().into()),1503 Error::<Test>::PublicMintingNotAllowed1504 );1505 });1506}15071508// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by owner.1509#[test]1510fn white_list_test_13() {1511 new_test_ext().execute_with(|| {1512 default_limits();1513 1514 let collection_id = create_test_collection(&CollectionMode::NFT, 1);15151516 let origin1 = Origin::signed(1);15171518 assert_ok!(TemplateModule::set_public_access_mode(1519 origin1.clone(),1520 collection_id,1521 AccessMode::WhiteList1522 ));1523 assert_ok!(TemplateModule::set_mint_permission(1524 origin1.clone(),1525 collection_id,1526 true1527 ));15281529 let data = default_nft_data();1530 create_test_item(collection_id, &data.into());1531 });1532}15331534// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by admin.1535#[test]1536fn white_list_test_14() {1537 new_test_ext().execute_with(|| {1538 default_limits();1539 1540 let collection_id = create_test_collection(&CollectionMode::NFT, 1);15411542 let origin1 = Origin::signed(1);1543 let origin2 = Origin::signed(2);15441545 assert_ok!(TemplateModule::set_public_access_mode(1546 origin1.clone(),1547 collection_id,1548 AccessMode::WhiteList1549 ));1550 assert_ok!(TemplateModule::set_mint_permission(1551 origin1.clone(),1552 collection_id,1553 true1554 ));15551556 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));15571558 assert_ok!(TemplateModule::create_item(1559 origin2.clone(),1560 1,1561 2,1562 default_nft_data().into()1563 ));1564 });1565}15661567// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens cannot be created by non-privileged and non-white listed address.1568#[test]1569fn white_list_test_15() {1570 new_test_ext().execute_with(|| {1571 default_limits();1572 1573 let collection_id = create_test_collection(&CollectionMode::NFT, 1);15741575 let origin1 = Origin::signed(1);1576 let origin2 = Origin::signed(2);15771578 assert_ok!(TemplateModule::set_public_access_mode(1579 origin1.clone(),1580 collection_id,1581 AccessMode::WhiteList1582 ));1583 assert_ok!(TemplateModule::set_mint_permission(1584 origin1.clone(),1585 collection_id,1586 true1587 ));15881589 assert_noop!(1590 TemplateModule::create_item(origin2.clone(), 1, 2, default_nft_data().into()),1591 Error::<Test>::AddresNotInWhiteList1592 );1593 });1594}15951596// If Public Access mode is set to WhiteList, and Mint Permission is set to true, tokens can be created by non-privileged and white listed address.1597#[test]1598fn white_list_test_16() {1599 new_test_ext().execute_with(|| {1600 default_limits();1601 1602 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16031604 let origin1 = Origin::signed(1);1605 let origin2 = Origin::signed(2);16061607 assert_ok!(TemplateModule::set_public_access_mode(1608 origin1.clone(),1609 collection_id,1610 AccessMode::WhiteList1611 ));1612 assert_ok!(TemplateModule::set_mint_permission(1613 origin1.clone(),1614 collection_id,1615 true1616 ));1617 assert_ok!(TemplateModule::add_to_white_list(origin1.clone(), collection_id, 2));16181619 assert_ok!(TemplateModule::create_item(1620 origin2.clone(),1621 1,1622 2,1623 default_nft_data().into()1624 ));1625 });1626}16271628// Total number of collections. Positive test1629#[test]1630fn total_number_collections_bound() {1631 new_test_ext().execute_with(|| {1632 default_limits();1633 1634 create_test_collection(&CollectionMode::NFT, 1);1635 });1636}16371638// Total number of collections. Negotive test1639#[test]1640fn total_number_collections_bound_neg() {1641 new_test_ext().execute_with(|| {1642 default_limits();16431644 let origin1 = Origin::signed(1);16451646 for i in 0..default_collection_numbers_limit() {1647 create_test_collection(&CollectionMode::NFT, i + 1);1648 }16491650 let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();1651 let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();1652 let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();16531654 // 11-th collection in chain. Expects error1655 assert_noop!(TemplateModule::create_collection(1656 origin1.clone(),1657 col_name1.clone(),1658 col_desc1.clone(),1659 token_prefix1.clone(),1660 CollectionMode::NFT1661 ), Error::<Test>::TotalCollectionsLimitExceeded);1662 });1663}16641665// Owned tokens by a single address. Positive test1666#[test]1667fn owned_tokens_bound() {1668 new_test_ext().execute_with(|| {1669 default_limits();1670 1671 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16721673 let data = default_nft_data();1674 create_test_item(collection_id, &data.clone().into());1675 create_test_item(collection_id, &data.into());1676 });1677}16781679// Owned tokens by a single address. Negotive test1680#[test]1681fn owned_tokens_bound_neg() {1682 new_test_ext().execute_with(|| {1683 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1684 collection_numbers_limit: 10,1685 account_token_ownership_limit: 1,1686 collections_admins_limit: 5,1687 custom_data_limit: 2048,1688 nft_sponsor_transfer_timeout: 15,1689 fungible_sponsor_transfer_timeout: 15,1690 refungible_sponsor_transfer_timeout: 15, 1691 }));1692 1693 let collection_id = create_test_collection(&CollectionMode::NFT, 1);16941695 let origin1 = Origin::signed(1);1696 let data = default_nft_data();1697 create_test_item(collection_id, &data.clone().into());16981699 assert_noop!(TemplateModule::create_item(1700 origin1.clone(),1701 1,1702 1,1703 data.into()1704 ), Error::<Test>::AddressOwnershipLimitExceeded);1705 });1706}17071708// Number of collection admins. Positive test1709#[test]1710fn collection_admins_bound() {1711 new_test_ext().execute_with(|| {1712 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1713 collection_numbers_limit: 10,1714 account_token_ownership_limit: 10,1715 collections_admins_limit: 2,1716 custom_data_limit: 2048,1717 nft_sponsor_transfer_timeout: 15,1718 fungible_sponsor_transfer_timeout: 15,1719 refungible_sponsor_transfer_timeout: 15, 1720 }));1721 1722 let collection_id = create_test_collection(&CollectionMode::NFT, 1);17231724 let origin1 = Origin::signed(1);1725 1726 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));1727 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 3));1728 });1729}17301731// Number of collection admins. Negotive test1732#[test]1733fn collection_admins_bound_neg() {1734 new_test_ext().execute_with(|| {1735 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1736 collection_numbers_limit: 10,1737 account_token_ownership_limit: 1,1738 collections_admins_limit: 1,1739 custom_data_limit: 2048,1740 nft_sponsor_transfer_timeout: 15,1741 fungible_sponsor_transfer_timeout: 15,1742 refungible_sponsor_transfer_timeout: 15, 1743 }));1744 1745 let collection_id = create_test_collection(&CollectionMode::NFT, 1);17461747 let origin1 = Origin::signed(1);17481749 assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 2));1750 assert_noop!(TemplateModule::add_collection_admin(origin1.clone(), collection_id, 3), Error::<Test>::CollectionAdminsLimitExceeded);1751 });1752}17531754// NFT custom data size. Negative test const_data.1755#[test]1756fn custom_data_size_nft_const_data_bound_neg() {1757 new_test_ext().execute_with(|| {1758 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1759 collection_numbers_limit: 10,1760 account_token_ownership_limit: 10,1761 collections_admins_limit: 5,1762 custom_data_limit: 2,1763 nft_sponsor_transfer_timeout: 15,1764 fungible_sponsor_transfer_timeout: 15,1765 refungible_sponsor_transfer_timeout: 15, 1766 }));1767 1768 let collection_id = create_test_collection(&CollectionMode::NFT, 1);17691770 let origin1 = Origin::signed(1);1771 let too_big_const_data = CreateItemData::NFT(CreateNftData{1772 const_data: vec![1, 2, 3, 4],1773 variable_data: vec![]1774 });17751776 assert_noop!(TemplateModule::create_item(1777 origin1.clone(),1778 collection_id,1779 1,1780 too_big_const_data1781 ), Error::<Test>::TokenConstDataLimitExceeded);1782 });1783}17841785// NFT custom data size. Negative test variable_data.1786#[test]1787fn custom_data_size_nft_variable_data_bound_neg() {1788 new_test_ext().execute_with(|| {1789 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1790 collection_numbers_limit: 10,1791 account_token_ownership_limit: 10,1792 collections_admins_limit: 5,1793 custom_data_limit: 2,1794 nft_sponsor_transfer_timeout: 15,1795 fungible_sponsor_transfer_timeout: 15,1796 refungible_sponsor_transfer_timeout: 15, 1797 }));17981799 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18001801 let origin1 = Origin::signed(1);1802 let too_big_const_data = CreateItemData::NFT(CreateNftData{1803 const_data: vec![],1804 variable_data: vec![1, 2, 3, 4]1805 });18061807 assert_noop!(TemplateModule::create_item(1808 origin1.clone(),1809 collection_id,1810 1,1811 too_big_const_data1812 ), Error::<Test>::TokenVariableDataLimitExceeded);1813 });1814}18151816// Re fungible custom data size. Negative test const_data.1817#[test]1818fn custom_data_size_re_fungible_const_data_bound_neg() {1819 new_test_ext().execute_with(|| {1820 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1821 collection_numbers_limit: 10,1822 account_token_ownership_limit: 10,1823 collections_admins_limit: 5,1824 custom_data_limit: 2,1825 nft_sponsor_transfer_timeout: 15,1826 fungible_sponsor_transfer_timeout: 15,1827 refungible_sponsor_transfer_timeout: 15, 1828 }));18291830 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18311832 let origin1 = Origin::signed(1);1833 let too_big_const_data = CreateItemData::NFT(CreateNftData{1834 const_data: vec![1, 2, 3, 4],1835 variable_data: vec![]1836 });18371838 assert_noop!(TemplateModule::create_item(1839 origin1.clone(),1840 collection_id,1841 1,1842 too_big_const_data1843 ), Error::<Test>::TokenConstDataLimitExceeded);1844 });1845}18461847// Re fungible custom data size. Negative test variable_data.1848#[test]1849fn custom_data_size_re_fungible_variable_data_bound_neg() {1850 new_test_ext().execute_with(|| {1851 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1852 collection_numbers_limit: 10,1853 account_token_ownership_limit: 10,1854 collections_admins_limit: 5,1855 custom_data_limit: 2,1856 nft_sponsor_transfer_timeout: 15,1857 fungible_sponsor_transfer_timeout: 15,1858 refungible_sponsor_transfer_timeout: 15, 1859 }));18601861 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18621863 let origin1 = Origin::signed(1);1864 let too_big_const_data = CreateItemData::NFT(CreateNftData{1865 const_data: vec![],1866 variable_data: vec![1, 2, 3, 4]1867 });18681869 assert_noop!(TemplateModule::create_item(1870 origin1.clone(),1871 collection_id,1872 1,1873 too_big_const_data1874 ), Error::<Test>::TokenVariableDataLimitExceeded);1875 });1876}1877// #endregion18781879#[test]1880fn set_const_on_chain_schema() {1881 new_test_ext().execute_with(|| {1882 default_limits();18831884 let collection_id = create_test_collection(&CollectionMode::NFT, 1);18851886 let origin1 = Origin::signed(1);1887 assert_ok!(TemplateModule::set_const_on_chain_schema(origin1, collection_id, b"test const on chain schema".to_vec()));18881889 assert_eq!(TemplateModule::collection(collection_id).const_on_chain_schema, b"test const on chain schema".to_vec());1890 assert_eq!(TemplateModule::collection(collection_id).variable_on_chain_schema, b"".to_vec());1891 });1892}18931894#[test]1895fn set_variable_on_chain_schema() {1896 new_test_ext().execute_with(|| {1897 default_limits();1898 1899 let collection_id = create_test_collection(&CollectionMode::NFT, 1);19001901 let origin1 = Origin::signed(1);1902 assert_ok!(TemplateModule::set_variable_on_chain_schema(origin1, collection_id, b"test variable on chain schema".to_vec()));19031904 assert_eq!(TemplateModule::collection(collection_id).const_on_chain_schema, b"".to_vec());1905 assert_eq!(TemplateModule::collection(collection_id).variable_on_chain_schema, b"test variable on chain schema".to_vec());1906 });1907}19081909#[test]1910fn set_variable_meta_data_on_nft_token_stores_variable_meta_data() {1911 new_test_ext().execute_with(|| {1912 default_limits();19131914 let collection_id = create_test_collection(&CollectionMode::NFT, 1);19151916 let origin1 = Origin::signed(1);1917 1918 let data = default_nft_data();1919 create_test_item(1, &data.into());1920 1921 let variable_data = b"test set_variable_meta_data method.".to_vec();1922 assert_ok!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone()));19231924 assert_eq!(TemplateModule::nft_item_id(collection_id, 1).variable_data, variable_data);1925 });1926}19271928#[test]1929fn set_variable_meta_data_on_re_fungible_token_stores_variable_meta_data() {1930 new_test_ext().execute_with(|| {1931 default_limits();19321933 let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1);19341935 let origin1 = Origin::signed(1);19361937 let data = default_re_fungible_data();1938 create_test_item(1, &data.into());19391940 let variable_data = b"test set_variable_meta_data method.".to_vec();1941 assert_ok!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone()));19421943 assert_eq!(TemplateModule::refungible_item_id(collection_id, 1).variable_data, variable_data);1944 });1945}194619471948#[test]1949fn set_variable_meta_data_on_fungible_token_fails() {1950 new_test_ext().execute_with(|| {1951 default_limits();19521953 let collection_id = create_test_collection(&CollectionMode::Fungible(3), 1);19541955 let origin1 = Origin::signed(1);19561957 let data = default_fungible_data();1958 create_test_item(1, &data.into());19591960 let variable_data = b"test set_variable_meta_data method.".to_vec();1961 assert_noop!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone()), Error::<Test>::CantStoreMetadataInFungibleTokens);1962 });1963}19641965#[test]1966fn set_variable_meta_data_on_nft_token_fails_for_big_data() {1967 new_test_ext().execute_with(|| {1968 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1969 collection_numbers_limit: default_collection_numbers_limit(),1970 account_token_ownership_limit: 10,1971 collections_admins_limit: 5,1972 custom_data_limit: 10,1973 nft_sponsor_transfer_timeout: 15,1974 fungible_sponsor_transfer_timeout: 15,1975 refungible_sponsor_transfer_timeout: 15, 1976 }));19771978 let collection_id = create_test_collection(&CollectionMode::NFT, 1);19791980 let origin1 = Origin::signed(1);19811982 let data = default_nft_data();1983 create_test_item(1, &data.into());19841985 let variable_data = b"test set_variable_meta_data method, bigger than limits.".to_vec();1986 assert_noop!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data), Error::<Test>::TokenVariableDataLimitExceeded);1987 });1988}19891990#[test]1991fn set_variable_meta_data_on_re_fungible_token_fails_for_big_data() {1992 new_test_ext().execute_with(|| {1993 assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { 1994 collection_numbers_limit: default_collection_numbers_limit(),1995 account_token_ownership_limit: 10,1996 collections_admins_limit: 5,1997 custom_data_limit: 10,1998 nft_sponsor_transfer_timeout: 15,1999 fungible_sponsor_transfer_timeout: 15,2000 refungible_sponsor_transfer_timeout: 15, 2001 }));200220032004 let collection_id = create_test_collection(&CollectionMode::ReFungible(3), 1);20052006 let origin1 = Origin::signed(1);20072008 let data = default_re_fungible_data();2009 create_test_item(1, &data.into());20102011 let variable_data = b"test set_variable_meta_data method, bigger than limits.".to_vec();2012 assert_noop!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data), Error::<Test>::TokenVariableDataLimitExceeded);2013 });2014}tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -18,10 +18,19 @@
"scripts": {
"test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
"load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
+ "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",
"testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
"testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
+ "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",
"testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
- "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
+ "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
+ "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",
+ "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",
+ "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
+ "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",
+ "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",
+ "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",
+ "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts"
},
"author": "",
"license": "Apache 2.0",
tests/src/addCollectionAdmin.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/addCollectionAdmin.test.ts
@@ -0,0 +1,130 @@
+import { ApiPromise } from '@polkadot/api';
+import BN from 'bn.js';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';
+import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {
+ it('Add collection admin.', async () => {
+ await usingApi(async (api) => {
+ const collectionId = await createCollectionExpectSuccess();
+ const alice = privateKey('//Alice');
+ const bob = privateKey('//Bob');
+
+ const collection: any = (await api.query.nft.collection(collectionId));
+ expect(collection.Owner.toString()).to.be.eq(alice.address);
+
+ const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);
+ await submitTransactionAsync(alice, changeAdminTx);
+
+ const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterAddAdmin).to.be.contains(bob.address);
+ });
+ });
+
+ it('Add admin using added collection admin.', async () => {
+ await usingApi(async (api) => {
+ const collectionId = await createCollectionExpectSuccess();
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const Charlie = privateKey('//CHARLIE');
+
+ const collection: any = (await api.query.nft.collection(collectionId));
+ expect(collection.Owner.toString()).to.be.eq(Alice.address);
+
+ const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
+ await submitTransactionAsync(Alice, changeAdminTx);
+
+ const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterAddAdmin).to.be.contains(Bob.address);
+
+ const changeAdminTxCharlie = api.tx.nft.addCollectionAdmin(collectionId, Charlie.address);
+ await submitTransactionAsync(Bob, changeAdminTxCharlie);
+ const adminListAfterAddNewAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterAddNewAdmin).to.be.contains(Bob.address);
+ expect(adminListAfterAddNewAdmin).to.be.contains(Charlie.address);
+ });
+ });
+});
+
+describe('Negative Integration Test addCollectionAdmin(collection_id, new_admin_id):', () => {
+ it("Not owner can't add collection admin.", async () => {
+ await usingApi(async (api) => {
+ const collectionId = await createCollectionExpectSuccess();
+ const alice = privateKey('//Alice');
+ const nonOwner = privateKey('//Bob_stash');
+
+ const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, alice.address);
+ await expect(submitTransactionExpectFailAsync(nonOwner, changeAdminTx)).to.be.rejected;
+
+ const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterAddAdmin).not.to.be.contains(alice.address);
+
+ // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+ await createCollectionExpectSuccess();
+ });
+ });
+ it("Can't add collection admin of not existing collection.", async () => {
+ await usingApi(async (api) => {
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = (1 << 32) - 1;
+ const alice = privateKey('//Alice');
+ const bob = privateKey('//Bob');
+
+ const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, bob.address);
+ await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;
+
+ // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+ await createCollectionExpectSuccess();
+ });
+ });
+
+ it("Can't add an admin to a destroyed collection.", async () => {
+ await usingApi(async (api) => {
+ const collectionId = await createCollectionExpectSuccess();
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ await destroyCollectionExpectSuccess(collectionId);
+ const changeOwnerTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
+ await expect(submitTransactionExpectFailAsync(Alice, changeOwnerTx)).to.be.rejected;
+
+ // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+ await createCollectionExpectSuccess();
+ });
+ });
+
+ it('Add an admin to a collection that has reached the maximum number of admins limit', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const accounts = [
+ 'GsvVmjr1CBHwQHw84pPHMDxgNY3iBLz6Qn7qS3CH8qPhrHz',
+ 'FoQJpPyadYccjavVdTWxpxU7rUEaYhfLCPwXgkfD6Zat9QP',
+ 'JKspFU6ohf1Grg3Phdzj2pSgWvsYWzSfKghhfzMbdhNBWs5',
+ 'Fr4NzY1udSFFLzb2R3qxVQkwz9cZraWkyfH4h3mVVk7BK7P',
+ 'DfnTB4z7eUvYRqcGtTpFsLC69o6tvBSC1pEv8vWPZFtCkaK',
+ 'HnMAUz7r2G8G3hB27SYNyit5aJmh2a5P4eMdDtACtMFDbam',
+ 'DE14BzQ1bDXWPKeLoAqdLAm1GpyAWaWF1knF74cEZeomTBM',
+ ];
+ const collectionId = await createCollectionExpectSuccess();
+
+ const chainLimit = await api.query.nft.chainLimit() as unknown as { collections_admins_limit: BN };
+ const chainLimitNumber = chainLimit.collections_admins_limit.toNumber();
+ expect(chainLimitNumber).to.be.equal(5);
+
+ for (let i = 0; i < chainLimitNumber; i++) {
+ const changeAdminTx = api.tx.nft.addCollectionAdmin(collectionId, accounts[i]);
+ await submitTransactionAsync(Alice, changeAdminTx);
+ const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterAddAdmin).to.be.contains(accounts[i]);
+ }
+
+ const tx = api.tx.nft.addCollectionAdmin(collectionId, accounts[chainLimitNumber]);
+ await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+ });
+ });
+});
tests/src/addToContractWhiteList.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/addToContractWhiteList.test.ts
@@ -0,0 +1,91 @@
+import chai from "chai";
+import chaiAsPromised from 'chai-as-promised';
+import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";
+import privateKey from "./substrate/privateKey";
+import {
+ deployFlipper,
+ getFlipValue
+} from "./util/contracthelpers";
+import {
+ getGenericResult
+} from "./util/helpers"
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+const value = 0;
+const gasLimit = 3000n * 1000000n;
+
+describe('Integration Test addToContractWhiteList', () => {
+
+ it(`Add an address to a contract white list`, async () => {
+ await usingApi(async api => {
+ const bob = privateKey("//Bob");
+ const [contract, deployer] = await deployFlipper(api);
+
+ const whiteListedBefore = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+ const addTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
+ const addEvents = await submitTransactionAsync(deployer, addTx);
+ const whiteListedAfter = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+
+ expect(getGenericResult(addEvents).success).to.be.true;
+ expect(whiteListedBefore).to.be.false;
+ expect(whiteListedAfter).to.be.true;
+ });
+ });
+
+ it(`Adding same address to white list repeatedly should not produce errors`, async () => {
+ await usingApi(async api => {
+ const bob = privateKey("//Bob");
+ const [contract, deployer] = await deployFlipper(api);
+
+ const whiteListedBefore = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+ const addTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
+ const addEvents = await submitTransactionAsync(deployer, addTx);
+ const whiteListedAfter = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+ const addAgainEvents = await submitTransactionAsync(deployer, addTx);
+ const whiteListedAgainAfter = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+
+ expect(getGenericResult(addEvents).success).to.be.true;
+ expect(whiteListedBefore).to.be.false;
+ expect(whiteListedAfter).to.be.true;
+ expect(getGenericResult(addAgainEvents).success).to.be.true;
+ expect(whiteListedAgainAfter).to.be.true;
+ });
+ });
+});
+
+describe('Negative Integration Test addToContractWhiteList', () => {
+
+ it(`Add an address to a white list of a non-contract`, async () => {
+ await usingApi(async api => {
+ const alice = privateKey("//Bob");
+ const bob = privateKey("//Bob");
+ const charlieGuineaPig = privateKey("//Charlie");
+
+ const whiteListedBefore = (await api.query.nft.contractWhiteList(charlieGuineaPig.address, bob.address)).toJSON();
+ const addTx = api.tx.nft.addToContractWhiteList(charlieGuineaPig.address, bob.address);
+ await expect(submitTransactionExpectFailAsync(alice, addTx)).to.be.rejected;
+ const whiteListedAfter = (await api.query.nft.contractWhiteList(charlieGuineaPig.address, bob.address)).toJSON();
+
+ expect(whiteListedBefore).to.be.false;
+ expect(whiteListedAfter).to.be.false;
+ });
+ });
+
+ it(`Add to a contract white list using a non-owner address`, async () => {
+ await usingApi(async api => {
+ const bob = privateKey("//Bob");
+ const [contract, deployer] = await deployFlipper(api);
+
+ const whiteListedBefore = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+ const addTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
+ await expect(submitTransactionExpectFailAsync(bob, addTx)).to.be.rejected;
+ const whiteListedAfter = (await api.query.nft.contractWhiteList(contract.address, bob.address)).toJSON();
+
+ expect(whiteListedBefore).to.be.false;
+ expect(whiteListedAfter).to.be.false;
+ });
+ });
+
+});
tests/src/addToWhiteList.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/addToWhiteList.test.ts
@@ -0,0 +1,81 @@
+import { IKeyringPair } from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import usingApi, { submitTransactionExpectFailAsync } from './substrate/substrate-api';
+import {
+ addToWhiteListExpectSuccess,
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+ enablePublicMintingExpectSuccess,
+ enableWhiteListExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let Alice: IKeyringPair;
+let Bob: IKeyringPair;
+
+describe('Integration Test ext. addToWhiteList()', () => {
+
+ before(async () => {
+ await usingApi(async (api) => {
+ Alice = privateKey('//Alice');
+ Bob = privateKey('//Bob');
+ });
+ });
+
+ it('Execute the extrinsic with parameters: Collection ID and address to add to the white list', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ });
+
+ it('Whitelisted minting: list restrictions', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await enablePublicMintingExpectSuccess(Alice, collectionId);
+ await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);
+ });
+});
+
+describe('Negative Integration Test ext. addToWhiteList()', () => {
+
+ it('White list an address in the collection that does not exist', async () => {
+ await usingApi(async (api) => {
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;
+ const Bob = privateKey('//Bob');
+
+ const tx = api.tx.nft.addToWhiteList(collectionId, Bob.address);
+ await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+ });
+ });
+
+ it('White list an address in the collection that was destroyed', async () => {
+ await usingApi(async (api) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = await createCollectionExpectSuccess();
+ await destroyCollectionExpectSuccess(collectionId);
+ const tx = api.tx.nft.addToWhiteList(collectionId, Bob.address);
+ await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+ });
+ });
+
+ it('White list an address in the collection that does not have white list access enabled', async () => {
+ await usingApi(async (api) => {
+ const Alice = privateKey('//Alice');
+ const Ferdie = privateKey('//Ferdie');
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await enablePublicMintingExpectSuccess(Alice, collectionId);
+ const tx = api.tx.nft.createItem(collectionId, Ferdie.address, 'NFT');
+ await expect(submitTransactionExpectFailAsync(Ferdie, tx)).to.be.rejected;
+ });
+ });
+
+});
tests/src/approve.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/approve.test.ts
@@ -0,0 +1,140 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+import { ApiPromise } from '@polkadot/api';
+import BN from 'bn.js';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi } from './substrate/substrate-api';
+import {
+ approveExpectFail,
+ approveExpectSuccess,
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {
+ it('Execute the extrinsic and check approvedList', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const nftCollectionId = await createCollectionExpectSuccess();
+ // nft
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+ // reFungible
+ const reFungibleCollectionId =
+ await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+ });
+ });
+
+ it('Remove approval by using 0 amount', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const nftCollectionId = await createCollectionExpectSuccess();
+ // nft
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);
+ await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);
+ await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);
+ // reFungible
+ const reFungibleCollectionId =
+ await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);
+ await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);
+ });
+ });
+});
+
+describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {
+ it('Approve for a collection that does not exist', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ // nft
+ const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);
+ // fungible
+ const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);
+ // reFungible
+ const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);
+ });
+ });
+
+ it('Approve for a collection that was destroyed', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ await destroyCollectionExpectSuccess(nftCollectionId);
+ await approveExpectFail(nftCollectionId, 1, Alice, Bob);
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ await destroyCollectionExpectSuccess(fungibleCollectionId);
+ await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);
+ // reFungible
+ const reFungibleCollectionId =
+ await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ await destroyCollectionExpectSuccess(reFungibleCollectionId);
+ await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);
+ });
+ });
+
+ it('Approve transfer of a token that does not exist', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ await approveExpectFail(nftCollectionId, 2, Alice, Bob);
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);
+ // reFungible
+ const reFungibleCollectionId =
+ await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);
+ });
+ });
+
+ it('Approve using the address that does not own the approved token', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const nftCollectionId = await createCollectionExpectSuccess();
+ // nft
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);
+ // reFungible
+ const reFungibleCollectionId =
+ await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);
+ });
+ });
+});
tests/src/burnItem.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/burnItem.test.ts
@@ -0,0 +1,237 @@
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';
+import { Keyring } from "@polkadot/api";
+import { IKeyringPair } from "@polkadot/types/types";
+import {
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ getGenericResult,
+ destroyCollectionExpectSuccess
+} from './util/helpers';
+import { nullPublicKey } from "./accounts";
+
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+
+describe('integration test: ext. burnItem():', () => {
+ before(async () => {
+ await usingApi(async (api) => {
+ const keyring = new Keyring({ type: 'sr25519' });
+ alice = keyring.addFromUri(`//Alice`);
+ bob = keyring.addFromUri(`//Bob`);
+ });
+ });
+
+ it('Burn item in NFT collection', async () => {
+ const createMode = 'NFT';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const events = await submitTransactionAsync(alice, tx);
+ const result = getGenericResult(events);
+ // Get the item
+ const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();
+ // What to expect
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ // tslint:disable-next-line:no-unused-expression
+ expect(item).to.be.not.null;
+ expect(item.Owner).to.be.equal(nullPublicKey);
+ });
+
+ });
+ it('Burn item in Fungible collection', async () => {
+ const createMode = 'Fungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});
+ await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens
+ const tokenId = 0; // ignored
+
+ await usingApi(async (api) => {
+ // Destroy 1 of 10
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
+ const events = await submitTransactionAsync(alice, tx);
+ const result = getGenericResult(events);
+
+ // Get alice balance
+ const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();
+
+ // What to expect
+ expect(result.success).to.be.true;
+ expect(balance).to.be.not.null;
+ expect(balance.Value).to.be.equal(9);
+ });
+
+ });
+ it('Burn item in ReFungible collection', async () => {
+ const createMode = 'ReFungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 2 }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
+ const events = await submitTransactionAsync(alice, tx);
+ const result = getGenericResult(events);
+
+ // Get alice balance
+ const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
+
+ // What to expect
+ expect(result.success).to.be.true;
+ expect(balance).to.be.not.null;
+ expect(balance.Owner.length).to.be.equal(0);
+ });
+
+ });
+
+ it('Burn owned portion of item in ReFungible collection', async () => {
+ const createMode = 'ReFungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 2 }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+
+ await usingApi(async (api) => {
+ // Transfer 1/100 of the token to Bob
+ const transfertx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 1);
+ const events1 = await submitTransactionAsync(alice, transfertx);
+ const result1 = getGenericResult(events1);
+
+ // Get balances
+ const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
+
+ // Bob burns his portion
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const events2 = await submitTransactionAsync(bob, tx);
+ const result2 = getGenericResult(events2);
+
+ // Get balances
+ const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
+ // console.log(balance);
+
+ // What to expect before burning
+ expect(result1.success).to.be.true;
+ expect(balanceBefore).to.be.not.null;
+ expect(balanceBefore.Owner.length).to.be.equal(2);
+ expect(balanceBefore.Owner[0].Owner).to.be.equal(alice.address);
+ expect(balanceBefore.Owner[0].Fraction).to.be.equal(99);
+ expect(balanceBefore.Owner[1].Owner).to.be.equal(bob.address);
+ expect(balanceBefore.Owner[1].Fraction).to.be.equal(1);
+
+ // What to expect after burning
+ expect(result2.success).to.be.true;
+ expect(balance).to.be.not.null;
+ expect(balance.Owner.length).to.be.equal(1);
+ expect(balance.Owner[0].Fraction).to.be.equal(99);
+ expect(balance.Owner[0].Owner).to.be.equal(alice.address);
+ });
+
+ });
+
+});
+
+describe('Negative integration test: ext. burnItem():', () => {
+ before(async () => {
+ await usingApi(async (api) => {
+ const keyring = new Keyring({ type: 'sr25519' });
+ alice = keyring.addFromUri(`//Alice`);
+ bob = keyring.addFromUri(`//Bob`);
+ });
+ });
+
+ it('Burn a token in a destroyed collection', async () => {
+ const createMode = 'NFT';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+ await destroyCollectionExpectSuccess(collectionId);
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const badTransaction = async function () {
+ await submitTransactionExpectFailAsync(alice, tx);
+ };
+ await expect(badTransaction()).to.be.rejected;
+ });
+
+ });
+
+ it('Burn a token that was never created', async () => {
+ const createMode = 'NFT';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});
+ const tokenId = 10;
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const badTransaction = async function () {
+ await submitTransactionExpectFailAsync(alice, tx);
+ };
+ await expect(badTransaction()).to.be.rejected;
+ });
+
+ });
+
+ it('Burn a token using the address that does not own it', async () => {
+ const createMode = 'NFT';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const badTransaction = async function () {
+ await submitTransactionExpectFailAsync(bob, tx);
+ };
+ await expect(badTransaction()).to.be.rejected;
+ });
+
+ });
+
+ it('Transfer a burned a token', async () => {
+ const createMode = 'NFT';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+
+ await usingApi(async (api) => {
+
+ const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const events1 = await submitTransactionAsync(alice, burntx);
+ const result1 = getGenericResult(events1);
+ expect(result1.success).to.be.true;
+
+ const tx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 0);
+ const badTransaction = async function () {
+ await submitTransactionExpectFailAsync(alice, tx);
+ };
+ await expect(badTransaction()).to.be.rejected;
+ });
+
+ });
+
+ it('Burn more than owned in Fungible collection', async () => {
+ const createMode = 'Fungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});
+ // Helper creates 10 fungible tokens
+ await createItemExpectSuccess(alice, collectionId, createMode);
+ const tokenId = 0; // ignored
+
+ await usingApi(async (api) => {
+ // Destroy 11 of 10
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);
+ const badTransaction = async function () {
+ await submitTransactionExpectFailAsync(alice, tx);
+ };
+ await expect(badTransaction()).to.be.rejected;
+
+ // Get alice balance
+ const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();
+
+ // What to expect
+ expect(balance).to.be.not.null;
+ expect(balance.Value).to.be.equal(10);
+ });
+
+ });
+
+});
tests/src/confirmSponsorship.test.tsdiffbeforeafterboth--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -89,7 +89,7 @@
});
it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {
- const collectionId = await createCollectionExpectSuccess({mode: 'Fungible'});
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0 }});
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
@@ -115,7 +115,7 @@
});
it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async () => {
- const collectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0 }});
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
@@ -170,7 +170,7 @@
});
});
- it('NFT: Sponsoring is rate limited', async () => {
+ it('NFT: Sponsoring of transfers is rate limited', async () => {
const collectionId = await createCollectionExpectSuccess();
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
@@ -210,7 +210,7 @@
});
it('Fungible: Sponsoring is rate limited', async () => {
- const collectionId = await createCollectionExpectSuccess({mode: 'Fungible'});
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0 }});
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
@@ -247,7 +247,7 @@
});
it('ReFungible: Sponsoring is rate limited', async () => {
- const collectionId = await createCollectionExpectSuccess({mode: 'ReFungible'});
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0 }});
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
@@ -285,6 +285,51 @@
});
});
+ it('NFT: Sponsoring of createItem is rate limited', async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await setCollectionSponsorExpectSuccess(collectionId, bob.address);
+ await confirmSponsorshipExpectSuccess(collectionId, '//Bob');
+
+ // Enable collection white list
+ await enableWhiteListExpectSuccess(alice, collectionId);
+
+ // Enable public minting
+ await enablePublicMintingExpectSuccess(alice, collectionId);
+
+ await usingApi(async (api) => {
+ // Find unused address
+ const zeroBalance = await findUnusedAddress(api);
+
+ // Add zeroBalance address to white list
+ await addToWhiteListExpectSuccess(alice, collectionId, zeroBalance.address);
+
+ // Mint token using unused address as signer - gets sponsored
+ await createItemExpectSuccess(zeroBalance, collectionId, 'NFT', zeroBalance.address);
+
+ // Second mint should fail
+ const AsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());
+
+ const consoleError = console.error;
+ const consoleLog = console.log;
+ console.error = () => {};
+ console.log = () => {};
+ const badTransaction = async function () {
+ await createItemExpectSuccess(zeroBalance, collectionId, 'NFT', zeroBalance.address);
+ };
+ await expect(badTransaction()).to.be.rejectedWith("Inability to pay some fees");
+ console.error = consoleError;
+ console.log = consoleLog;
+ const BsponsorBalance = new BigNumber((await api.query.system.account(bob.address)).data.free.toString());
+
+ // Try again after Zero gets some balance - now it should succeed
+ const balancetx = api.tx.balances.transfer(zeroBalance.address, 1e15);
+ await submitTransactionAsync(alice, balancetx);
+ await createItemExpectSuccess(zeroBalance, collectionId, 'NFT', zeroBalance.address);
+
+ expect(BsponsorBalance.isEqualTo(AsponsorBalance)).to.be.true;
+ });
+ });
+
});
describe('(!negative test!) integration test: ext. removeCollectionSponsor():', () => {
tests/src/contracts.test.tsdiffbeforeafterboth--- a/tests/src/contracts.test.ts
+++ b/tests/src/contracts.test.ts
@@ -2,95 +2,20 @@
import chaiAsPromised from 'chai-as-promised';
import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";
import fs from "fs";
-import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";
-import { IKeyringPair } from "@polkadot/types/types";
-import { ApiPromise, Keyring } from "@polkadot/api";
-import { ApiTypes, SubmittableExtrinsic } from "@polkadot/api/types";
+import { Abi, ContractPromise as Contract } from "@polkadot/api-contract";
import privateKey from "./substrate/privateKey";
+import {
+ deployFlipper,
+ getFlipValue
+} from "./util/contracthelpers";
chai.use(chaiAsPromised);
const expect = chai.expect;
-import { BigNumber } from 'bignumber.js';
-import { findUnusedAddress } from './util/helpers';
const value = 0;
const gasLimit = 3000n * 1000000n;
-const endowment = `1000000000000000`;
const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';
-
-function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {
- return new Promise<Blueprint>(async (resolve, reject) => {
- const unsub = await code
- .createBlueprint()
- .signAndSend(alice, (result) => {
- if (result.status.isInBlock || result.status.isFinalized) {
- // here we have an additional field in the result, containing the blueprint
- resolve(result.blueprint);
- unsub();
- }
- })
- });
-}
-
-function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
- return new Promise<any>(async (resolve, reject) => {
- const endowment = 1000000000000000n;
- const initValue = true;
-
- const unsub = await blueprint.tx
- .new(endowment, gasLimit, initValue)
- .signAndSend(alice, (result) => {
- if (result.status.isInBlock || result.status.isFinalized) {
- unsub();
- resolve(result);
- }
- });
- });
-}
-async function prepareDeployer(api: ApiPromise) {
- // Find unused address
- const deployer = await findUnusedAddress(api);
-
- // Transfer balance to it
- const keyring = new Keyring({ type: 'sr25519' });
- const alice = keyring.addFromUri(`//Alice`);
- let amount = new BigNumber(endowment);
- amount = amount.plus(1e15);
- const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());
- await submitTransactionAsync(alice, tx);
-
- return deployer;
-}
-
-export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {
- const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));
- const abi = new Abi(metadata);
-
- const deployer = await prepareDeployer(api);
-
- const wasm = fs.readFileSync('./src/flipper/flipper.wasm');
-
- const code = new CodePromise(api, abi, wasm);
-
- const blueprint = await deployBlueprint(deployer, code);
- const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;
-
- const initialGetResponse = await getFlipValue(contract, deployer);
- expect(initialGetResponse).to.be.true;
-
- return [contract, deployer];
-}
-
-async function getFlipValue(contract: Contract, deployer: IKeyringPair) {
- const result = await contract.query.get(deployer.address, value, gasLimit);
-
- if(!result.result.isSuccess) {
- throw `Failed to get flipper value`;
- }
- return (result.result.asSuccess.data[0] == 0x00) ? false : true;
-}
-
describe('Contracts', () => {
it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {
await usingApi(async api => {
@@ -103,68 +28,6 @@
const afterFlipGetResponse = await getFlipValue(contract, deployer);
expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');
- });
- });
-
- it(`Whitelisted account can call contract.`, async () => {
- await usingApi(async api => {
- const bob = privateKey("//Bob");
-
- const [contract, deployer] = await deployFlipper(api);
-
- let expectedFlipValue = await getFlipValue(contract, deployer);
-
- const flip = contract.exec('flip', value, gasLimit);
- await submitTransactionAsync(bob, flip);
- expectedFlipValue = !expectedFlipValue;
- const afterFlip = await getFlipValue(contract,deployer);
- expect(afterFlip).to.be.eq(expectedFlipValue, `Anyone can call new contract.`);
-
- const deployerCanFlip = async () => {
- expectedFlipValue = !expectedFlipValue;
- const deployerFlip = contract.exec('flip', value, gasLimit);
- await submitTransactionAsync(deployer, deployerFlip);
- const aliceFlip1Response = await getFlipValue(contract, deployer);
- expect(aliceFlip1Response).to.be.eq(expectedFlipValue, `Deployer always can flip.`);
- };
- await deployerCanFlip();
-
- const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
- const enableResult = await submitTransactionAsync(deployer, enableWhiteListTx);
- const flipWithEnabledWhiteList = contract.exec('flip', value, gasLimit);
- await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledWhiteList)).to.be.rejected;
- const flipValueAfterEnableWhiteList = await getFlipValue(contract, deployer);
- expect(flipValueAfterEnableWhiteList).to.be.eq(expectedFlipValue, `Enabling whitelist doesn't make it possible to call contract for everyone.`);
-
- await deployerCanFlip();
-
- const addBobToWhiteListTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
- const addBobResult = await submitTransactionAsync(deployer, addBobToWhiteListTx);
- const flipWithWhitelistedBob = contract.exec('flip', value, gasLimit);
- await submitTransactionAsync(bob, flipWithWhitelistedBob);
- expectedFlipValue = !expectedFlipValue;
- const flipAfterWhiteListed = await getFlipValue(contract,deployer);
- expect(flipAfterWhiteListed).to.be.eq(expectedFlipValue, `Bob was whitelisted, now he can flip.`);
-
- await deployerCanFlip();
-
- const removeBobFromWhiteListTx = api.tx.nft.removeFromContractWhiteList(contract.address, bob.address);
- const removeBobResult = await submitTransactionAsync(deployer, removeBobFromWhiteListTx);
- const bobRemoved = contract.exec('flip', value, gasLimit);
- await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;
- const afterBobRemoved = await getFlipValue(contract, deployer);
- expect(afterBobRemoved).to.be.eq(expectedFlipValue, `Bob can't call contract, now when he is removeed from white list.`);
-
- await deployerCanFlip();
-
- const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);
- const disableWhiteListResult = await submitTransactionAsync(deployer, disableWhiteListTx);
- const whiteListDisabledFlip = contract.exec('flip', value, gasLimit);
- await submitTransactionAsync(bob, whiteListDisabledFlip);
- expectedFlipValue = !expectedFlipValue;
- const afterWhiteListDisabled = await getFlipValue(contract,deployer);
- expect(afterWhiteListDisabled).to.be.eq(expectedFlipValue, `Anyone can call contract with disabled whitelist.`);
-
});
});
tests/src/createCollection.test.tsdiffbeforeafterboth--- a/tests/src/createCollection.test.ts
+++ b/tests/src/createCollection.test.ts
@@ -5,15 +5,15 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
-import { default as usingApi } from "./substrate/substrate-api";
-import { createCollectionExpectSuccess, createCollectionExpectFailure, CollectionMode } from "./util/helpers";
+import { default as usingApi } from './substrate/substrate-api';
+import { createCollectionExpectFailure, createCollectionExpectSuccess } from './util/helpers';
chai.use(chaiAsPromised);
const expect = chai.expect;
describe('integration test: ext. createCollection():', () => {
it('Create new NFT collection', async () => {
- await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+ await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
});
it('Create new NFT collection whith collection_name of maximum length (64 bytes)', async () => {
await createCollectionExpectSuccess({name: 'A'.repeat(64)});
@@ -25,34 +25,35 @@
await createCollectionExpectSuccess({tokenPrefix: 'A'.repeat(16)});
});
it('Create new Fungible collection', async () => {
- await createCollectionExpectSuccess({mode: 'Fungible'});
+ await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
});
it('Create new ReFungible collection', async () => {
- await createCollectionExpectSuccess({mode: 'ReFungible'});
+ await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
});
});
describe('(!negative test!) integration test: ext. createCollection():', () => {
it('(!negative test!) create new NFT collection whith incorrect data (mode)', async () => {
await usingApi(async (api) => {
- const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
+ const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString(), 10);
- const badTransaction = async function () {
- await createCollectionExpectSuccess({mode: 'BadMode' as CollectionMode});
+ const badTransaction = async () => {
+ await createCollectionExpectSuccess({mode: {type: 'Invalid'}});
};
+ // tslint:disable-next-line:no-unused-expression
expect(badTransaction()).to.be.rejected;
- const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());
+ const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString(), 10);
expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Incorrect collection created.');
});
});
it('(!negative test!) create new NFT collection whith incorrect data (collection_name)', async () => {
- await createCollectionExpectFailure({name: 'A'.repeat(65)});
+ await createCollectionExpectFailure({ name: 'A'.repeat(65), mode: {type: 'NFT'}});
});
it('(!negative test!) create new NFT collection whith incorrect data (collection_description)', async () => {
- await createCollectionExpectFailure({description: 'A'.repeat(257)});
+ await createCollectionExpectFailure({ description: 'A'.repeat(257), mode: { type: 'NFT' }});
});
it('(!negative test!) create new NFT collection whith incorrect data (token_prefix)', async () => {
- await createCollectionExpectFailure({tokenPrefix: 'A'.repeat(17)});
+ await createCollectionExpectFailure({tokenPrefix: 'A'.repeat(17), mode: {type: 'NFT'}});
});
});
tests/src/createItem.test.tsdiffbeforeafterboth--- a/tests/src/createItem.test.ts
+++ b/tests/src/createItem.test.ts
@@ -18,17 +18,17 @@
it('Create new item in NFT collection', async () => {
const createMode = 'NFT';
- const newCollectionID = await createCollectionExpectSuccess({mode: createMode});
+ const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode}});
await createItemExpectSuccess(alice, newCollectionID, createMode);
});
it('Create new item in Fungible collection', async () => {
const createMode = 'Fungible';
- const newCollectionID = await createCollectionExpectSuccess({mode: createMode});
+ const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});
await createItemExpectSuccess(alice, newCollectionID, createMode);
});
it('Create new item in ReFungible collection', async () => {
const createMode = 'ReFungible';
- const newCollectionID = await createCollectionExpectSuccess({mode: createMode});
+ const newCollectionID = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});
await createItemExpectSuccess(alice, newCollectionID, createMode);
});
});
tests/src/destroyCollection.test.tsdiffbeforeafterboth--- a/tests/src/destroyCollection.test.ts
+++ b/tests/src/destroyCollection.test.ts
@@ -14,11 +14,11 @@
await destroyCollectionExpectSuccess(collectionId);
});
it('Fungible collection can be destroyed', async () => {
- const collectionId = await createCollectionExpectSuccess({ mode: 'Fungible' });
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
await destroyCollectionExpectSuccess(collectionId);
});
it('ReFungible collection can be destroyed', async () => {
- const collectionId = await createCollectionExpectSuccess({ mode: 'ReFungible' });
+ const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
await destroyCollectionExpectSuccess(collectionId);
});
});
tests/src/removeCollectionAdmin.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/removeCollectionAdmin.test.ts
@@ -0,0 +1,82 @@
+import { ApiPromise } from '@polkadot/api';
+import BN from 'bn.js';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';
+import {createCollectionExpectSuccess, destroyCollectionExpectSuccess} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test removeCollectionAdmin(collection_id, account_id):', () => {
+ it('Remove collection admin.', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const collectionId = await createCollectionExpectSuccess();
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const collection: any = (await api.query.nft.collection(collectionId));
+ expect(collection.Owner.toString()).to.be.eq(Alice.address);
+ // first - add collection admin Bob
+ const addAdminTx = api.tx.nft.addCollectionAdmin(collectionId, Bob.address);
+ await submitTransactionAsync(Alice, addAdminTx);
+
+ const adminListAfterAddAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterAddAdmin).to.be.contains(Bob.address);
+
+ // then remove bob from admins of collection
+ const removeAdminTx = api.tx.nft.removeCollectionAdmin(collectionId, Bob.address);
+ await submitTransactionAsync(Alice, removeAdminTx);
+
+ const adminListAfterRemoveAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListAfterRemoveAdmin).not.to.be.contains(Bob.address);
+ });
+ });
+});
+
+describe('Negative Integration Test removeCollectionAdmin(collection_id, account_id):', () => {
+ it('Can\'t remove collection admin from not existing collection', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = (1 << 32) - 1;
+ const alice = privateKey('//Alice');
+ const bob = privateKey('//Bob');
+
+ const changeOwnerTx = api.tx.nft.removeCollectionAdmin(collectionId, bob.address);
+ await expect(submitTransactionExpectFailAsync(alice, changeOwnerTx)).to.be.rejected;
+
+ // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+ await createCollectionExpectSuccess();
+ });
+ });
+
+ it('Can\'t remove collection admin from deleted collection', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = await createCollectionExpectSuccess();
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+
+ await destroyCollectionExpectSuccess(collectionId);
+
+ const changeOwnerTx = api.tx.nft.removeCollectionAdmin(collectionId, Bob.address);
+ await expect(submitTransactionExpectFailAsync(Alice, changeOwnerTx)).to.be.rejected;
+
+ // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
+ await createCollectionExpectSuccess();
+ });
+ });
+
+ it('Remove admin from collection that has no admins', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const collectionId = await createCollectionExpectSuccess();
+
+ const adminListBeforeAddAdmin: any = (await api.query.nft.adminList(collectionId));
+ expect(adminListBeforeAddAdmin).to.have.lengthOf(0);
+
+ const tx = api.tx.nft.removeCollectionAdmin(collectionId, Alice.address);
+ await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+ });
+ });
+});
tests/src/setCollectionLimits.test.tsdiffbeforeafterboth--- a/tests/src/setCollectionLimits.test.ts
+++ b/tests/src/setCollectionLimits.test.ts
@@ -32,7 +32,7 @@
});
it('choose or create collection for testing', async () => {
await usingApi(async () => {
- collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+ collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
});
});
});
tests/src/setCollectionSponsor.test.tsdiffbeforeafterboth--- a/tests/src/setCollectionSponsor.test.ts
+++ b/tests/src/setCollectionSponsor.test.ts
@@ -30,11 +30,11 @@
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
});
it('Set Fungible collection sponsor', async () => {
- const collectionId = await createCollectionExpectSuccess({ mode: 'Fungible' });
+ const collectionId = await createCollectionExpectSuccess({ mode: {type: 'Fungible', decimalPoints: 0} });
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
});
it('Set ReFungible collection sponsor', async () => {
- const collectionId = await createCollectionExpectSuccess({ mode: 'ReFungible' });
+ const collectionId = await createCollectionExpectSuccess({ mode: {type: 'ReFungible', decimalPoints: 0} });
await setCollectionSponsorExpectSuccess(collectionId, bob.address);
});
tests/src/setPublicAccessMode.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/setPublicAccessMode.test.ts
@@ -0,0 +1,89 @@
+// https://unique-network.readthedocs.io/en/latest/jsapi.html#setschemaversion
+import { ApiPromise, Keyring } from '@polkadot/api';
+import { IKeyringPair } from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import usingApi, {submitTransactionExpectFailAsync} from './substrate/substrate-api';
+import {
+ addToWhiteListExpectSuccess,
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+ enablePublicMintingExpectSuccess,
+ enableWhiteListExpectSuccess,
+} from './util/helpers';
+import { utf16ToStr } from './util/util';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let Alice: IKeyringPair;
+let Bob: IKeyringPair;
+
+describe('Integration Test setPublicAccessMode(): ', () => {
+ before(async () => {
+ await usingApi(async (api) => {
+ Alice = privateKey('//Alice');
+ Bob = privateKey('//Bob');
+ });
+ });
+
+ it('Run extrinsic with collection id parameters, set the whitelist mode for the collection', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const collectionId: number = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await enablePublicMintingExpectSuccess(Alice, collectionId);
+ await addToWhiteListExpectSuccess(Alice, collectionId, Bob.address);
+ await createItemExpectSuccess(Bob, collectionId, 'NFT', Bob.address);
+ });
+ });
+
+ it('Whitelisted collection limits', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const collectionId = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await enablePublicMintingExpectSuccess(Alice, collectionId);
+ const tx = api.tx.nft.createItem(collectionId, Bob.address, 'NFT');
+ await expect(submitTransactionExpectFailAsync(Bob, tx)).to.be.rejected;
+ });
+ });
+});
+
+describe('Negative Integration Test ext. setPublicAccessMode(): ', () => {
+ it('Set a non-existent collection', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ // tslint:disable-next-line: radix
+ const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;
+ const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');
+ await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+ });
+ });
+
+ it('Set the collection that has been deleted', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = await createCollectionExpectSuccess();
+ await destroyCollectionExpectSuccess(collectionId);
+ const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');
+ await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
+ });
+ });
+
+ it('Re-set the list mode already set in quantity', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const collectionId: number = await createCollectionExpectSuccess();
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ await enableWhiteListExpectSuccess(Alice, collectionId);
+ });
+ });
+
+ it('Execute method not on behalf of the collection owner', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ // tslint:disable-next-line: no-bitwise
+ const collectionId = await createCollectionExpectSuccess();
+ const tx = api.tx.nft.setPublicAccessMode(collectionId, 'WhiteList');
+ await expect(submitTransactionExpectFailAsync(Bob, tx)).to.be.rejected;
+ });
+ });
+});
tests/src/setSchemaVersion.test.tsdiffbeforeafterboth--- a/tests/src/setSchemaVersion.test.ts
+++ b/tests/src/setSchemaVersion.test.ts
@@ -35,7 +35,7 @@
});
it('choose or create collection for testing', async () => {
await usingApi(async () => {
- collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+ collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'NFT'}});
});
});
});
@@ -93,12 +93,6 @@
const nonExistedCollectionId = collectionCount + 1;
tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');
await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;
- /*try {
- await submitTransactionAsync(alice, tx);
- } catch (e) {
- // tslint:disable-next-line:no-unused-expression
- expect(e).to.be.exist;
- }*/
});
});
@@ -119,13 +113,6 @@
await destroyCollectionExpectSuccess(collectionIdForTesting);
tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');
await expect(submitTransactionExpectFailAsync(alice, tx)).to.be.rejected;
- /*try {
- tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');
- await submitTransactionAsync(alice, tx);
- } catch (e) {
- // tslint:disable-next-line:no-unused-expression
- expect(e).to.be.exist;
- }*/
});
});
});
tests/src/substrate/get-balance.tsdiffbeforeafterboth--- a/tests/src/substrate/get-balance.ts
+++ b/tests/src/substrate/get-balance.ts
@@ -3,12 +3,12 @@
// file 'LICENSE', which is part of this source code package.
//
-import { ApiPromise } from "@polkadot/api";
-import promisifySubstrate from "./promisify-substrate";
-import {AccountInfo} from "@polkadot/types/interfaces/system";
+import { ApiPromise } from '@polkadot/api';
+import {AccountInfo} from '@polkadot/types/interfaces/system';
+import promisifySubstrate from './promisify-substrate';
-export default async function getBalance(api: ApiPromise, accounts: string[]): Promise<bigint[]> {
- const balance = promisifySubstrate(api, (accounts: string[]) => api.query.system.account.multi(accounts));
+export default async function getBalance(api: ApiPromise, accounts: string[]): Promise<Array<bigint>> {
+ const balance = promisifySubstrate(api, (acc: string[]) => api.query.system.account.multi(acc));
const responce = await balance(accounts) as unknown as AccountInfo[];
- return responce.map(r => r.data.free.toBigInt().valueOf());
+ return responce.map((r) => r.data.free.toBigInt().valueOf());
}
tests/src/substrate/substrate-api.tsdiffbeforeafterboth--- a/tests/src/substrate/substrate-api.ts
+++ b/tests/src/substrate/substrate-api.ts
@@ -58,21 +58,22 @@
return TransactionStatus.Fail;
}
-export function submitTransactionAsync(sender: IKeyringPair, transaction: SubmittableExtrinsic<ApiTypes>): Promise<EventRecord[]> {
- return new Promise(async function(resolve, reject) {
+export function
+submitTransactionAsync(sender: IKeyringPair, transaction: SubmittableExtrinsic<ApiTypes>): Promise<EventRecord[]> {
+ return new Promise(async (resolve, reject) => {
try {
await transaction.signAndSend(sender, ({ events = [], status }) => {
const transactionStatus = getTransactionStatus(events, status);
- if (transactionStatus == TransactionStatus.Success) {
+ if (transactionStatus === TransactionStatus.Success) {
resolve(events);
- } else if (transactionStatus == TransactionStatus.Fail) {
+ } else if (transactionStatus === TransactionStatus.Fail) {
console.log(`Something went wrong with transaction. Status: ${status}`);
reject(events);
}
});
} catch (e) {
- console.log("Error: ", e);
+ console.log('Error: ', e);
reject(e);
}
});
@@ -90,7 +91,6 @@
res(rec);
console.error = consoleError;
console.log = consoleLog;
-
});
};
const reject = (errror: any) => {
@@ -104,6 +104,8 @@
await transaction.signAndSend(sender, ({ events = [], status }) => {
const transactionStatus = getTransactionStatus(events, status);
+ console.log('transactionStatus', transactionStatus, 'events', events);
+
if (transactionStatus == TransactionStatus.Success) {
resolve(events);
} else if (transactionStatus == TransactionStatus.Fail) {
@@ -114,4 +116,4 @@
reject(e);
}
});
-}
\ No newline at end of file
+}
tests/src/toggleContractWhiteList.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/toggleContractWhiteList.test.ts
@@ -0,0 +1,151 @@
+import chai from "chai";
+import chaiAsPromised from 'chai-as-promised';
+import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";
+import privateKey from "./substrate/privateKey";
+import {
+ deployFlipper,
+ getFlipValue
+} from "./util/contracthelpers";
+import {
+ getGenericResult
+} from "./util/helpers"
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+const value = 0;
+const gasLimit = 3000n * 1000000n;
+
+describe('Integration Test toggleContractWhiteList', () => {
+
+ it(`Enable white list contract mode`, async () => {
+ await usingApi(async api => {
+ const [contract, deployer] = await deployFlipper(api);
+
+ const enabledBefore = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+ const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
+ const enableEvents = await submitTransactionAsync(deployer, enableWhiteListTx);
+ const enabled = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+
+ expect(getGenericResult(enableEvents).success).to.be.true;
+ expect(enabledBefore).to.be.false;
+ expect(enabled).to.be.true;
+ });
+ });
+
+ it(`Only whitelisted account can call contract`, async () => {
+ await usingApi(async api => {
+ const bob = privateKey("//Bob");
+
+ const [contract, deployer] = await deployFlipper(api);
+
+ let flipValueBefore = await getFlipValue(contract, deployer);
+ const flip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, flip);
+ const flipValueAfter = await getFlipValue(contract,deployer);
+ expect(flipValueAfter).to.be.eq(!flipValueBefore, `Anyone can call new contract.`);
+
+ const deployerCanFlip = async () => {
+ let flipValueBefore = await getFlipValue(contract, deployer);
+ const deployerFlip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(deployer, deployerFlip);
+ const aliceFlip1Response = await getFlipValue(contract, deployer);
+ expect(aliceFlip1Response).to.be.eq(!flipValueBefore, `Deployer always can flip.`);
+ };
+ await deployerCanFlip();
+
+ flipValueBefore = await getFlipValue(contract, deployer);
+ const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
+ const enableResult = await submitTransactionAsync(deployer, enableWhiteListTx);
+ const flipWithEnabledWhiteList = contract.exec('flip', value, gasLimit);
+ await expect(submitTransactionExpectFailAsync(bob, flipWithEnabledWhiteList)).to.be.rejected;
+ const flipValueAfterEnableWhiteList = await getFlipValue(contract, deployer);
+ expect(flipValueAfterEnableWhiteList).to.be.eq(flipValueBefore, `Enabling whitelist doesn't make it possible to call contract for everyone.`);
+
+ await deployerCanFlip();
+
+ flipValueBefore = await getFlipValue(contract, deployer);
+ const addBobToWhiteListTx = api.tx.nft.addToContractWhiteList(contract.address, bob.address);
+ const addBobResult = await submitTransactionAsync(deployer, addBobToWhiteListTx);
+ const flipWithWhitelistedBob = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, flipWithWhitelistedBob);
+ const flipAfterWhiteListed = await getFlipValue(contract,deployer);
+ expect(flipAfterWhiteListed).to.be.eq(!flipValueBefore, `Bob was whitelisted, now he can flip.`);
+
+ await deployerCanFlip();
+
+ flipValueBefore = await getFlipValue(contract, deployer);
+ const removeBobFromWhiteListTx = api.tx.nft.removeFromContractWhiteList(contract.address, bob.address);
+ const removeBobResult = await submitTransactionAsync(deployer, removeBobFromWhiteListTx);
+ const bobRemoved = contract.exec('flip', value, gasLimit);
+ await expect(submitTransactionExpectFailAsync(bob, bobRemoved)).to.be.rejected;
+ const afterBobRemoved = await getFlipValue(contract, deployer);
+ expect(afterBobRemoved).to.be.eq(flipValueBefore, `Bob can't call contract, now when he is removeed from white list.`);
+
+ await deployerCanFlip();
+
+ flipValueBefore = await getFlipValue(contract, deployer);
+ const disableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, false);
+ const disableWhiteListResult = await submitTransactionAsync(deployer, disableWhiteListTx);
+ const whiteListDisabledFlip = contract.exec('flip', value, gasLimit);
+ await submitTransactionAsync(bob, whiteListDisabledFlip);
+ const afterWhiteListDisabled = await getFlipValue(contract,deployer);
+ expect(afterWhiteListDisabled).to.be.eq(!flipValueBefore, `Anyone can call contract with disabled whitelist.`);
+
+ });
+ });
+
+ it(`Enabling white list repeatedly should not produce errors`, async () => {
+ await usingApi(async api => {
+ const [contract, deployer] = await deployFlipper(api);
+
+ const enabledBefore = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+ const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
+ const enableEvents = await submitTransactionAsync(deployer, enableWhiteListTx);
+ const enabled = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+ const enableAgainEvents = await submitTransactionAsync(deployer, enableWhiteListTx);
+ const enabledAgain = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+
+ expect(getGenericResult(enableEvents).success).to.be.true;
+ expect(enabledBefore).to.be.false;
+ expect(enabled).to.be.true;
+ expect(getGenericResult(enableAgainEvents).success).to.be.true;
+ expect(enabledAgain).to.be.true;
+ });
+ });
+
+});
+
+describe('Negative Integration Test toggleContractWhiteList', () => {
+
+ it(`Enable white list for a non-contract`, async () => {
+ await usingApi(async api => {
+ const alice = privateKey("//Alice");
+ const bobGuineaPig = privateKey("//Bob");
+
+ const enabledBefore = (await api.query.nft.contractWhiteListEnabled(bobGuineaPig.address)).toJSON();
+ const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(bobGuineaPig.address, true);
+ await expect(submitTransactionExpectFailAsync(alice, enableWhiteListTx)).to.be.rejected;
+ const enabled = (await api.query.nft.contractWhiteListEnabled(bobGuineaPig.address)).toJSON();
+
+ expect(enabledBefore).to.be.false;
+ expect(enabled).to.be.false;
+ });
+ });
+
+ it(`Enable white list using a non-owner address`, async () => {
+ await usingApi(async api => {
+ const bob = privateKey("//Bob");
+ const [contract, deployer] = await deployFlipper(api);
+
+ const enabledBefore = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+ const enableWhiteListTx = api.tx.nft.toggleContractWhiteList(contract.address, true);
+ await expect(submitTransactionExpectFailAsync(bob, enableWhiteListTx)).to.be.rejected;
+ const enabled = (await api.query.nft.contractWhiteListEnabled(contract.address)).toJSON();
+
+ expect(enabledBefore).to.be.false;
+ expect(enabled).to.be.false;
+ });
+ });
+
+});
tests/src/transfer.test.tsdiffbeforeafterboth--- a/tests/src/transfer.test.ts
+++ b/tests/src/transfer.test.ts
@@ -3,49 +3,174 @@
// file 'LICENSE', which is part of this source code package.
//
-import { expect, assert } from "chai";
-import { default as usingApi, submitTransactionAsync } from "./substrate/substrate-api";
-import { alicesPublicKey, bobsPublicKey, ferdiesPublicKey } from "./accounts";
-import privateKey from "./substrate/privateKey";
-import getBalance from "./substrate/get-balance";
-import { BigNumber } from 'bignumber.js';
-import { findUnusedAddress } from './util/helpers'
+import { ApiPromise } from '@polkadot/api';
+import { IKeyringPair } from '@polkadot/types/types';
+import { expect } from 'chai';
+import { alicesPublicKey, bobsPublicKey } from './accounts';
+import getBalance from './substrate/get-balance';
+import privateKey from './substrate/privateKey';
+import { default as usingApi, submitTransactionAsync } from './substrate/substrate-api';
+import {
+ burnItemExpectSuccess, createCollectionExpectSuccess, createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+ findUnusedAddress,
+ getCreateCollectionResult,
+ getCreateItemResult,
+ transferExpectFail,
+ transferExpectSuccess,
+} from './util/helpers';
-describe('Transfer', () => {
- it('Balance transfers', async () => {
- await usingApi(async api => {
+let Alice: IKeyringPair;
+let Bob: IKeyringPair;
+let Charlie: IKeyringPair;
+
+describe('Integration Test Transfer(recipient, collection_id, item_id, value)', () => {
+ it('Balance transfers and check balance', async () => {
+ await usingApi(async (api: ApiPromise) => {
const [alicesBalanceBefore, bobsBalanceBefore] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);
const alicePrivateKey = privateKey('//Alice');
-
+
const transfer = api.tx.balances.transfer(bobsPublicKey, 1n);
- const result = await submitTransactionAsync(alicePrivateKey, transfer);
+ const events = await submitTransactionAsync(alicePrivateKey, transfer);
+ const result = getCreateItemResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
const [alicesBalanceAfter, bobsBalanceAfter] = await getBalance(api, [alicesPublicKey, bobsPublicKey]);
+ // tslint:disable-next-line:no-unused-expression
expect(alicesBalanceAfter < alicesBalanceBefore).to.be.true;
+ // tslint:disable-next-line:no-unused-expression
expect(bobsBalanceAfter > bobsBalanceBefore).to.be.true;
});
});
it('Inability to pay fees error message is correct', async () => {
- await usingApi(async api => {
+ await usingApi(async (api) => {
// Find unused address
const pk = await findUnusedAddress(api);
- const error = console.error;
- const log = console.log;
- console.log = function () {};
- console.error = function () {};
-
const badTransfer = api.tx.balances.transfer(bobsPublicKey, 1n);
- const badTransaction = async function () {
- const result = await submitTransactionAsync(pk, badTransfer);
+ // const events = await submitTransactionAsync(pk, badTransfer);
+ const badTransaction = async () => {
+ const events = await submitTransactionAsync(pk, badTransfer);
+ const result = getCreateCollectionResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.false;
};
- await expect(badTransaction()).to.be.rejectedWith("Inability to pay some fees");
+ expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees , e.g. account balance too low');
+ });
+ });
- console.log = log;
- console.error = error;
+ it('Create collection, balance transfers and check balance', async () => {
+ await usingApi(async (api) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await transferExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1, 'Fungible');
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await transferExpectSuccess(reFungibleCollectionId,
+ newReFungibleTokenId, Alice, Bob, 1, 'ReFungible');
});
});
});
+
+describe('Negative Integration Test Transfer(recipient, collection_id, item_id, value)', () => {
+ before(async () => {
+ await usingApi(async (api: ApiPromise) => {
+ Alice = privateKey('//Alice');
+ Bob = privateKey('//Bob');
+ Charlie = privateKey('//Charlie');
+ });
+ });
+ it('Transfer with not existed collection_id', async () => {
+ await usingApi(async (api) => {
+ // nft
+ const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await transferExpectFail(nftCollectionCount + 1, 1, Alice, Bob, 1);
+ // fungible
+ const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await transferExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob, 1);
+ // reFungible
+ const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await transferExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob, 1);
+ });
+ });
+ it('Transfer with deleted collection_id', async () => {
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await destroyCollectionExpectSuccess(nftCollectionId);
+ await transferExpectFail(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await destroyCollectionExpectSuccess(fungibleCollectionId);
+ await transferExpectFail(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1, 'Fungible');
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await destroyCollectionExpectSuccess(reFungibleCollectionId);
+ await transferExpectFail(reFungibleCollectionId,
+ newReFungibleTokenId, Alice, Bob, 1, 'ReFungible');
+ });
+ it('Transfer with not existed item_id', async () => {
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ await transferExpectFail(nftCollectionId, 2, Alice, Bob, 1, 'NFT');
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ await transferExpectFail(fungibleCollectionId, 2, Alice, Bob, 1, 'Fungible');
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ await transferExpectFail(reFungibleCollectionId,
+ 2, Alice, Bob, 1, 'ReFungible');
+ });
+ it('Transfer with deleted item_id', async () => {
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await burnItemExpectSuccess(Alice, nftCollectionId, newNftTokenId, 1);
+ await transferExpectFail(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await burnItemExpectSuccess(Alice, fungibleCollectionId, newFungibleTokenId, 10);
+ await transferExpectFail(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1, 'Fungible');
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+ await transferExpectFail(reFungibleCollectionId,
+ newReFungibleTokenId, Alice, Bob, 1, 'ReFungible');
+ });
+ it('Transfer with recipient that is not owner', async () => {
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await transferExpectFail(nftCollectionId, newNftTokenId, Charlie, Bob, 1, 'NFT');
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await transferExpectFail(fungibleCollectionId, newFungibleTokenId, Charlie, Bob, 1, 'Fungible');
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await transferExpectFail(reFungibleCollectionId,
+ newReFungibleTokenId, Charlie, Bob, 1, 'ReFungible');
+ });
+});
tests/src/transferFrom.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/transferFrom.test.ts
@@ -0,0 +1,188 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+import { ApiPromise } from '@polkadot/api';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import { default as usingApi } from './substrate/substrate-api';
+import {
+ approveExpectFail,
+ approveExpectSuccess,
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+ transferFromExpectFail,
+ transferFromExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {
+ it('Execute the extrinsic and check nftItemList - owner of token', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const Charlie = privateKey('//CHARLIE');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+
+ await transferFromExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1, 'NFT');
+
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+ await transferFromExpectSuccess(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1, 'Fungible');
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+ await transferFromExpectSuccess(reFungibleCollectionId,
+ newReFungibleTokenId, Bob, Alice, Charlie, 1, 'ReFungible');
+ });
+ });
+});
+
+describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {
+ it('transferFrom for a collection that does not exist', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const Charlie = privateKey('//CHARLIE');
+ // nft
+ const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);
+
+ await transferFromExpectFail(nftCollectionCount + 1, 1, Bob, Alice, Charlie, 1);
+
+ // fungible
+ const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);
+
+ await transferFromExpectFail(fungibleCollectionCount + 1, 1, Bob, Alice, Charlie, 1);
+ // reFungible
+ const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;
+ await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);
+
+ await transferFromExpectFail(reFungibleCollectionCount + 1, 1, Bob, Alice, Charlie, 1);
+ });
+ });
+
+ /* it('transferFrom for a collection that was destroyed', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ this test copies approve negative test
+ });
+ }); */
+
+ /* it('transferFrom a token that does not exist', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ this test copies approve negative test
+ });
+ }); */
+
+ /* it('transferFrom a token that was deleted', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ this test copies approve negative test
+ });
+ }); */
+
+ it('transferFrom for not approved address', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const Charlie = privateKey('//CHARLIE');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+
+ await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1);
+
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1);
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await transferFromExpectFail(reFungibleCollectionId,
+ newReFungibleTokenId, Bob, Alice, Charlie, 1);
+ });
+ });
+
+ it('transferFrom incorrect token count', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const Charlie = privateKey('//CHARLIE');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
+
+ await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 2);
+
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+ await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 2);
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
+ await transferFromExpectFail(reFungibleCollectionId,
+ newReFungibleTokenId, Bob, Alice, Charlie, 2);
+ });
+ });
+
+ it('execute transferFrom from account that is not owner of collection', async () => {
+ await usingApi(async (api: ApiPromise) => {
+ const Alice = privateKey('//Alice');
+ const Bob = privateKey('//Bob');
+ const Charlie = privateKey('//CHARLIE');
+ const Dave = privateKey('//DAVE');
+ // nft
+ const nftCollectionId = await createCollectionExpectSuccess();
+ const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
+ try {
+ await approveExpectFail(nftCollectionId, newNftTokenId, Dave, Bob);
+ await transferFromExpectFail(nftCollectionId, newNftTokenId, Dave, Alice, Charlie, 1);
+ } catch (e) {
+ // tslint:disable-next-line:no-unused-expression
+ expect(e).to.be.exist;
+ }
+
+ // await transferFromExpectFail(nftCollectionId, newNftTokenId, Dave, Alice, Charlie, 1);
+
+ // fungible
+ const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
+ const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
+ try {
+ await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Dave, Bob);
+ await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Dave, Alice, Charlie, 1);
+ } catch (e) {
+ // tslint:disable-next-line:no-unused-expression
+ expect(e).to.be.exist;
+ }
+ // reFungible
+ const reFungibleCollectionId = await
+ createCollectionExpectSuccess({mode: {type: 'ReFungible', decimalPoints: 0}});
+ const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
+ try {
+ await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Dave, Bob);
+ await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, Dave, Alice, Charlie, 1);
+ } catch (e) {
+ // tslint:disable-next-line:no-unused-expression
+ expect(e).to.be.exist;
+ }
+ });
+ });
+});
tests/src/util/contracthelpers.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/util/contracthelpers.ts
@@ -0,0 +1,94 @@
+//
+// This file is subject to the terms and conditions defined in
+// file 'LICENSE', which is part of this source code package.
+//
+
+import chai from "chai";
+import chaiAsPromised from 'chai-as-promised';
+import { submitTransactionAsync } from "../substrate/substrate-api";
+import fs from "fs";
+import { Abi, BlueprintPromise as Blueprint, CodePromise, ContractPromise as Contract } from "@polkadot/api-contract";
+import { IKeyringPair } from "@polkadot/types/types";
+import { ApiPromise, Keyring } from "@polkadot/api";
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+import { BigNumber } from 'bignumber.js';
+import { findUnusedAddress } from '../util/helpers';
+
+const value = 0;
+const gasLimit = 3000n * 1000000n;
+const endowment = `1000000000000000`;
+
+function deployBlueprint(alice: IKeyringPair, code: CodePromise): Promise<Blueprint> {
+ return new Promise<Blueprint>(async (resolve, reject) => {
+ const unsub = await code
+ .createBlueprint()
+ .signAndSend(alice, (result) => {
+ if (result.status.isInBlock || result.status.isFinalized) {
+ // here we have an additional field in the result, containing the blueprint
+ resolve(result.blueprint);
+ unsub();
+ }
+ })
+ });
+}
+
+function deployContract(alice: IKeyringPair, blueprint: Blueprint) : Promise<any> {
+ return new Promise<any>(async (resolve, reject) => {
+ const endowment = 1000000000000000n;
+ const initValue = true;
+
+ const unsub = await blueprint.tx
+ .new(endowment, gasLimit, initValue)
+ .signAndSend(alice, (result) => {
+ if (result.status.isInBlock || result.status.isFinalized) {
+ unsub();
+ resolve(result);
+ }
+ });
+ });
+}
+
+async function prepareDeployer(api: ApiPromise) {
+ // Find unused address
+ const deployer = await findUnusedAddress(api);
+
+ // Transfer balance to it
+ const keyring = new Keyring({ type: 'sr25519' });
+ const alice = keyring.addFromUri(`//Alice`);
+ let amount = new BigNumber(endowment);
+ amount = amount.plus(1e15);
+ const tx = api.tx.balances.transfer(deployer.address, amount.toFixed());
+ await submitTransactionAsync(alice, tx);
+
+ return deployer;
+}
+
+export async function deployFlipper(api: ApiPromise): Promise<[Contract, IKeyringPair]> {
+ const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));
+ const abi = new Abi(metadata);
+
+ const deployer = await prepareDeployer(api);
+
+ const wasm = fs.readFileSync('./src/flipper/flipper.wasm');
+
+ const code = new CodePromise(api, abi, wasm);
+
+ const blueprint = await deployBlueprint(deployer, code);
+ const contract = (await deployContract(deployer, blueprint))['contract'] as Contract;
+
+ const initialGetResponse = await getFlipValue(contract, deployer);
+ expect(initialGetResponse).to.be.true;
+
+ return [contract, deployer];
+}
+
+export async function getFlipValue(contract: Contract, deployer: IKeyringPair) {
+ const result = await contract.query.get(deployer.address, value, gasLimit);
+
+ if(!result.result.isSuccess) {
+ throw `Failed to get flipper value`;
+ }
+ return (result.result.asSuccess.data[0] == 0x00) ? false : true;
+}
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -3,20 +3,20 @@
// file 'LICENSE', which is part of this source code package.
//
+import { ApiPromise, Keyring } from '@polkadot/api';
+import { Enum, Struct } from '@polkadot/types/codec';
+import type { AccountId, EventRecord } from '@polkadot/types/interfaces';
+import { u128 } from '@polkadot/types/primitive';
+import { IKeyringPair } from '@polkadot/types/types';
+import { BigNumber } from 'bignumber.js';
+import BN from 'bn.js';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
-import type { AccountId, EventRecord } from '@polkadot/types/interfaces';
-import { ApiPromise, Keyring } from "@polkadot/api";
-import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";
+import { alicesPublicKey, nullPublicKey } from '../accounts';
import privateKey from '../substrate/privateKey';
-import { alicesPublicKey, nullPublicKey } from "../accounts";
-import { strToUTF16, utf16ToStr, hexToStr } from './util';
-import { IKeyringPair } from '@polkadot/types/types';
-import { BigNumber } from 'bignumber.js';
-import { Struct, Enum } from '@polkadot/types/codec';
-import { u128 } from '@polkadot/types/primitive';
+import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from '../substrate/substrate-api';
import { ICollectionInterface } from '../types';
-import BN from "bn.js";
+import { hexToStr, strToUTF16, utf16ToStr } from './util';
chai.use(chaiAsPromised);
const expect = chai.expect;
@@ -25,24 +25,45 @@
success: boolean,
};
-type CreateCollectionResult = {
- success: boolean,
- collectionId: number
-};
+interface CreateCollectionResult {
+ success: boolean;
+ collectionId: number;
+}
+
+interface CreateItemResult {
+ success: boolean;
+ collectionId: number;
+ itemId: number;
+}
+
+interface IReFungibleOwner {
+ Fraction: BN;
+ Owner: number[];
+}
+
+interface ITokenDataType {
+ Owner: number[];
+ ConstData: number[];
+ VariableData: number[];
+}
+
+interface IFungibleTokenDataType {
+ Value: BN;
+}
-type CreateItemResult = {
- success: boolean,
- collectionId: number,
- itemId: number
-};
+interface IReFungibleTokenDataType {
+ Owner: IReFungibleOwner[];
+ ConstData: number[];
+ VariableData: number[];
+}
export function getGenericResult(events: EventRecord[]): GenericResult {
- let result: GenericResult = {
- success: false
- }
+ const result: GenericResult = {
+ success: false,
+ };
events.forEach(({ phase, event: { data, method, section } }) => {
// console.log(` ${phase}: ${section}.${method}:: ${data}`);
- if (method == 'ExtrinsicSuccess') {
+ if (method === 'ExtrinsicSuccess') {
result.success = true;
}
});
@@ -60,10 +81,10 @@
collectionId = parseInt(data[0].toString());
}
});
- let result: CreateCollectionResult = {
+ const result: CreateCollectionResult = {
success,
- collectionId
- }
+ collectionId,
+ };
return result;
}
@@ -80,27 +101,60 @@
itemId = parseInt(data[1].toString());
}
});
- let result: CreateItemResult = {
+ const result: CreateItemResult = {
success,
collectionId,
- itemId
- }
+ itemId,
+ };
return result;
}
-export type CollectionMode = 'NFT' | 'Fungible' | 'ReFungible';
+interface Invalid {
+ type: 'Invalid';
+}
+
+interface Nft {
+ type: 'NFT';
+}
+
+interface Fungible {
+ type: 'Fungible';
+ decimalPoints: number;
+}
+
+interface ReFungible {
+ type: 'ReFungible';
+ decimalPoints: number;
+}
+
+interface Nft {
+ type: 'NFT'
+}
+
+interface Fungible {
+ type: 'Fungible',
+ decimalPoints: number
+}
+
+interface ReFungible {
+ type: 'ReFungible',
+ decimalPoints: number
+}
+
+type CollectionMode = Nft | Fungible | ReFungible | Invalid;
+
export type CreateCollectionParams = {
mode: CollectionMode,
name: string,
description: string,
- tokenPrefix: string
+ tokenPrefix: string,
};
const defaultCreateCollectionParams: CreateCollectionParams = {
+ description: 'description',
+ mode: { type: 'NFT' },
name: 'name',
- description: 'description',
- mode: 'NFT',
- tokenPrefix: 'prefix'
+ tokenPrefix: 'prefix',
}
export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {
@@ -109,25 +163,39 @@
let collectionId: number = 0;
await usingApi(async (api) => {
// Get number of collections before the transaction
- const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
+ const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);
// Run the CreateCollection transaction
const alicePrivateKey = privateKey('//Alice');
- const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);
+
+ let modeprm = {};
+ if (mode.type === 'NFT') {
+ modeprm = {nft: null};
+ } else if (mode.type === 'Fungible') {
+ modeprm = {fungible: mode.decimalPoints};
+ } else if (mode.type === 'ReFungible') {
+ modeprm = {refungible: mode.decimalPoints};
+ } else if (mode.type === 'Invalid') {
+ modeprm = {invalid: null};
+ }
+
+ const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);
const events = await submitTransactionAsync(alicePrivateKey, tx);
const result = getCreateCollectionResult(events);
// Get number of collections after the transaction
- const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
+ const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();
// What to expect
+ // tslint:disable-next-line:no-unused-expression
expect(result.success).to.be.true;
expect(result.collectionId).to.be.equal(BcollectionCount);
+ // tslint:disable-next-line:no-unused-expression
expect(collection).to.be.not.null;
- expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');
+ expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');
expect(collection.Owner).to.be.equal(alicesPublicKey);
expect(utf16ToStr(collection.Name)).to.be.equal(name);
expect(utf16ToStr(collection.Description)).to.be.equal(description);
@@ -138,17 +206,28 @@
return collectionId;
}
-
+
export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {
const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};
+ let modeprm = {};
+ if (mode.type === 'NFT') {
+ modeprm = {nft: null};
+ } else if (mode.type === 'Fungible') {
+ modeprm = {fungible: mode.decimalPoints};
+ } else if (mode.type === 'ReFungible') {
+ modeprm = {refungible: mode.decimalPoints};
+ } else if (mode.type === 'Invalid') {
+ modeprm = {invalid: null};
+ }
+
await usingApi(async (api) => {
// Get number of collections before the transaction
const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
// Run the CreateCollection transaction
const alicePrivateKey = privateKey('//Alice');
- const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);
+ const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);
const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;
const result = getCreateCollectionResult(events);
@@ -156,11 +235,12 @@
const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());
// What to expect
+ // tslint:disable-next-line:no-unused-expression
expect(result.success).to.be.false;
expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');
});
}
-
+
export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {
let bal = new BigNumber(0);
let unused;
@@ -170,7 +250,7 @@
unused = keyring.addFromUri(`//${randomSeed}`);
bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());
} while (bal.toFixed() != '0');
- return unused;
+ return unused;
}
function getDestroyResult(events: EventRecord[]): boolean {
@@ -201,7 +281,7 @@
const events = await submitTransactionAsync(alicePrivateKey, tx);
const result = getDestroyResult(events);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
// What to expect
@@ -220,7 +300,7 @@
const events = await submitTransactionAsync(alicePrivateKey, tx);
const result = getGenericResult(events);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
// What to expect
@@ -239,7 +319,7 @@
const events = await submitTransactionAsync(alicePrivateKey, tx);
const result = getGenericResult(events);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
// What to expect
@@ -278,7 +358,7 @@
const events = await submitTransactionAsync(sender, tx);
const result = getGenericResult(events);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
// What to expect
@@ -300,48 +380,200 @@
export interface CreateFungibleData extends Struct {
readonly value: u128;
-};
+}
-export interface CreateReFungibleData extends Struct {};
-export interface CreateNftData extends Struct {};
+export interface CreateReFungibleData extends Struct {}
+export interface CreateNftData extends Struct {}
export interface CreateItemData extends Enum {
- NFT: CreateNftData,
- Fungible: CreateFungibleData,
- ReFungible: CreateReFungibleData
-};
+ NFT: CreateNftData;
+ Fungible: CreateFungibleData;
+ ReFungible: CreateReFungibleData;
+}
+
+export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, value);
+ const events = await submitTransactionAsync(owner, tx);
+ const result = getGenericResult(events);
+ // Get the item
+ const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();
+ // What to expect
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ // tslint:disable-next-line:no-unused-expression
+ expect(item).to.be.not.null;
+ expect(item.Owner).to.be.equal(nullPublicKey);
+ });
+}
+
+export async function
+approveExpectSuccess(collectionId: number,
+ tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {
+ await usingApi(async (api: ApiPromise) => {
+ const allowanceBefore =
+ await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;
+ const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);
+ const events = await submitTransactionAsync(owner, approveNftTx);
+ const result = getCreateItemResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ const allowanceAfter =
+ await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;
+ expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);
+ });
+}
+
+export async function
+transferFromExpectSuccess(collectionId: number,
+ tokenId: number,
+ accountApproved: IKeyringPair,
+ accountFrom: IKeyringPair,
+ accountTo: IKeyringPair,
+ value: number = 1,
+ type: string = 'NFT') {
+ await usingApi(async (api: ApiPromise) => {
+ let balanceBefore = new BN(0);
+ if (type === 'Fungible') {
+ balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;
+ }
+ const transferFromTx = await api.tx.nft.transferFrom(
+ accountFrom.address, accountTo.address, collectionId, tokenId, value);
+ const events = await submitTransactionAsync(accountFrom, transferFromTx);
+ const result = getCreateItemResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ if (type === 'NFT') {
+ const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;
+ expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);
+ }
+ if (type === 'Fungible') {
+ const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;
+ expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);
+ }
+ if (type === 'ReFungible') {
+ const nftItemData =
+ await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;
+ expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);
+ expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);
+ }
+ });
+}
+
+export async function
+transferFromExpectFail(collectionId: number,
+ tokenId: number,
+ accountApproved: IKeyringPair,
+ accountFrom: IKeyringPair,
+ accountTo: IKeyringPair,
+ value: number = 1) {
+ await usingApi(async (api: ApiPromise) => {
+ const transferFromTx = await api.tx.nft.transferFrom(
+ accountFrom.address, accountTo.address, collectionId, tokenId, value);
+ const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;
+ const result = getCreateCollectionResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.false;
+ });
+}
+
+export async function
+transferExpectSuccess(collectionId: number,
+ tokenId: number,
+ sender: IKeyringPair,
+ recipient: IKeyringPair,
+ value: number = 1,
+ type: string = 'NFT') {
+ await usingApi(async (api: ApiPromise) => {
+ let balanceBefore = new BN(0);
+ if (type === 'Fungible') {
+ balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;
+ }
+ const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);
+ const events = await submitTransactionAsync(sender, transferTx);
+ const result = getCreateItemResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.true;
+ if (type === 'NFT') {
+ const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;
+ expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);
+ }
+ if (type === 'Fungible') {
+ const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;
+ expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);
+ }
+ if (type === 'ReFungible') {
+ const nftItemData =
+ await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;
+ expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);
+ expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);
+ }
+ });
+}
+
+export async function
+transferExpectFail(collectionId: number,
+ tokenId: number,
+ sender: IKeyringPair,
+ recipient: IKeyringPair,
+ value: number = 1,
+ type: string = 'NFT') {
+ await usingApi(async (api: ApiPromise) => {
+ const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);
+ const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;
+ if (events && Array.isArray(events)) {
+ const result = getCreateCollectionResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.false;
+ }
+ });
+}
+
+export async function
+approveExpectFail(collectionId: number,
+ tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {
+ await usingApi(async (api: ApiPromise) => {
+ const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);
+ const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;
+ const result = getCreateCollectionResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.false;
+ });
+}
-export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {
+export async function createItemExpectSuccess(
+ sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {
let newItemId: number = 0;
await usingApi(async (api) => {
- const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());
- const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
+ const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);
+ const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
const AItemBalance = new BigNumber(Aitem.Value);
- if (owner === '') owner = sender.address;
+ if (owner === '') {
+ owner = sender.address;
+ }
let tx;
- if (createMode == 'Fungible') {
- let createData = {fungible: {value: 10}};
+ if (createMode === 'Fungible') {
+ const createData = {fungible: {value: 10}};
tx = api.tx.nft.createItem(collectionId, owner, createData);
- }
- else {
+ } else {
tx = api.tx.nft.createItem(collectionId, owner, createMode);
}
const events = await submitTransactionAsync(sender, tx);
const result = getCreateItemResult(events);
- const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());
- const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
+ const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);
+ const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();
const BItemBalance = new BigNumber(Bitem.Value);
// What to expect
+ // tslint:disable-next-line:no-unused-expression
expect(result.success).to.be.true;
- if (createMode == 'Fungible') {
+ if (createMode === 'Fungible') {
expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);
- }
- else {
- expect(BItemCount).to.be.equal(AItemCount+1);
+ } else {
+ expect(BItemCount).to.be.equal(AItemCount + 1);
}
expect(collectionId).to.be.equal(result.collectionId);
expect(BItemCount).to.be.equal(result.itemId);
@@ -358,10 +590,11 @@
const events = await submitTransactionAsync(sender, tx);
const result = getGenericResult(events);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
// What to expect
+ // tslint:disable-next-line:no-unused-expression
expect(result.success).to.be.true;
expect(collection.Access).to.be.equal('WhiteList');
});
@@ -375,7 +608,7 @@
const events = await submitTransactionAsync(sender, tx);
const result = getGenericResult(events);
- // Get the collection
+ // Get the collection
const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
// What to expect
@@ -387,17 +620,22 @@
export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {
await usingApi(async (api) => {
+ const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();
+
// Run the transaction
const tx = api.tx.nft.addToWhiteList(collectionId, address);
const events = await submitTransactionAsync(sender, tx);
const result = getGenericResult(events);
- // Get the collection
- const collection: any = (await api.query.nft.collection(collectionId)).toJSON();
+ const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();
// What to expect
+ // tslint:disable-next-line:no-unused-expression
expect(result.success).to.be.true;
- expect(collection.MintMode).to.be.equal(true);
+ // tslint:disable-next-line: no-unused-expression
+ expect(whiteListedBefore).to.be.false;
+ // tslint:disable-next-line: no-unused-expression
+ expect(whiteListedAfter).to.be.true;
});
}