difftreelog
Merge pull request #299 from UniqueNetwork/feature/CORE-296
in: master
CORE-296 Add error "NotSufficientFounds" and test it.
3 files changed
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -318,6 +318,9 @@
AddressIsZero,
/// Target collection doesn't supports this operation
UnsupportedOperation,
+
+ /// Not sufficient founds to perform action
+ NotSufficientFounds,
}
#[pallet::storage]
@@ -470,7 +473,7 @@
WithdrawReasons::TRANSFER,
ExistenceRequirement::KeepAlive,
)
- .map_err(|_| Error::<T>::NoPermission)?;
+ .map_err(|_| Error::<T>::NotSufficientFounds)?;
}
<CreatedCollectionCount<T>>::put(created_count);
pallets/unique/src/mock.rsdiffbeforeafterboth1#![allow(clippy::from_over_into)]23use crate as pallet_template;4use sp_core::{H160, 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, runner::stack::MaybeMirroredLog};13use pallet_common::account::{EvmBackwardsAddressMapping, CrossAccountId};14use codec::{Encode, Decode, MaxEncodedLen};15use scale_info::TypeInfo;16use up_data_structs::ConstU32;1718type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;19type Block = frame_system::mocking::MockBlock<Test>;2021// Configure a mock runtime to test the pallet.22frame_support::construct_runtime!(23 pub enum Test where24 Block = Block,25 NodeBlock = Block,26 UncheckedExtrinsic = UncheckedExtrinsic,27 {28 System: frame_system::{Pallet, Call, Config, Storage, Event<T>},29 TemplateModule: pallet_template::{Pallet, Call, Storage},30 Balances: pallet_balances::{Pallet, Call, Storage},31 Common: pallet_common::{Pallet, Storage, Event<T>},32 Fungible: pallet_fungible::{Pallet, Storage},33 Refungible: pallet_refungible::{Pallet, Storage},34 Nonfungible: pallet_nonfungible::{Pallet, Storage},35 }36);3738parameter_types! {39 pub const BlockHashCount: u64 = 250;40 pub const SS58Prefix: u8 = 42;41}4243impl system::Config for Test {44 type BaseCallFilter = Everything;45 type BlockWeights = ();46 type BlockLength = ();47 type DbWeight = ();48 type Origin = Origin;49 type Call = Call;50 type Index = u64;51 type BlockNumber = u64;52 type Hash = H256;53 type Hashing = BlakeTwo256;54 type AccountId = u64;55 type Lookup = IdentityLookup<Self::AccountId>;56 type Header = Header;57 type Event = ();58 type BlockHashCount = BlockHashCount;59 type Version = ();60 type PalletInfo = PalletInfo;61 type AccountData = pallet_balances::AccountData<u64>;62 type OnNewAccount = ();63 type OnKilledAccount = ();64 type SystemWeightInfo = ();65 type SS58Prefix = SS58Prefix;66 type OnSetCode = ();67 type MaxConsumers = ConstU32<16>;68}6970parameter_types! {71 pub const ExistentialDeposit: u64 = 1;72 pub const MaxLocks: u32 = 50;73}74//frame_system::Module<Test>;75impl pallet_balances::Config for Test {76 type AccountStore = System;77 type Balance = u64;78 type DustRemoval = ();79 type Event = ();80 type ExistentialDeposit = ExistentialDeposit;81 type WeightInfo = ();82 type MaxLocks = MaxLocks;83 type MaxReserves = ();84 type ReserveIdentifier = [u8; 8];85}8687parameter_types! {88 pub const TransactionByteFee: u64 = 1;89 pub const OperationalFeeMultiplier: u8 = 5;90}9192impl pallet_transaction_payment::Config for Test {93 type OnChargeTransaction = CurrencyAdapter<pallet_balances::Pallet<Test>, ()>;94 type TransactionByteFee = TransactionByteFee;95 type WeightToFee = IdentityFee<u64>;96 type FeeMultiplierUpdate = ();97 type OperationalFeeMultiplier = OperationalFeeMultiplier;98}99100parameter_types! {101 pub const MinimumPeriod: u64 = 1;102}103impl pallet_timestamp::Config for Test {104 type Moment = u64;105 type OnTimestampSet = ();106 type MinimumPeriod = MinimumPeriod;107 type WeightInfo = ();108}109110parameter_types! {111 pub const CollectionCreationPrice: u32 = 0;112 pub TreasuryAccountId: u64 = 1234;113 pub EthereumChainId: u32 = 1111;114}115116pub struct TestEvmAddressMapping;117impl AddressMapping<u64> for TestEvmAddressMapping {118 fn into_account_id(_addr: sp_core::H160) -> u64 {119 unimplemented!()120 }121}122123pub struct TestEvmBackwardsAddressMapping;124impl EvmBackwardsAddressMapping<u64> for TestEvmBackwardsAddressMapping {125 fn from_account_id(_account_id: u64) -> sp_core::H160 {126 unimplemented!()127 }128}129130#[derive(Encode, Decode, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, TypeInfo, MaxEncodedLen)]131pub struct TestCrossAccountId(u64, sp_core::H160);132impl CrossAccountId<u64> for TestCrossAccountId {133 fn as_sub(&self) -> &u64 {134 &self.0135 }136 fn as_eth(&self) -> &sp_core::H160 {137 &self.1138 }139 fn from_sub(sub: u64) -> Self {140 let mut eth = [0; 20];141 eth[12..20].copy_from_slice(&sub.to_be_bytes());142 Self(sub, sp_core::H160(eth))143 }144 fn from_eth(eth: sp_core::H160) -> Self {145 let mut sub_raw = [0; 8];146 sub_raw.copy_from_slice(ð.0[0..8]);147 let sub = u64::from_be_bytes(sub_raw);148 Self(sub, eth)149 }150 fn conv_eq(&self, other: &Self) -> bool {151 self.as_sub() == other.as_sub()152 }153}154155impl Default for TestCrossAccountId {156 fn default() -> Self {157 Self::from_sub(0)158 }159}160161pub struct TestEtheremTransactionSender;162impl pallet_ethereum::EthereumTransactionSender for TestEtheremTransactionSender {163 fn submit_logs_transaction(164 _source: H160,165 _tx: pallet_ethereum::Transaction,166 _logs: Vec<MaybeMirroredLog>,167 ) {168 }169}170171impl pallet_evm_coder_substrate::Config for Test {172 type EthereumTransactionSender = TestEtheremTransactionSender;173 type GasWeightMapping = ();174}175176impl pallet_common::Config for Test {177 type Event = ();178 type EvmBackwardsAddressMapping = TestEvmBackwardsAddressMapping;179 type EvmAddressMapping = TestEvmAddressMapping;180 type CrossAccountId = TestCrossAccountId;181182 type Currency = Balances;183 type CollectionCreationPrice = CollectionCreationPrice;184 type TreasuryAccountId = TreasuryAccountId;185}186187impl pallet_fungible::Config for Test {188 type WeightInfo = ();189}190impl pallet_refungible::Config for Test {191 type WeightInfo = ();192}193impl pallet_nonfungible::Config for Test {194 type WeightInfo = ();195}196197impl pallet_template::Config for Test {198 type Event = ();199 type WeightInfo = ();200}201202// Build genesis storage according to the mock runtime.203pub fn new_test_ext() -> sp_io::TestExternalities {204 system::GenesisConfig::default()205 .build_storage::<Test>()206 .unwrap()207 .into()208}pallets/unique/src/tests.rsdiffbeforeafterboth--- a/pallets/unique/src/tests.rs
+++ b/pallets/unique/src/tests.rs
@@ -7,9 +7,26 @@
CreateReFungibleData, MAX_DECIMAL_POINTS, COLLECTION_ADMINS_LIMIT, MetaUpdatePermission,
TokenId, MAX_TOKEN_OWNERSHIP,
};
-use frame_support::{assert_noop, assert_ok};
+use frame_support::{assert_noop, assert_ok, assert_err};
use sp_std::convert::TryInto;
+use pallet_balances;
+fn add_balance(user: u64, value: u64) {
+ const DONOR_USER: u64 = 999;
+ assert_ok!(<pallet_balances::Pallet<Test>>::set_balance(
+ Origin::root(),
+ DONOR_USER,
+ value,
+ 0
+ ));
+ assert_ok!(<pallet_balances::Pallet<Test>>::force_transfer(
+ Origin::root(),
+ DONOR_USER,
+ user,
+ value
+ ));
+}
+
fn default_nft_data() -> CreateNftData {
CreateNftData {
const_data: vec![1, 2, 3].try_into().unwrap(),
@@ -34,6 +51,8 @@
owner: u64,
id: CollectionId,
) -> CollectionId {
+ add_balance(owner, CollectionCreationPrice::get() as u64 + 1);
+
let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
@@ -121,6 +140,30 @@
}
#[test]
+fn check_not_sufficient_founds() {
+ new_test_ext().execute_with(|| {
+ let acc: u64 = 1;
+ <pallet_balances::Pallet<Test>>::set_balance(Origin::root(), acc, 0, 0).unwrap();
+
+ let name: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let description: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix: Vec<u8> = b"token_prefix1\0".to_vec();
+
+ let data: CreateCollectionData<<Test as system::Config>::AccountId> =
+ CreateCollectionData {
+ name: name.try_into().unwrap(),
+ description: description.try_into().unwrap(),
+ token_prefix: token_prefix.try_into().unwrap(),
+ mode: CollectionMode::NFT,
+ ..Default::default()
+ };
+
+ let result = TemplateModule::create_collection_ex(Origin::signed(acc), data);
+ assert_err!(result, <CommonError<Test>>::NotSufficientFounds);
+ });
+}
+
+#[test]
fn create_fungible_collection_fails_with_large_decimal_numbers() {
new_test_ext().execute_with(|| {
let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();