From 6d406a445076ea063a4c9ff6c9bc00dfe5f1892d Mon Sep 17 00:00:00 2001 From: usetech-llc Date: Mon, 09 Nov 2020 12:52:21 +0000 Subject: [PATCH] Merge pull request #15 from usetech-llc/fix/nftpar-150 Unit tests repaired --- --- a/pallets/nft/Cargo.toml +++ b/pallets/nft/Cargo.toml @@ -80,6 +80,27 @@ branch = 'v2.0.0_release' version = '2.0.0' +[dependencies.pallet-balances] +default-features = false +git = 'https://github.com/usetech-llc/substrate.git' +package = 'pallet-balances' +branch = 'v2.0.0_release' +version = '2.0.0' + +[dependencies.pallet-timestamp] +default-features = false +git = 'https://github.com/usetech-llc/substrate.git' +package = 'pallet-timestamp' +branch = 'v2.0.0_release' +version = '2.0.0' + +[dependencies.pallet-randomness-collective-flip] +default-features = false +git = 'https://github.com/usetech-llc/substrate.git' +package = 'pallet-randomness-collective-flip' +branch = 'v2.0.0_release' +version = '2.0.0' + [features] default = ['std'] std = [ @@ -87,6 +108,9 @@ "serde/std", 'frame-support/std', 'frame-system/std', + 'pallet-balances/std', + 'pallet-timestamp/std', + 'pallet-randomness-collective-flip/std', 'sp-std/std', 'sp-runtime/std', 'frame-benchmarking/std', --- a/pallets/nft/src/mock.rs +++ b/pallets/nft/src/mock.rs @@ -1,20 +1,27 @@ // Creating mock runtime here use crate::{Module, Trait}; + +use pallet_contracts::{ + ContractAddressFor, TrieId, TrieIdGenerator, +}; + use frame_support::{ impl_outer_origin, parameter_types, weights::{ - constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, - Weight, + // constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}, + Weight, IdentityFee, }, }; use frame_system as system; +use transaction_payment; use sp_core::H256; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup, Saturating}, Perbill, }; +pub use pallet_balances; impl_outer_origin! { pub enum Origin for Test {} @@ -56,13 +63,101 @@ type AvailableBlockRatio = AvailableBlockRatio; type Version = (); type PalletInfo = (); - type AccountData = (); + type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); type SystemWeightInfo = (); } -impl Trait for Test { + +parameter_types! { + pub const ExistentialDeposit: u64 = 1; + pub const MaxLocks: u32 = 50; +} + +type System = frame_system::Module; +impl pallet_balances::Trait for Test { + type AccountStore = System; + type Balance = u64; + type DustRemoval = (); type Event = (); + type ExistentialDeposit = ExistentialDeposit; + type WeightInfo = (); + type MaxLocks = MaxLocks; +} + +parameter_types! { + pub const TransactionByteFee: u64 = 1; +} +impl transaction_payment::Trait for Test { + type Currency = pallet_balances::Module; + type OnTransactionPayment = (); + type TransactionByteFee = TransactionByteFee; + type WeightToFee = IdentityFee; + type FeeMultiplierUpdate = (); +} + + +parameter_types! { + pub const MinimumPeriod: u64 = 1; +} +impl pallet_timestamp::Trait for Test { + type Moment = u64; + type OnTimestampSet = (); + type MinimumPeriod = MinimumPeriod; + type WeightInfo = (); +} + +type Timestamp = pallet_timestamp::Module; +type Randomness = pallet_randomness_collective_flip::Module; + +parameter_types! { + pub const TombstoneDeposit: u64 = 1; + pub const RentByteFee: u64 = 1; + pub const RentDepositOffset: u64 = 1; + pub const SurchargeReward: u64 = 1; +} + +pub struct DummyTrieIdGenerator; +impl TrieIdGenerator for DummyTrieIdGenerator { + fn trie_id(account_id: &u64) -> TrieId { + let new_seed = *account_id + 1; + let mut res = vec![]; + res.extend_from_slice(&new_seed.to_le_bytes()); + res.extend_from_slice(&account_id.to_le_bytes()); + res + } +} + +pub struct DummyContractAddressFor; +impl ContractAddressFor for DummyContractAddressFor { + fn contract_address_for(_code_hash: &H256, _data: &[u8], origin: &u64) -> u64 { + *origin + 1 + } +} + +impl pallet_contracts::Trait for Test { + type Time = Timestamp; + type Randomness = Randomness; + type Currency = pallet_balances::Module; + type Event = (); + type DetermineContractAddress = DummyContractAddressFor; + type TrieIdGenerator = DummyTrieIdGenerator; + type RentPayment = (); + type SignedClaimHandicap = pallet_contracts::DefaultSignedClaimHandicap; + type TombstoneDeposit = TombstoneDeposit; + type StorageSizeOffset = pallet_contracts::DefaultStorageSizeOffset; + type RentByteFee = RentByteFee; + type RentDepositOffset = RentDepositOffset; + type SurchargeReward = SurchargeReward; + type MaxDepth = pallet_contracts::DefaultMaxDepth; + type MaxValueSize = pallet_contracts::DefaultMaxValueSize; + type WeightPrice = (); +} + +impl Trait for Test { + type Event = (); + type WeightInfo = (); + } pub type TemplateModule = Module; --- a/pallets/nft/src/tests.rs +++ b/pallets/nft/src/tests.rs @@ -394,6 +394,77 @@ assert_eq!(TemplateModule::balance_count(1, 1), 1); assert_eq!(TemplateModule::address_tokens(1, 1), [1]); + // neg transfer + assert_noop!(TemplateModule::transfer_from( + origin2.clone(), + 1, + 2, + 1, + 1, + 1), "Only item owner, collection owner and admins can modify items"); + + // do approve + assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1)); + assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 1); + assert_eq!( + TemplateModule::approved(1, (1, 1))[0], + ApprovePermissions { + approved: 2, + amount: 100000000 + } + ); + + assert_ok!(TemplateModule::transfer_from( + origin2.clone(), + 1, + 2, + 1, + 1, + 1 + )); + assert_eq!(TemplateModule::approved(1, (1, 1)).len(), 0); + }); +} + +#[test] +fn nft_approve_and_transfer_from_white_list() { + new_test_ext().execute_with(|| { + let col_name1: Vec = "Test1\0".encode_utf16().collect::>(); + let col_desc1: Vec = "TestDescription1\0".encode_utf16().collect::>(); + let token_prefix1: Vec = b"token_prefix1\0".to_vec(); + let mode: CollectionMode = CollectionMode::NFT(2000); + + assert_ok!(TemplateModule::set_chain_limits(RawOrigin::Root.into(), ChainLimits { + collection_numbers_limit: 10, + account_token_ownership_limit: 10, + collections_admins_limit: 5, + custom_data_limit: 2048, + nft_sponsor_transfer_timeout: 15, + fungible_sponsor_transfer_timeout: 15, + refungible_sponsor_transfer_timeout: 15, + })); + + let origin1 = Origin::signed(1); + let origin2 = Origin::signed(2); + assert_ok!(TemplateModule::create_collection( + origin1.clone(), + col_name1.clone(), + col_desc1.clone(), + token_prefix1.clone(), + mode + )); + assert_eq!(TemplateModule::collection(1).owner, 1); + + assert_ok!(TemplateModule::create_item( + origin1.clone(), + 1, + [1, 2, 3].to_vec(), + 1 + )); + assert_eq!(TemplateModule::nft_item_id(1, 1).data, [1, 2, 3].to_vec()); + assert_eq!(TemplateModule::balance_count(1, 1), 1); + assert_eq!(TemplateModule::address_tokens(1, 1), [1]); + assert_ok!(TemplateModule::set_mint_permission( origin1.clone(), 1, -- gitstuff