diff --git a/Cargo.lock b/Cargo.lock index 1183aa0bfc..f4eebba6ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2076,7 +2076,6 @@ dependencies = [ name = "dex-general" version = "0.4.4" dependencies = [ - "cumulus-primitives-core", "frame-benchmarking", "frame-support", "frame-system", @@ -2092,8 +2091,6 @@ dependencies = [ "sp-io", "sp-runtime", "sp-std", - "xcm", - "xcm-executor", ] [[package]] diff --git a/crates/dex-general/Cargo.toml b/crates/dex-general/Cargo.toml index b6242aa093..cbffed6931 100644 --- a/crates/dex-general/Cargo.toml +++ b/crates/dex-general/Cargo.toml @@ -20,20 +20,13 @@ frame-system = { git = "https://github.com/paritytech/substrate", default-featur sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" } sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -pallet-balances = { git = 'https://github.com/paritytech/substrate', default-features = false, branch = 'polkadot-v0.9.31' } - -# Polkadot -xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false } -xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false } - -# Cumulus -cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", default-features = false, branch = "polkadot-v0.9.31" } # Orml dependencies orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false } orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false } [dev-dependencies] +pallet-balances = { git = 'https://github.com/paritytech/substrate', default-features = false, branch = 'polkadot-v0.9.31' } sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" } sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.31" } @@ -47,19 +40,14 @@ std = [ "sp-core/std", "frame-support/std", "frame-system/std", + "frame-benchmarking/std", "sp-runtime/std", "sp-std/std", "sp-arithmetic/std", - "pallet-balances/std", - - "xcm/std", - "xcm-executor/std", - "cumulus-primitives-core/std", "orml-traits/std", "orml-tokens/std", ] - runtime-benchmarks = [ "frame-benchmarking", ] diff --git a/crates/dex-general/rpc/runtime-api/src/lib.rs b/crates/dex-general/rpc/runtime-api/src/lib.rs index 7c539985d2..bcc21a1221 100644 --- a/crates/dex-general/rpc/runtime-api/src/lib.rs +++ b/crates/dex-general/rpc/runtime-api/src/lib.rs @@ -16,9 +16,6 @@ sp_api::decl_runtime_apis! { AssetBalance: Codec, AssetId: Codec { - - fn get_balance(asset_id: AssetId, owner: AccountId) -> AssetBalance; - fn get_pair_by_asset_id( asset_0: AssetId, asset_1: AssetId diff --git a/crates/dex-general/rpc/src/lib.rs b/crates/dex-general/rpc/src/lib.rs index 7cb8add79d..e3da4d42df 100644 --- a/crates/dex-general/rpc/src/lib.rs +++ b/crates/dex-general/rpc/src/lib.rs @@ -23,9 +23,6 @@ pub use dex_general_rpc_runtime_api::DexGeneralApi as DexGeneralRuntimeApi; #[rpc(client, server)] pub trait DexGeneralApi { - #[method(name = "dexGeneral_getBalance")] - fn get_balance(&self, asset_id: AssetId, account: AccountId, at: Option) -> RpcResult; - #[method(name = "dexGeneral_getPairByAssetId")] fn get_pair_by_asset_id( &self, @@ -144,20 +141,6 @@ where .map_err(runtime_error_into_rpc_err) } - fn get_balance( - &self, - asset_id: AssetId, - account: AccountId, - at: Option<::Hash>, - ) -> RpcResult { - let api = self.client.runtime_api(); - let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - - api.get_balance(&at, asset_id, account) - .map(|asset_balance| asset_balance.into()) - .map_err(runtime_error_into_rpc_err) - } - fn get_pair_by_asset_id( &self, asset_0: AssetId, diff --git a/crates/dex-general/src/benchmarking.rs b/crates/dex-general/src/benchmarking.rs index c010be1330..59c96f28ea 100644 --- a/crates/dex-general/src/benchmarking.rs +++ b/crates/dex-general/src/benchmarking.rs @@ -9,26 +9,13 @@ use crate::Pallet as DexGeneral; use frame_benchmarking::{benchmarks, whitelisted_caller}; use frame_support::assert_ok; use frame_system::RawOrigin; +use sp_runtime::SaturatedConversion; const UNIT: u128 = 1_000_000_000_000; -const ASSET_0: AssetId = AssetId { - chain_id: 2001, - asset_type: 2, - asset_index: 515, -}; - -const ASSET_1: AssetId = AssetId { - chain_id: 2001, - asset_type: 2, - asset_index: 516, -}; - -const ASSET_2: AssetId = AssetId { - chain_id: 2001, - asset_type: 2, - asset_index: 518, -}; +const ASSET_0: u32 = 0; +const ASSET_1: u32 = 1; +const ASSET_2: u32 = 2; pub fn lookup_of_account( who: T::AccountId, @@ -48,7 +35,7 @@ fn run_to_block(n: u32) { benchmarks! { - where_clause { where T::AssetId: From} + where_clause { where T::AssetId: From } set_fee_receiver{ let caller: T::AccountId = whitelisted_caller(); @@ -61,21 +48,21 @@ benchmarks! { create_pair { let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); } : _(RawOrigin::Root, ASSET_0.into(), ASSET_1.into()) bootstrap_create { - let reward:Vec = vec![ASSET_0.into()]; + let reward: Vec = vec![ASSET_0.into()]; let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)]; }: _(RawOrigin::Root, ASSET_0.into(), ASSET_1.into(), 1000, 1000, 1000_000_000, 1000_000_000, 100u128.saturated_into(), reward, reward_amounts) bootstrap_contribute{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); let reward:Vec = vec![ASSET_0.into()]; let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)]; @@ -97,8 +84,8 @@ benchmarks! { bootstrap_claim{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); let reward:Vec = vec![ASSET_0.into()]; let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)]; @@ -138,8 +125,8 @@ benchmarks! { bootstrap_end{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); let reward:Vec = vec![ASSET_0.into()]; let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)]; @@ -193,8 +180,8 @@ benchmarks! { bootstrap_refund{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); let reward:Vec = vec![ASSET_0.into()]; let reward_amounts: Vec<(T::AssetId, u128)> = vec![(ASSET_1.into(), 0)]; @@ -225,8 +212,8 @@ benchmarks! { add_liquidity{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); assert_ok!(DexGeneral::::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into())); @@ -236,8 +223,8 @@ benchmarks! { remove_liquidity{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); assert_ok!(DexGeneral::::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into())); @@ -257,9 +244,9 @@ benchmarks! { swap_exact_assets_for_assets{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_2.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_2.into(), &caller, 1000 * UNIT)); assert_ok!(DexGeneral::::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into())); assert_ok!(DexGeneral::::create_pair((RawOrigin::Root).into(), ASSET_1.into(), ASSET_2.into())); @@ -290,9 +277,9 @@ benchmarks! { swap_assets_for_exact_assets{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_2.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_2.into(), &caller, 1000 * UNIT)); assert_ok!(DexGeneral::::create_pair((RawOrigin::Root).into(), ASSET_1.into(), ASSET_2.into())); assert_ok!(DexGeneral::::create_pair((RawOrigin::Root).into(), ASSET_0.into(), ASSET_1.into())); diff --git a/crates/dex-general/src/fee/migration.rs b/crates/dex-general/src/fee/migration.rs deleted file mode 100644 index c852e10f1a..0000000000 --- a/crates/dex-general/src/fee/migration.rs +++ /dev/null @@ -1,161 +0,0 @@ -use crate::*; -use frame_support::{pallet_prelude::StorageVersion, traits::OnRuntimeUpgrade}; -use sp_std::vec::Vec; - -/// The log target. -const TARGET: &'static str = "dex_general::migration"; - -pub mod v0 { - use super::*; - - #[derive(Clone, Copy, Encode, Decode, RuntimeDebug, PartialEq, Eq, MaxEncodedLen, TypeInfo)] - pub enum PairStatus { - Trading(PairMetadata), - Bootstrap(BootstrapParameter), - Disable, - } - - impl Default for PairStatus { - fn default() -> Self { - Self::Disable - } - } - - #[derive(Encode, Decode, Clone, Copy, RuntimeDebug, PartialEq, Eq, MaxEncodedLen, TypeInfo)] - pub struct PairMetadata { - pub pair_account: Account, - pub total_supply: Balance, - } - - #[frame_support::storage_alias] - pub(crate) type PairStatuses = StorageMap< - Pallet, - Twox64Concat, - (::AssetId, ::AssetId), - PairStatus::BlockNumber, ::AccountId>, - ValueQuery, - >; -} - -pub mod v1 { - use super::*; - - /// Migrate the pallet from V0 to V1. - pub struct MigrateToV1(sp_std::marker::PhantomData); - - impl OnRuntimeUpgrade for MigrateToV1 { - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, &'static str> { - assert_eq!(StorageVersion::get::>(), 0, "Can only upgrade from version 0"); - - let old_entries = v0::PairStatuses::::iter().count(); - log::info!(target: TARGET, "{old_entries} entries will be migrated"); - - Ok((old_entries as u32).encode()) - } - - fn on_runtime_upgrade() -> Weight { - let version = StorageVersion::get::>(); - if version != 0 { - log::warn!( - target: TARGET, - "skipping v0 to v1 migration: executed on wrong storage version.\ - Expected version 0, found {:?}", - version, - ); - return T::DbWeight::get().reads(1); - } - - let mut weight = T::DbWeight::get().reads_writes(2, 1); - - let pair_statuses_storage_map_v0 = v0::PairStatuses::::drain().collect::>(); - weight.saturating_accrue(T::DbWeight::get().reads(pair_statuses_storage_map_v0.len() as u64)); - for (pair, old_pair_status) in pair_statuses_storage_map_v0.into_iter() { - let new_pair_status = match old_pair_status { - v0::PairStatus::Trading(metadata) => PairStatus::Trading(PairMetadata { - pair_account: metadata.pair_account, - total_supply: metadata.total_supply, - fee_rate: DEFAULT_FEE_RATE, - }), - v0::PairStatus::Bootstrap(params) => PairStatus::Bootstrap(params), - v0::PairStatus::Disable => PairStatus::Disable, - }; - PairStatuses::::insert(pair, new_pair_status); - weight.saturating_accrue(T::DbWeight::get().writes(1)); - } - - log::info!(target: TARGET, "Finished migration..."); - - StorageVersion::new(1).put::>(); - weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), &'static str> { - let old_entries: u32 = Decode::decode(&mut &state[..]).expect("pre_upgrade provides a valid state; qed"); - let new_entries = PairStatuses::::iter().count() as u32; - if new_entries != old_entries { - log::error!( - target: TARGET, - "migrated {} entries, expected {}", - new_entries, - old_entries - ); - } - assert_eq!(StorageVersion::get::>(), 1, "Must upgrade"); - Ok(()) - } - } -} - -#[cfg(test)] -#[cfg(feature = "try-runtime")] -mod test { - use super::*; - use crate::fee::mock::*; - use frame_support::assert_ok; - - const DOT_ASSET_ID: AssetId = AssetId { - chain_id: 200, - asset_type: LOCAL, - asset_index: 2, - }; - const BTC_ASSET_ID: AssetId = AssetId { - chain_id: 300, - asset_type: RESERVED, - asset_index: 3, - }; - - #[test] - #[allow(deprecated)] - fn migration_v0_to_v1_works() { - new_test_ext().execute_with(|| { - // assume that we are at v0 - StorageVersion::new(0).put::(); - - let pair = (DOT_ASSET_ID, BTC_ASSET_ID); - v0::PairStatuses::::insert( - pair, - v0::PairStatus::Trading(v0::PairMetadata { - pair_account: 1, - total_supply: 100, - }), - ); - - let state = v1::MigrateToV1::::pre_upgrade().unwrap(); - let _w = v1::MigrateToV1::::on_runtime_upgrade(); - assert_ok!(v1::MigrateToV1::::post_upgrade(state)); - - assert_eq!( - PairStatuses::::get(pair), - PairStatus::Trading(PairMetadata { - pair_account: 1, - total_supply: 100, - fee_rate: DEFAULT_FEE_RATE - }) - ); - - assert_eq!(StorageVersion::get::(), 1); - }); - } -} diff --git a/crates/dex-general/src/fee/mock.rs b/crates/dex-general/src/fee/mock.rs index 59d67ad8d0..d3744ab121 100644 --- a/crates/dex-general/src/fee/mock.rs +++ b/crates/dex-general/src/fee/mock.rs @@ -3,21 +3,13 @@ //! Test utilities -use std::marker::PhantomData; - use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; -use frame_support::{ - dispatch::{DispatchError, DispatchResult}, - pallet_prelude::GenesisBuild, - parameter_types, - traits::Contains, - PalletId, -}; -use orml_traits::{parameter_type_with_key, MultiCurrency}; +use frame_support::{parameter_types, traits::Contains, PalletId}; +use orml_traits::parameter_type_with_key; use sp_core::H256; use sp_runtime::{ testing::Header, @@ -26,10 +18,7 @@ use sp_runtime::{ }; use crate as pallet_dex_general; -pub use crate::{ - AssetBalance, AssetId, Config, DexGeneralMultiAssets, MultiAssetsHandler, PairLpGenerate, Pallet, ParaId, - LIQUIDITY, LOCAL, NATIVE, RESERVED, -}; +pub use crate::{AssetBalance, AssetInfo, Config, GenerateLpAssetId, Pallet}; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -41,16 +30,38 @@ pub enum CurrencyId { LpToken(u8, u8), } +impl CurrencyId { + pub fn join_lp_token(currency_id_0: Self, currency_id_1: Self) -> Option { + let lp_token_0 = match currency_id_0 { + CurrencyId::Token(x) => x, + _ => return None, + }; + let lp_token_1 = match currency_id_1 { + CurrencyId::Token(y) => y, + _ => return None, + }; + Some(CurrencyId::LpToken(lp_token_0, lp_token_1)) + } +} + +impl AssetInfo for CurrencyId { + fn is_support(&self) -> bool { + match self { + Self::Token(_) => true, + _ => false, + } + } +} + frame_support::construct_runtime!( pub enum Test where Block = Block, NodeBlock = Block, UncheckedExtrinsic = UncheckedExtrinsic, { - System: frame_system::{Pallet, Call, Config, Storage, Event} = 0, - Balances: pallet_balances::{Pallet, Call, Storage, Config, Event} = 8, - DexGeneral: pallet_dex_general::{Pallet, Call, Storage, Event} = 9, - Tokens: orml_tokens::{Pallet, Storage, Event, Config} = 11, + System: frame_system::{Pallet, Call, Config, Storage, Event}, + DexGeneral: pallet_dex_general::{Pallet, Call, Storage, Event}, + Tokens: orml_tokens::{Pallet, Storage, Event, Config}, } ); @@ -117,25 +128,19 @@ impl orml_tokens::Config for Test { type CurrencyHooks = (); } -impl pallet_balances::Config for Test { - type Balance = u128; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Pallet; - type WeightInfo = (); - type MaxLocks = (); - type MaxReserves = MaxReserves; - type ReserveIdentifier = [u8; 8]; +pub struct PairLpIdentity; +impl GenerateLpAssetId for PairLpIdentity { + fn generate_lp_asset_id(asset_0: CurrencyId, asset_1: CurrencyId) -> Option { + CurrencyId::join_lp_token(asset_0, asset_1) + } } impl Config for Test { type RuntimeEvent = RuntimeEvent; - type MultiAssetsHandler = DexGeneralMultiAssets>; + type MultiCurrency = Tokens; type PalletId = DexGeneralPalletId; - type AssetId = AssetId; - type LpGenerate = PairLpGenerate; - type SelfParaId = (); + type AssetId = CurrencyId; + type LpGenerate = PairLpIdentity; type WeightInfo = (); } @@ -146,17 +151,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { .build_storage::() .unwrap() .into(); - pallet_balances::GenesisConfig:: { - balances: vec![ - (1, 34028236692093846346337460743176821145), - (2, 10), - (3, 10), - (4, 10), - (5, 10), - ], - } - .assimilate_storage(&mut t) - .unwrap(); pallet_dex_general::GenesisConfig:: { fee_receiver: None, @@ -165,73 +159,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { .assimilate_storage(&mut t) .unwrap(); - orml_tokens::GenesisConfig:: { - balances: vec![ - (1, CurrencyId::Token(1), 34028236692093846346337460743176821145), - (1, CurrencyId::Token(2), 34028236692093846346337460743176821145), - (1, CurrencyId::Token(3), 34028236692093846346337460743176821145), - ], - } - .assimilate_storage(&mut t) - .unwrap(); - t.into() } -pub struct LocalAssetAdaptor(PhantomData); - type AccountId = u128; - -fn asset_id_to_currency_id(asset_id: &AssetId) -> Result { - if asset_id.asset_type == LIQUIDITY { - let token0_id = ((asset_id.asset_index & 0xFFFF0000) >> 16) as u8; - let token1_id = (asset_id.asset_index & 0x0000FFFF) as u8; - return Ok(CurrencyId::LpToken(token0_id, token1_id)); - } - if asset_id.asset_type == LOCAL { - let token_id = asset_id.asset_index as u8; - return Ok(CurrencyId::Token(token_id)); - } - Err(()) -} - -impl MultiAssetsHandler for LocalAssetAdaptor -where - Local: MultiCurrency, -{ - fn balance_of(asset_id: AssetId, who: &AccountId) -> AssetBalance { - asset_id_to_currency_id(&asset_id).map_or(AssetBalance::default(), |currency_id| { - Local::free_balance(currency_id, who) - }) - } - - fn total_supply(asset_id: AssetId) -> AssetBalance { - asset_id_to_currency_id(&asset_id).map_or(AssetBalance::default(), |currency_id| { - Local::total_issuance(currency_id) - }) - } - - fn is_exists(asset_id: AssetId) -> bool { - asset_id_to_currency_id(&asset_id).map_or(false, |currency_id| { - Local::total_issuance(currency_id) > AssetBalance::default() - }) - } - - fn transfer(asset_id: AssetId, origin: &AccountId, target: &AccountId, amount: AssetBalance) -> DispatchResult { - asset_id_to_currency_id(&asset_id).map_or(Err(DispatchError::CannotLookup), |currency_id| { - Local::transfer(currency_id, origin, target, amount) - }) - } - - fn deposit(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result { - asset_id_to_currency_id(&asset_id).map_or(Ok(AssetBalance::default()), |currency_id| { - Local::deposit(currency_id, origin, amount).map(|_| amount) - }) - } - - fn withdraw(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result { - asset_id_to_currency_id(&asset_id).map_or(Ok(AssetBalance::default()), |currency_id| { - Local::withdraw(currency_id, origin, amount).map(|_| amount) - }) - } -} diff --git a/crates/dex-general/src/fee/mod.rs b/crates/dex-general/src/fee/mod.rs index 9287013b57..7c32d0b585 100644 --- a/crates/dex-general/src/fee/mod.rs +++ b/crates/dex-general/src/fee/mod.rs @@ -5,5 +5,3 @@ mod mock; #[cfg(test)] mod tests; - -mod migration; diff --git a/crates/dex-general/src/fee/tests.rs b/crates/dex-general/src/fee/tests.rs index 64131475da..16c217f58c 100644 --- a/crates/dex-general/src/fee/tests.rs +++ b/crates/dex-general/src/fee/tests.rs @@ -2,33 +2,24 @@ // Licensed under Apache 2.0. use super::mock::*; -use crate::{AssetId, Error, MultiAssetsHandler}; +use crate::Error; use frame_support::{assert_noop, assert_ok, error::BadOrigin}; use frame_system::RawOrigin; +use orml_traits::MultiCurrency; use sp_core::U256; -const DOT_ASSET_ID: AssetId = AssetId { - chain_id: 200, - asset_type: LOCAL, - asset_index: 2, -}; +const DOT_ASSET_ID: CurrencyId = CurrencyId::Token(2); +const BTC_ASSET_ID: CurrencyId = CurrencyId::Token(3); -const BTC_ASSET_ID: AssetId = AssetId { - chain_id: 300, - asset_type: RESERVED, - asset_index: 3, -}; +const LP_DOT_BTC: CurrencyId = CurrencyId::LpToken(2, 3); const ALICE: u128 = 1; const BOB: u128 = 2; const CHARLIE: u128 = 3; const DOT_UNIT: u128 = 1000_000_000_000_000; const BTC_UNIT: u128 = 1000_000_00; -const LP_DOT_BTC: AssetId = AssetId { - chain_id: 0, - asset_type: 2, - asset_index: 12885034496, -}; + +const MAX_BALANCE: u128 = u128::MAX - 1; #[test] fn fee_meta_getter_should_work() { @@ -75,8 +66,16 @@ fn turn_on_protocol_fee_only_add_liquidity_no_fee_should_work() { // 2. first add_liquidity - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); let total_supply_dot: u128 = 1 * DOT_UNIT; let total_supply_btc: u128 = 1 * BTC_UNIT; @@ -100,10 +99,10 @@ fn turn_on_protocol_fee_only_add_liquidity_no_fee_should_work() { let lp_of_alice_0 = 316227766016; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(DOT_UNIT) * U256::from(BTC_UNIT) @@ -127,18 +126,18 @@ fn turn_on_protocol_fee_only_add_liquidity_no_fee_should_work() { let lp_of_alice_1 = 16127616066816u128; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_1 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(51 * DOT_UNIT) * U256::from(51 * BTC_UNIT) ); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let balance_dot = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let balance_btc = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let balance_dot = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let balance_btc = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); //println!("balance_DOT {}, balance_BTC {}", balance_dot, balance_btc); assert_eq!(balance_dot, 51000000000000000); @@ -157,15 +156,15 @@ fn turn_on_protocol_fee_only_add_liquidity_no_fee_should_work() { 100 )); - let lp_total = ::MultiAssetsHandler::total_supply(LP_DOT_BTC); + let lp_total = ::MultiCurrency::total_issuance(LP_DOT_BTC); let lp_of_alice_2 = 31939004367616u128; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_2 ); let lp_of_bob = 0u128; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), + ::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), lp_of_bob ); assert_eq!(lp_total, lp_of_alice_2 + lp_of_bob); @@ -190,8 +189,16 @@ fn turn_on_protocol_fee_remove_liquidity_should_work() { // 2. first add_liquidity - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 10000 * DOT_UNIT)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 10000 * BTC_UNIT)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 10000 * DOT_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 10000 * BTC_UNIT + )); let total_supply_dot: u128 = 1 * DOT_UNIT; let total_supply_btc: u128 = 1 * BTC_UNIT; @@ -215,10 +222,10 @@ fn turn_on_protocol_fee_remove_liquidity_should_work() { let lp_of_alice_0 = 316227766016; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(DOT_UNIT) * U256::from(BTC_UNIT) @@ -242,18 +249,18 @@ fn turn_on_protocol_fee_remove_liquidity_should_work() { let lp_of_alice_1 = 16127616066816u128; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_1 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(51 * DOT_UNIT) * U256::from(51 * BTC_UNIT) ); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let balance_dot = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let balance_btc = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let balance_dot = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let balance_btc = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); //println!("balance_DOT {}, balance_BTC {}", balance_dot, balance_btc); assert_eq!(balance_dot, 51000000000000000); @@ -272,14 +279,14 @@ fn turn_on_protocol_fee_remove_liquidity_should_work() { 100 )); - let lp_total = ::MultiAssetsHandler::total_supply(LP_DOT_BTC); + let lp_total = ::MultiCurrency::total_issuance(LP_DOT_BTC); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_1 - lp_of_alice_0 ); let lp_of_bob = 0u128; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), + ::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), lp_of_bob ); assert_eq!(lp_total, lp_of_alice_1 - lp_of_alice_0 + lp_of_bob); @@ -304,9 +311,21 @@ fn turn_on_protocol_fee_swap_have_fee_should_work() { // 2. first add_liquidity - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, DOT_UNIT * 1000)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, BTC_UNIT * 1000)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, DOT_UNIT * 1000)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + DOT_UNIT * 1000 + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + BTC_UNIT * 1000 + )); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &CHARLIE, + DOT_UNIT * 1000 + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -330,10 +349,10 @@ fn turn_on_protocol_fee_swap_have_fee_should_work() { let lp_of_alice_0 = 316227766016; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(DOT_UNIT) * U256::from(BTC_UNIT) @@ -350,26 +369,26 @@ fn turn_on_protocol_fee_swap_have_fee_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(DOT_UNIT) * U256::from(BTC_UNIT) ); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let balance_dot = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let balance_btc = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let balance_dot = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let balance_btc = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); //println!("balance_DOT {}, balance_BTC {}", balance_dot, balance_btc); assert_eq!(balance_dot, 2000000000000000); assert_eq!(balance_btc, 50075113); let k_last = DexPallet::k_last(sorted_pair); - let reserve_0 = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let reserve_1 = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let reserve_0 = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let reserve_1 = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); let root_k = U256::from(reserve_0) .saturating_mul(U256::from(reserve_1)) .integer_sqrt(); @@ -377,7 +396,7 @@ fn turn_on_protocol_fee_swap_have_fee_should_work() { assert!(root_k > root_k_last); - let lp_total = ::MultiAssetsHandler::total_supply(LP_DOT_BTC); + let lp_total = ::MultiCurrency::total_issuance(LP_DOT_BTC); let numerator = U256::from(lp_total).saturating_mul(root_k.saturating_sub(root_k_last)); let denominator = root_k.saturating_mul(U256::from(5u128)).saturating_add(root_k_last); let expect_fee = numerator.checked_div(denominator).unwrap_or_default(); @@ -394,17 +413,17 @@ fn turn_on_protocol_fee_swap_have_fee_should_work() { 100 )); - let lp_total = ::MultiAssetsHandler::total_supply(LP_DOT_BTC); + let lp_total = ::MultiCurrency::total_issuance(LP_DOT_BTC); let lp_of_alice_2 = 474361420078u128; assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_2 ); let lp_of_bob = 39548424u128; assert_eq!(expect_fee, U256::from(lp_of_bob)); assert_eq!( - U256::from(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB)), + U256::from(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB)), expect_fee ); assert_eq!(lp_total, lp_of_alice_2 + lp_of_bob); @@ -427,9 +446,21 @@ fn turn_on_protocol_fee_swap_have_fee_at_should_work() { // 2. first add_liquidity - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100_000_000 * DOT_UNIT)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 100_000_000 * BTC_UNIT)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, 100_000_000 * DOT_UNIT)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 100_000_000 * DOT_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 100_000_000 * BTC_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &CHARLIE, + 100_000_000 * DOT_UNIT + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -456,10 +487,10 @@ fn turn_on_protocol_fee_swap_have_fee_at_should_work() { .integer_sqrt() .as_u128(); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(total_supply_btc) * U256::from(total_supply_dot) @@ -476,11 +507,11 @@ fn turn_on_protocol_fee_swap_have_fee_at_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); //println!("{:#?}", lp_of_alice_0); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); assert_eq!( DexPallet::k_last(sorted_pair), U256::from(total_supply_btc) * U256::from(total_supply_dot) @@ -489,8 +520,8 @@ fn turn_on_protocol_fee_swap_have_fee_at_should_work() { let k_last = DexPallet::k_last(sorted_pair); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let reserve_0 = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let reserve_1 = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let reserve_0 = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let reserve_1 = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); assert_eq!(reserve_0, total_supply_dot + 1 * DOT_UNIT); assert_eq!(reserve_1, total_supply_btc - 99699900); @@ -502,7 +533,7 @@ fn turn_on_protocol_fee_swap_have_fee_at_should_work() { assert!(root_k > root_k_last); - let lp_total = ::MultiAssetsHandler::total_supply(LP_DOT_BTC); + let lp_total = ::MultiCurrency::total_issuance(LP_DOT_BTC); let numerator = U256::from(lp_total).saturating_mul(root_k.saturating_sub(root_k_last)); let denominator = root_k.saturating_mul(U256::from(5u128)).saturating_add(root_k_last); let expect_fee = numerator.checked_div(denominator).unwrap_or_default(); @@ -522,21 +553,21 @@ fn turn_on_protocol_fee_swap_have_fee_at_should_work() { 100 )); - let lp_fee = ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB); + let lp_fee = ::MultiCurrency::free_balance(LP_DOT_BTC, &BOB); let alice_lp_add = (U256::from(lp_of_alice_0 + lp_fee) * U256::from(added_btc) / U256::from(reserve_1)).as_u128(); - let lp_total = ::MultiAssetsHandler::total_supply(LP_DOT_BTC); + let lp_total = ::MultiCurrency::total_issuance(LP_DOT_BTC); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 + alice_lp_add ); - let lp_of_bob = ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB); + let lp_of_bob = ::MultiCurrency::free_balance(LP_DOT_BTC, &BOB); assert_eq!(expect_fee, U256::from(lp_of_bob)); assert_eq!( - U256::from(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB)), + U256::from(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB)), expect_fee ); assert_eq!(lp_total, lp_of_alice_0 + alice_lp_add + lp_of_bob); @@ -559,9 +590,21 @@ fn get_amount_out( fn should_set_custom_exchange_fee() { new_test_ext().execute_with(|| { // add_liquidity - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100_000_000 * DOT_UNIT)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 100_000_000 * BTC_UNIT)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, 100_000_000 * DOT_UNIT)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 100_000_000 * DOT_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 100_000_000 * BTC_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &CHARLIE, + 100_000_000 * DOT_UNIT + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -596,10 +639,10 @@ fn should_set_custom_exchange_fee() { .integer_sqrt() .as_u128(); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); - assert_eq!(::MultiAssetsHandler::balance_of(LP_DOT_BTC, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(LP_DOT_BTC, &BOB), 0); // swap assert_ok!(DexPallet::inner_swap_exact_assets_for_assets( @@ -611,13 +654,13 @@ fn should_set_custom_exchange_fee() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(LP_DOT_BTC, &ALICE), + ::MultiCurrency::free_balance(LP_DOT_BTC, &ALICE), lp_of_alice_0 ); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let reserve_0 = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let reserve_1 = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let reserve_0 = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let reserve_1 = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); assert_eq!(reserve_0, total_supply_dot + DOT_UNIT); diff --git a/crates/dex-general/src/foreign/mock.rs b/crates/dex-general/src/foreign/mock.rs deleted file mode 100644 index 9d339b73b4..0000000000 --- a/crates/dex-general/src/foreign/mock.rs +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright 2021-2022 Zenlink. -// Licensed under Apache 2.0. - -//! Test utilities -use frame_support::{parameter_types, PalletId}; -use sp_core::H256; -use sp_runtime::{ - testing::Header, - traits::{BlakeTwo256, IdentityLookup}, -}; - -use crate as pallet_dex_general; -pub use crate::{ - AssetId, Config, DexGeneralMultiAssets, MultiAssetsHandler, PairLpGenerate, Pallet, ParaId, LIQUIDITY, LOCAL, - NATIVE, RESERVED, -}; - -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; -type Block = frame_system::mocking::MockBlock; - -frame_support::construct_runtime!( - pub enum Test where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Config, Storage, Event} = 0, - Balances: pallet_balances::{Pallet, Call, Storage, Config, Event} = 8, - DexGeneral: pallet_dex_general::{Pallet, Call, Storage, Event} = 9, - } -); - -parameter_types! { - pub const ExistentialDeposit: u64 = 1; - - pub const BlockHashCount: u64 = 250; - pub const DexGeneralPalletId: PalletId = PalletId(*b"dex/genr"); - pub const MaxReserves: u32 = 50; -} - -impl frame_system::Config for Test { - type BaseCallFilter = frame_support::traits::Everything; - type RuntimeOrigin = RuntimeOrigin; - type Index = u64; - type RuntimeCall = RuntimeCall; - type BlockNumber = u64; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = u128; - type Lookup = IdentityLookup; - type Header = Header; - type RuntimeEvent = RuntimeEvent; - type BlockHashCount = BlockHashCount; - type DbWeight = (); - type Version = (); - type AccountData = pallet_balances::AccountData; - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type PalletInfo = PalletInfo; - type BlockWeights = (); - type BlockLength = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; -} - -impl pallet_balances::Config for Test { - type Balance = u128; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Pallet; - type WeightInfo = (); - type MaxLocks = (); - type MaxReserves = MaxReserves; - type ReserveIdentifier = [u8; 8]; -} - -impl Config for Test { - type RuntimeEvent = RuntimeEvent; - type MultiAssetsHandler = DexGeneralMultiAssets; - type PalletId = DexGeneralPalletId; - type AssetId = AssetId; - type LpGenerate = PairLpGenerate; - type SelfParaId = (); - type WeightInfo = (); -} - -pub type DexPallet = Pallet; - -pub fn new_test_ext() -> sp_io::TestExternalities { - let mut t = frame_system::GenesisConfig::default() - .build_storage::() - .unwrap() - .into(); - pallet_balances::GenesisConfig:: { - balances: vec![ - (1, 34028236692093846346337460743176821145), - (2, 10), - (3, 10), - (4, 10), - (5, 10), - ], - } - .assimilate_storage(&mut t) - .unwrap(); - t.into() -} diff --git a/crates/dex-general/src/foreign/mod.rs b/crates/dex-general/src/foreign/mod.rs deleted file mode 100644 index fe41c412e3..0000000000 --- a/crates/dex-general/src/foreign/mod.rs +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright 2021-2022 Zenlink. -// Licensed under Apache 2.0. - -//! # Foreign Asset Module -//! -//! ## Overview -//! -//! Built-in assets module in which handles the foreign assets that are -//! reserved on other chains and teleported to this chain by xcm - -use super::*; - -#[cfg(test)] -mod mock; -#[cfg(test)] -mod tests; - -// The foreign which reserved other chain assets -impl Pallet { - /// public mutable functions - - /// Implement of the transfer function. - pub(crate) fn foreign_transfer( - id: T::AssetId, - owner: &T::AccountId, - target: &T::AccountId, - amount: AssetBalance, - ) -> DispatchResult { - let owner_balance = >::get((&id, owner)); - ensure!(owner_balance >= amount, Error::::InsufficientAssetBalance); - - let new_balance = owner_balance.checked_sub(amount).ok_or(Error::::Overflow)?; - - >::mutate((id, owner), |balance| *balance = new_balance); - >::try_mutate((id, target), |balance| -> DispatchResult { - *balance = balance.checked_add(amount).ok_or(Error::::Overflow)?; - Ok(()) - })?; - - Self::deposit_event(Event::Transferred(id, owner.clone(), target.clone(), amount)); - - Ok(()) - } - - /// Increase the total supply of the foreign - /// Note: no need check Exists, because it be created when it not exist - pub(crate) fn foreign_mint(id: T::AssetId, owner: &T::AccountId, amount: AssetBalance) -> DispatchResult { - if !Self::foreign_list().contains(&id) { - >::mutate(|assets| assets.push(id)); - } - - let new_balance = >::get((id, owner)) - .checked_add(amount) - .ok_or(Error::::Overflow)?; - - >::try_mutate::<_, _, Error, _>((id, owner), |balance| { - *balance = new_balance; - - Ok(()) - })?; - - >::try_mutate::<_, _, Error, _>(id, |supply| { - *supply = supply.checked_add(amount).ok_or(Error::::Overflow)?; - - Ok(()) - })?; - - Self::deposit_event(Event::Minted(id, owner.clone(), amount)); - - Ok(()) - } - - /// Decrease the total supply of the foreign - pub(crate) fn foreign_burn(id: T::AssetId, owner: &T::AccountId, amount: AssetBalance) -> DispatchResult { - ensure!(Self::foreign_list().contains(&id), Error::::AssetNotExists); - let new_balance = >::get((id, owner)) - .checked_sub(amount) - .ok_or(Error::::InsufficientAssetBalance)?; - - >::mutate((id, owner), |balance| *balance = new_balance); - - >::try_mutate(id, |supply| -> DispatchResult { - *supply = supply.checked_sub(amount).ok_or(Error::::Overflow)?; - Ok(()) - })?; - - Self::deposit_event(Event::Burned(id, owner.clone(), amount)); - - Ok(()) - } - - // Public immutable functions - - /// Get the foreign `id` balance of `owner`. - pub fn foreign_balance_of(id: T::AssetId, owner: &T::AccountId) -> AssetBalance { - Self::foreign_ledger((id, owner)) - } - - /// Get the total supply of an foreign `id`. - pub fn foreign_total_supply(id: T::AssetId) -> AssetBalance { - Self::foreign_meta(id) - } - - pub fn foreign_is_exists(id: T::AssetId) -> bool { - Self::foreign_list().contains(&id) - } -} diff --git a/crates/dex-general/src/foreign/tests.rs b/crates/dex-general/src/foreign/tests.rs deleted file mode 100644 index 3b91592b07..0000000000 --- a/crates/dex-general/src/foreign/tests.rs +++ /dev/null @@ -1,308 +0,0 @@ -// Copyright 2021-2022 Zenlink. -// Licensed under Apache 2.0. - -use frame_support::{assert_noop, assert_ok}; -use frame_system::RawOrigin; - -use super::{mock::*, AssetId, Error, MultiAssetsHandler}; - -// Native Currency -const DEV_ASSET_ID: AssetId = AssetId { - chain_id: 0, - asset_type: NATIVE, - asset_index: 0, -}; - -// Foreign Liquidity -const LP_ASSET_ID: AssetId = AssetId { - chain_id: 100, - asset_type: LIQUIDITY, - asset_index: 1, -}; - -const DOT_ASSET_ID: AssetId = AssetId { - chain_id: 200, - asset_type: LOCAL, - asset_index: 2, -}; - -const BTC_ASSET_ID: AssetId = AssetId { - chain_id: 300, - asset_type: RESERVED, - asset_index: 3, -}; - -const ETH_ASSET_ID: AssetId = AssetId { - chain_id: 400, - asset_type: NATIVE, - asset_index: 0, -}; - -const ALICE: u128 = 1; -const BOB: u128 = 2; -const CHARLIE: u128 = 3; - -#[test] -fn foreign_mint_should_work() { - new_test_ext().execute_with(|| { - assert!(!DexPallet::foreign_is_exists(LP_ASSET_ID)); - assert!(!DexPallet::foreign_is_exists(DOT_ASSET_ID)); - assert!(!DexPallet::foreign_is_exists(BTC_ASSET_ID)); - assert!(!DexPallet::foreign_is_exists(ETH_ASSET_ID)); - - assert_ok!(DexPallet::foreign_mint(LP_ASSET_ID, &ALICE, 0)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 0)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 100)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &ALICE, 1000)); - - // Native Currency - assert!(::MultiAssetsHandler::is_exists(DEV_ASSET_ID)); - - assert!(DexPallet::foreign_is_exists(LP_ASSET_ID)); - assert!(DexPallet::foreign_is_exists(DOT_ASSET_ID)); - assert!(DexPallet::foreign_is_exists(BTC_ASSET_ID)); - assert!(DexPallet::foreign_is_exists(ETH_ASSET_ID)); - - assert_eq!(DexPallet::foreign_balance_of(LP_ASSET_ID, &ALICE), 0); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 0); - assert_eq!(DexPallet::foreign_balance_of(BTC_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_balance_of(ETH_ASSET_ID, &ALICE), 1000); - }); -} - -#[test] -fn foreign_mint_more_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, 100)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, 100)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &BOB, 100)); - - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, 1000)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &CHARLIE, 1000)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &CHARLIE, 1000)); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 100); - assert_eq!(DexPallet::foreign_balance_of(BTC_ASSET_ID, &BOB), 100); - assert_eq!(DexPallet::foreign_balance_of(ETH_ASSET_ID, &BOB), 100); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &CHARLIE), 1000); - assert_eq!(DexPallet::foreign_balance_of(BTC_ASSET_ID, &CHARLIE), 1000); - assert_eq!(DexPallet::foreign_balance_of(ETH_ASSET_ID, &CHARLIE), 1000); - }); -} - -#[test] -fn querying_assets_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_ok!(DexPallet::foreign_transfer(DOT_ASSET_ID, &ALICE, &BOB, 50)); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 50); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 50); - - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - }); -} - -#[test] -fn querying_total_supply_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - - assert_ok!(DexPallet::foreign_transfer(DOT_ASSET_ID, &ALICE, &BOB, 50)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 50); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 50); - - assert_ok!(DexPallet::foreign_transfer(DOT_ASSET_ID, &BOB, &CHARLIE, 31)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 50); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 19); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &CHARLIE), 31); - - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - }); -} - -#[test] -fn transferring_amount_above_available_balance_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - - assert_ok!(DexPallet::transfer( - RawOrigin::Signed(ALICE).into(), - DOT_ASSET_ID, - BOB, - 50 - )); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 50); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 50); - }); -} - -#[test] -fn transferring_zero_unit_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 0); - - assert_ok!(DexPallet::transfer( - RawOrigin::Signed(ALICE).into(), - DOT_ASSET_ID, - BOB, - 0 - )); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 0); - }); -} - -#[test] -fn transferring_more_units_than_total_supply_should_not_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_noop!( - DexPallet::transfer(RawOrigin::Signed(ALICE).into(), DOT_ASSET_ID, BOB, 101), - Error::::InsufficientAssetBalance - ); - }); -} - -#[test] -fn foreign_burn_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - - assert_ok!(DexPallet::foreign_burn(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 0); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 0); - }); -} - -#[test] -fn foreign_burn_should_not_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - - assert_noop!( - DexPallet::foreign_burn(DOT_ASSET_ID, &ALICE, 200), - Error::::InsufficientAssetBalance, - ); - }); -} - -#[test] -fn foreign_mint_transfer_burn_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - - assert_ok!(DexPallet::foreign_burn(DOT_ASSET_ID, &ALICE, 100)); - - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 200)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 200); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 200); - - assert_ok!(DexPallet::transfer( - RawOrigin::Signed(ALICE).into(), - DOT_ASSET_ID, - BOB, - 50 - )); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 150); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 50); - - assert_noop!( - DexPallet::foreign_burn(DOT_ASSET_ID, &ALICE, 200), - Error::::InsufficientAssetBalance, - ); - }); -} - -#[test] -fn foreign_multi_asset_total_supply_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, 100)); - assert_eq!(DexPallet::foreign_balance_of(BTC_ASSET_ID, &BOB), 100); - assert_eq!(DexPallet::foreign_total_supply(BTC_ASSET_ID), 100); - - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &CHARLIE, 100)); - assert_eq!(DexPallet::foreign_balance_of(ETH_ASSET_ID, &CHARLIE), 100); - assert_eq!(DexPallet::foreign_total_supply(ETH_ASSET_ID), 100); - - assert_eq!( - DexPallet::foreign_list(), - vec![ - AssetId { - chain_id: 200, - asset_type: LOCAL, - asset_index: 2 - }, - AssetId { - chain_id: 300, - asset_type: RESERVED, - asset_index: 3 - }, - AssetId { - chain_id: 400, - asset_type: NATIVE, - asset_index: 0 - } - ] - ); - }); -} - -#[test] -fn foreign_multi_asset_withdraw_to_dex_general_should_work() { - new_test_ext().execute_with(|| { - assert_eq!(::MultiAssetsHandler::total_supply(DOT_ASSET_ID), 0); - assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &ALICE), - 0 - ); - - assert_ok!(::MultiAssetsHandler::deposit(DOT_ASSET_ID, &ALICE, 100)); - assert_ok!(::MultiAssetsHandler::withdraw(DOT_ASSET_ID, &ALICE, 50)); - - assert_eq!(::MultiAssetsHandler::total_supply(DOT_ASSET_ID), 50); - assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &ALICE), - 50 - ); - }); -} - -#[test] -fn foreign_multi_asset_transfer_should_work() { - new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 100)); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 100); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - - assert_ok!(::MultiAssetsHandler::transfer( - DOT_ASSET_ID, - &ALICE, - &BOB, - 50 - )); - - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &ALICE), 50); - assert_eq!(DexPallet::foreign_balance_of(DOT_ASSET_ID, &BOB), 50); - assert_eq!(DexPallet::foreign_total_supply(DOT_ASSET_ID), 100); - }); -} diff --git a/crates/dex-general/src/lib.rs b/crates/dex-general/src/lib.rs index 8a53819cea..dfda64debe 100644 --- a/crates/dex-general/src/lib.rs +++ b/crates/dex-general/src/lib.rs @@ -17,28 +17,16 @@ #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::unused_unit)] -#[cfg(feature = "std")] -use serde::{Deserialize, Serialize}; - pub use pallet::*; use codec::{Decode, Encode, FullCodec}; -use frame_support::{ - inherent::Vec, - pallet_prelude::*, - sp_runtime::SaturatedConversion, - traits::{Currency, ExistenceRequirement, ExistenceRequirement::KeepAlive, Get, WithdrawReasons}, - PalletId, RuntimeDebug, -}; +use frame_support::{inherent::Vec, pallet_prelude::*, traits::Get, PalletId, RuntimeDebug}; +use orml_traits::MultiCurrency; use sp_core::U256; use sp_runtime::traits::{AccountIdConversion, Hash, MaybeSerializeDeserialize, One, StaticLookup, Zero}; -use sp_std::{collections::btree_map::BTreeMap, convert::TryInto, fmt::Debug, marker::PhantomData, prelude::*, vec}; - -pub use cumulus_primitives_core::ParaId; +use sp_std::{collections::btree_map::BTreeMap, convert::TryInto, fmt::Debug, prelude::*, vec}; mod fee; -mod foreign; -mod multiassets; mod primitives; mod rpc; mod swap; @@ -50,15 +38,17 @@ pub mod benchmarking; mod default_weights; pub use default_weights::WeightInfo; -pub use multiassets::{DexGeneralMultiAssets, MultiAssetsHandler}; pub use primitives::{ - AssetBalance, AssetId, AssetInfo, BootstrapParameter, PairLpGenerate, PairMetadata, PairStatus, + AssetBalance, AssetInfo, BootstrapParameter, PairMetadata, PairStatus, PairStatus::{Bootstrap, Disable, Trading}, - DEFAULT_FEE_RATE, FEE_ADJUSTMENT, LIQUIDITY, LOCAL, NATIVE, RESERVED, + DEFAULT_FEE_RATE, FEE_ADJUSTMENT, }; pub use rpc::PairInfo; pub use traits::{ExportDexGeneral, GenerateLpAssetId}; +#[allow(type_alias_bounds)] +type AccountIdOf = ::AccountId; + #[frame_support::pallet] pub mod pallet { use super::*; @@ -70,8 +60,8 @@ pub mod pallet { pub trait Config: frame_system::Config { /// Because this pallet emits events, it depends on the runtime's definition of an event. type RuntimeEvent: From> + IsType<::RuntimeEvent>; - /// The assets interface beyond native currency and other assets. - type MultiAssetsHandler: MultiAssetsHandler; + /// The trait control all currencies + type MultiCurrency: MultiCurrency, CurrencyId = Self::AssetId, Balance = AssetBalance>; /// This pallet id. #[pallet::constant] type PalletId: Get; @@ -89,10 +79,6 @@ pub mod pallet { + MaxEncodedLen; /// Generate the AssetId for the pair. type LpGenerate: GenerateLpAssetId; - - /// This parachain id. - type SelfParaId: Get; - /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } @@ -102,22 +88,6 @@ pub mod pallet { #[pallet::generate_store(pub(super) trait Store)] pub struct Pallet(_); - /// Foreign foreign storage - #[pallet::storage] - #[pallet::getter(fn foreign_ledger)] - /// The number of units of assets held by any given account. - pub type ForeignLedger = - StorageMap<_, Blake2_128Concat, (T::AssetId, T::AccountId), AssetBalance, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn foreign_meta)] - /// TWOX-NOTE: `AssetId` is trusted, so this is safe. - pub type ForeignMeta = StorageMap<_, Twox64Concat, T::AssetId, AssetBalance, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn foreign_list)] - pub type ForeignList = StorageValue<_, Vec, ValueQuery>; - #[pallet::storage] #[pallet::getter(fn k_last)] /// Refer: https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2Pair.sol#L88 @@ -229,15 +199,6 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - /// Foreign Asset - - /// Some assets were transferred. \[asset_id, owner, target, amount\] - Transferred(T::AssetId, T::AccountId, T::AccountId, AssetBalance), - /// Some assets were burned. \[asset_id, owner, amount\] - Burned(T::AssetId, T::AccountId, AssetBalance), - /// Some assets were minted. \[asset_id, owner, amount\] - Minted(T::AssetId, T::AccountId, AssetBalance), - /// Swap /// Create a trading pair. \[asset_0, asset_1\] @@ -418,7 +379,9 @@ pub mod pallet { /// - `send_to`: /// (1) Some(receiver): it turn on the protocol fee and the new receiver account. /// (2) None: it turn off the protocol fee. + #[pallet::call_index(0)] #[pallet::weight(T::WeightInfo::set_fee_receiver())] + #[frame_support::transactional] pub fn set_fee_receiver( origin: OriginFor, send_to: Option<::Source>, @@ -447,7 +410,9 @@ pub mod pallet { /// 0 means no protocol fee. /// 30 means 0.3% * 100% = 0.0030. /// default is 5 and means 0.3% * 1 / 6 = 0.0005. + #[pallet::call_index(1)] #[pallet::weight(T::WeightInfo::set_fee_point())] + #[frame_support::transactional] pub fn set_fee_point(origin: OriginFor, fee_point: u8) -> DispatchResult { ensure_root(origin)?; ensure!(fee_point <= 30, Error::::InvalidFeePoint); @@ -458,7 +423,9 @@ pub mod pallet { } /// Set the exchange fee rate. + #[pallet::call_index(2)] #[pallet::weight(T::WeightInfo::set_fee_point())] + #[frame_support::transactional] pub fn set_exchange_fee( origin: OriginFor, asset_0: T::AssetId, @@ -490,30 +457,6 @@ pub mod pallet { Ok(()) } - /// Move some assets from one holder to another. - /// - /// # Arguments - /// - /// - `asset_id`: The foreign id. - /// - `target`: The receiver of the foreign. - /// - `amount`: The amount of the foreign to transfer. - #[pallet::weight(1_000_000)] - pub fn transfer( - origin: OriginFor, - asset_id: T::AssetId, - recipient: ::Source, - #[pallet::compact] amount: AssetBalance, - ) -> DispatchResult { - let origin = ensure_signed(origin)?; - let target = T::Lookup::lookup(recipient)?; - let balance = T::MultiAssetsHandler::balance_of(asset_id, &origin); - ensure!(balance >= amount, Error::::InsufficientAssetBalance); - - T::MultiAssetsHandler::transfer(asset_id, &origin, &target, amount)?; - - Ok(()) - } - /// Create pair by two assets. /// /// The order of assets does not effect the result. @@ -522,7 +465,9 @@ pub mod pallet { /// /// - `asset_0`: Asset which make up Pair /// - `asset_1`: Asset which make up Pair + #[pallet::call_index(3)] #[pallet::weight(T::WeightInfo::create_pair())] + #[frame_support::transactional] pub fn create_pair(origin: OriginFor, asset_0: T::AssetId, asset_1: T::AssetId) -> DispatchResult { ensure_root(origin)?; ensure!( @@ -532,9 +477,6 @@ pub mod pallet { ensure!(asset_0 != asset_1, Error::::DeniedCreatePair); - ensure!(T::MultiAssetsHandler::is_exists(asset_0), Error::::AssetNotExists); - ensure!(T::MultiAssetsHandler::is_exists(asset_1), Error::::AssetNotExists); - let pair = Self::sort_asset_id(asset_0, asset_1); PairStatuses::::try_mutate(pair, |status| match status { Trading(_) => Err(Error::::PairAlreadyExists), @@ -581,6 +523,7 @@ pub mod pallet { /// - `amount_0_min`: Minimum amount of asset_0 added to the pair /// - `amount_1_min`: Minimum amount of asset_1 added to the pair /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(4)] #[pallet::weight(T::WeightInfo::add_liquidity())] #[frame_support::transactional] #[allow(clippy::too_many_arguments)] @@ -625,6 +568,7 @@ pub mod pallet { /// - `amount_asset_1_min`: Minimum amount of asset_1 to exact /// - `recipient`: Account that accepts withdrawal of assets /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(5)] #[pallet::weight(T::WeightInfo::remove_liquidity())] #[frame_support::transactional] #[allow(clippy::too_many_arguments)] @@ -667,6 +611,7 @@ pub mod pallet { /// - `path`: path can convert to pairs. /// - `recipient`: Account that receive the target asset /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(6)] #[pallet::weight(T::WeightInfo::swap_exact_assets_for_assets())] #[frame_support::transactional] pub fn swap_exact_assets_for_assets( @@ -695,6 +640,7 @@ pub mod pallet { /// - `path`: path can convert to pairs. /// - `recipient`: Account that receive the target asset /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(7)] #[pallet::weight(T::WeightInfo::swap_assets_for_exact_assets())] #[frame_support::transactional] pub fn swap_assets_for_exact_assets( @@ -727,6 +673,7 @@ pub mod pallet { /// - `capacity_supply_0`: The max amount of asset_0 total contribute /// - `capacity_supply_1`: The max amount of asset_1 total contribute /// - `end`: The earliest ending block. + #[pallet::call_index(8)] #[pallet::weight(T::WeightInfo::bootstrap_create())] #[frame_support::transactional] #[allow(clippy::too_many_arguments)] @@ -838,6 +785,7 @@ pub mod pallet { /// - `amount_0_contribute`: The amount of asset_0 contribute to this bootstrap pair /// - `amount_1_contribute`: The amount of asset_1 contribute to this bootstrap pair /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(9)] #[pallet::weight(T::WeightInfo::bootstrap_contribute())] #[frame_support::transactional] pub fn bootstrap_contribute( @@ -868,6 +816,7 @@ pub mod pallet { /// - `asset_0`: Asset which make up bootstrap pair /// - `asset_1`: Asset which make up bootstrap pair /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(10)] #[pallet::weight(T::WeightInfo::bootstrap_claim())] #[frame_support::transactional] pub fn bootstrap_claim( @@ -892,6 +841,7 @@ pub mod pallet { /// /// - `asset_0`: Asset which make up bootstrap pair /// - `asset_1`: Asset which make up bootstrap pair + #[pallet::call_index(11)] #[pallet::weight(T::WeightInfo::bootstrap_end())] #[frame_support::transactional] pub fn bootstrap_end(origin: OriginFor, asset_0: T::AssetId, asset_1: T::AssetId) -> DispatchResult { @@ -914,6 +864,7 @@ pub mod pallet { /// - `capacity_supply_0`: The new max amount of asset_0 total contribute /// - `capacity_supply_1`: The new max amount of asset_1 total contribute /// - `end`: The earliest ending block. + #[pallet::call_index(12)] #[pallet::weight(T::WeightInfo::bootstrap_update())] #[frame_support::transactional] #[allow(clippy::too_many_arguments)] @@ -995,6 +946,7 @@ pub mod pallet { /// /// - `asset_0`: Asset which make up bootstrap pair /// - `asset_1`: Asset which make up bootstrap pair + #[pallet::call_index(13)] #[pallet::weight(T::WeightInfo::bootstrap_refund())] #[frame_support::transactional] pub fn bootstrap_refund(origin: OriginFor, asset_0: T::AssetId, asset_1: T::AssetId) -> DispatchResult { @@ -1002,6 +954,7 @@ pub mod pallet { Self::do_bootstrap_refund(who, asset_0, asset_1) } + #[pallet::call_index(14)] #[pallet::weight(100_000_000)] #[frame_support::transactional] pub fn bootstrap_charge_reward( @@ -1022,7 +975,7 @@ pub mod pallet { for (asset_id, amount) in &charge_rewards { let already_charge_amount = rewards.get(asset_id).ok_or(Error::::NoRewardTokens)?; - T::MultiAssetsHandler::transfer(*asset_id, &who, &Self::account_id(), *amount)?; + T::MultiCurrency::transfer(*asset_id, &who, &Self::account_id(), *amount)?; let new_charge_amount = already_charge_amount.checked_add(*amount).ok_or(Error::::Overflow)?; rewards.insert(*asset_id, new_charge_amount); @@ -1036,6 +989,7 @@ pub mod pallet { Ok(()) } + #[pallet::call_index(15)] #[pallet::weight(100_000_000)] #[frame_support::transactional] pub fn bootstrap_withdraw_reward( @@ -1050,7 +1004,7 @@ pub mod pallet { BootstrapRewards::::try_mutate(pair, |rewards| -> DispatchResult { for (asset_id, amount) in rewards { - T::MultiAssetsHandler::transfer(*asset_id, &Self::account_id(), &recipient, *amount)?; + T::MultiCurrency::transfer(*asset_id, &Self::account_id(), &recipient, *amount)?; *amount = Zero::zero(); } diff --git a/crates/dex-general/src/multiassets.rs b/crates/dex-general/src/multiassets.rs deleted file mode 100644 index 8be2196a24..0000000000 --- a/crates/dex-general/src/multiassets.rs +++ /dev/null @@ -1,177 +0,0 @@ -// Copyright 2021-2022 Zenlink. -// Licensed under Apache 2.0. - -use super::*; - -pub trait MultiAssetsHandler { - fn balance_of(asset_id: AssetId, who: &AccountId) -> AssetBalance; - - fn total_supply(asset_id: AssetId) -> AssetBalance; - - fn is_exists(asset_id: AssetId) -> bool; - - fn transfer(asset_id: AssetId, origin: &AccountId, target: &AccountId, amount: AssetBalance) -> DispatchResult { - let withdrawn = Self::withdraw(asset_id, origin, amount)?; - let _ = Self::deposit(asset_id, target, withdrawn)?; - - Ok(()) - } - - fn deposit(asset_id: AssetId, target: &AccountId, amount: AssetBalance) -> Result; - - fn withdraw(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result; -} - -impl MultiAssetsHandler for () { - fn balance_of(_asset_id: AssetId, _who: &AccountId) -> AssetBalance { - Default::default() - } - - fn total_supply(_asset_id: AssetId) -> AssetBalance { - Default::default() - } - - fn is_exists(_asset_id: AssetId) -> bool { - false - } - - fn transfer(_asset_id: AssetId, _origin: &AccountId, _target: &AccountId, _amount: AssetBalance) -> DispatchResult { - Ok(()) - } - - fn deposit(_asset_id: AssetId, _target: &AccountId, _amount: AssetBalance) -> Result { - Ok(Default::default()) - } - - fn withdraw(_asset_id: AssetId, _origin: &AccountId, _amount: AssetBalance) -> Result { - Ok(Default::default()) - } -} - -pub struct DexGeneralMultiAssets(PhantomData<(T, Native, Local, Other)>); - -impl, NativeCurrency, Local, Other> MultiAssetsHandler - for DexGeneralMultiAssets, NativeCurrency, Local, Other> -where - NativeCurrency: Currency, - Local: MultiAssetsHandler, - Other: MultiAssetsHandler, -{ - fn balance_of(asset_id: AssetId, who: &::AccountId) -> AssetBalance { - let self_chain_id: u32 = T::SelfParaId::get(); - match asset_id.asset_type { - NATIVE if asset_id.is_native(self_chain_id) => { - NativeCurrency::free_balance(who).saturated_into::() - } - LOCAL | LIQUIDITY if asset_id.chain_id == self_chain_id => Local::balance_of(asset_id, who), - RESERVED if asset_id.chain_id == self_chain_id => Other::balance_of(asset_id, who), - _ if asset_id.is_foreign(self_chain_id) => Pallet::::foreign_balance_of(asset_id, who), - _ => Default::default(), - } - } - - fn total_supply(asset_id: AssetId) -> AssetBalance { - let self_chain_id: u32 = T::SelfParaId::get(); - match asset_id.asset_type { - NATIVE if asset_id.is_native(T::SelfParaId::get()) => { - NativeCurrency::total_issuance().saturated_into::() - } - LOCAL | LIQUIDITY if asset_id.chain_id == self_chain_id => Local::total_supply(asset_id), - RESERVED if asset_id.chain_id == self_chain_id => Other::total_supply(asset_id), - _ if asset_id.is_foreign(self_chain_id) => Pallet::::foreign_total_supply(asset_id), - _ => Default::default(), - } - } - - fn is_exists(asset_id: AssetId) -> bool { - let self_chain_id: u32 = T::SelfParaId::get(); - match asset_id.asset_type { - NATIVE if asset_id.chain_id == self_chain_id => asset_id.is_native(T::SelfParaId::get()), - LOCAL | LIQUIDITY if asset_id.chain_id == self_chain_id => Local::is_exists(asset_id), - RESERVED if asset_id.chain_id == self_chain_id => Other::is_exists(asset_id), - _ if asset_id.is_foreign(T::SelfParaId::get()) => Pallet::::foreign_is_exists(asset_id), - _ => Default::default(), - } - } - - fn transfer( - asset_id: AssetId, - origin: &::AccountId, - target: &::AccountId, - amount: AssetBalance, - ) -> DispatchResult { - let self_chain_id: u32 = T::SelfParaId::get(); - match asset_id.asset_type { - NATIVE if asset_id.is_native(T::SelfParaId::get()) => { - let balance_amount = amount - .try_into() - .map_err(|_| DispatchError::Other("AmountToBalanceConversionFailed"))?; - - NativeCurrency::transfer(origin, target, balance_amount, KeepAlive) - } - LOCAL | LIQUIDITY if asset_id.chain_id == self_chain_id => { - Local::transfer(asset_id, origin, target, amount) - } - RESERVED if asset_id.chain_id == self_chain_id => Other::transfer(asset_id, origin, target, amount), - _ if asset_id.is_foreign(T::SelfParaId::get()) => { - Pallet::::foreign_transfer(asset_id, origin, target, amount) - } - _ => Err(Error::::UnsupportedAssetType.into()), - } - } - - fn deposit( - asset_id: AssetId, - target: &::AccountId, - amount: AssetBalance, - ) -> Result { - let self_chain_id: u32 = T::SelfParaId::get(); - match asset_id.asset_type { - NATIVE if asset_id.is_native(T::SelfParaId::get()) => { - let balance_amount = amount - .try_into() - .map_err(|_| DispatchError::Other("AmountToBalanceConversionFailed"))?; - - let _ = NativeCurrency::deposit_creating(target, balance_amount); - - Ok(amount) - } - LOCAL | LIQUIDITY if asset_id.chain_id == self_chain_id => Local::deposit(asset_id, target, amount), - RESERVED if asset_id.chain_id == self_chain_id => Other::deposit(asset_id, target, amount), - _ if asset_id.is_foreign(T::SelfParaId::get()) => { - Pallet::::foreign_mint(asset_id, target, amount).map(|_| amount) - } - _ => Err(Error::::UnsupportedAssetType.into()), - } - } - - fn withdraw( - asset_id: AssetId, - origin: &::AccountId, - amount: AssetBalance, - ) -> Result { - let self_chain_id: u32 = T::SelfParaId::get(); - match asset_id.asset_type { - NATIVE if asset_id.is_native(self_chain_id) => { - let balance_amount = amount - .try_into() - .map_err(|_| DispatchError::Other("AmountToBalanceConversionFailed"))?; - - let _ = NativeCurrency::withdraw( - origin, - balance_amount, - WithdrawReasons::TRANSFER, - ExistenceRequirement::AllowDeath, - )?; - - Ok(amount) - } - LOCAL | LIQUIDITY if asset_id.chain_id == self_chain_id => Local::withdraw(asset_id, origin, amount), - RESERVED if asset_id.chain_id == self_chain_id => Other::withdraw(asset_id, origin, amount), - _ if asset_id.is_foreign(T::SelfParaId::get()) => { - Pallet::::foreign_burn(asset_id, origin, amount).map(|_| amount) - } - _ => Err(Error::::UnsupportedAssetType.into()), - } - } -} diff --git a/crates/dex-general/src/primitives.rs b/crates/dex-general/src/primitives.rs index e870c0329b..aa6d5ab361 100644 --- a/crates/dex-general/src/primitives.rs +++ b/crates/dex-general/src/primitives.rs @@ -3,7 +3,6 @@ use super::*; use scale_info::TypeInfo; -use sp_std::marker::PhantomData; pub type AssetBalance = u128; @@ -11,62 +10,10 @@ pub type AssetBalance = u128; pub const DEFAULT_FEE_RATE: u128 = 3; pub const FEE_ADJUSTMENT: u128 = 1000; -/// Native currency -pub const NATIVE: u8 = 0; -/// Swap module asset -pub const LIQUIDITY: u8 = 1; -/// Other asset type on this chain -pub const LOCAL: u8 = 2; -/// Reserved for future -pub const RESERVED: u8 = 3; - -/// AssetId use to locate assets in framed base chain. -#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] -#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Default))] -pub struct AssetId { - /// Parachain ID - pub chain_id: u32, - /// Pallet ID - pub asset_type: u8, - /// Index of asset within that pallet - pub asset_index: u64, -} - pub trait AssetInfo { fn is_support(&self) -> bool; } -impl AssetInfo for AssetId { - fn is_support(&self) -> bool { - matches!(self.asset_type, NATIVE | LIQUIDITY | LOCAL | RESERVED) - } -} - -impl AssetId { - pub fn is_native(&self, self_chain_id: u32) -> bool { - self.chain_id == self_chain_id && self.asset_type == NATIVE && self.asset_index == 0 - } - - pub fn is_foreign(&self, self_chain_id: u32) -> bool { - self.chain_id != self_chain_id - } -} - -pub struct PairLpGenerate(PhantomData); -impl GenerateLpAssetId for PairLpGenerate { - fn generate_lp_asset_id(asset_0: AssetId, asset_1: AssetId) -> Option { - let currency_0 = (asset_0.asset_index & 0x0000_0000_0000_ffff) << 16; - let currency_1 = (asset_1.asset_index & 0x0000_0000_0000_ffff) << 32; - let discr = 6u64 << 8; - let index = currency_0 + currency_1 + discr; - Some(AssetId { - chain_id: T::SelfParaId::get(), - asset_type: LOCAL, - asset_index: index, - }) - } -} - /// Status for TradingPair #[derive(Clone, Copy, Encode, Decode, RuntimeDebug, PartialEq, Eq, MaxEncodedLen, TypeInfo)] pub enum PairStatus { diff --git a/crates/dex-general/src/rpc.rs b/crates/dex-general/src/rpc.rs index f245b1db42..b965364bc7 100644 --- a/crates/dex-general/src/rpc.rs +++ b/crates/dex-general/src/rpc.rs @@ -49,8 +49,8 @@ impl Pallet { let sorted_pair = Self::sort_asset_id(asset_0, asset_1); match Self::pair_status(sorted_pair) { Trading(metadata) => { - let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, &metadata.pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, &metadata.pair_account); + let reserve_0 = T::MultiCurrency::free_balance(asset_0, &metadata.pair_account); + let reserve_1 = T::MultiCurrency::free_balance(asset_1, &metadata.pair_account); Self::calculate_added_amount( amount_0_desired, amount_1_desired, @@ -84,10 +84,10 @@ impl Pallet { asset_0, asset_1, account: pair_account.clone(), - total_liquidity: T::MultiAssetsHandler::total_supply(lp_asset_id), + total_liquidity: T::MultiCurrency::total_issuance(lp_asset_id), holding_liquidity: Zero::zero(), - reserve_0: T::MultiAssetsHandler::balance_of(asset_0, &pair_account), - reserve_1: T::MultiAssetsHandler::balance_of(asset_1, &pair_account), + reserve_0: T::MultiCurrency::free_balance(asset_0, &pair_account), + reserve_1: T::MultiCurrency::free_balance(asset_1, &pair_account), lp_asset_id, status, }) diff --git a/crates/dex-general/src/swap/mock.rs b/crates/dex-general/src/swap/mock.rs index c42da50723..ed82d39068 100644 --- a/crates/dex-general/src/swap/mock.rs +++ b/crates/dex-general/src/swap/mock.rs @@ -3,20 +3,12 @@ //! Test utilities -#[cfg(feature = "std")] -use std::marker::PhantomData; - use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use serde::{Deserialize, Serialize}; -use frame_support::{ - dispatch::{DispatchError, DispatchResult}, - pallet_prelude::GenesisBuild, - parameter_types, - traits::Contains, - PalletId, -}; +use frame_support::{parameter_types, traits::Contains, PalletId}; +use orml_traits::parameter_type_with_key; use sp_core::H256; use sp_runtime::{ testing::Header, @@ -25,11 +17,7 @@ use sp_runtime::{ }; use crate as pallet_dex_general; -pub use crate::{ - AssetBalance, AssetId, Config, DexGeneralMultiAssets, MultiAssetsHandler, PairLpGenerate, Pallet, ParaId, - LIQUIDITY, LOCAL, NATIVE, RESERVED, -}; -use orml_traits::{parameter_type_with_key, MultiCurrency}; +pub use crate::{AssetBalance, AssetInfo, Config, GenerateLpAssetId, Pallet}; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -41,16 +29,38 @@ pub enum CurrencyId { LpToken(u8, u8), } +impl CurrencyId { + pub fn join_lp_token(currency_id_0: Self, currency_id_1: Self) -> Option { + let lp_token_0 = match currency_id_0 { + CurrencyId::Token(x) => x, + _ => return None, + }; + let lp_token_1 = match currency_id_1 { + CurrencyId::Token(y) => y, + _ => return None, + }; + Some(CurrencyId::LpToken(lp_token_0, lp_token_1)) + } +} + +impl AssetInfo for CurrencyId { + fn is_support(&self) -> bool { + match self { + Self::Token(_) => true, + _ => false, + } + } +} + frame_support::construct_runtime!( pub enum Test where Block = Block, NodeBlock = Block, UncheckedExtrinsic = UncheckedExtrinsic, { - System: frame_system::{Pallet, Call, Config, Storage, Event} = 0, - Balances: pallet_balances::{Pallet, Call, Storage, Config, Event} = 8, - DexGeneral: pallet_dex_general::{Pallet, Call, Storage, Event} = 9, - Tokens: orml_tokens::{Pallet, Storage, Event, Config} = 11, + System: frame_system::{Pallet, Call, Config, Storage, Event}, + DexGeneral: pallet_dex_general::{Pallet, Call, Storage, Event}, + Tokens: orml_tokens::{Pallet, Storage, Event, Config}, } ); @@ -117,25 +127,19 @@ impl orml_tokens::Config for Test { type CurrencyHooks = (); } -impl pallet_balances::Config for Test { - type Balance = u128; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Pallet; - type WeightInfo = (); - type MaxLocks = (); - type MaxReserves = MaxReserves; - type ReserveIdentifier = [u8; 8]; +pub struct PairLpIdentity; +impl GenerateLpAssetId for PairLpIdentity { + fn generate_lp_asset_id(asset_0: CurrencyId, asset_1: CurrencyId) -> Option { + CurrencyId::join_lp_token(asset_0, asset_1) + } } impl Config for Test { type RuntimeEvent = RuntimeEvent; - type MultiAssetsHandler = DexGeneralMultiAssets>; + type MultiCurrency = Tokens; type PalletId = DexGeneralPalletId; - type AssetId = AssetId; - type LpGenerate = PairLpGenerate; - type SelfParaId = (); + type AssetId = CurrencyId; + type LpGenerate = PairLpIdentity; type WeightInfo = (); } @@ -146,27 +150,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { .build_storage::() .unwrap() .into(); - pallet_balances::GenesisConfig:: { - balances: vec![ - (1, 34028236692093846346337460743176821145), - (2, 10), - (3, 10), - (4, 10), - (5, 10), - ], - } - .assimilate_storage(&mut t) - .unwrap(); - - orml_tokens::GenesisConfig:: { - balances: vec![ - (1, CurrencyId::Token(1), 34028236692093846346337460743176821145), - (1, CurrencyId::Token(2), 34028236692093846346337460743176821145), - (1, CurrencyId::Token(3), 34028236692093846346337460743176821145), - ], - } - .assimilate_storage(&mut t) - .unwrap(); pallet_dex_general::GenesisConfig:: { fee_receiver: None, @@ -178,60 +161,4 @@ pub fn new_test_ext() -> sp_io::TestExternalities { t.into() } -pub struct LocalAssetAdaptor(PhantomData); - type AccountId = u128; - -fn asset_id_to_currency_id(asset_id: &AssetId) -> Result { - let discr = (asset_id.asset_index & 0x0000_0000_0000_ff00) >> 8; - return if discr == 6 { - let token0_id = ((asset_id.asset_index & 0x0000_0000_ffff_0000) >> 16) as u8; - let token1_id = ((asset_id.asset_index & 0x0000_ffff_0000_0000) >> 16) as u8; - Ok(CurrencyId::LpToken(token0_id, token1_id)) - } else { - let token_id = asset_id.asset_index as u8; - - Ok(CurrencyId::Token(token_id)) - }; -} - -impl MultiAssetsHandler for LocalAssetAdaptor -where - Local: MultiCurrency, -{ - fn balance_of(asset_id: AssetId, who: &AccountId) -> AssetBalance { - asset_id_to_currency_id(&asset_id).map_or(AssetBalance::default(), |currency_id| { - Local::free_balance(currency_id, who) - }) - } - - fn total_supply(asset_id: AssetId) -> AssetBalance { - asset_id_to_currency_id(&asset_id).map_or(AssetBalance::default(), |currency_id| { - Local::total_issuance(currency_id) - }) - } - - fn is_exists(asset_id: AssetId) -> bool { - asset_id_to_currency_id(&asset_id).map_or(false, |currency_id| { - Local::total_issuance(currency_id) > AssetBalance::default() - }) - } - - fn transfer(asset_id: AssetId, origin: &AccountId, target: &AccountId, amount: AssetBalance) -> DispatchResult { - asset_id_to_currency_id(&asset_id).map_or(Err(DispatchError::CannotLookup), |currency_id| { - Local::transfer(currency_id, origin, target, amount) - }) - } - - fn deposit(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result { - asset_id_to_currency_id(&asset_id).map_or(Ok(AssetBalance::default()), |currency_id| { - Local::deposit(currency_id, origin, amount).map(|_| amount) - }) - } - - fn withdraw(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result { - asset_id_to_currency_id(&asset_id).map_or(Ok(AssetBalance::default()), |currency_id| { - Local::withdraw(currency_id, origin, amount).map(|_| amount) - }) - } -} diff --git a/crates/dex-general/src/swap/mod.rs b/crates/dex-general/src/swap/mod.rs index 33eb48493e..6dd8661a8c 100644 --- a/crates/dex-general/src/swap/mod.rs +++ b/crates/dex-general/src/swap/mod.rs @@ -64,8 +64,8 @@ impl Pallet { let pair = Self::sort_asset_id(asset_0, asset_1); PairStatuses::::try_mutate(pair, |status| { if let Trading(parameter) = status { - let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, ¶meter.pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, ¶meter.pair_account); + let reserve_0 = T::MultiCurrency::free_balance(asset_0, ¶meter.pair_account); + let reserve_1 = T::MultiCurrency::free_balance(asset_1, ¶meter.pair_account); let (amount_0, amount_1) = Self::calculate_added_amount( amount_0_desired, @@ -76,8 +76,8 @@ impl Pallet { reserve_1, )?; - let balance_asset_0 = T::MultiAssetsHandler::balance_of(asset_0, who); - let balance_asset_1 = T::MultiAssetsHandler::balance_of(asset_1, who); + let balance_asset_0 = T::MultiCurrency::free_balance(asset_0, who); + let balance_asset_1 = T::MultiCurrency::free_balance(asset_1, who); ensure!( balance_asset_0 >= amount_0 && balance_asset_1 >= amount_1, Error::::InsufficientAssetBalance @@ -88,7 +88,7 @@ impl Pallet { let mint_fee = Self::mint_protocol_fee(reserve_0, reserve_1, asset_0, asset_1, parameter.total_supply)?; if let Some(fee_to) = Self::fee_meta().0 { if mint_fee > 0 && Self::fee_meta().1 > 0 { - T::MultiAssetsHandler::deposit(lp_asset_id, &fee_to, mint_fee).map(|_| mint_fee)?; + T::MultiCurrency::deposit(lp_asset_id, &fee_to, mint_fee).map(|_| mint_fee)?; parameter.total_supply = parameter .total_supply .checked_add(mint_fee) @@ -105,16 +105,16 @@ impl Pallet { .checked_add(mint_liquidity) .ok_or(Error::::Overflow)?; - T::MultiAssetsHandler::deposit(lp_asset_id, who, mint_liquidity).map(|_| mint_liquidity)?; + T::MultiCurrency::deposit(lp_asset_id, who, mint_liquidity).map(|_| mint_liquidity)?; - T::MultiAssetsHandler::transfer(asset_0, who, ¶meter.pair_account, amount_0)?; - T::MultiAssetsHandler::transfer(asset_1, who, ¶meter.pair_account, amount_1)?; + T::MultiCurrency::transfer(asset_0, who, ¶meter.pair_account, amount_0)?; + T::MultiCurrency::transfer(asset_1, who, ¶meter.pair_account, amount_1)?; if let Some(_fee_to) = Self::fee_meta().0 { if Self::fee_meta().1 > 0 { // update reserve_0 and reserve_1 - let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, ¶meter.pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, ¶meter.pair_account); + let reserve_0 = T::MultiCurrency::free_balance(asset_0, ¶meter.pair_account); + let reserve_1 = T::MultiCurrency::free_balance(asset_1, ¶meter.pair_account); let last_k_value = U256::from(reserve_0) .checked_mul(U256::from(reserve_1)) @@ -152,8 +152,8 @@ impl Pallet { let pair = Self::sort_asset_id(asset_0, asset_1); PairStatuses::::try_mutate(pair, |status| { if let Trading(parameter) = status { - let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, ¶meter.pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, ¶meter.pair_account); + let reserve_0 = T::MultiCurrency::free_balance(asset_0, ¶meter.pair_account); + let reserve_1 = T::MultiCurrency::free_balance(asset_1, ¶meter.pair_account); let amount_0 = Self::calculate_share_amount(remove_liquidity, parameter.total_supply, reserve_0); let amount_1 = Self::calculate_share_amount(remove_liquidity, parameter.total_supply, reserve_1); @@ -169,7 +169,7 @@ impl Pallet { if let Some(fee_to) = Self::fee_meta().0 { if mint_fee > 0 && Self::fee_meta().1 > 0 { //Self::mutate_liquidity(asset_0, asset_1, &fee_to, mint_fee, true)?; - T::MultiAssetsHandler::deposit(lp_asset_id, &fee_to, mint_fee).map(|_| mint_fee)?; + T::MultiCurrency::deposit(lp_asset_id, &fee_to, mint_fee).map(|_| mint_fee)?; parameter.total_supply = parameter .total_supply .checked_add(mint_fee) @@ -183,16 +183,16 @@ impl Pallet { .ok_or(Error::::InsufficientLiquidity)?; // Self::mutate_liquidity(asset_0, asset_1, who, remove_liquidity, false)?; - T::MultiAssetsHandler::withdraw(lp_asset_id, who, remove_liquidity).map(|_| remove_liquidity)?; + T::MultiCurrency::withdraw(lp_asset_id, who, remove_liquidity).map(|_| remove_liquidity)?; - T::MultiAssetsHandler::transfer(asset_0, ¶meter.pair_account, recipient, amount_0)?; - T::MultiAssetsHandler::transfer(asset_1, ¶meter.pair_account, recipient, amount_1)?; + T::MultiCurrency::transfer(asset_0, ¶meter.pair_account, recipient, amount_0)?; + T::MultiCurrency::transfer(asset_1, ¶meter.pair_account, recipient, amount_1)?; if let Some(_fee_to) = Self::fee_meta().0 { if Self::fee_meta().1 > 0 { // update reserve_0 and reserve_1 - let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, ¶meter.pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, ¶meter.pair_account); + let reserve_0 = T::MultiCurrency::free_balance(asset_0, ¶meter.pair_account); + let reserve_1 = T::MultiCurrency::free_balance(asset_1, ¶meter.pair_account); let last_k_value = U256::from(reserve_0) .checked_mul(U256::from(reserve_1)) @@ -234,7 +234,7 @@ impl Pallet { let pair_account = Self::pair_account_id(path[0], path[1]); - T::MultiAssetsHandler::transfer(path[0], who, &pair_account, amount_in)?; + T::MultiCurrency::transfer(path[0], who, &pair_account, amount_in)?; Self::swap(&amounts, path, recipient)?; Self::deposit_event(Event::AssetSwap( @@ -261,7 +261,7 @@ impl Pallet { let pair_account = Self::pair_account_id(path[0], path[1]); - T::MultiAssetsHandler::transfer(path[0], who, &pair_account, amounts[0])?; + T::MultiCurrency::transfer(path[0], who, &pair_account, amounts[0])?; Self::swap(&amounts, path, recipient)?; Self::deposit_event(Event::AssetSwap( @@ -456,8 +456,8 @@ impl Pallet { while i > 0 { let pair_account = Self::pair_account_id(path[i], path[i - 1]); - let reserve_0 = T::MultiAssetsHandler::balance_of(path[i], &pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(path[i - 1], &pair_account); + let reserve_0 = T::MultiCurrency::free_balance(path[i], &pair_account); + let reserve_1 = T::MultiCurrency::free_balance(path[i - 1], &pair_account); ensure!( reserve_1 > Zero::zero() && reserve_0 > Zero::zero(), @@ -505,8 +505,8 @@ impl Pallet { for i in 0..len { let pair_account = Self::pair_account_id(path[i], path[i + 1]); - let reserve_0 = T::MultiAssetsHandler::balance_of(path[i], &pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(path[i + 1], &pair_account); + let reserve_0 = T::MultiCurrency::free_balance(path[i], &pair_account); + let reserve_1 = T::MultiCurrency::free_balance(path[i + 1], &pair_account); ensure!( reserve_1 > Zero::zero() && reserve_0 > Zero::zero(), @@ -579,8 +579,8 @@ impl Pallet { _ => Err(Error::::PairNotExists), }?; - let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, pair_account); - let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, pair_account); + let reserve_0 = T::MultiCurrency::free_balance(asset_0, pair_account); + let reserve_1 = T::MultiCurrency::free_balance(asset_1, pair_account); ensure!( amount_0 <= reserve_0 && amount_1 <= reserve_1, @@ -588,11 +588,11 @@ impl Pallet { ); if amount_0 > Zero::zero() { - T::MultiAssetsHandler::transfer(asset_0, pair_account, recipient, amount_0)?; + T::MultiCurrency::transfer(asset_0, pair_account, recipient, amount_0)?; } if amount_1 > Zero::zero() { - T::MultiAssetsHandler::transfer(asset_1, pair_account, recipient, amount_1)?; + T::MultiCurrency::transfer(asset_1, pair_account, recipient, amount_1)?; } Ok(()) @@ -663,8 +663,8 @@ impl Pallet { let pair_account = Self::account_id(); - T::MultiAssetsHandler::transfer(pair.0, &who, &pair_account, amount_0_contribute)?; - T::MultiAssetsHandler::transfer(pair.1, &who, &pair_account, amount_1_contribute)?; + T::MultiCurrency::transfer(pair.0, &who, &pair_account, amount_0_contribute)?; + T::MultiCurrency::transfer(pair.1, &who, &pair_account, amount_1_contribute)?; let accumulated_supply_0 = bootstrap_parameter .accumulated_supply @@ -715,21 +715,21 @@ impl Pallet { let pair_account = Self::pair_account_id(pair.0, pair.1); let lp_asset_id = Self::lp_pairs(pair).ok_or(Error::::InsufficientAssetBalance)?; - T::MultiAssetsHandler::transfer( + T::MultiCurrency::transfer( pair.0, &bootstrap_parameter.pair_account, &pair_account, bootstrap_parameter.accumulated_supply.0, )?; - T::MultiAssetsHandler::transfer( + T::MultiCurrency::transfer( pair.1, &bootstrap_parameter.pair_account, &pair_account, bootstrap_parameter.accumulated_supply.1, )?; - T::MultiAssetsHandler::deposit(lp_asset_id, &pair_account, total_lp_supply).map(|_| total_lp_supply)?; + T::MultiCurrency::deposit(lp_asset_id, &pair_account, total_lp_supply).map(|_| total_lp_supply)?; PairStatuses::::insert( pair, @@ -820,7 +820,7 @@ impl Pallet { let pair_account = Self::pair_account_id(pair.0, pair.1); let lp_asset_id = Self::lp_pairs(pair).ok_or(Error::::InsufficientAssetBalance)?; - T::MultiAssetsHandler::transfer(lp_asset_id, &pair_account, &recipient, claim_liquidity)?; + T::MultiCurrency::transfer(lp_asset_id, &pair_account, &recipient, claim_liquidity)?; let bootstrap_total_liquidity = U256::from(bootstrap_parameter.accumulated_supply.0) .checked_mul(U256::from(bootstrap_parameter.accumulated_supply.1)) @@ -879,8 +879,8 @@ impl Pallet { BootstrapPersonalSupply::::try_mutate_exists((pair, &who), |contribution| -> DispatchResult { if let Some((amount_0_contribute, amount_1_contribute)) = contribution.take() { let pair_account = Self::account_id(); - T::MultiAssetsHandler::transfer(pair.0, &pair_account, &who, amount_0_contribute)?; - T::MultiAssetsHandler::transfer(pair.1, &pair_account, &who, amount_1_contribute)?; + T::MultiCurrency::transfer(pair.0, &pair_account, &who, amount_0_contribute)?; + T::MultiCurrency::transfer(pair.1, &pair_account, &who, amount_1_contribute)?; PairStatuses::::try_mutate(pair, |status| -> DispatchResult { if let Bootstrap(parameter) = status { @@ -936,7 +936,7 @@ impl Pallet { let limits = Self::get_bootstrap_limits(pair); for (asset_id, limit) in limits.into_iter() { - if T::MultiAssetsHandler::balance_of(asset_id, account) < limit { + if T::MultiCurrency::free_balance(asset_id, account) < limit { return false; } } @@ -963,7 +963,7 @@ impl Pallet { .and_then(|n| TryInto::::try_into(n).ok()) .ok_or(Error::::Overflow)?; - T::MultiAssetsHandler::transfer(asset_id, reward_holder, owner, owner_reward)?; + T::MultiCurrency::transfer(asset_id, reward_holder, owner, owner_reward)?; distribute_rewards.push((asset_id, owner_reward)); } diff --git a/crates/dex-general/src/swap/tests.rs b/crates/dex-general/src/swap/tests.rs index d880839035..c98b9f3ecb 100644 --- a/crates/dex-general/src/swap/tests.rs +++ b/crates/dex-general/src/swap/tests.rs @@ -1,42 +1,20 @@ // Copyright 2021-2022 Zenlink. // Licensed under Apache 2.0. -use super::{mock::*, AssetId, Error, MultiAssetsHandler}; +use super::{mock::*, Error}; use crate::primitives::PairStatus::Trading; use frame_support::{assert_noop, assert_ok}; use frame_system::RawOrigin; +use orml_traits::MultiCurrency; use sp_core::U256; use sp_runtime::{traits::Zero, DispatchError::BadOrigin}; -const DOT_ASSET_ID: AssetId = AssetId { - chain_id: 200, - asset_type: LOCAL, - asset_index: 2, -}; - -const KSM_ASSET_ID: AssetId = AssetId { - chain_id: 200, - asset_type: LOCAL, - asset_index: 3, -}; - -const BTC_ASSET_ID: AssetId = AssetId { - chain_id: 300, - asset_type: RESERVED, - asset_index: 3, -}; - -const ETH_ASSET_ID: AssetId = AssetId { - chain_id: 300, - asset_type: NATIVE, - asset_index: 0, -}; - -const DOT_BTC_LP_ID: AssetId = AssetId { - chain_id: 0, - asset_type: 2, - asset_index: 12885034496, -}; +const DOT_ASSET_ID: CurrencyId = CurrencyId::Token(2); +const BTC_ASSET_ID: CurrencyId = CurrencyId::Token(3); +const KSM_ASSET_ID: CurrencyId = CurrencyId::Token(3); +const ETH_ASSET_ID: CurrencyId = CurrencyId::Token(4); + +const DOT_BTC_LP_ID: CurrencyId = CurrencyId::LpToken(2, 3); const ALICE: u128 = 1; const BOB: u128 = 2; @@ -45,11 +23,21 @@ const DOT_UNIT: u128 = 1000_000_000_000_000; const BTC_UNIT: u128 = 1000_000_00; const ETH_UNIT: u128 = 1000_000_000_000; +const MAX_BALANCE: u128 = u128::MAX - 1; + #[test] fn add_liquidity_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -71,7 +59,7 @@ fn add_liquidity_should_work() { 100 )); - let mint_liquidity = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE); + let mint_liquidity = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE); assert_eq!(mint_liquidity, 316227766016); let total_supply_dot = 50 * DOT_UNIT; @@ -89,10 +77,10 @@ fn add_liquidity_should_work() { )); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let balance_dot = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let balance_btc = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let balance_dot = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let balance_btc = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); - let mint_liquidity = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE); + let mint_liquidity = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE); assert_eq!(mint_liquidity, 16127616066816); assert_eq!(balance_dot, 51000000000000000); @@ -105,8 +93,16 @@ fn add_liquidity_should_work() { #[test] fn remove_liquidity_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -137,8 +133,8 @@ fn remove_liquidity_should_work() { 100 )); - let balance_dot = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB); - let balance_btc = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &BOB); + let balance_dot = ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB); + let balance_btc = ::MultiCurrency::free_balance(BTC_ASSET_ID, &BOB); assert_eq!(balance_dot, 316227766016); assert_eq!(balance_btc, 31622); @@ -150,8 +146,16 @@ fn remove_liquidity_should_work() { #[test] fn foreign_get_in_price_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -200,8 +204,16 @@ fn foreign_get_in_price_should_work() { #[test] fn foreign_get_out_price_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -251,8 +263,16 @@ fn foreign_get_out_price_should_work() { #[test] fn inner_swap_exact_assets_for_assets_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); let total_supply_dot = 50000 * DOT_UNIT; let total_supply_btc = 50000 * BTC_UNIT; @@ -274,8 +294,8 @@ fn inner_swap_exact_assets_for_assets_should_work() { )); let pair_dot_btc = DexGeneral::pair_account_id(DOT_ASSET_ID, BTC_ASSET_ID); - let balance_dot = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &pair_dot_btc); - let balance_btc = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &pair_dot_btc); + let balance_dot = ::MultiCurrency::free_balance(DOT_ASSET_ID, &pair_dot_btc); + let balance_btc = ::MultiCurrency::free_balance(BTC_ASSET_ID, &pair_dot_btc); // println!("balance_dot {} balance_btc {}", balance_dot, balance_btc); assert_eq!(balance_dot, 50000000000000000000); @@ -292,7 +312,7 @@ fn inner_swap_exact_assets_for_assets_should_work() { &BOB, )); - let btc_balance = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &BOB); + let btc_balance = ::MultiCurrency::free_balance(BTC_ASSET_ID, &BOB); // println!("btc_balance {}", btc_balance); assert_eq!(btc_balance, 99698012); @@ -309,7 +329,7 @@ fn inner_swap_exact_assets_for_assets_should_work() { &path, &BOB, )); - let dot_balance = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB); + let dot_balance = ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB); // println!("dot_balance {}", dot_balance); assert_eq!(dot_balance, 997019939603584) @@ -319,9 +339,21 @@ fn inner_swap_exact_assets_for_assets_should_work() { #[test] fn inner_swap_exact_assets_for_assets_in_pairs_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + ETH_ASSET_ID, + &ALICE, + MAX_BALANCE + )); let total_supply_dot = 5000 * DOT_UNIT; let total_supply_btc = 5000 * BTC_UNIT; @@ -369,7 +401,7 @@ fn inner_swap_exact_assets_for_assets_in_pairs_should_work() { &path, &BOB, )); - let eth_balance = ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB); + let eth_balance = ::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB); // println!("eth_balance {}", eth_balance); assert_eq!(eth_balance, 993613333572); @@ -377,7 +409,7 @@ fn inner_swap_exact_assets_for_assets_in_pairs_should_work() { let path = vec![ETH_ASSET_ID, BTC_ASSET_ID, DOT_ASSET_ID]; let amount_in = 1 * ETH_UNIT; let amount_out_min = 1 * DOT_UNIT * 996 / 1000 * 996 / 1000; - let dot_balance = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB); + let dot_balance = ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB); // println!("dot_balance {}", dot_balance); assert_eq!(dot_balance, 0); @@ -389,7 +421,7 @@ fn inner_swap_exact_assets_for_assets_in_pairs_should_work() { &path, &BOB, )); - let dot_balance = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB); + let dot_balance = ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB); // println!("dot_balance {}", dot_balance); assert_eq!(dot_balance, 994405843102918); @@ -402,8 +434,16 @@ fn inner_swap_assets_for_exact_assets_should_work() { let total_supply_dot = 10000 * DOT_UNIT; let total_supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, total_supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, total_supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + total_supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + total_supply_btc + )); let supply_dot = 5000 * DOT_UNIT; let supply_btc = 5000 * BTC_UNIT; @@ -433,11 +473,11 @@ fn inner_swap_assets_for_exact_assets_should_work() { &path, &BOB )); - let btc_balance = ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &BOB); + let btc_balance = ::MultiCurrency::free_balance(BTC_ASSET_ID, &BOB); assert_eq!(btc_balance, amount_out); let amount_in_dot = - total_supply_dot - supply_dot - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &ALICE); + total_supply_dot - supply_dot - ::MultiCurrency::free_balance(DOT_ASSET_ID, &ALICE); // println!("amount in {}", amount_in_dot); assert_eq!(amount_in_dot, 1003209669015047); @@ -454,7 +494,7 @@ fn inner_swap_assets_for_exact_assets_should_work() { &path, &BOB )); - let dot_balance = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB); + let dot_balance = ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB); // println!("dot_balance {}", dot_balance); assert_eq!(dot_balance, 1000000000000000); @@ -462,7 +502,7 @@ fn inner_swap_assets_for_exact_assets_should_work() { assert_eq!(dot_balance, amount_out); let amount_in_btc = - total_supply_btc - supply_btc - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &ALICE); + total_supply_btc - supply_btc - ::MultiCurrency::free_balance(BTC_ASSET_ID, &ALICE); // println!("amount in {}", amount_in_btc); assert_eq!(amount_in_btc, 100280779); @@ -478,9 +518,21 @@ fn inner_swap_assets_for_exact_assets_in_pairs_should_work() { let total_supply_btc = 10000 * BTC_UNIT; let total_supply_eth = 10000 * ETH_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, total_supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, total_supply_btc)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &ALICE, total_supply_eth)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + total_supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + total_supply_btc + )); + assert_ok!(::MultiCurrency::deposit( + ETH_ASSET_ID, + &ALICE, + total_supply_eth + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -521,7 +573,7 @@ fn inner_swap_assets_for_exact_assets_in_pairs_should_work() { let path = vec![DOT_ASSET_ID, BTC_ASSET_ID, ETH_ASSET_ID]; let amount_out = 1 * ETH_UNIT; let amount_in_max = 1 * DOT_UNIT * 1004 / 1000 * 1004 / 1000; - let bob_dev_balance = ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB); + let bob_dev_balance = ::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB); assert_ok!(DexPallet::inner_swap_assets_for_exact_assets( &ALICE, amount_out, @@ -529,7 +581,7 @@ fn inner_swap_assets_for_exact_assets_in_pairs_should_work() { &path, &BOB )); - let eth_balance = ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB); + let eth_balance = ::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB); // println!("eth_balance {}", eth_balance); assert_eq!(eth_balance, 1000000000000); @@ -546,7 +598,7 @@ fn inner_swap_assets_for_exact_assets_in_pairs_should_work() { &path, &BOB )); - let dot_balance = ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB); + let dot_balance = ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB); assert_eq!(dot_balance, amount_out); }) } @@ -554,8 +606,8 @@ fn inner_swap_assets_for_exact_assets_in_pairs_should_work() { #[test] fn create_bootstrap_should_work() { new_test_ext().execute_with(|| { - assert_ok!(::MultiAssetsHandler::deposit(DOT_ASSET_ID, &ALICE, 0)); - assert_ok!(::MultiAssetsHandler::deposit(ETH_ASSET_ID, &ALICE, 0)); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &ALICE, 0)); + assert_ok!(::MultiCurrency::deposit(ETH_ASSET_ID, &ALICE, 0)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -596,8 +648,8 @@ fn create_bootstrap_should_work() { #[test] fn update_bootstrap_should_work() { new_test_ext().execute_with(|| { - assert_ok!(::MultiAssetsHandler::deposit(DOT_ASSET_ID, &ALICE, 0)); - assert_ok!(::MultiAssetsHandler::deposit(ETH_ASSET_ID, &ALICE, 0)); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &ALICE, 0)); + assert_ok!(::MultiCurrency::deposit(ETH_ASSET_ID, &ALICE, 0)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -649,8 +701,16 @@ fn bootstrap_contribute_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), DOT_ASSET_ID, @@ -684,10 +744,18 @@ fn bootstrap_contribute_end_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -746,10 +814,18 @@ fn bootstrap_contribute_claim_reward_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -801,10 +877,7 @@ fn bootstrap_contribute_claim_reward_should_work() { _ => Err(()), }); - assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE), - 0 - ); + assert_eq!(::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE), 0); assert_ok!(DexPallet::bootstrap_claim( RawOrigin::Signed(ALICE).into(), @@ -814,11 +887,11 @@ fn bootstrap_contribute_claim_reward_should_work() { 1000, )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE), total_supply / 2 ); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &pair_dot_btc), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &pair_dot_btc), total_supply / 2 ); @@ -827,7 +900,7 @@ fn bootstrap_contribute_claim_reward_should_work() { Error::::ZeroContribute ); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE), total_supply / 2 ); @@ -839,11 +912,11 @@ fn bootstrap_contribute_claim_reward_should_work() { 1000, )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &BOB), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &BOB), total_supply / 2 ); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &pair_dot_btc), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &pair_dot_btc), 0 ); }) @@ -856,10 +929,18 @@ fn refund_in_disable_bootstrap_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -905,11 +986,11 @@ fn refund_in_disable_bootstrap_should_work() { BTC_ASSET_ID, )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB), supply_dot ); assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(BTC_ASSET_ID, &BOB), supply_btc ); @@ -933,8 +1014,16 @@ fn disable_bootstrap_removed_after_all_refund_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -980,8 +1069,16 @@ fn bootstrap_pair_deny_swap_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -1057,10 +1154,18 @@ fn refund_in_success_bootstrap_should_not_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -1119,10 +1224,18 @@ fn refund_in_ongoing_bootstrap_should_not_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, supply_btc)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + supply_dot + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + supply_btc + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -1160,10 +1273,18 @@ fn create_pair_in_disable_bootstrap_should_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 1 * DOT_UNIT)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 1 * BTC_UNIT)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 1 * DOT_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 1 * BTC_UNIT + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -1211,11 +1332,11 @@ fn create_pair_in_disable_bootstrap_should_work() { BTC_ASSET_ID, )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(DOT_ASSET_ID, &BOB), supply_dot ); assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(BTC_ASSET_ID, &BOB), supply_btc ); @@ -1229,7 +1350,7 @@ fn create_pair_in_disable_bootstrap_should_work() { Error::::ZeroContribute ); - let mint_liquidity = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE); + let mint_liquidity = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE); assert_eq!(mint_liquidity, 316227766016); }) } @@ -1241,10 +1362,18 @@ fn create_bootstrap_in_disable_bootstrap() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 1 * DOT_UNIT)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 1 * BTC_UNIT)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 1 * DOT_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 1 * BTC_UNIT + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -1315,7 +1444,7 @@ fn create_bootstrap_in_disable_bootstrap() { 1000, )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &BOB), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &BOB), 2000000000000 ); }) @@ -1328,10 +1457,18 @@ fn create_pair_in_ongoing_bootstrap_should_not_work() { let supply_dot = 10000 * DOT_UNIT; let supply_btc = 10000 * BTC_UNIT; - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 1 * DOT_UNIT)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 1 * BTC_UNIT)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, supply_dot)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, supply_btc)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 1 * DOT_UNIT + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 1 * BTC_UNIT + )); + assert_ok!(::MultiCurrency::deposit(DOT_ASSET_ID, &BOB, supply_dot)); + assert_ok!(::MultiCurrency::deposit(BTC_ASSET_ID, &BOB, supply_btc)); assert_ok!(DexPallet::bootstrap_create( RawOrigin::Root.into(), @@ -1355,8 +1492,16 @@ fn create_pair_in_ongoing_bootstrap_should_not_work() { #[test] fn liquidity_at_boundary_should_work() { new_test_ext().execute_with(|| { - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE + )); assert_ok!(DexPallet::create_pair( RawOrigin::Root.into(), @@ -1368,29 +1513,23 @@ fn liquidity_at_boundary_should_work() { RawOrigin::Signed(ALICE).into(), DOT_ASSET_ID, BTC_ASSET_ID, - u128::MAX, - u128::MAX, + MAX_BALANCE, + MAX_BALANCE, 0, 0, 100 )); - let mint_liquidity = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE); - assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &ALICE), - 0 - ); - assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &ALICE), - 0 - ); + let mint_liquidity = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE); + assert_eq!(::MultiCurrency::free_balance(DOT_ASSET_ID, &ALICE), 0); + assert_eq!(::MultiCurrency::free_balance(BTC_ASSET_ID, &ALICE), 0); - assert_eq!(mint_liquidity, u128::MAX); + assert_eq!(mint_liquidity, MAX_BALANCE); assert_ok!(DexPallet::remove_liquidity( RawOrigin::Signed(ALICE).into(), DOT_ASSET_ID, BTC_ASSET_ID, - u128::MAX, + MAX_BALANCE, 0, 0, ALICE, @@ -1398,20 +1537,20 @@ fn liquidity_at_boundary_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &ALICE), - u128::MAX + ::MultiCurrency::free_balance(DOT_ASSET_ID, &ALICE), + MAX_BALANCE ); assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &ALICE), - u128::MAX + ::MultiCurrency::free_balance(BTC_ASSET_ID, &ALICE), + MAX_BALANCE ); assert_ok!(DexPallet::add_liquidity( RawOrigin::Signed(ALICE).into(), DOT_ASSET_ID, BTC_ASSET_ID, - u128::MAX, - u128::MAX, + MAX_BALANCE, + MAX_BALANCE, 0, 0, 100 @@ -1421,7 +1560,7 @@ fn liquidity_at_boundary_should_work() { RawOrigin::Signed(ALICE).into(), DOT_ASSET_ID, BTC_ASSET_ID, - u128::MAX / 2, + MAX_BALANCE / 2, 0, 0, ALICE, @@ -1429,12 +1568,12 @@ fn liquidity_at_boundary_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &ALICE), - u128::MAX / 2 + ::MultiCurrency::free_balance(DOT_ASSET_ID, &ALICE), + MAX_BALANCE / 2 ); assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &ALICE), - u128::MAX / 2 + ::MultiCurrency::free_balance(BTC_ASSET_ID, &ALICE), + MAX_BALANCE / 2 ); }) } @@ -1443,12 +1582,36 @@ fn liquidity_at_boundary_should_work() { fn multi_bootstrap_contribute_claim_should_work() { new_test_ext().execute_with(|| { System::set_block_number(1); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, u128::MAX / 4 - 1)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, u128::MAX / 4 - 1)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, u128::MAX / 4 - 1)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, u128::MAX / 4 - 1)); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, u128::MAX / 4 - 1)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &CHARLIE, u128::MAX / 4 - 1)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + MAX_BALANCE / 4 - 1 + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + MAX_BALANCE / 4 - 1 + )); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &BOB, + MAX_BALANCE / 4 - 1 + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &BOB, + MAX_BALANCE / 4 - 1 + )); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &CHARLIE, + MAX_BALANCE / 4 - 1 + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &CHARLIE, + MAX_BALANCE / 4 - 1 + )); let unit = 1_000_000_000_000_000_000u128; assert_ok!(DexPallet::bootstrap_create( @@ -1498,7 +1661,7 @@ fn multi_bootstrap_contribute_claim_should_work() { BTC_ASSET_ID )); - let total_lp = ::MultiAssetsHandler::total_supply(DOT_BTC_LP_ID); + let total_lp = ::MultiCurrency::total_issuance(DOT_BTC_LP_ID); assert_eq!( U256::from(300_000_000 * unit) .saturating_mul(U256::from(300_000_000 * unit)) @@ -1539,13 +1702,13 @@ fn multi_bootstrap_contribute_claim_should_work() { // 100000000000000000000000000 (200000000 * 10^18 * 300000000 * 10^18 + 0) / (300000000 * // 10^18 *2) = 100000000000000000000000000 alice_lp = 100000000000000000000000000 - let alice_lp = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE); + let alice_lp = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE); assert_eq!(alice_lp, 100000000000000000000000000); - let bob_lp = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &BOB); + let bob_lp = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &BOB); assert_eq!(bob_lp, 100000000000000000000000000); - let charlie_lp = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &CHARLIE); + let charlie_lp = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &CHARLIE); assert_eq!(charlie_lp, 100000000000000000000000000); }) } @@ -1619,8 +1782,16 @@ fn bootstrap_charge_reward_should_work() { [(DOT_ASSET_ID, 2000 * unit), (BTC_ASSET_ID, 1000 * unit)].to_vec(), )); - assert_ok!(DexPallet::foreign_mint(KSM_ASSET_ID, &BOB, 1000 * unit)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &BOB, 2000 * unit)); + assert_ok!(::MultiCurrency::deposit( + KSM_ASSET_ID, + &BOB, + 1000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + ETH_ASSET_ID, + &BOB, + 2000 * unit + )); assert_ok!(DexPallet::bootstrap_charge_reward( RawOrigin::Signed(BOB).into(), @@ -1630,16 +1801,16 @@ fn bootstrap_charge_reward_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &DexPallet::account_id()), 2000 * unit ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &DexPallet::account_id()), 1000 * unit ); - assert_eq!(::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB), 0); - assert_eq!(::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(KSM_ASSET_ID, &BOB), 0); }) } @@ -1661,8 +1832,16 @@ fn bootstrap_withdraw_reward_after_charge_should_work() { [(DOT_ASSET_ID, 2000 * unit), (BTC_ASSET_ID, 1000 * unit)].to_vec(), )); - assert_ok!(DexPallet::foreign_mint(KSM_ASSET_ID, &BOB, 1000 * unit)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &BOB, 2000 * unit)); + assert_ok!(::MultiCurrency::deposit( + KSM_ASSET_ID, + &BOB, + 1000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + ETH_ASSET_ID, + &BOB, + 2000 * unit + )); assert_ok!(DexPallet::bootstrap_charge_reward( RawOrigin::Signed(BOB).into(), @@ -1679,20 +1858,20 @@ fn bootstrap_withdraw_reward_after_charge_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &DexPallet::account_id()), 0 ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &DexPallet::account_id()), 0 ); assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &ALICE), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &ALICE), 2000 * unit ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &ALICE), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &ALICE), 1000 * unit ); @@ -1721,8 +1900,16 @@ fn bootstrap_charge_reward_with_insufficient_account_should_not_work() { [(DOT_ASSET_ID, 2000 * unit), (BTC_ASSET_ID, 1000 * unit)].to_vec(), )); - assert_ok!(DexPallet::foreign_mint(KSM_ASSET_ID, &BOB, 1000 * unit)); - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &BOB, 2000 * unit)); + assert_ok!(::MultiCurrency::deposit( + KSM_ASSET_ID, + &BOB, + 1000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + ETH_ASSET_ID, + &BOB, + 2000 * unit + )); assert_noop!( DexPallet::bootstrap_charge_reward( @@ -1735,20 +1922,20 @@ fn bootstrap_charge_reward_with_insufficient_account_should_not_work() { ); assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &DexPallet::account_id()), 0 ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &DexPallet::account_id()), 0 ); assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB), 2000 * unit ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &BOB), 1000 * unit ); }) @@ -1775,8 +1962,16 @@ fn bootstrap_contribute_below_limits_should_not_work() { )); // charlie's asset below limit, - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, 100 * unit)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &CHARLIE, 2000 * unit)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &CHARLIE, + 100 * unit + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &CHARLIE, + 2000 * unit + )); assert_noop!( DexPallet::bootstrap_contribute( @@ -1791,20 +1986,20 @@ fn bootstrap_contribute_below_limits_should_not_work() { ); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(DOT_ASSET_ID, &DexPallet::account_id()), 0 ); assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &DexPallet::account_id()), + ::MultiCurrency::free_balance(BTC_ASSET_ID, &DexPallet::account_id()), 0 ); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_ASSET_ID, &CHARLIE), + ::MultiCurrency::free_balance(DOT_ASSET_ID, &CHARLIE), 100 * unit ); assert_eq!( - ::MultiAssetsHandler::balance_of(BTC_ASSET_ID, &CHARLIE), + ::MultiCurrency::free_balance(BTC_ASSET_ID, &CHARLIE), 2000 * unit ); }) @@ -1829,15 +2024,39 @@ fn bootstrap_contribute_exceed_limits_should_work() { )); // alice mint asset - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 2_000_000 * unit)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 4_000_000 * unit)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 2_000_000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 4_000_000 * unit + )); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &CHARLIE, 2_000_000 * unit)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &CHARLIE, 4_000_000 * unit)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &CHARLIE, + 2_000_000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &CHARLIE, + 4_000_000 * unit + )); // alice will charge - assert_ok!(DexPallet::foreign_mint(ETH_ASSET_ID, &ALICE, 20_000 * unit)); - assert_ok!(DexPallet::foreign_mint(KSM_ASSET_ID, &ALICE, 10_000 * unit)); + assert_ok!(::MultiCurrency::deposit( + ETH_ASSET_ID, + &ALICE, + 20_000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + KSM_ASSET_ID, + &ALICE, + 10_000 * unit + )); assert_ok!(DexPallet::bootstrap_charge_reward( RawOrigin::Signed(ALICE).into(), @@ -1847,8 +2066,16 @@ fn bootstrap_contribute_exceed_limits_should_work() { )); // bob's asset == limit - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, 2_000 * unit)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, 1_000 * unit)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &BOB, + 2_000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &BOB, + 1_000 * unit + )); assert_ok!(DexPallet::bootstrap_contribute( RawOrigin::Signed(ALICE).into(), @@ -1914,7 +2141,7 @@ fn bootstrap_contribute_exceed_limits_should_work() { // * (2000000000000000000000000 * 4001000000000000000000000 + 4000000000000000000000000 * // 2002000000000000000000000) / (2002000000000000000000000 *2)) // = 2_828_427_323_371_633_862_327_510 - let alice_lp = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE); + let alice_lp = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE); assert_eq!(alice_lp, 2_828_427_323_371_633_862_327_509u128); // bob_lp = sqrt((2000000000000000000000 * 4001000000000000000000000 + @@ -1922,7 +2149,7 @@ fn bootstrap_contribute_exceed_limits_should_work() { // * (2000000000000000000000 * 4001000000000000000000000 + 1000000000000000000000 * 2002000000000000000000000) / // (2002000000000000000000000 *2)) // = 1767_369_577_951_894_138_583 - let bob_lp = ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &BOB); + let bob_lp = ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &BOB); assert_eq!(bob_lp, 1767_369_577_951_894_138_582u128); // bootstrap_mint_lp = 2828427323371633862327510 + 1767369577951894138583 = @@ -1933,11 +2160,11 @@ fn bootstrap_contribute_exceed_limits_should_work() { // bob_reward_ksm = 1767369577951894138582 * 10000000000000000000000 / // (2828427323371633862327510 + 1767369577951894138583) = 6_244_692_573_110_468_636 assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB), 12_489_385_146_220_937_273 ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &BOB), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &BOB), 6_244_692_573_110_468_636 ); @@ -1946,11 +2173,11 @@ fn bootstrap_contribute_exceed_limits_should_work() { // alice_reward_ksm = 2828427323371633862327510 * 10000000000000000000000 / // (2828427323371633862327510 + 1767369577951894138583) = 9_993_755_307_426_889_531_363 assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &ALICE), + ::MultiCurrency::free_balance(ETH_ASSET_ID, &ALICE), 19_987_510_614_853_779_062_726 ); assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &ALICE), + ::MultiCurrency::free_balance(KSM_ASSET_ID, &ALICE), 9_993_755_307_426_889_531_363 ); }) @@ -1974,11 +2201,27 @@ fn bootstrap_zero_reward_claim_should_work() { [(DOT_ASSET_ID, 2000 * unit), (BTC_ASSET_ID, 1000 * unit)].to_vec(), )); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &ALICE, 2_000_000 * unit)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &ALICE, 4_000_000 * unit)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &ALICE, + 2_000_000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &ALICE, + 4_000_000 * unit + )); - assert_ok!(DexPallet::foreign_mint(DOT_ASSET_ID, &BOB, 2_000 * unit)); - assert_ok!(DexPallet::foreign_mint(BTC_ASSET_ID, &BOB, 1_000 * unit)); + assert_ok!(::MultiCurrency::deposit( + DOT_ASSET_ID, + &BOB, + 2_000 * unit + )); + assert_ok!(::MultiCurrency::deposit( + BTC_ASSET_ID, + &BOB, + 1_000 * unit + )); assert_ok!(DexPallet::bootstrap_contribute( RawOrigin::Signed(ALICE).into(), @@ -2023,30 +2266,24 @@ fn bootstrap_zero_reward_claim_should_work() { )); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &ALICE), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &ALICE), 2_828_427_323_371_633_862_327_509 ); assert_eq!( - ::MultiAssetsHandler::balance_of(DOT_BTC_LP_ID, &BOB), + ::MultiCurrency::free_balance(DOT_BTC_LP_ID, &BOB), 1767_369_577_951_894_138_582 ); assert_eq!( - ::MultiAssetsHandler::total_supply(DOT_BTC_LP_ID), + ::MultiCurrency::total_issuance(DOT_BTC_LP_ID), 2_830_194_692_949_585_756_466_093 ); - assert_eq!(::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &BOB), 0); - assert_eq!(::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(ETH_ASSET_ID, &BOB), 0); + assert_eq!(::MultiCurrency::free_balance(KSM_ASSET_ID, &BOB), 0); - assert_eq!( - ::MultiAssetsHandler::balance_of(ETH_ASSET_ID, &ALICE), - 0 - ); - assert_eq!( - ::MultiAssetsHandler::balance_of(KSM_ASSET_ID, &ALICE), - 0 - ); + assert_eq!(::MultiCurrency::free_balance(ETH_ASSET_ID, &ALICE), 0); + assert_eq!(::MultiCurrency::free_balance(KSM_ASSET_ID, &ALICE), 0); }) } diff --git a/crates/dex-stable/src/lib.rs b/crates/dex-stable/src/lib.rs index 1641f3fd1f..a0b577f65e 100644 --- a/crates/dex-stable/src/lib.rs +++ b/crates/dex-stable/src/lib.rs @@ -311,6 +311,7 @@ pub mod pallet { /// - `admin_fee_receiver`: The admin fee receiver of created pool. /// - `lp_currency_symbol`: The symbol of created pool lp currency. /// - `lp_currency_decimal`: The decimal of created pool lp currency. + #[pallet::call_index(0)] #[pallet::weight(T::WeightInfo::create_base_pool())] #[transactional] pub fn create_base_pool( @@ -377,6 +378,7 @@ pub mod pallet { /// - `admin_fee_receiver`: The admin fee receiver of created pool. /// - `lp_currency_symbol`: The symbol of created pool lp currency. /// - `lp_currency_decimal`: The decimal of created pool lp currency. + #[pallet::call_index(1)] #[pallet::weight(T::WeightInfo::create_meta_pool())] #[transactional] pub fn create_meta_pool( @@ -450,6 +452,7 @@ pub mod pallet { /// - `amounts`: Supply amounts of currencies. /// - `min_mint_amount`: The min amount of lp currency get. /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(2)] #[pallet::weight(T::WeightInfo::add_liquidity())] #[transactional] pub fn add_liquidity( @@ -480,6 +483,7 @@ pub mod pallet { /// - `in_amount`: The amounts of currencies swap. /// - `min_mint_amount`: The min amount of receive currency. /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(3)] #[pallet::weight(T::WeightInfo::swap())] #[transactional] pub fn swap( @@ -518,6 +522,7 @@ pub mod pallet { /// - `lp_amount`: The amounts of lp currency. /// - `min_amounts`: The min amounts of pool's currencies to get. /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(4)] #[pallet::weight(T::WeightInfo::remove_liquidity())] #[transactional] pub fn remove_liquidity( @@ -547,6 +552,7 @@ pub mod pallet { /// - `index`: The index of receive currency. /// - `min_amount`: The min amounts of received currency; /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(5)] #[pallet::weight(T::WeightInfo::remove_liquidity_one_currency())] #[transactional] pub fn remove_liquidity_one_currency( @@ -576,6 +582,7 @@ pub mod pallet { /// - `amounts`: The specify amounts of receive currencies. /// - `max_burn_amount`: The max amount of burned lp currency. /// - `deadline`: Height of the cutoff block of this transaction + #[pallet::call_index(6)] #[pallet::weight(T::WeightInfo::remove_liquidity_imbalance())] #[transactional] pub fn remove_liquidity_imbalance( @@ -607,6 +614,7 @@ pub mod pallet { /// - `base_amounts`: Supply amounts of currencies to base pool. /// - `min_to_mint`: The min amount of pool lp currency get. /// - `deadline`: Height of the cutoff block of this transaction. + #[pallet::call_index(7)] #[pallet::weight(T::WeightInfo::add_pool_and_base_pool_liquidity())] #[transactional] pub fn add_pool_and_base_pool_liquidity( @@ -646,6 +654,7 @@ pub mod pallet { /// - `min_amounts_meta`: The min amounts of pool's currencies to get. /// - `min_amounts_base`: The min amounts of basic pool's currencies to get. /// - `deadline`: Height of the cutoff block of this transaction. + #[pallet::call_index(8)] #[pallet::weight(T::WeightInfo::remove_pool_and_base_pool_liquidity())] #[transactional] pub fn remove_pool_and_base_pool_liquidity( @@ -686,6 +695,7 @@ pub mod pallet { /// - `i`: The index of target currency in basic pool. /// - `min_amount`: The min amounts of received currency. /// - `deadline`: Height of the cutoff block of this transaction. + #[pallet::call_index(9)] #[pallet::weight(T::WeightInfo::remove_pool_and_base_pool_liquidity_one_currency())] #[transactional] pub fn remove_pool_and_base_pool_liquidity_one_currency( @@ -727,6 +737,7 @@ pub mod pallet { /// - `dx`: The amounts of swap currency. /// - `min_dy`: The min amounts of target currency. /// - `deadline`: Height of the cutoff block of this transaction. + #[pallet::call_index(10)] #[pallet::weight(T::WeightInfo::swap_pool_from_base())] #[transactional] pub fn swap_pool_from_base( @@ -761,6 +772,7 @@ pub mod pallet { /// - `dx`: The amounts of swap currency. /// - `min_dy`: The min amounts of target currency. /// - `deadline`: Height of the cutoff block of this transaction. + #[pallet::call_index(11)] #[pallet::weight(T::WeightInfo::swap_pool_to_base())] #[transactional] pub fn swap_pool_to_base( @@ -783,6 +795,7 @@ pub mod pallet { Ok(()) } + #[pallet::call_index(12)] #[pallet::weight(T::WeightInfo::swap_meta_pool_underlying())] #[transactional] pub fn swap_meta_pool_underlying( @@ -827,6 +840,7 @@ pub mod pallet { /// /// - `pool_id`: The id of pool. /// - `fee_receiver`: The new admin fee receiver of this pool. + #[pallet::call_index(13)] #[pallet::weight(1_000_000)] #[transactional] pub fn update_fee_receiver( @@ -856,6 +870,7 @@ pub mod pallet { /// /// - `pool_id`: The id of pool. /// - `new_swap_fee`: The new swap fee of this pool. + #[pallet::call_index(14)] #[pallet::weight(1_000_000)] #[transactional] pub fn set_swap_fee(origin: OriginFor, pool_id: T::PoolId, new_swap_fee: Number) -> DispatchResult { @@ -879,6 +894,7 @@ pub mod pallet { /// /// - `pool_id`: The id of pool. /// - `new_admin_fee`: The new admin fee of this pool. + #[pallet::call_index(15)] #[pallet::weight(1_000_000)] #[transactional] pub fn set_admin_fee(origin: OriginFor, pool_id: T::PoolId, new_admin_fee: Number) -> DispatchResult { @@ -905,6 +921,7 @@ pub mod pallet { /// - `pool_id`: The id of pool. /// - `future_a`: The new A to ramp towards. /// - `future_a_time`: Timestamp when the new A should be reached + #[pallet::call_index(16)] #[pallet::weight(1_000_000)] #[transactional] pub fn ramp_a( @@ -990,6 +1007,7 @@ pub mod pallet { /// # Argument /// /// - `pool_id`: The id of pool. + #[pallet::call_index(17)] #[pallet::weight(1_000_000)] #[transactional] pub fn stop_ramp_a(origin: OriginFor, pool_id: T::PoolId) -> DispatchResult { @@ -1027,6 +1045,7 @@ pub mod pallet { /// # Argument /// /// - `pool_id`: The id of pool. + #[pallet::call_index(18)] #[pallet::weight(T::WeightInfo::withdraw_admin_fee())] #[transactional] pub fn withdraw_admin_fee(origin: OriginFor, pool_id: T::PoolId) -> DispatchResult { diff --git a/crates/dex-swap-router/src/benchmarking.rs b/crates/dex-swap-router/src/benchmarking.rs index 5068b9b480..490d4b51ef 100644 --- a/crates/dex-swap-router/src/benchmarking.rs +++ b/crates/dex-swap-router/src/benchmarking.rs @@ -11,7 +11,7 @@ use frame_benchmarking::{benchmarks, whitelisted_caller}; use frame_support::assert_ok; use frame_system::RawOrigin; -use dex_general::{AssetId, MultiAssetsHandler, Pallet as NormalAmmPallet}; +use dex_general::Pallet as NormalAmmPallet; use dex_stable::Pallet as StableAmmPallet; use orml_traits::MultiCurrency; @@ -21,16 +21,8 @@ const INITIAL_A_VALUE: u128 = 50; const SWAP_FEE: u128 = 10000000; const ADMIN_FEE: u128 = 0; -const ASSET_0: AssetId = AssetId { - chain_id: 2001, - asset_type: 2, - asset_index: 515, -}; -const ASSET_1: AssetId = AssetId { - chain_id: 2001, - asset_type: 2, - asset_index: 514, -}; +const ASSET_0: u32 = 0; +const ASSET_1: u32 = 1; fn token1 + Default>() -> CurrencyId { CurrencyId::try_from(513u64).unwrap_or_default() @@ -43,16 +35,16 @@ fn token2 + Default>() -> CurrencyId { benchmarks! { where_clause { where T: Config + dex_general::Config + dex_stable::Config, ::CurrencyId: TryFrom + Default, - ::AssetId: From, + ::AssetId: From, ::StableCurrencyId: TryFrom + Default, - ::NormalCurrencyId: From, + ::NormalCurrencyId: From, } swap_exact_token_for_tokens_through_stable_pool{ let caller: T::AccountId = whitelisted_caller(); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); - assert_ok!(::MultiAssetsHandler::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_0.into(), &caller, 1000 * UNIT)); + assert_ok!(::MultiCurrency::deposit(ASSET_1.into(), &caller, 1000 * UNIT)); let stable_token1 = token1::<::CurrencyId>(); let stable_token2 = token2::<::CurrencyId>(); diff --git a/crates/dex-swap-router/src/lib.rs b/crates/dex-swap-router/src/lib.rs index c6d0ad03c7..3512b495d0 100644 --- a/crates/dex-swap-router/src/lib.rs +++ b/crates/dex-swap-router/src/lib.rs @@ -115,6 +115,7 @@ pub mod pallet { #[pallet::call] impl Pallet { + #[pallet::call_index(0)] #[pallet::weight(T::WeightInfo::swap_exact_token_for_tokens_through_stable_pool())] #[transactional] pub fn swap_exact_token_for_tokens_through_stable_pool( diff --git a/crates/dex-swap-router/src/mock.rs b/crates/dex-swap-router/src/mock.rs index f154f69ed1..725167eadf 100644 --- a/crates/dex-swap-router/src/mock.rs +++ b/crates/dex-swap-router/src/mock.rs @@ -8,13 +8,7 @@ use codec::{Decode, Encode, MaxEncodedLen}; use scale_info::TypeInfo; use serde::{Deserialize, Serialize}; -use frame_support::{ - dispatch::{DispatchError, DispatchResult}, - pallet_prelude::GenesisBuild, - parameter_types, - traits::Contains, - PalletId, -}; +use frame_support::{pallet_prelude::GenesisBuild, parameter_types, traits::Contains, PalletId}; use sp_core::H256; use sp_runtime::{ testing::Header, @@ -24,7 +18,7 @@ use sp_runtime::{ use crate as router; use crate::{Config, Pallet}; -use dex_general::{AssetBalance, AssetId, DexGeneralMultiAssets, MultiAssetsHandler, PairLpGenerate, LOCAL}; +use dex_general::GenerateLpAssetId; use dex_stable::traits::{StablePoolLpCurrencyIdGenerate, ValidateCurrency}; use orml_traits::{parameter_type_with_key, MultiCurrency}; @@ -41,7 +35,6 @@ parameter_types! { pub const MaxLocks:u32 = 50; pub const MinimumPeriod: Moment = SLOT_DURATION / 2; pub const PoolCurrencySymbolLimit: u32 = 50; - pub SelfParaId: u32 = CHAIN_ID; } parameter_type_with_key! { @@ -71,6 +64,29 @@ pub enum CurrencyId { LpToken(TokenSymbol, TokenSymbol), } +impl CurrencyId { + pub fn join_lp_token(currency_id_0: Self, currency_id_1: Self) -> Option { + let lp_token_0 = match currency_id_0 { + CurrencyId::Token(x) => x, + _ => return None, + }; + let lp_token_1 = match currency_id_1 { + CurrencyId::Token(y) => y, + _ => return None, + }; + Some(CurrencyId::LpToken(lp_token_0, lp_token_1)) + } +} + +impl dex_general::AssetInfo for CurrencyId { + fn is_support(&self) -> bool { + match self { + Self::Token(_) => true, + _ => false, + } + } +} + #[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, MaxEncodedLen, Ord, TypeInfo)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] pub enum PoolToken { @@ -129,23 +145,11 @@ impl orml_tokens::Config for Test { type CurrencyHooks = (); } -impl pallet_balances::Config for Test { - type Balance = u128; - type DustRemoval = (); - type RuntimeEvent = RuntimeEvent; - type ExistentialDeposit = ExistentialDeposit; - type AccountStore = frame_system::Pallet; - type WeightInfo = (); - type MaxLocks = (); - type MaxReserves = MaxReserves; - type ReserveIdentifier = [u8; 8]; -} - pub type Moment = u64; +pub type Balance = u128; + pub const MILLISECS_PER_BLOCK: Moment = 12000; pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; -pub const CHAIN_ID: u32 = 200u32; -pub type Balance = u128; impl pallet_timestamp::Config for Test { type MinimumPeriod = MinimumPeriod; @@ -167,13 +171,19 @@ impl dex_stable::Config for Test { type WeightInfo = (); } +pub struct PairLpIdentity; +impl GenerateLpAssetId for PairLpIdentity { + fn generate_lp_asset_id(asset_0: CurrencyId, asset_1: CurrencyId) -> Option { + CurrencyId::join_lp_token(asset_0, asset_1) + } +} + impl dex_general::Config for Test { type RuntimeEvent = RuntimeEvent; - type MultiAssetsHandler = DexGeneralMultiAssets>; + type MultiCurrency = Tokens; type PalletId = DexGeneralPalletId; - type AssetId = AssetId; - type LpGenerate = PairLpGenerate; - type SelfParaId = SelfParaId; + type AssetId = CurrencyId; + type LpGenerate = PairLpIdentity; type WeightInfo = (); } @@ -182,7 +192,7 @@ impl Config for Test { type StablePoolId = PoolId; type Balance = Balance; type StableCurrencyId = CurrencyId; - type NormalCurrencyId = AssetId; + type NormalCurrencyId = CurrencyId; type NormalAmm = DexGeneral; type StableAMM = StableAMM; type WeightInfo = (); @@ -223,62 +233,6 @@ where } } -pub fn asset_id_to_currency_id(asset_id: &AssetId) -> Result { - let discr = (asset_id.asset_index & 0x0000_0000_0000_ff00) >> 8; - return if discr == 6 { - let token0_id = ((asset_id.asset_index & 0x0000_0000_ffff_0000) >> 16) as u8; - let token1_id = ((asset_id.asset_index & 0x0000_ffff_0000_0000) >> 16) as u8; - Ok(CurrencyId::LpToken(token0_id, token1_id)) - } else { - let token_id = asset_id.asset_index as u8; - - Ok(CurrencyId::Token(token_id)) - }; -} - -pub struct LocalAssetAdaptor(PhantomData); - -impl MultiAssetsHandler for LocalAssetAdaptor -where - Local: MultiCurrency, -{ - fn balance_of(asset_id: AssetId, who: &AccountId) -> AssetBalance { - asset_id_to_currency_id(&asset_id).map_or(AssetBalance::default(), |currency_id| { - Local::free_balance(currency_id, who) - }) - } - - fn total_supply(asset_id: AssetId) -> AssetBalance { - asset_id_to_currency_id(&asset_id).map_or(AssetBalance::default(), |currency_id| { - Local::total_issuance(currency_id) - }) - } - - fn is_exists(asset_id: AssetId) -> bool { - asset_id_to_currency_id(&asset_id).map_or(false, |currency_id| { - Local::total_issuance(currency_id) > AssetBalance::default() - }) - } - - fn transfer(asset_id: AssetId, origin: &AccountId, target: &AccountId, amount: AssetBalance) -> DispatchResult { - asset_id_to_currency_id(&asset_id).map_or(Err(DispatchError::CannotLookup), |currency_id| { - Local::transfer(currency_id, origin, target, amount) - }) - } - - fn deposit(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result { - asset_id_to_currency_id(&asset_id).map_or(Ok(AssetBalance::default()), |currency_id| { - Local::deposit(currency_id, origin, amount).map(|_| amount) - }) - } - - fn withdraw(asset_id: AssetId, origin: &AccountId, amount: AssetBalance) -> Result { - asset_id_to_currency_id(&asset_id).map_or(Ok(AssetBalance::default()), |currency_id| { - Local::withdraw(currency_id, origin, amount).map(|_| amount) - }) - } -} - frame_support::construct_runtime!( pub enum Test where Block = Block, @@ -288,7 +242,6 @@ frame_support::construct_runtime!( System: frame_system::{Pallet, Call, Config, Storage, Event} = 0, Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent} = 1, - Balances: pallet_balances::{Pallet, Call, Storage, Config, Event} = 8, StableAMM: dex_stable::{Pallet, Call, Storage, Event} = 9, Tokens: orml_tokens::{Pallet, Storage, Event, Config} = 11, DexGeneral: dex_general::{Pallet, Call, Storage, Event} = 12, @@ -316,28 +269,14 @@ pub const TOKEN2_UNIT: u128 = 1_000_000_000_000_000_000; pub const TOKEN3_UNIT: u128 = 1_000_000; pub const TOKEN4_UNIT: u128 = 1_000_000; -pub const TOKEN1_ASSET_ID: AssetId = AssetId { - chain_id: CHAIN_ID, - asset_type: LOCAL, - asset_index: 1, -}; - -pub const TOKEN2_ASSET_ID: AssetId = AssetId { - chain_id: CHAIN_ID, - asset_type: LOCAL, - asset_index: 2, -}; +pub const TOKEN1_ASSET_ID: CurrencyId = CurrencyId::Token(1); +pub const TOKEN2_ASSET_ID: CurrencyId = CurrencyId::Token(2); pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default() .build_storage::() .unwrap() .into(); - pallet_balances::GenesisConfig:: { - balances: vec![(USER1, u128::MAX)], - } - .assimilate_storage(&mut t) - .unwrap(); orml_tokens::GenesisConfig:: { balances: vec![ diff --git a/parachain/runtime/interlay/src/lib.rs b/parachain/runtime/interlay/src/lib.rs index d130c5b3e6..4af7d1fa80 100644 --- a/parachain/runtime/interlay/src/lib.rs +++ b/parachain/runtime/interlay/src/lib.rs @@ -1414,13 +1414,6 @@ impl_runtime_apis! { } impl dex_general_rpc_runtime_api::DexGeneralApi for Runtime { - fn get_balance( - _asset_id: CurrencyId, - _owner: AccountId - ) -> dex_general::AssetBalance { - Default::default() - } - fn get_pair_by_asset_id( _asset_0: CurrencyId, _asset_1: CurrencyId diff --git a/parachain/runtime/kintsugi/src/lib.rs b/parachain/runtime/kintsugi/src/lib.rs index 31b6036190..66febcc2a5 100644 --- a/parachain/runtime/kintsugi/src/lib.rs +++ b/parachain/runtime/kintsugi/src/lib.rs @@ -1677,13 +1677,6 @@ impl_runtime_apis! { } impl dex_general_rpc_runtime_api::DexGeneralApi for Runtime { - fn get_balance( - _asset_id: CurrencyId, - _owner: AccountId - ) -> dex_general::AssetBalance { - Default::default() - } - fn get_pair_by_asset_id( _asset_0: CurrencyId, _asset_1: CurrencyId diff --git a/parachain/runtime/testnet-interlay/src/dex.rs b/parachain/runtime/testnet-interlay/src/dex.rs index b040ad992d..7c0863a685 100644 --- a/parachain/runtime/testnet-interlay/src/dex.rs +++ b/parachain/runtime/testnet-interlay/src/dex.rs @@ -7,9 +7,7 @@ use orml_traits::MultiCurrency; use sp_std::marker::PhantomData; use xcm::latest::prelude::*; -pub use dex_general::{ - AssetBalance, DexGeneralMultiAssets, GenerateLpAssetId, MultiAssetsHandler, PairInfo, LIQUIDITY, LOCAL, -}; +pub use dex_general::{AssetBalance, GenerateLpAssetId, PairInfo}; pub use dex_stable::traits::{StablePoolLpCurrencyIdGenerate, ValidateCurrency}; @@ -17,38 +15,6 @@ parameter_types! { pub const DexGeneralPalletId: PalletId = PalletId(*b"dex/genr"); pub const DexStablePalletId: PalletId = PalletId(*b"dex/stab"); pub const StringLimit: u32 = 50; - - // XCM - pub SelfParaId: u32 = ParachainInfo::get().into(); -} - -pub struct MultiAssetsAdaptor(PhantomData); - -impl MultiAssetsHandler for MultiAssetsAdaptor -where - Tokens: MultiCurrency, -{ - fn balance_of(asset_id: CurrencyId, who: &AccountId) -> AssetBalance { - Tokens::free_balance(asset_id, who) - } - - fn total_supply(asset_id: CurrencyId) -> AssetBalance { - Tokens::total_issuance(asset_id) - } - - fn is_exists(asset_id: CurrencyId) -> bool { - Tokens::total_issuance(asset_id) > AssetBalance::default() - } - - fn deposit(asset_id: CurrencyId, target: &AccountId, amount: AssetBalance) -> Result { - Tokens::deposit(asset_id, target, amount)?; - Ok(amount) - } - - fn withdraw(asset_id: CurrencyId, origin: &AccountId, amount: AssetBalance) -> Result { - Tokens::withdraw(asset_id, origin, amount)?; - Ok(amount) - } } pub struct PairLpIdentity; @@ -60,12 +26,10 @@ impl GenerateLpAssetId for PairLpIdentity { impl dex_general::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type MultiAssetsHandler = MultiAssetsAdaptor; + type MultiCurrency = Tokens; type PalletId = DexGeneralPalletId; type AssetId = CurrencyId; type LpGenerate = PairLpIdentity; - // NOTE: XCM not supported - type SelfParaId = SelfParaId; type WeightInfo = (); } diff --git a/parachain/runtime/testnet-interlay/src/lib.rs b/parachain/runtime/testnet-interlay/src/lib.rs index a59afac5bc..cfdd2a9afc 100644 --- a/parachain/runtime/testnet-interlay/src/lib.rs +++ b/parachain/runtime/testnet-interlay/src/lib.rs @@ -1637,14 +1637,6 @@ impl_runtime_apis! { } impl dex_general_rpc_runtime_api::DexGeneralApi for Runtime { - fn get_balance( - asset_id: CurrencyId, - owner: AccountId - ) -> dex_general::AssetBalance { - use dex_general::MultiAssetsHandler; - ::MultiAssetsHandler::balance_of(asset_id, &owner) - } - fn get_pair_by_asset_id( asset_0: CurrencyId, asset_1: CurrencyId diff --git a/parachain/runtime/testnet-kintsugi/src/dex.rs b/parachain/runtime/testnet-kintsugi/src/dex.rs index 2559f5c90a..0b47a62037 100644 --- a/parachain/runtime/testnet-kintsugi/src/dex.rs +++ b/parachain/runtime/testnet-kintsugi/src/dex.rs @@ -7,9 +7,7 @@ use orml_traits::MultiCurrency; use sp_std::marker::PhantomData; use xcm::latest::prelude::*; -pub use dex_general::{ - AssetBalance, DexGeneralMultiAssets, GenerateLpAssetId, MultiAssetsHandler, PairInfo, LIQUIDITY, LOCAL, -}; +pub use dex_general::{AssetBalance, GenerateLpAssetId, PairInfo}; pub use dex_stable::traits::{StablePoolLpCurrencyIdGenerate, ValidateCurrency}; @@ -17,38 +15,6 @@ parameter_types! { pub const DexGeneralPalletId: PalletId = PalletId(*b"dex/genr"); pub const DexStablePalletId: PalletId = PalletId(*b"dex/stbl"); pub const StringLimit: u32 = 50; - - // XCM - pub SelfParaId: u32 = ParachainInfo::get().into(); -} - -pub struct MultiAssetsAdaptor(PhantomData); - -impl MultiAssetsHandler for MultiAssetsAdaptor -where - Tokens: MultiCurrency, -{ - fn balance_of(asset_id: CurrencyId, who: &AccountId) -> AssetBalance { - Tokens::free_balance(asset_id, who) - } - - fn total_supply(asset_id: CurrencyId) -> AssetBalance { - Tokens::total_issuance(asset_id) - } - - fn is_exists(asset_id: CurrencyId) -> bool { - Tokens::total_issuance(asset_id) > AssetBalance::default() - } - - fn deposit(asset_id: CurrencyId, target: &AccountId, amount: AssetBalance) -> Result { - Tokens::deposit(asset_id, target, amount)?; - Ok(amount) - } - - fn withdraw(asset_id: CurrencyId, origin: &AccountId, amount: AssetBalance) -> Result { - Tokens::withdraw(asset_id, origin, amount)?; - Ok(amount) - } } pub struct PairLpIdentity; @@ -60,12 +26,10 @@ impl GenerateLpAssetId for PairLpIdentity { impl dex_general::Config for Runtime { type RuntimeEvent = RuntimeEvent; - type MultiAssetsHandler = MultiAssetsAdaptor; + type MultiCurrency = Tokens; type PalletId = DexGeneralPalletId; type AssetId = CurrencyId; type LpGenerate = PairLpIdentity; - // NOTE: XCM not supported - type SelfParaId = SelfParaId; type WeightInfo = (); } diff --git a/parachain/runtime/testnet-kintsugi/src/lib.rs b/parachain/runtime/testnet-kintsugi/src/lib.rs index 72f442eb56..e3617a34e7 100644 --- a/parachain/runtime/testnet-kintsugi/src/lib.rs +++ b/parachain/runtime/testnet-kintsugi/src/lib.rs @@ -1685,14 +1685,6 @@ impl_runtime_apis! { } impl dex_general_rpc_runtime_api::DexGeneralApi for Runtime { - fn get_balance( - asset_id: CurrencyId, - owner: AccountId - ) -> dex_general::AssetBalance { - use dex_general::MultiAssetsHandler; - ::MultiAssetsHandler::balance_of(asset_id, &owner) - } - fn get_pair_by_asset_id( asset_0: CurrencyId, asset_1: CurrencyId diff --git a/standalone/runtime/src/lib.rs b/standalone/runtime/src/lib.rs index cc6fc93999..c4fc514a77 100644 --- a/standalone/runtime/src/lib.rs +++ b/standalone/runtime/src/lib.rs @@ -1624,13 +1624,6 @@ impl_runtime_apis! { } impl dex_general_rpc_runtime_api::DexGeneralApi for Runtime { - fn get_balance( - _asset_id: CurrencyId, - _owner: AccountId - ) -> dex_general::AssetBalance { - Default::default() - } - fn get_pair_by_asset_id( _asset_0: CurrencyId, _asset_1: CurrencyId