difftreelog
Merge pull request #137 from usetech-llc/feature/NFTPAR-299_storage_option_refactor
in: master
Fix unit tests
3 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -1295,11 +1295,9 @@
Self::check_white_list(&target_collection, &spender)?;
}
- let allowance_exists = <Allowances<T>>::contains_key(collection_id, (item_id, &sender, &spender));
- let mut allowance: u128 = amount;
- if allowance_exists {
- allowance += <Allowances<T>>::get(collection_id, (item_id, &sender, &spender));
- }
+ let allowance: u128 = amount
+ .checked_add(<Allowances<T>>::get(collection_id, (item_id, &sender, &spender)))
+ .ok_or(Error::<T>::NumOverflow)?;
if let Some(limit) = allowance_limit {
ensure!(limit >= allowance, Error::<T>::TokenValueTooLow);
}
@@ -1334,23 +1332,16 @@
let sender = ensure_signed(origin)?;
let target_collection = Self::get_collection(collection_id)?;
-
- let mut appoved_transfer = false;
// Check approval
- let mut approval: u128 = 0;
- if <Allowances<T>>::contains_key(collection_id, (item_id, &from, &sender)) {
- approval = <Allowances<T>>::get(collection_id, (item_id, &from, &sender));
- ensure!(approval >= value, Error::<T>::TokenValueNotEnough);
- appoved_transfer = true;
- }
+ let approval: u128 = <Allowances<T>>::get(collection_id, (item_id, &from, &sender));
// Limits check
Self::is_correct_transfer(&target_collection, &recipient)?;
// Transfer permissions check
ensure!(
- appoved_transfer ||
+ approval >= value ||
(
target_collection.limits.owner_can_transfer &&
Self::is_owner_or_admin_permissions(&target_collection, sender.clone())
pallets/nft/src/mock.rsdiffbeforeafterboth1use crate as pallet_template;2use sp_core::H256;3use frame_support::{ 4 parameter_types,5 weights::IdentityFee,6};7use sp_runtime::{8 traits::{BlakeTwo256, IdentityLookup}, 9 testing::Header, 10 Perbill,11};12use pallet_transaction_payment::{ CurrencyAdapter};13use frame_system as system;1415type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;16type Block = frame_system::mocking::MockBlock<Test>; 1718// Configure a mock runtime to test the pallet.19frame_support::construct_runtime!(20 pub enum Test where21 Block = Block,22 NodeBlock = Block,23 UncheckedExtrinsic = UncheckedExtrinsic,24 {25 System: frame_system::{Module, Call, Config, Storage, Event<T>},26 TemplateModule: pallet_template::{Module, Call, Storage},27 }28);2930parameter_types! {31 pub const BlockHashCount: u64 = 250;32 pub const SS58Prefix: u8 = 42;33}3435impl system::Config for Test {36 type BaseCallFilter = ();37 type BlockWeights = ();38 type BlockLength = ();39 type DbWeight = ();40 type Origin = Origin;41 type Call = Call;42 type Index = u64;43 type BlockNumber = u64;44 type Hash = H256;45 type Hashing = BlakeTwo256;46 type AccountId = u64;47 type Lookup = IdentityLookup<Self::AccountId>;48 type Header = Header;49 type Event = ();50 type BlockHashCount = BlockHashCount;51 type Version = ();52 type PalletInfo = PalletInfo;53 type AccountData = pallet_balances::AccountData<u64>;54 type OnNewAccount = ();55 type OnKilledAccount = ();56 type SystemWeightInfo = ();57 type SS58Prefix = SS58Prefix;58}5960parameter_types! {61 pub const ExistentialDeposit: u64 = 1;62 pub const MaxLocks: u32 = 50;63}64//frame_system::Module<Test>;65impl pallet_balances::Config for Test {66 type AccountStore = System;67 type Balance = u64;68 type DustRemoval = ();69 type Event = ();70 type ExistentialDeposit = ExistentialDeposit;71 type WeightInfo = ();72 type MaxLocks = MaxLocks;73}7475parameter_types! {76 pub const TransactionByteFee: u64 = 1;77}7879impl pallet_transaction_payment::Config for Test {80 type OnChargeTransaction = CurrencyAdapter<pallet_balances::Module<Test>, ()>;81 type TransactionByteFee = TransactionByteFee;82 type WeightToFee = IdentityFee<u64>;83 type FeeMultiplierUpdate = ();84}8586parameter_types! {87 pub const MinimumPeriod: u64 = 1;88}89impl pallet_timestamp::Config for Test {90 type Moment = u64;91 type OnTimestampSet = ();92 type MinimumPeriod = MinimumPeriod;93 type WeightInfo = ();94}9596type Timestamp = pallet_timestamp::Module<Test>;97type Randomness = pallet_randomness_collective_flip::Module<Test>;9899parameter_types! {100 pub const TombstoneDeposit: u64 = 1;101 pub const DepositPerContract: u64 = 1;102 pub const DepositPerStorageByte: u64 = 1;103 pub const DepositPerStorageItem: u64 = 1;104 pub RentFraction: Perbill = Perbill::from_rational_approximation(1u32, 30 * 24 * 60 * 10);105 pub const SurchargeReward: u64 = 1;106 pub const SignedClaimHandicap: u32 = 2;107 pub const MaxDepth: u32 = 32;108 pub const MaxValueSize: u32 = 16 * 1024;109 pub DeletionWeightLimit: u64 = u64::MAX;//Perbill::from_percent(10);110 pub DeletionQueueDepth: u32 = 10;111}112113impl pallet_contracts::Config for Test {114 type Time = Timestamp;115 type Randomness = Randomness;116 type Currency = pallet_balances::Module<Test>;117 type Event = ();118 type RentPayment = ();119 type SignedClaimHandicap = SignedClaimHandicap;120 type TombstoneDeposit = TombstoneDeposit;121 type DepositPerContract = DepositPerContract;122 type DepositPerStorageByte = DepositPerStorageByte;123 type DepositPerStorageItem = DepositPerStorageItem;124 type RentFraction = RentFraction;125 type SurchargeReward = SurchargeReward;126 type DeletionWeightLimit = DeletionWeightLimit;127 type MaxDepth = MaxDepth;128 type DeletionQueueDepth = DeletionQueueDepth;129 type MaxValueSize = MaxValueSize;130 type ChainExtension = ();131 type MaxCodeSize = ();132 type WeightPrice = ();133 type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;134}135136parameter_types! {137 pub const CollectionCreationPrice: u64 = 1_000_000_000_000;138}139140impl pallet_template::Config for Test {141 type Event = ();142 type WeightInfo = ();143 type CollectionCreationPrice = CollectionCreationPrice;144}145146// Build genesis storage according to the mock runtime.147pub fn new_test_ext() -> sp_io::TestExternalities {148 system::GenesisConfig::default().build_storage::<Test>().unwrap().into()149}1use crate as pallet_template;2use sp_core::H256;3use frame_support::{ 4 parameter_types,5 weights::IdentityFee,6};7use sp_runtime::{8 traits::{BlakeTwo256, IdentityLookup}, 9 testing::Header, 10 Perbill,11};12use pallet_transaction_payment::{ CurrencyAdapter};13use frame_system as system;1415type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;16type Block = frame_system::mocking::MockBlock<Test>; 1718// Configure a mock runtime to test the pallet.19frame_support::construct_runtime!(20 pub enum Test where21 Block = Block,22 NodeBlock = Block,23 UncheckedExtrinsic = UncheckedExtrinsic,24 {25 System: frame_system::{Module, Call, Config, Storage, Event<T>},26 TemplateModule: pallet_template::{Module, Call, Storage},27 Balances: pallet_balances::{Module, Call, Storage},28 }29);3031parameter_types! {32 pub const BlockHashCount: u64 = 250;33 pub const SS58Prefix: u8 = 42;34}3536impl system::Config for Test {37 type BaseCallFilter = ();38 type BlockWeights = ();39 type BlockLength = ();40 type DbWeight = ();41 type Origin = Origin;42 type Call = Call;43 type Index = u64;44 type BlockNumber = u64;45 type Hash = H256;46 type Hashing = BlakeTwo256;47 type AccountId = u64;48 type Lookup = IdentityLookup<Self::AccountId>;49 type Header = Header;50 type Event = ();51 type BlockHashCount = BlockHashCount;52 type Version = ();53 type PalletInfo = PalletInfo;54 type AccountData = pallet_balances::AccountData<u64>;55 type OnNewAccount = ();56 type OnKilledAccount = ();57 type SystemWeightInfo = ();58 type SS58Prefix = SS58Prefix;59}6061parameter_types! {62 pub const ExistentialDeposit: u64 = 1;63 pub const MaxLocks: u32 = 50;64}65//frame_system::Module<Test>;66impl pallet_balances::Config for Test {67 type AccountStore = System;68 type Balance = u64;69 type DustRemoval = ();70 type Event = ();71 type ExistentialDeposit = ExistentialDeposit;72 type WeightInfo = ();73 type MaxLocks = MaxLocks;74}7576parameter_types! {77 pub const TransactionByteFee: u64 = 1;78}7980impl pallet_transaction_payment::Config for Test {81 type OnChargeTransaction = CurrencyAdapter<pallet_balances::Module<Test>, ()>;82 type TransactionByteFee = TransactionByteFee;83 type WeightToFee = IdentityFee<u64>;84 type FeeMultiplierUpdate = ();85}8687parameter_types! {88 pub const MinimumPeriod: u64 = 1;89}90impl pallet_timestamp::Config for Test {91 type Moment = u64;92 type OnTimestampSet = ();93 type MinimumPeriod = MinimumPeriod;94 type WeightInfo = ();95}9697type Timestamp = pallet_timestamp::Module<Test>;98type Randomness = pallet_randomness_collective_flip::Module<Test>;99100parameter_types! {101 pub const TombstoneDeposit: u64 = 1;102 pub const DepositPerContract: u64 = 1;103 pub const DepositPerStorageByte: u64 = 1;104 pub const DepositPerStorageItem: u64 = 1;105 pub RentFraction: Perbill = Perbill::from_rational_approximation(1u32, 30 * 24 * 60 * 10);106 pub const SurchargeReward: u64 = 1;107 pub const SignedClaimHandicap: u32 = 2;108 pub const MaxDepth: u32 = 32;109 pub const MaxValueSize: u32 = 16 * 1024;110 pub DeletionWeightLimit: u64 = u64::MAX;//Perbill::from_percent(10);111 pub DeletionQueueDepth: u32 = 10;112}113114impl pallet_contracts::Config for Test {115 type Time = Timestamp;116 type Randomness = Randomness;117 type Currency = pallet_balances::Module<Test>;118 type Event = ();119 type RentPayment = ();120 type SignedClaimHandicap = SignedClaimHandicap;121 type TombstoneDeposit = TombstoneDeposit;122 type DepositPerContract = DepositPerContract;123 type DepositPerStorageByte = DepositPerStorageByte;124 type DepositPerStorageItem = DepositPerStorageItem;125 type RentFraction = RentFraction;126 type SurchargeReward = SurchargeReward;127 type DeletionWeightLimit = DeletionWeightLimit;128 type MaxDepth = MaxDepth;129 type DeletionQueueDepth = DeletionQueueDepth;130 type MaxValueSize = MaxValueSize;131 type ChainExtension = ();132 type MaxCodeSize = ();133 type WeightPrice = ();134 type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;135}136137parameter_types! {138 pub const CollectionCreationPrice: u32 = 0;139 pub TreasuryAccountId: u64 = 1234;140}141142impl pallet_template::Config for Test {143 type Event = ();144 type WeightInfo = ();145 type CollectionCreationPrice = CollectionCreationPrice;146 type Currency = pallet_balances::Module<Test>;147 type TreasuryAccountId = TreasuryAccountId;148}149150// Build genesis storage according to the mock runtime.151pub fn new_test_ext() -> sp_io::TestExternalities {152 system::GenesisConfig::default().build_storage::<Test>().unwrap().into()153}pallets/nft/src/tests.rsdiffbeforeafterboth--- a/pallets/nft/src/tests.rs
+++ b/pallets/nft/src/tests.rs
@@ -121,8 +121,9 @@
let data = default_nft_data();
create_test_item(collection_id, &data.clone().into());
- assert_eq!(TemplateModule::nft_item_id(collection_id, 1).const_data, data.const_data);
- assert_eq!(TemplateModule::nft_item_id(collection_id, 1).variable_data, data.variable_data);
+ let item = TemplateModule::nft_item_id(collection_id, 1).unwrap();
+ assert_eq!(item.const_data, data.const_data);
+ assert_eq!(item.variable_data, data.variable_data);
});
}
@@ -146,8 +147,9 @@
items_data.clone().into_iter().map(|d| { d.into() }).collect()
));
for (index, data) in items_data.iter().enumerate() {
- assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).const_data.to_vec(), data.const_data);
- assert_eq!(TemplateModule::nft_item_id(1, (index + 1) as TokenId).variable_data.to_vec(), data.variable_data);
+ let item = TemplateModule::nft_item_id(1, (index + 1) as TokenId).unwrap();
+ assert_eq!(item.const_data.to_vec(), data.const_data);
+ assert_eq!(item.variable_data.to_vec(), data.variable_data);
}
});
}
@@ -160,16 +162,17 @@
let data = default_re_fungible_data();
create_test_item(collection_id, &data.clone().into());
+ let item = TemplateModule::refungible_item_id(collection_id, 1).unwrap();
assert_eq!(
- TemplateModule::refungible_item_id(collection_id, 1).const_data,
+ item.const_data,
data.const_data
);
assert_eq!(
- TemplateModule::refungible_item_id(collection_id, 1).variable_data,
+ item.variable_data,
data.variable_data
);
assert_eq!(
- TemplateModule::refungible_item_id(collection_id, 1).owner[0],
+ item.owner[0],
Ownership {
owner: 1,
fraction: 1023
@@ -197,7 +200,7 @@
));
for (index, data) in items_data.iter().enumerate() {
- let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId);
+ let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId).unwrap();
assert_eq!(item.const_data.to_vec(), data.const_data);
assert_eq!(item.variable_data.to_vec(), data.variable_data);
assert_eq!(
@@ -299,28 +302,31 @@
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
- assert_eq!(
- TemplateModule::refungible_item_id(collection_id, 1).const_data,
- data.const_data
- );
- assert_eq!(
- TemplateModule::refungible_item_id(collection_id, 1).variable_data,
- data.variable_data
- );
- assert_eq!(
- TemplateModule::refungible_item_id(collection_id, 1).owner[0],
- Ownership {
- owner: 1,
- fraction: 1023
- }
- );
+ {
+ let item = TemplateModule::refungible_item_id(collection_id, 1).unwrap();
+ assert_eq!(
+ item.const_data,
+ data.const_data
+ );
+ assert_eq!(
+ item.variable_data,
+ data.variable_data
+ );
+ assert_eq!(
+ item.owner[0],
+ Ownership {
+ owner: 1,
+ fraction: 1023
+ }
+ );
+ }
assert_eq!(TemplateModule::balance_count(1, 1), 1023);
assert_eq!(TemplateModule::address_tokens(1, 1), [1]);
// change owner scenario
assert_ok!(TemplateModule::transfer(origin1.clone(), 2, 1, 1, 1023));
assert_eq!(
- TemplateModule::refungible_item_id(1, 1).owner[0],
+ TemplateModule::refungible_item_id(1, 1).unwrap().owner[0],
Ownership {
owner: 2,
fraction: 1023
@@ -333,20 +339,23 @@
// split item scenario
assert_ok!(TemplateModule::transfer(origin2.clone(), 3, 1, 1, 500));
- assert_eq!(
- TemplateModule::refungible_item_id(1, 1).owner[0],
- Ownership {
- owner: 2,
- fraction: 523
- }
- );
- assert_eq!(
- TemplateModule::refungible_item_id(1, 1).owner[1],
- Ownership {
- owner: 3,
- fraction: 500
- }
- );
+ {
+ let item = TemplateModule::refungible_item_id(1, 1).unwrap();
+ assert_eq!(
+ item.owner[0],
+ Ownership {
+ owner: 2,
+ fraction: 523
+ }
+ );
+ assert_eq!(
+ item.owner[1],
+ Ownership {
+ owner: 3,
+ fraction: 500
+ }
+ );
+ }
assert_eq!(TemplateModule::balance_count(1, 2), 523);
assert_eq!(TemplateModule::balance_count(1, 3), 500);
assert_eq!(TemplateModule::address_tokens(1, 2), [1]);
@@ -354,20 +363,23 @@
// split item and new owner has account scenario
assert_ok!(TemplateModule::transfer(origin2.clone(), 3, 1, 1, 200));
- assert_eq!(
- TemplateModule::refungible_item_id(1, 1).owner[0],
- Ownership {
- owner: 2,
- fraction: 323
- }
- );
- assert_eq!(
- TemplateModule::refungible_item_id(1, 1).owner[1],
- Ownership {
- owner: 3,
- fraction: 700
- }
- );
+ {
+ let item = TemplateModule::refungible_item_id(1, 1).unwrap();
+ assert_eq!(
+ item.owner[0],
+ Ownership {
+ owner: 2,
+ fraction: 323
+ }
+ );
+ assert_eq!(
+ item.owner[1],
+ Ownership {
+ owner: 3,
+ fraction: 700
+ }
+ );
+ }
assert_eq!(TemplateModule::balance_count(1, 2), 323);
assert_eq!(TemplateModule::balance_count(1, 3), 700);
assert_eq!(TemplateModule::address_tokens(1, 2), [1]);
@@ -390,7 +402,7 @@
let origin1 = Origin::signed(1);
// default scenario
assert_ok!(TemplateModule::transfer(origin1.clone(), 2, 1, 1, 1000));
- assert_eq!(TemplateModule::nft_item_id(1, 1).owner, 2);
+ assert_eq!(TemplateModule::nft_item_id(1, 1).unwrap().owner, 2);
assert_eq!(TemplateModule::balance_count(1, 1), 0);
assert_eq!(TemplateModule::balance_count(1, 2), 1);
// assert_eq!(TemplateModule::address_tokens(1, 1), []);
@@ -456,7 +468,7 @@
let data = default_nft_data();
create_test_item(collection_id, &data.clone().into());
- assert_eq!(TemplateModule::nft_item_id(1, 1).const_data, data.const_data);
+ assert_eq!(TemplateModule::nft_item_id(1, 1).unwrap().const_data, data.const_data);
assert_eq!(TemplateModule::balance_count(1, 1), 1);
assert_eq!(TemplateModule::address_tokens(1, 1), [1]);
@@ -605,7 +617,7 @@
1,
1,
4
- ), Error::<Test>::TokenValueNotEnough);
+ ), Error::<Test>::NoPermission);
});
}
@@ -685,7 +697,7 @@
assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 5));
assert_noop!(
TemplateModule::burn_item(origin1.clone(), 1, 1, 5),
- Error::<Test>::TokenNotFound
+ Error::<Test>::TokenValueNotEnough
);
assert_eq!(TemplateModule::balance_count(1, 1), 0);
@@ -807,9 +819,9 @@
assert_eq!(TemplateModule::balance_count(nft_collection_id, 1), 1);
assert_eq!(TemplateModule::balance_count(fungible_collection_id, 1), 5);
assert_eq!(TemplateModule::balance_count(re_fungible_collection_id, 1), 1023);
- assert_eq!(TemplateModule::nft_item_id(nft_collection_id, 1).owner, 1);
+ assert_eq!(TemplateModule::nft_item_id(nft_collection_id, 1).unwrap().owner, 1);
assert_eq!(TemplateModule::fungible_item_id(fungible_collection_id, 1).value, 5);
- assert_eq!(TemplateModule::refungible_item_id(re_fungible_collection_id, 1).owner[0].owner, 1);
+ assert_eq!(TemplateModule::refungible_item_id(re_fungible_collection_id, 1).unwrap().owner[0].owner, 1);
});
}
@@ -1876,7 +1888,7 @@
let variable_data = b"test set_variable_meta_data method.".to_vec();
assert_ok!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone()));
- assert_eq!(TemplateModule::nft_item_id(collection_id, 1).variable_data, variable_data);
+ assert_eq!(TemplateModule::nft_item_id(collection_id, 1).unwrap().variable_data, variable_data);
});
}
@@ -1895,7 +1907,7 @@
let variable_data = b"test set_variable_meta_data method.".to_vec();
assert_ok!(TemplateModule::set_variable_meta_data(origin1, collection_id, 1, variable_data.clone()));
- assert_eq!(TemplateModule::refungible_item_id(collection_id, 1).variable_data, variable_data);
+ assert_eq!(TemplateModule::refungible_item_id(collection_id, 1).unwrap().variable_data, variable_data);
});
}