difftreelog
Fix Refungible balance tests
in: master
2 files changed
pallets/nft/src/mock.rsdiffbeforeafterboth1#![allow(clippy::from_over_into)]23use crate as pallet_template;4use sp_core::H256;5use frame_support::{parameter_types, traits::Everything, weights::IdentityFee};6use sp_runtime::{7 traits::{BlakeTwo256, IdentityLookup},8 testing::Header,9};10use pallet_transaction_payment::{CurrencyAdapter};11use frame_system as system;12use pallet_evm::AddressMapping;13use crate::{EvmBackwardsAddressMapping, CrossAccountId};14use codec::{Encode, Decode};15use scale_info::TypeInfo;1617type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;18type Block = frame_system::mocking::MockBlock<Test>;1920// Configure a mock runtime to test the pallet.21frame_support::construct_runtime!(22 pub enum Test where23 Block = Block,24 NodeBlock = Block,25 UncheckedExtrinsic = UncheckedExtrinsic,26 {27 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},28 TemplateModule: pallet_template::{Pallet, Call, Storage},29 Balances: pallet_balances::{Pallet, Call, Storage},30 }31);3233parameter_types! {34 pub const BlockHashCount: u64 = 250;35 pub const SS58Prefix: u8 = 42;36}3738impl system::Config for Test {39 type BaseCallFilter = Everything;40 type BlockWeights = ();41 type BlockLength = ();42 type DbWeight = ();43 type Origin = Origin;44 type Call = Call;45 type Index = u64;46 type BlockNumber = u64;47 type Hash = H256;48 type Hashing = BlakeTwo256;49 type AccountId = u64;50 type Lookup = IdentityLookup<Self::AccountId>;51 type Header = Header;52 type Event = ();53 type BlockHashCount = BlockHashCount;54 type Version = ();55 type PalletInfo = PalletInfo;56 type AccountData = pallet_balances::AccountData<u64>;57 type OnNewAccount = ();58 type OnKilledAccount = ();59 type SystemWeightInfo = ();60 type SS58Prefix = SS58Prefix;61 type OnSetCode = ();62}6364parameter_types! {65 pub const ExistentialDeposit: u64 = 1;66 pub const MaxLocks: u32 = 50;67}68//frame_system::Module<Test>;69impl pallet_balances::Config for Test {70 type AccountStore = System;71 type Balance = u64;72 type DustRemoval = ();73 type Event = ();74 type ExistentialDeposit = ExistentialDeposit;75 type WeightInfo = ();76 type MaxLocks = MaxLocks;77 type MaxReserves = ();78 type ReserveIdentifier = [u8; 8];79}8081parameter_types! {82 pub const TransactionByteFee: u64 = 1;83 pub const OperationalFeeMultiplier: u8 = 5;84}8586impl pallet_transaction_payment::Config for Test {87 type OnChargeTransaction = CurrencyAdapter<pallet_balances::Pallet<Test>, ()>;88 type TransactionByteFee = TransactionByteFee;89 type WeightToFee = IdentityFee<u64>;90 type FeeMultiplierUpdate = ();91 type OperationalFeeMultiplier = OperationalFeeMultiplier;92}9394parameter_types! {95 pub const MinimumPeriod: u64 = 1;96}97impl pallet_timestamp::Config for Test {98 type Moment = u64;99 type OnTimestampSet = ();100 type MinimumPeriod = MinimumPeriod;101 type WeightInfo = ();102}103104parameter_types! {105 pub const CollectionCreationPrice: u32 = 0;106 pub TreasuryAccountId: u64 = 1234;107 pub EthereumChainId: u32 = 1111;108}109110pub struct TestEvmAddressMapping;111impl AddressMapping<u64> for TestEvmAddressMapping {112 fn into_account_id(_addr: sp_core::H160) -> u64 {113 unimplemented!()114 }115}116117pub struct TestEvmBackwardsAddressMapping;118impl EvmBackwardsAddressMapping<u64> for TestEvmBackwardsAddressMapping {119 fn from_account_id(_account_id: u64) -> sp_core::H160 {120 unimplemented!()121 }122}123124#[derive(Encode, Decode, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, TypeInfo)]125pub struct TestCrossAccountId(u64, sp_core::H160);126impl CrossAccountId<u64> for TestCrossAccountId {127 fn from_sub(sub: u64) -> Self {128 let mut eth = [0; 20];129 eth[12..20].copy_from_slice(&sub.to_be_bytes());130 Self(sub, sp_core::H160(eth))131 }132 fn as_sub(&self) -> &u64 {133 &self.0134 }135 fn from_eth(eth: sp_core::H160) -> Self {136 let mut sub_raw = [0; 8];137 sub_raw.copy_from_slice(ð.0[0..8]);138 let sub = u64::from_be_bytes(sub_raw);139 Self(sub, eth)140 }141 fn as_eth(&self) -> &sp_core::H160 {142 &self.1143 }144}145146pub struct TestEtheremTransactionSender;147impl pallet_ethereum::EthereumTransactionSender for TestEtheremTransactionSender {148 fn submit_logs_transaction(149 _tx: pallet_ethereum::Transaction,150 _logs: Vec<pallet_ethereum::Log>,151 ) -> Result<(), sp_runtime::DispatchError> {152 Ok(())153 }154}155156impl pallet_evm_coder_substrate::Config for Test {157 type EthereumTransactionSender = TestEtheremTransactionSender;158}159160impl pallet_template::Config for Test {161 type Event = ();162 type WeightInfo = ();163 type CollectionCreationPrice = CollectionCreationPrice;164 type Currency = pallet_balances::Pallet<Test>;165 type TreasuryAccountId = TreasuryAccountId;166 type EvmAddressMapping = TestEvmAddressMapping;167 type EvmBackwardsAddressMapping = TestEvmBackwardsAddressMapping;168 type CrossAccountId = TestCrossAccountId;169}170171// Build genesis storage according to the mock runtime.172pub fn new_test_ext() -> sp_io::TestExternalities {173 system::GenesisConfig::default()174 .build_storage::<Test>()175 .unwrap()176 .into()177}1#![allow(clippy::from_over_into)]23use crate as pallet_template;4use sp_core::H256;5use frame_support::{parameter_types, traits::Everything, weights::IdentityFee};6use sp_runtime::{7 traits::{BlakeTwo256, IdentityLookup},8 testing::Header,9};10use pallet_transaction_payment::{CurrencyAdapter};11use frame_system as system;12use pallet_evm::AddressMapping;13use pallet_common::account::{EvmBackwardsAddressMapping, CrossAccountId};14use codec::{Encode, Decode};15use scale_info::TypeInfo;1617type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;18type Block = frame_system::mocking::MockBlock<Test>;1920// Configure a mock runtime to test the pallet.21frame_support::construct_runtime!(22 pub enum Test where23 Block = Block,24 NodeBlock = Block,25 UncheckedExtrinsic = UncheckedExtrinsic,26 {27 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},28 TemplateModule: pallet_template::{Pallet, Call, Storage},29 Balances: pallet_balances::{Pallet, Call, Storage},30 }31);3233parameter_types! {34 pub const BlockHashCount: u64 = 250;35 pub const SS58Prefix: u8 = 42;36}3738impl system::Config for Test {39 type BaseCallFilter = Everything;40 type BlockWeights = ();41 type BlockLength = ();42 type DbWeight = ();43 type Origin = Origin;44 type Call = Call;45 type Index = u64;46 type BlockNumber = u64;47 type Hash = H256;48 type Hashing = BlakeTwo256;49 type AccountId = u64;50 type Lookup = IdentityLookup<Self::AccountId>;51 type Header = Header;52 type Event = ();53 type BlockHashCount = BlockHashCount;54 type Version = ();55 type PalletInfo = PalletInfo;56 type AccountData = pallet_balances::AccountData<u64>;57 type OnNewAccount = ();58 type OnKilledAccount = ();59 type SystemWeightInfo = ();60 type SS58Prefix = SS58Prefix;61 type OnSetCode = ();62}6364parameter_types! {65 pub const ExistentialDeposit: u64 = 1;66 pub const MaxLocks: u32 = 50;67}68//frame_system::Module<Test>;69impl pallet_balances::Config for Test {70 type AccountStore = System;71 type Balance = u64;72 type DustRemoval = ();73 type Event = ();74 type ExistentialDeposit = ExistentialDeposit;75 type WeightInfo = ();76 type MaxLocks = MaxLocks;77 type MaxReserves = ();78 type ReserveIdentifier = [u8; 8];79}8081parameter_types! {82 pub const TransactionByteFee: u64 = 1;83 pub const OperationalFeeMultiplier: u8 = 5;84}8586impl pallet_transaction_payment::Config for Test {87 type OnChargeTransaction = CurrencyAdapter<pallet_balances::Pallet<Test>, ()>;88 type TransactionByteFee = TransactionByteFee;89 type WeightToFee = IdentityFee<u64>;90 type FeeMultiplierUpdate = ();91 type OperationalFeeMultiplier = OperationalFeeMultiplier;92}9394parameter_types! {95 pub const MinimumPeriod: u64 = 1;96}97impl pallet_timestamp::Config for Test {98 type Moment = u64;99 type OnTimestampSet = ();100 type MinimumPeriod = MinimumPeriod;101 type WeightInfo = ();102}103104parameter_types! {105 pub const CollectionCreationPrice: u32 = 0;106 pub TreasuryAccountId: u64 = 1234;107 pub EthereumChainId: u32 = 1111;108}109110pub struct TestEvmAddressMapping;111impl AddressMapping<u64> for TestEvmAddressMapping {112 fn into_account_id(_addr: sp_core::H160) -> u64 {113 unimplemented!()114 }115}116117pub struct TestEvmBackwardsAddressMapping;118impl EvmBackwardsAddressMapping<u64> for TestEvmBackwardsAddressMapping {119 fn from_account_id(_account_id: u64) -> sp_core::H160 {120 unimplemented!()121 }122}123124#[derive(Encode, Decode, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, TypeInfo)]125pub struct TestCrossAccountId(u64, sp_core::H160);126impl CrossAccountId<u64> for TestCrossAccountId {127 fn from_sub(sub: u64) -> Self {128 let mut eth = [0; 20];129 eth[12..20].copy_from_slice(&sub.to_be_bytes());130 Self(sub, sp_core::H160(eth))131 }132 fn as_sub(&self) -> &u64 {133 &self.0134 }135 fn from_eth(eth: sp_core::H160) -> Self {136 let mut sub_raw = [0; 8];137 sub_raw.copy_from_slice(ð.0[0..8]);138 let sub = u64::from_be_bytes(sub_raw);139 Self(sub, eth)140 }141 fn as_eth(&self) -> &sp_core::H160 {142 &self.1143 }144}145146pub struct TestEtheremTransactionSender;147impl pallet_ethereum::EthereumTransactionSender for TestEtheremTransactionSender {148 fn submit_logs_transaction(149 _tx: pallet_ethereum::Transaction,150 _logs: Vec<pallet_ethereum::Log>,151 ) -> Result<(), sp_runtime::DispatchError> {152 Ok(())153 }154}155156impl pallet_evm_coder_substrate::Config for Test {157 type EthereumTransactionSender = TestEtheremTransactionSender;158}159160impl pallet_template::Config for Test {161 type WeightInfo = ();162}163164// Build genesis storage according to the mock runtime.165pub fn new_test_ext() -> sp_io::TestExternalities {166 system::GenesisConfig::default()167 .build_storage::<Test>()168 .unwrap()169 .into()170}pallets/nft/src/tests.rsdiffbeforeafterboth--- a/pallets/nft/src/tests.rs
+++ b/pallets/nft/src/tests.rs
@@ -1,7 +1,7 @@
// Tests to be written here
use super::*;
use crate::mock::*;
-use crate::{AccessMode, CollectionMode, Ownership, CreateItemData};
+use crate::{AccessMode, CollectionMode, CreateItemData};
use nft_data_structs::{
CreateNftData, CreateFungibleData, CreateReFungibleData, CollectionId, TokenId,
MAX_DECIMAL_POINTS,
@@ -178,15 +178,10 @@
let data = default_re_fungible_data();
create_test_item(collection_id, &data.clone().into());
let item = TemplateModule::refungible_item_id(collection_id, 1).unwrap();
+ let balance = TemplateModule::balance(collection_id, 1, account(1));
assert_eq!(item.const_data, data.const_data.into_inner());
assert_eq!(item.variable_data, data.variable_data.into_inner());
- assert_eq!(
- item.owner[0],
- Ownership {
- owner: account(1),
- fraction: 1023
- }
- );
+ assert_eq!(balance, 1023);
});
}
@@ -215,15 +210,10 @@
));
for (index, data) in items_data.into_iter().enumerate() {
let item = TemplateModule::refungible_item_id(1, (index + 1) as TokenId).unwrap();
+ let balance = TemplateModule::balance(1, 1, account(1));
assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());
assert_eq!(item.variable_data.to_vec(), data.variable_data.into_inner());
- assert_eq!(
- item.owner[0],
- Ownership {
- owner: account(1),
- fraction: 1023
- }
- );
+ assert_eq!(balance, 1023);
}
});
}
@@ -318,28 +308,19 @@
let origin2 = Origin::signed(2);
{
let item = TemplateModule::refungible_item_id(collection_id, 1).unwrap();
+ let balance = TemplateModule::balance(collection_id, 1, account(1));
assert_eq!(item.const_data, data.const_data.into_inner());
assert_eq!(item.variable_data, data.variable_data.into_inner());
- assert_eq!(
- item.owner[0],
- Ownership {
- owner: account(1),
- fraction: 1023
- }
- );
+ assert_eq!(balance, 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, account(2), 1, 1, 1023));
- assert_eq!(
- TemplateModule::refungible_item_id(1, 1).unwrap().owner[0],
- Ownership {
- owner: account(2),
- fraction: 1023
- }
- );
+
+ let balance2 = TemplateModule::balance(collection_id, 1, account(2));
+ assert_eq!(balance2, 1023);
assert_eq!(TemplateModule::balance_count(1, 1), 0);
assert_eq!(TemplateModule::balance_count(1, 2), 1023);
// assert_eq!(TemplateModule::address_tokens(1, 1), []);
@@ -355,20 +336,10 @@
));
{
let item = TemplateModule::refungible_item_id(1, 1).unwrap();
- assert_eq!(
- item.owner[0],
- Ownership {
- owner: account(2),
- fraction: 523
- }
- );
- assert_eq!(
- item.owner[1],
- Ownership {
- owner: account(3),
- fraction: 500
- }
- );
+ let balance2 = TemplateModule::balance(collection_id, 1, account(2));
+ let balance3 = TemplateModule::balance(collection_id, 1, account(3));
+ assert_eq!(balance2, 523);
+ assert_eq!(balance3, 500);
}
assert_eq!(TemplateModule::balance_count(1, 2), 523);
assert_eq!(TemplateModule::balance_count(1, 3), 500);
@@ -379,20 +350,10 @@
assert_ok!(TemplateModule::transfer(origin2, account(3), 1, 1, 200));
{
let item = TemplateModule::refungible_item_id(1, 1).unwrap();
- assert_eq!(
- item.owner[0],
- Ownership {
- owner: account(2),
- fraction: 323
- }
- );
- assert_eq!(
- item.owner[1],
- Ownership {
- owner: account(3),
- fraction: 700
- }
- );
+ let balance2 = TemplateModule::balance(collection_id, 1, account(2));
+ let balance3 = TemplateModule::balance(collection_id, 1, account(3));
+ assert_eq!(balance2, 323);
+ assert_eq!(balance3, 700);
}
assert_eq!(TemplateModule::balance_count(1, 2), 323);
assert_eq!(TemplateModule::balance_count(1, 3), 700);