Skip to content
Snippets Groups Projects
mock.rs 4.49 KiB
Newer Older
  • Learn to ignore specific revisions
  • Shaopeng Wang's avatar
    Shaopeng Wang committed
    //! Mocks for the currencies module.
    
    #![cfg(test)]
    
    
    use super::*;
    
    Shaun Wang's avatar
    Shaun Wang committed
    use frame_support::{construct_runtime, parameter_types, PalletId};
    
    use orml_traits::parameter_type_with_key;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    use sp_core::H256;
    
    wangjj9219's avatar
    wangjj9219 committed
    use sp_runtime::{
    	testing::Header,
    	traits::{AccountIdConversion, IdentityLookup},
    
    Shaun Wang's avatar
    Shaun Wang committed
    	AccountId32,
    
    wangjj9219's avatar
    wangjj9219 committed
    };
    
    brettkolodny's avatar
    brettkolodny committed
    use crate as currencies;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    
    parameter_types! {
    	pub const BlockHashCount: u64 = 250;
    }
    
    
    wangjj9219's avatar
    wangjj9219 committed
    pub type AccountId = AccountId32;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    impl frame_system::Config for Runtime {
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type Origin = Origin;
    
    brettkolodny's avatar
    brettkolodny committed
    	type Call = Call;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type Index = u64;
    	type BlockNumber = u64;
    	type Hash = H256;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type Hashing = ::sp_runtime::traits::BlakeTwo256;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type AccountId = AccountId;
    	type Lookup = IdentityLookup<Self::AccountId>;
    	type Header = Header;
    
    brettkolodny's avatar
    brettkolodny committed
    	type Event = Event;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type BlockHashCount = BlockHashCount;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type BlockWeights = ();
    	type BlockLength = ();
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type Version = ();
    
    brettkolodny's avatar
    brettkolodny committed
    	type PalletInfo = PalletInfo;
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type AccountData = pallet_balances::AccountData<u64>;
    	type OnNewAccount = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type OnKilledAccount = ();
    
    	type DbWeight = ();
    
    	type BaseCallFilter = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type SystemWeightInfo = ();
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type SS58Prefix = ();
    
    	type OnSetCode = ();
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    }
    
    type CurrencyId = u32;
    type Balance = u64;
    
    parameter_types! {
    
    	pub const ExistentialDeposit: u64 = 1;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    impl pallet_balances::Config for Runtime {
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type Balance = Balance;
    
    	type DustRemoval = ();
    
    brettkolodny's avatar
    brettkolodny committed
    	type Event = Event;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type ExistentialDeposit = ExistentialDeposit;
    
    	type AccountStore = frame_system::Pallet<Runtime>;
    
    	type MaxLocks = ();
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    	type WeightInfo = ();
    
    wangjj9219's avatar
    wangjj9219 committed
    parameter_type_with_key! {
    
    wangjj9219's avatar
    wangjj9219 committed
    	pub ExistentialDeposits: |_currency_id: CurrencyId| -> Balance {
    
    wangjj9219's avatar
    wangjj9219 committed
    		Default::default()
    	};
    }
    
    parameter_types! {
    
    Shaun Wang's avatar
    Shaun Wang committed
    	pub DustAccount: AccountId = PalletId(*b"orml/dst").into_account();
    
    	pub MaxLocks: u32 = 100_000;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    impl orml_tokens::Config for Runtime {
    
    brettkolodny's avatar
    brettkolodny committed
    	type Event = Event;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type Balance = Balance;
    	type Amount = i64;
    	type CurrencyId = CurrencyId;
    
    	type WeightInfo = ();
    
    	type ExistentialDeposits = ExistentialDeposits;
    
    	type OnDust = orml_tokens::TransferDust<Runtime, DustAccount>;
    
    	type MaxLocks = MaxLocks;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    }
    
    pub const NATIVE_CURRENCY_ID: CurrencyId = 1;
    pub const X_TOKEN_ID: CurrencyId = 2;
    
    parameter_types! {
    	pub const GetNativeCurrencyId: CurrencyId = NATIVE_CURRENCY_ID;
    }
    
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    impl Config for Runtime {
    
    brettkolodny's avatar
    brettkolodny committed
    	type Event = Event;
    
    	type MultiCurrency = Tokens;
    
    	type NativeCurrency = AdaptedBasicCurrency;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	type GetNativeCurrencyId = GetNativeCurrencyId;
    
    	type WeightInfo = ();
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    }
    pub type NativeCurrency = NativeCurrencyOf<Runtime>;
    
    pub type AdaptedBasicCurrency = BasicCurrencyAdapter<Runtime, PalletBalances, i64, u64>;
    
    wangjj9219's avatar
    wangjj9219 committed
    type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
    type Block = frame_system::mocking::MockBlock<Runtime>;
    
    brettkolodny's avatar
    brettkolodny committed
    
    construct_runtime!(
    	pub enum Runtime where
    		Block = Block,
    		NodeBlock = Block,
    		UncheckedExtrinsic = UncheckedExtrinsic,
    	{
    
    		System: frame_system::{Pallet, Call, Storage, Config, Event<T>},
    		Currencies: currencies::{Pallet, Call, Event<T>},
    		Tokens: orml_tokens::{Pallet, Storage, Event<T>, Config<T>},
    		PalletBalances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
    
    wangjj9219's avatar
    wangjj9219 committed
    pub const ALICE: AccountId = AccountId32::new([1u8; 32]);
    pub const BOB: AccountId = AccountId32::new([2u8; 32]);
    pub const EVA: AccountId = AccountId32::new([5u8; 32]);
    
    pub const ID_1: LockIdentifier = *b"1       ";
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    
    pub struct ExtBuilder {
    
    	endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    }
    
    impl Default for ExtBuilder {
    	fn default() -> Self {
    		Self {
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    		}
    	}
    }
    
    impl ExtBuilder {
    
    	pub fn balances(mut self, endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>) -> Self {
    		self.endowed_accounts = endowed_accounts;
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    		self
    	}
    
    	pub fn one_hundred_for_alice_n_bob(self) -> Self {
    
    		self.balances(vec![
    			(ALICE, NATIVE_CURRENCY_ID, 100),
    			(BOB, NATIVE_CURRENCY_ID, 100),
    			(ALICE, X_TOKEN_ID, 100),
    			(BOB, X_TOKEN_ID, 100),
    		])
    
    Shaopeng Wang's avatar
    Shaopeng Wang committed
    	pub fn build(self) -> sp_io::TestExternalities {
    
    Xiliang Chen's avatar
    Xiliang Chen committed
    		let mut t = frame_system::GenesisConfig::default()
    
    			.build_storage::<Runtime>()
    			.unwrap();
    
    		pallet_balances::GenesisConfig::<Runtime> {
    			balances: self
    				.endowed_accounts
    				.clone()
    				.into_iter()
    				.filter(|(_, currency_id, _)| *currency_id == NATIVE_CURRENCY_ID)
    				.map(|(account_id, _, initial_balance)| (account_id, initial_balance))
    				.collect::<Vec<_>>(),
    		}
    		.assimilate_storage(&mut t)
    		.unwrap();
    
    
    		orml_tokens::GenesisConfig::<Runtime> {
    
    			endowed_accounts: self
    				.endowed_accounts
    				.into_iter()
    				.filter(|(_, currency_id, _)| *currency_id != NATIVE_CURRENCY_ID)
    				.collect::<Vec<_>>(),
    
    		.assimilate_storage(&mut t)
    		.unwrap();