Skip to content
Snippets Groups Projects
mock.rs 4.2 KiB
Newer Older
Shaopeng Wang's avatar
Shaopeng Wang committed
//! Mocks for the currencies module.

#![cfg(test)]

zjb0807's avatar
zjb0807 committed
use frame_support::{impl_outer_event, impl_outer_origin, parameter_types};
use pallet_balances;
Shaopeng Wang's avatar
Shaopeng Wang committed
use primitives::H256;
Xiliang Chen's avatar
Xiliang Chen committed
use sp_runtime::{testing::Header, traits::IdentityLookup, Perbill};
Shaopeng Wang's avatar
Shaopeng Wang committed

use tokens;

use super::*;

zjb0807's avatar
zjb0807 committed
mod currencies {
	pub use crate::Event;
}

impl_outer_event! {
	pub enum TestEvent for Runtime {
		frame_system<T>,
		currencies<T>,
		tokens<T>,
		pallet_balances<T>,
	}
}

Shaopeng Wang's avatar
Shaopeng Wang committed
impl_outer_origin! {
	pub enum Origin for Runtime where system = frame_system {}
Shaopeng Wang's avatar
Shaopeng Wang committed
}

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Runtime;
parameter_types! {
	pub const BlockHashCount: u64 = 250;
	pub const MaximumBlockWeight: u32 = 1024;
	pub const MaximumBlockLength: u32 = 2 * 1024;
	pub const AvailableBlockRatio: Perbill = Perbill::one();
}

pub type AccountId = u64;
Xiliang Chen's avatar
Xiliang Chen committed
impl frame_system::Trait for Runtime {
Shaopeng Wang's avatar
Shaopeng Wang committed
	type Origin = Origin;
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;
zjb0807's avatar
zjb0807 committed
	type Event = TestEvent;
Shaopeng Wang's avatar
Shaopeng Wang committed
	type BlockHashCount = BlockHashCount;
	type MaximumBlockWeight = MaximumBlockWeight;
	type MaximumBlockLength = MaximumBlockLength;
	type AvailableBlockRatio = AvailableBlockRatio;
	type Version = ();
	type ModuleToIndex = ();
Xiliang Chen's avatar
Xiliang Chen committed
	type AccountData = pallet_balances::AccountData<u64>;
	type OnNewAccount = ();
	type OnReapAccount = ();
zjb0807's avatar
zjb0807 committed
pub type System = system::Module<Runtime>;
Shaopeng Wang's avatar
Shaopeng Wang committed

type CurrencyId = u32;
type Balance = u64;

parameter_types! {
	pub const ExistentialDeposit: u64 = 1;
impl pallet_balances::Trait for Runtime {
Shaopeng Wang's avatar
Shaopeng Wang committed
	type Balance = Balance;
	type DustRemoval = ();
zjb0807's avatar
zjb0807 committed
	type Event = TestEvent;
Shaopeng Wang's avatar
Shaopeng Wang committed
	type ExistentialDeposit = ExistentialDeposit;
Xiliang Chen's avatar
Xiliang Chen committed
	type AccountStore = frame_system::Module<Runtime>;
pub type PalletBalances = pallet_balances::Module<Runtime>;
Shaopeng Wang's avatar
Shaopeng Wang committed

impl tokens::Trait for Runtime {
zjb0807's avatar
zjb0807 committed
	type Event = TestEvent;
Shaopeng Wang's avatar
Shaopeng Wang committed
	type Balance = Balance;
	type Amount = i64;
	type CurrencyId = CurrencyId;
	type ExistentialDeposit = ExistentialDeposit;
	type DustRemoval = ();
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;
}

impl Trait for Runtime {
zjb0807's avatar
zjb0807 committed
	type Event = TestEvent;
Shaopeng Wang's avatar
Shaopeng Wang committed
	type MultiCurrency = tokens::Module<Runtime>;
	type NativeCurrency = AdaptedBasicCurrency;
Shaopeng Wang's avatar
Shaopeng Wang committed
	type GetNativeCurrencyId = GetNativeCurrencyId;
}
pub type Currencies = Module<Runtime>;
pub type NativeCurrency = NativeCurrencyOf<Runtime>;
pub type AdaptedBasicCurrency = BasicCurrencyAdapter<Runtime, PalletBalances, Balance>;
Shaopeng Wang's avatar
Shaopeng Wang committed

pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const EVA: AccountId = 5;

pub struct ExtBuilder {
	endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
	// whether the configs are for `pallet_balances` or not
	is_for_pallet_balances: bool,
Shaopeng Wang's avatar
Shaopeng Wang committed
}

impl Default for ExtBuilder {
	fn default() -> Self {
		Self {
			is_for_pallet_balances: false,
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),
		])
	pub fn make_for_pallet_balances(mut self) -> Self {
		self.is_for_pallet_balances = true;
Shaopeng Wang's avatar
Shaopeng Wang committed
		self
	}

	pub fn build(self) -> runtime_io::TestExternalities {
Xiliang Chen's avatar
Xiliang Chen committed
		let mut t = frame_system::GenesisConfig::default()
			.build_storage::<Runtime>()
			.unwrap();
		if self.is_for_pallet_balances {
			pallet_balances::GenesisConfig::<Runtime> {
Shaopeng Wang's avatar
Shaopeng Wang committed
				balances: self
					.endowed_accounts
					.into_iter()
					.filter(|(_, currency_id, _)| *currency_id == X_TOKEN_ID)
					.map(|(account_id, _, initial_balance)| (account_id, initial_balance))
					.collect::<Vec<_>>(),
Shaopeng Wang's avatar
Shaopeng Wang committed
			}
			.assimilate_storage(&mut t)
			.unwrap();
		} else {
			tokens::GenesisConfig::<Runtime> {
				endowed_accounts: self.endowed_accounts,
			}
			.assimilate_storage(&mut t)
			.unwrap();
		}

		t.into()
	}
}