// This file is part of Substrate. // Copyright (C) Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Test environment for Asset Conversion pallet. use super::*; use crate as pallet_asset_conversion; use frame_support::{ construct_runtime, derive_impl, instances::{Instance1, Instance2}, ord_parameter_types, parameter_types, traits::{ tokens::{ fungible::{NativeFromLeft, NativeOrWithId, UnionOf}, imbalance::ResolveAssetTo, }, AsEnsureOriginWithArg, ConstU128, ConstU32, ConstU64, }, PalletId, }; use frame_system::{EnsureSigned, EnsureSignedBy}; use sp_arithmetic::Permill; use sp_core::H256; use sp_runtime::{ traits::{AccountIdConversion, BlakeTwo256, IdentityLookup}, BuildStorage, }; use sp_std::default::Default; type Block = frame_system::mocking::MockBlock; construct_runtime!( pub enum Test { System: frame_system, Balances: pallet_balances, Assets: pallet_assets::, PoolAssets: pallet_assets::, AssetConversion: pallet_asset_conversion, } ); #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Test { type BaseCallFilter = frame_support::traits::Everything; type BlockWeights = (); type BlockLength = (); type RuntimeOrigin = RuntimeOrigin; type RuntimeCall = RuntimeCall; type Nonce = u64; type Hash = H256; type Hashing = BlakeTwo256; type AccountId = u128; type Lookup = IdentityLookup; type Block = Block; type RuntimeEvent = RuntimeEvent; type BlockHashCount = ConstU64<250>; type DbWeight = (); type Version = (); type PalletInfo = PalletInfo; type AccountData = pallet_balances::AccountData; type OnNewAccount = (); type OnKilledAccount = (); type SystemWeightInfo = (); type SS58Prefix = (); type OnSetCode = (); type MaxConsumers = ConstU32<16>; } impl pallet_balances::Config for Test { type Balance = u128; type DustRemoval = (); type RuntimeEvent = RuntimeEvent; type ExistentialDeposit = ConstU128<100>; type AccountStore = System; type WeightInfo = (); type MaxLocks = (); type MaxReserves = ConstU32<50>; type ReserveIdentifier = [u8; 8]; type FreezeIdentifier = (); type MaxFreezes = (); type RuntimeHoldReason = (); type RuntimeFreezeReason = (); } impl pallet_assets::Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = u128; type RemoveItemsLimit = ConstU32<1000>; type AssetId = u32; type AssetIdParameter = u32; type Currency = Balances; type CreateOrigin = AsEnsureOriginWithArg>; type ForceOrigin = frame_system::EnsureRoot; type AssetDeposit = ConstU128<1>; type AssetAccountDeposit = ConstU128<10>; type MetadataDepositBase = ConstU128<1>; type MetadataDepositPerByte = ConstU128<1>; type ApprovalDeposit = ConstU128<1>; type StringLimit = ConstU32<50>; type Freezer = (); type Extra = (); type WeightInfo = (); type CallbackHandle = (); pallet_assets::runtime_benchmarks_enabled! { type BenchmarkHelper = (); } } impl pallet_assets::Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = u128; type RemoveItemsLimit = ConstU32<1000>; type AssetId = u32; type AssetIdParameter = u32; type Currency = Balances; type CreateOrigin = AsEnsureOriginWithArg>; type ForceOrigin = frame_system::EnsureRoot; type AssetDeposit = ConstU128<0>; type AssetAccountDeposit = ConstU128<0>; type MetadataDepositBase = ConstU128<0>; type MetadataDepositPerByte = ConstU128<0>; type ApprovalDeposit = ConstU128<0>; type StringLimit = ConstU32<50>; type Freezer = (); type Extra = (); type WeightInfo = (); type CallbackHandle = (); pallet_assets::runtime_benchmarks_enabled! { type BenchmarkHelper = (); } } parameter_types! { pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon"); pub const Native: NativeOrWithId = NativeOrWithId::Native; pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0); } ord_parameter_types! { pub const AssetConversionOrigin: u128 = AccountIdConversion::::into_account_truncating(&AssetConversionPalletId::get()); } pub type NativeAndAssets = UnionOf, u128>; pub type AscendingLocator = Ascending>; pub type WithFirstAssetLocator = WithFirstAsset>; impl Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = ::Balance; type HigherPrecisionBalance = sp_core::U256; type AssetKind = NativeOrWithId; type Assets = NativeAndAssets; type PoolId = (Self::AssetKind, Self::AssetKind); type PoolLocator = Chain; type PoolAssetId = u32; type PoolAssets = PoolAssets; type PoolSetupFee = ConstU128<100>; // should be more or equal to the existential deposit type PoolSetupFeeAsset = Native; type PoolSetupFeeTarget = ResolveAssetTo; type PalletId = AssetConversionPalletId; type WeightInfo = (); type LPFee = ConstU32<3>; // means 0.3% type LiquidityWithdrawalFee = LiquidityWithdrawalFee; type MaxSwapPathLength = ConstU32<4>; type MintMinLiquidity = ConstU128<100>; // 100 is good enough when the main currency has 12 decimals. #[cfg(feature = "runtime-benchmarks")] type BenchmarkHelper = (); } pub(crate) fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); pallet_balances::GenesisConfig:: { balances: vec![(1, 10000), (2, 20000), (3, 30000), (4, 40000)], } .assimilate_storage(&mut t) .unwrap(); let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| System::set_block_number(1)); ext }