diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index dbbb45ba8e2..b765425e87a 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -228,10 +228,7 @@ pub fn testnet_parachain_config(para_id: ParaId) -> Result { vec![hex!["30035c21ba9eda780130f2029a80c3e962f56588bc04c36be95a225cb536fb55"].into()], hex!["30035c21ba9eda780130f2029a80c3e962f56588bc04c36be95a225cb536fb55"].into(), // SAME AS ROOT vec![], - vec![ - (b"KSM".to_vec(), 1_000u128), - (b"KUSD".to_vec(), 1_000u128), - ], + vec![(b"KSM".to_vec(), 1_000u128), (b"KUSD".to_vec(), 1_000u128)], vec![(1, Price::from_float(0.0000212)), (2, Price::from_float(0.000806))], ) }, @@ -315,10 +312,7 @@ pub fn parachain_development_config(para_id: ParaId) -> Result("Alice"), // SAME AS ROOT get_vesting_config_for_test(), - vec![ - (b"KSM".to_vec(), 1_000u128), - (b"KUSD".to_vec(), 1_000u128), - ], + vec![(b"KSM".to_vec(), 1_000u128), (b"KUSD".to_vec(), 1_000u128)], vec![(1, Price::from_float(0.0000212)), (2, Price::from_float(0.000806))], ) }, @@ -464,10 +458,7 @@ pub fn local_parachain_config(para_id: ParaId) -> Result { ], get_account_id_from_seed::("Alice"), // SAME AS ROOT get_vesting_config_for_test(), - vec![ - (b"KSM".to_vec(), 1_000u128), - (b"KUSD".to_vec(), 1_000u128), - ], + vec![(b"KSM".to_vec(), 1_000u128), (b"KUSD".to_vec(), 1_000u128)], vec![(1, Price::from_float(0.0000212)), (2, Price::from_float(0.000806))], ) }, diff --git a/node/src/testing_chain_spec.rs b/node/src/testing_chain_spec.rs index 4dfae1ab467..70d6d364a03 100644 --- a/node/src/testing_chain_spec.rs +++ b/node/src/testing_chain_spec.rs @@ -19,7 +19,7 @@ #![allow(clippy::too_many_arguments)] use cumulus_primitives_core::ParaId; -use primitives::{BlockNumber, Price, AssetId}; +use primitives::{AssetId, BlockNumber, Price}; use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; use sc_service::ChainType; use serde::{Deserialize, Serialize}; @@ -142,10 +142,7 @@ pub fn parachain_development_config(para_id: ParaId) -> Result("Alice"), // SAME AS ROOT get_vesting_config_for_test(), - vec![ - (b"KSM".to_vec(), 1_000u128), - (b"KUSD".to_vec(), 1_000u128), - ], + vec![(b"KSM".to_vec(), 1_000u128), (b"KUSD".to_vec(), 1_000u128)], vec![(1, Price::from_float(0.0000212)), (2, Price::from_float(0.000806))], ) }, @@ -221,10 +218,7 @@ pub fn local_parachain_config(para_id: ParaId) -> Result { ], get_account_id_from_seed::("Alice"), // SAME AS ROOT get_vesting_config_for_test(), - vec![ - (b"KSM".to_vec(), 1_000u128), - (b"KUSD".to_vec(), 1_000u128), - ], + vec![(b"KSM".to_vec(), 1_000u128), (b"KUSD".to_vec(), 1_000u128)], vec![(1, Price::from_float(0.0000212)), (2, Price::from_float(0.000806))], ) }, diff --git a/pallets/duster/src/lib.rs b/pallets/duster/src/lib.rs index 2be68585803..e1e28f52282 100644 --- a/pallets/duster/src/lib.rs +++ b/pallets/duster/src/lib.rs @@ -194,11 +194,7 @@ pub mod pallet { /// /// Caller is rewarded with chosen reward in native currency. #[pallet::weight((::WeightInfo::dust_account(), DispatchClass::Normal, Pays::Yes))] - pub fn dust_account( - origin: OriginFor, - account: T::AccountId, - currency_id: T::CurrencyId, - ) -> DispatchResult { + pub fn dust_account(origin: OriginFor, account: T::AccountId, currency_id: T::CurrencyId) -> DispatchResult { let who = ensure_signed(origin)?; ensure!(Self::blacklisted(&account).is_none(), Error::::AccountBlacklisted); diff --git a/pallets/exchange/benchmarking/src/mock.rs b/pallets/exchange/benchmarking/src/mock.rs index 600dfaee9c2..74f868a5e58 100644 --- a/pallets/exchange/benchmarking/src/mock.rs +++ b/pallets/exchange/benchmarking/src/mock.rs @@ -29,10 +29,11 @@ use sp_runtime::{ }; use frame_system::EnsureSigned; -use pallet_xyk::AssetPairAccountIdFor; use primitives::{ constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO, MIN_POOL_LIQUIDITY, MIN_TRADING_LIMIT}, - fee, AssetId, Balance, + fee, + traits::AssetPairAccountIdFor, + AssetId, Balance, }; pub type Amount = i128; @@ -121,7 +122,7 @@ impl orml_tokens::Config for Test { pub struct AssetPairAccountIdTest(); impl AssetPairAccountIdFor for AssetPairAccountIdTest { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> u64 { + fn from_assets(asset_a: AssetId, asset_b: AssetId, _: &str) -> u64 { let mut a = asset_a as u128; let mut b = asset_b as u128; if a > b { diff --git a/pallets/exchange/src/direct.rs b/pallets/exchange/src/direct.rs index c18e491cf90..cbc87e5717e 100644 --- a/pallets/exchange/src/direct.rs +++ b/pallets/exchange/src/direct.rs @@ -81,36 +81,36 @@ impl<'a, T: Config> DirectTradeData<'a, T> { match (&self.intention_a.sell_or_buy, &self.intention_b.sell_or_buy) { (IntentionType::SELL, IntentionType::SELL) => { if !Self::reserve_if_can( - self.intention_a.assets.asset_in, - &self.intention_a.who, - self.amount_from_a, + self.intention_a.assets.asset_in, + &self.intention_a.who, + self.amount_from_a, ) { - return false; + return false; } if !Self::reserve_if_can( - self.intention_a.assets.asset_out, - &self.intention_b.who, - self.amount_from_b, + self.intention_a.assets.asset_out, + &self.intention_b.who, + self.amount_from_b, ) { - return false; + return false; } let transfer = Transfer:: { - from: &self.intention_a.who, - to: &self.intention_b.who, - asset: self.intention_a.assets.asset_in, - amount: self.amount_from_a - transfer_a_fee, - fee_transfer: false, + from: &self.intention_a.who, + to: &self.intention_b.who, + asset: self.intention_a.assets.asset_in, + amount: self.amount_from_a - transfer_a_fee, + fee_transfer: false, }; self.transfers.push(transfer); let transfer = Transfer:: { - from: &self.intention_b.who, - to: &self.intention_a.who, - asset: self.intention_a.assets.asset_out, - amount: self.amount_from_b - transfer_b_fee, - fee_transfer: false, + from: &self.intention_b.who, + to: &self.intention_a.who, + asset: self.intention_a.assets.asset_out, + amount: self.amount_from_b - transfer_b_fee, + fee_transfer: false, }; self.transfers.push(transfer); @@ -134,36 +134,36 @@ impl<'a, T: Config> DirectTradeData<'a, T> { } (IntentionType::BUY, IntentionType::BUY) => { if !Self::reserve_if_can( - self.intention_a.assets.asset_in, - &self.intention_a.who, - self.amount_from_a + transfer_a_fee, + self.intention_a.assets.asset_in, + &self.intention_a.who, + self.amount_from_a + transfer_a_fee, ) { - return false; + return false; } if !Self::reserve_if_can( - self.intention_a.assets.asset_out, - &self.intention_b.who, - self.amount_from_b + transfer_b_fee, + self.intention_a.assets.asset_out, + &self.intention_b.who, + self.amount_from_b + transfer_b_fee, ) { - return false; + return false; } let transfer = Transfer:: { - from: &self.intention_a.who, - to: &self.intention_b.who, - asset: self.intention_a.assets.asset_in, - amount: self.amount_from_a, - fee_transfer: false, + from: &self.intention_a.who, + to: &self.intention_b.who, + asset: self.intention_a.assets.asset_in, + amount: self.amount_from_a, + fee_transfer: false, }; self.transfers.push(transfer); let transfer = Transfer:: { - from: &self.intention_b.who, - to: &self.intention_a.who, - asset: self.intention_a.assets.asset_out, - amount: self.amount_from_b, - fee_transfer: false, + from: &self.intention_b.who, + to: &self.intention_a.who, + asset: self.intention_a.assets.asset_out, + amount: self.amount_from_b, + fee_transfer: false, }; self.transfers.push(transfer); @@ -187,36 +187,36 @@ impl<'a, T: Config> DirectTradeData<'a, T> { } (IntentionType::BUY, IntentionType::SELL) => { if !Self::reserve_if_can( - self.intention_a.assets.asset_in, - &self.intention_a.who, - self.amount_from_a + transfer_a_fee, + self.intention_a.assets.asset_in, + &self.intention_a.who, + self.amount_from_a + transfer_a_fee, ) { - return false; + return false; } if !Self::reserve_if_can( - self.intention_a.assets.asset_out, - &self.intention_b.who, - self.amount_from_b, + self.intention_a.assets.asset_out, + &self.intention_b.who, + self.amount_from_b, ) { - return false; + return false; } let transfer = Transfer:: { - from: &self.intention_a.who, - to: &self.intention_b.who, - asset: self.intention_a.assets.asset_in, - amount: self.amount_from_a - transfer_a_fee, - fee_transfer: false, + from: &self.intention_a.who, + to: &self.intention_b.who, + asset: self.intention_a.assets.asset_in, + amount: self.amount_from_a - transfer_a_fee, + fee_transfer: false, }; self.transfers.push(transfer); let transfer = Transfer:: { - from: &self.intention_b.who, - to: &self.intention_a.who, - asset: self.intention_a.assets.asset_out, - amount: self.amount_from_b, - fee_transfer: false, + from: &self.intention_b.who, + to: &self.intention_a.who, + asset: self.intention_a.assets.asset_out, + amount: self.amount_from_b, + fee_transfer: false, }; self.transfers.push(transfer); @@ -231,36 +231,36 @@ impl<'a, T: Config> DirectTradeData<'a, T> { } (IntentionType::SELL, IntentionType::BUY) => { if !Self::reserve_if_can( - self.intention_a.assets.asset_in, - &self.intention_a.who, - self.amount_from_a, + self.intention_a.assets.asset_in, + &self.intention_a.who, + self.amount_from_a, ) { - return false; + return false; } if !Self::reserve_if_can( - self.intention_a.assets.asset_out, - &self.intention_b.who, - self.amount_from_b + transfer_b_fee, + self.intention_a.assets.asset_out, + &self.intention_b.who, + self.amount_from_b + transfer_b_fee, ) { - return false; + return false; } let transfer = Transfer:: { - from: &self.intention_a.who, - to: &self.intention_b.who, - asset: self.intention_a.assets.asset_in, - amount: self.amount_from_a, - fee_transfer: false, + from: &self.intention_a.who, + to: &self.intention_b.who, + asset: self.intention_a.assets.asset_in, + amount: self.amount_from_a, + fee_transfer: false, }; self.transfers.push(transfer); let transfer = Transfer:: { - from: &self.intention_b.who, - to: &self.intention_a.who, - asset: self.intention_a.assets.asset_out, - amount: self.amount_from_b - transfer_b_fee, - fee_transfer: false, + from: &self.intention_b.who, + to: &self.intention_a.who, + asset: self.intention_a.assets.asset_out, + amount: self.amount_from_b - transfer_b_fee, + fee_transfer: false, }; self.transfers.push(transfer); diff --git a/pallets/exchange/src/lib.rs b/pallets/exchange/src/lib.rs index e447eeb9112..7e0334f8d7c 100644 --- a/pallets/exchange/src/lib.rs +++ b/pallets/exchange/src/lib.rs @@ -57,8 +57,8 @@ type IntentionId = ::Hash; pub type Intention = ExchangeIntention<::AccountId, Balance, IntentionId>; // Re-export pallet items so that they can be accessed from the crate namespace. -pub use pallet::*; use frame_support::pallet_prelude::*; +pub use pallet::*; #[frame_support::pallet] pub mod pallet { @@ -195,7 +195,7 @@ pub mod pallet { MinimumTradeLimitNotReached, /// Overflow - IntentionCountOverflow + IntentionCountOverflow, } /// Intention count for current block diff --git a/pallets/exchange/src/mock.rs b/pallets/exchange/src/mock.rs index 60a6f75ace2..29196181e8f 100644 --- a/pallets/exchange/src/mock.rs +++ b/pallets/exchange/src/mock.rs @@ -31,10 +31,11 @@ use pallet_xyk as xyk; use frame_support::traits::{Everything, GenesisBuild, Get}; use frame_system::EnsureSigned; -use pallet_xyk::AssetPairAccountIdFor; use primitives::{ constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO, MIN_POOL_LIQUIDITY, MIN_TRADING_LIMIT}, - fee, AssetId, Balance, + fee, + traits::AssetPairAccountIdFor, + AssetId, Balance, }; use std::cell::RefCell; @@ -138,7 +139,7 @@ impl orml_tokens::Config for Test { pub struct AssetPairAccountIdTest(); impl AssetPairAccountIdFor for AssetPairAccountIdTest { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> u64 { + fn from_assets(asset_a: AssetId, asset_b: AssetId, _: &str) -> u64 { let mut a = asset_a as u128; let mut b = asset_b as u128; if a > b { diff --git a/pallets/lbp/src/benchmarking.rs b/pallets/lbp/src/benchmarking.rs index 5b0aedd6907..e3503126fb1 100644 --- a/pallets/lbp/src/benchmarking.rs +++ b/pallets/lbp/src/benchmarking.rs @@ -30,7 +30,7 @@ fn funded_account(name: &'static str, index: u32) -> T::AccountId { benchmarks! { create_pool { let caller = funded_account::("caller", 0); - let pool_id = T::AssetPairPoolId::from_assets(ASSET_A_ID, ASSET_B_ID); + let pool_id = LBP::::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID); }: _(RawOrigin::Root, caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, Fee::default(), caller) verify { @@ -40,7 +40,7 @@ benchmarks! { update_pool_data { let caller = funded_account::("caller", 0); let fee_collector = funded_account::("fee_collector", 0); - let pool_id = T::AssetPairPoolId::from_assets(ASSET_A_ID, ASSET_B_ID); + let pool_id = LBP::::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID); let new_start = T::BlockNumber::from(50_u32); let new_end = T::BlockNumber::from(100_u32); let new_initial_weight = 45_250_600; @@ -60,7 +60,7 @@ benchmarks! { add_liquidity { let caller = funded_account::("caller", 0); - let pool_id = T::AssetPairPoolId::from_assets(ASSET_A_ID, ASSET_B_ID); + let pool_id = LBP::::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID); LBP::::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, Fee::default(), caller.clone())?; ensure!(PoolData::::contains_key(&pool_id), "Pool does not exist."); @@ -73,7 +73,7 @@ benchmarks! { remove_liquidity { let caller = funded_account::("caller", 0); - let pool_id = T::AssetPairPoolId::from_assets(ASSET_A_ID, ASSET_B_ID); + let pool_id = LBP::::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID); LBP::::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, Fee::default(), caller.clone())?; ensure!(PoolData::::contains_key(&pool_id), "Pool does not exist."); @@ -93,7 +93,7 @@ benchmarks! { let amount : Balance = 100_000_000; let max_limit: Balance = 10_000_000; - let pool_id = T::AssetPairPoolId::from_assets(ASSET_A_ID, ASSET_B_ID); + let pool_id = LBP::::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID); LBP::::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, Fee::default(), fee_collector.clone())?; ensure!(PoolData::::contains_key(&pool_id), "Pool does not exist."); @@ -119,7 +119,7 @@ benchmarks! { let asset_in: AssetId = ASSET_B_ID; let amount : Balance = 100_000_000; let max_limit: Balance = 1_000_000_000; - let pool_id = T::AssetPairPoolId::from_assets(ASSET_A_ID, ASSET_B_ID); + let pool_id = LBP::::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID); LBP::::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, Fee::default(), fee_collector.clone())?; ensure!(PoolData::::contains_key(&pool_id), "Pool does not exist."); diff --git a/pallets/lbp/src/lib.rs b/pallets/lbp/src/lib.rs index af5a9bc7ea9..f659d45ec8a 100644 --- a/pallets/lbp/src/lib.rs +++ b/pallets/lbp/src/lib.rs @@ -6,8 +6,7 @@ use codec::{Decode, Encode}; use frame_support::sp_runtime::{ - app_crypto::sp_core::crypto::UncheckedFrom, - traits::{AtLeast32BitUnsigned, BlockNumberProvider, Hash, Saturating, Zero}, + traits::{AtLeast32BitUnsigned, BlockNumberProvider, Saturating, Zero}, DispatchError, RuntimeDebug, }; use frame_support::{ @@ -19,7 +18,7 @@ use frame_support::{ use frame_system::ensure_signed; use hydra_dx_math::types::LBPWeight; use orml_traits::{MultiCurrency, MultiCurrencyExtended, MultiReservableCurrency}; -use primitives::traits::{AMMTransfer, AMM}; +use primitives::traits::{AMMTransfer, AssetPairAccountIdFor, AMM}; use primitives::{ asset::AssetPair, constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO}, @@ -31,7 +30,7 @@ use scale_info::TypeInfo; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; -use sp_std::{marker::PhantomData, vec, vec::Vec}; +use sp_std::{vec, vec::Vec}; #[cfg(test)] mod mock; @@ -174,7 +173,7 @@ pub mod pallet { type LBPWeightFunction: LBPWeightCalculation; /// Mapping of asset pairs to unique pool identities - type AssetPairPoolId: AssetPairPoolIdFor>; + type AssetPairAccountId: AssetPairAccountIdFor>; /// Weight information for the extrinsics type WeightInfo: WeightInfo; @@ -506,7 +505,7 @@ pub mod pallet { let (asset_a, asset_b) = (amount_a.0, amount_b.0); let (amount_a, amount_b) = (amount_a.1, amount_b.1); - let pool_id = T::AssetPairPoolId::from_assets(asset_a, asset_b); + let pool_id = Self::pair_account_from_assets(asset_a, asset_b); let pool_data = >::try_get(&pool_id).map_err(|_| Error::::PoolNotFound)?; ensure!(who == pool_data.owner, Error::::NotOwner); @@ -855,40 +854,20 @@ impl Pallet { .just_fee(pool_data.fee) .ok_or::>(Error::::FeeAmountInvalid)?) } -} - -pub trait AssetPairPoolIdFor { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> PoolId; -} - -pub struct AssetPairPoolId(PhantomData); -impl AssetPairPoolIdFor> for AssetPairPoolId -where - PoolId: UncheckedFrom + AsRef<[u8]>, -{ - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> PoolId { - let mut buf: Vec = b"lbp".to_vec(); - - if asset_a < asset_b { - buf.extend_from_slice(&asset_a.to_le_bytes()); - buf.extend_from_slice(&asset_b.to_le_bytes()); - } else { - buf.extend_from_slice(&asset_b.to_le_bytes()); - buf.extend_from_slice(&asset_a.to_le_bytes()); - } - PoolId::::unchecked_from(T::Hashing::hash(&buf[..])) + pub fn pair_account_from_assets(asset_a: AssetId, asset_b: AssetId) -> PoolId { + T::AssetPairAccountId::from_assets(asset_a, asset_b, "lbp") } } impl AMM> for Pallet { fn exists(assets: AssetPair) -> bool { - let pair_account = T::AssetPairPoolId::from_assets(assets.asset_in, assets.asset_out); + let pair_account = Self::pair_account_from_assets(assets.asset_in, assets.asset_out); >::contains_key(&pair_account) } fn get_pair_id(assets: AssetPair) -> T::AccountId { - T::AssetPairPoolId::from_assets(assets.asset_in, assets.asset_out) + Self::pair_account_from_assets(assets.asset_in, assets.asset_out) } fn get_pool_assets(pool_account_id: &T::AccountId) -> Option> { diff --git a/pallets/lbp/src/mock.rs b/pallets/lbp/src/mock.rs index e09de0678d7..ed42f4b0167 100644 --- a/pallets/lbp/src/mock.rs +++ b/pallets/lbp/src/mock.rs @@ -2,7 +2,7 @@ use super::*; use crate as lbp; -use crate::{AssetPairPoolIdFor, Config}; +use crate::{AssetPairAccountIdFor, Config}; use frame_support::parameter_types; use frame_support::traits::{Everything, GenesisBuild}; use orml_traits::parameter_type_with_key; @@ -96,10 +96,10 @@ impl orml_tokens::Config for Test { type DustRemovalWhitelist = Everything; } -pub struct AssetPairPoolIdTest(); +pub struct AssetPairAccountIdTest(); -impl AssetPairPoolIdFor for AssetPairPoolIdTest { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> u64 { +impl AssetPairAccountIdFor for AssetPairAccountIdTest { + fn from_assets(asset_a: AssetId, asset_b: AssetId, _: &str) -> u64 { let mut a = asset_a as u128; let mut b = asset_b as u128; if a > b { @@ -123,7 +123,7 @@ impl Config for Test { type NativeAssetId = NativeAssetId; type CreatePoolOrigin = frame_system::EnsureRoot; type LBPWeightFunction = lbp::LBPWeightFunction; - type AssetPairPoolId = AssetPairPoolIdTest; + type AssetPairAccountId = AssetPairAccountIdTest; type WeightInfo = (); type MinTradingLimit = MinTradingLimit; type MinPoolLiquidity = MinPoolLiquidity; diff --git a/pallets/lbp/src/tests.rs b/pallets/lbp/src/tests.rs index e2027dd4087..909dec925ce 100644 --- a/pallets/lbp/src/tests.rs +++ b/pallets/lbp/src/tests.rs @@ -248,9 +248,14 @@ fn create_pool_should_work() { assert_eq!(pool_data.fee, Fee::default()); assert_eq!(pool_data.fee_collector, CHARLIE); - expect_events(vec![ - Event::LiquidityAdded(ACA_DOT_POOL_ID, ACA, DOT, 1_000_000_000, 2_000_000_000).into(), - ]); + expect_events(vec![Event::LiquidityAdded( + ACA_DOT_POOL_ID, + ACA, + DOT, + 1_000_000_000, + 2_000_000_000, + ) + .into()]); }); } @@ -311,9 +316,14 @@ fn create_same_pool_should_not_work() { Error::::PoolAlreadyExists ); - expect_events(vec![ - Event::LiquidityAdded(ACA_DOT_POOL_ID, ACA, DOT, 1_000_000_000, 2_000_000_000).into(), - ]); + expect_events(vec![Event::LiquidityAdded( + ACA_DOT_POOL_ID, + ACA, + DOT, + 1_000_000_000, + 2_000_000_000, + ) + .into()]); }); } diff --git a/pallets/transaction-multi-payment/benchmarking/src/mock.rs b/pallets/transaction-multi-payment/benchmarking/src/mock.rs index 549fbc7293d..3d54d91e437 100644 --- a/pallets/transaction-multi-payment/benchmarking/src/mock.rs +++ b/pallets/transaction-multi-payment/benchmarking/src/mock.rs @@ -33,11 +33,12 @@ use orml_currencies::BasicCurrencyAdapter; use pallet_transaction_multi_payment::MultiCurrencyAdapter; use primitives::{ constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO, MIN_POOL_LIQUIDITY, MIN_TRADING_LIMIT}, - fee, Amount, AssetId, Balance, + fee, + traits::AssetPairAccountIdFor, + Amount, AssetId, Balance, }; use frame_support::traits::Get; -use pallet_xyk::AssetPairAccountIdFor; use std::cell::RefCell; use frame_benchmarking::frame_support::weights::Pays; @@ -162,7 +163,7 @@ impl pallet_transaction_payment::Config for Test { pub struct AssetPairAccountIdTest(); impl AssetPairAccountIdFor for AssetPairAccountIdTest { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> u64 { + fn from_assets(asset_a: AssetId, asset_b: AssetId, _: &str) -> u64 { let mut a = asset_a as u128; let mut b = asset_b as u128; if a > b { diff --git a/pallets/transaction-multi-payment/src/mock.rs b/pallets/transaction-multi-payment/src/mock.rs index 9d728fc4051..fe9fd193787 100644 --- a/pallets/transaction-multi-payment/src/mock.rs +++ b/pallets/transaction-multi-payment/src/mock.rs @@ -33,10 +33,11 @@ use frame_support::weights::Weight; use orml_currencies::BasicCurrencyAdapter; use primitives::{ constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO, MIN_POOL_LIQUIDITY, MIN_TRADING_LIMIT}, - fee, Amount, AssetId, Balance, Price, + fee, + traits::AssetPairAccountIdFor, + Amount, AssetId, Balance, Price, }; -use pallet_xyk::AssetPairAccountIdFor; use std::cell::RefCell; use frame_support::traits::{Everything, GenesisBuild, Get}; @@ -192,7 +193,7 @@ impl pallet_transaction_payment::Config for Test { pub struct AssetPairAccountIdTest(); impl AssetPairAccountIdFor for AssetPairAccountIdTest { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> u64 { + fn from_assets(asset_a: AssetId, asset_b: AssetId, _: &str) -> u64 { let mut a = asset_a as u128; let mut b = asset_b as u128; if a > b { diff --git a/pallets/xyk/src/lib.rs b/pallets/xyk/src/lib.rs index 28b3c721ffa..167282c9853 100644 --- a/pallets/xyk/src/lib.rs +++ b/pallets/xyk/src/lib.rs @@ -28,20 +28,16 @@ #![allow(clippy::unused_unit)] #![allow(clippy::upper_case_acronyms)] -use frame_support::sp_runtime::{ - traits::{Hash, Zero}, - DispatchError, -}; +use frame_support::sp_runtime::{traits::Zero, DispatchError}; use frame_support::{dispatch::DispatchResult, ensure, traits::Get, transactional}; use frame_system::ensure_signed; use primitives::{asset::AssetPair, constants::chain::MIN_POOL_LIQUIDITY, fee, traits::AMM, AssetId, Balance, Price}; -use sp_std::{marker::PhantomData, vec, vec::Vec}; +use sp_std::{vec, vec::Vec}; -use frame_support::sp_runtime::app_crypto::sp_core::crypto::UncheckedFrom; use frame_support::sp_runtime::FixedPointNumber; use orml_traits::{MultiCurrency, MultiCurrencyExtended}; use primitives::fee::WithFee; -use primitives::traits::AMMTransfer; +use primitives::traits::{AMMTransfer, AssetPairAccountIdFor}; use primitives::Amount; #[cfg(test)] @@ -556,30 +552,6 @@ pub mod pallet { } } -pub trait AssetPairAccountIdFor { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> AccountId; -} - -pub struct AssetPairAccountId(PhantomData); - -impl AssetPairAccountIdFor for AssetPairAccountId -where - T::AccountId: UncheckedFrom + AsRef<[u8]>, -{ - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> T::AccountId { - let mut buf = Vec::new(); - buf.extend_from_slice(b"hydradx"); - if asset_a < asset_b { - buf.extend_from_slice(&asset_a.to_le_bytes()); - buf.extend_from_slice(&asset_b.to_le_bytes()); - } else { - buf.extend_from_slice(&asset_b.to_le_bytes()); - buf.extend_from_slice(&asset_a.to_le_bytes()); - } - T::AccountId::unchecked_from(T::Hashing::hash(&buf[..])) - } -} - impl Pallet { /// Return balance of each asset in selected liquidity pool. pub fn get_pool_balances(pool_address: T::AccountId) -> Option> { @@ -606,6 +578,10 @@ impl Pallet { .just_fee(T::GetExchangeFee::get()) .ok_or::>(Error::::FeeAmountInvalid)?) } + + pub fn pair_account_from_assets(asset_a: AssetId, asset_b: AssetId) -> T::AccountId { + T::AssetPairAccountId::from_assets(asset_a, asset_b, "xyk") + } } // Implementation of AMM API which makes possible to plug the AMM pool into the exchange pallet. @@ -615,7 +591,7 @@ impl AMM for Pallet { } fn get_pair_id(assets: AssetPair) -> T::AccountId { - T::AssetPairAccountId::from_assets(assets.asset_in, assets.asset_out) + Self::pair_account_from_assets(assets.asset_in, assets.asset_out) } fn get_pool_assets(pool_account_id: &T::AccountId) -> Option> { diff --git a/pallets/xyk/src/mock.rs b/pallets/xyk/src/mock.rs index de205d3003d..e74d5d4acc5 100644 --- a/pallets/xyk/src/mock.rs +++ b/pallets/xyk/src/mock.rs @@ -16,7 +16,7 @@ // limitations under the License. use crate as xyk; -use crate::{AssetPairAccountIdFor, Config}; +use crate::Config; use frame_support::parameter_types; use frame_system as system; use orml_traits::parameter_type_with_key; @@ -29,7 +29,9 @@ use sp_runtime::{ use frame_support::traits::{Everything, GenesisBuild, Get}; use primitives::{ constants::chain::{MAX_IN_RATIO, MAX_OUT_RATIO, MIN_POOL_LIQUIDITY, MIN_TRADING_LIMIT}, - fee, AssetId, Balance, + fee, + traits::AssetPairAccountIdFor, + AssetId, Balance, }; use frame_system::EnsureSigned; @@ -139,7 +141,7 @@ impl orml_tokens::Config for Test { pub struct AssetPairAccountIdTest(); impl AssetPairAccountIdFor for AssetPairAccountIdTest { - fn from_assets(asset_a: AssetId, asset_b: AssetId) -> u64 { + fn from_assets(asset_a: AssetId, asset_b: AssetId, _: &str) -> u64 { let mut a = asset_a as u128; let mut b = asset_b as u128; if a > b { diff --git a/primitives/src/traits.rs b/primitives/src/traits.rs index 04faef1d334..a08f8604799 100644 --- a/primitives/src/traits.rs +++ b/primitives/src/traits.rs @@ -159,3 +159,7 @@ pub trait ShareTokenRegistry: Registry { + fn from_assets(asset_a: AssetId, asset_b: AssetId, identifier: &str) -> AccountId; +} diff --git a/runtime/basilisk/src/lib.rs b/runtime/basilisk/src/lib.rs index 265c06f650b..d370a4a9c9b 100644 --- a/runtime/basilisk/src/lib.rs +++ b/runtime/basilisk/src/lib.rs @@ -38,12 +38,14 @@ use sp_core::{ OpaqueMetadata, }; use sp_runtime::{ + app_crypto::sp_core::crypto::UncheckedFrom, create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdConversion, BlakeTwo256, Block as BlockT, IdentityLookup}, transaction_validity::{TransactionSource, TransactionValidity}, ApplyExtrinsicResult, Perbill, }; use sp_std::convert::From; +use sp_std::marker::PhantomData; use sp_std::prelude::*; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -60,9 +62,8 @@ use frame_support::{ DispatchClass, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }, }; -use pallet_lbp::AssetPairPoolIdFor; use pallet_transaction_payment::TargetedFeeAdjustment; -use pallet_xyk::AssetPairAccountIdFor; +use primitives::traits::AssetPairAccountIdFor; pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; #[allow(clippy::all)] @@ -193,6 +194,25 @@ impl BlockNumberProvider for RelayChainBlockNumberProvi } } +pub struct AssetPairAccountId(PhantomData); +impl AssetPairAccountIdFor for AssetPairAccountId +where + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ + fn from_assets(asset_a: AssetId, asset_b: AssetId, identifier: &str) -> T::AccountId { + let mut buf: Vec = identifier.as_bytes().to_vec(); + + if asset_a < asset_b { + buf.extend_from_slice(&asset_a.to_le_bytes()); + buf.extend_from_slice(&asset_b.to_le_bytes()); + } else { + buf.extend_from_slice(&asset_b.to_le_bytes()); + buf.extend_from_slice(&asset_a.to_le_bytes()); + } + T::AccountId::unchecked_from(::hash(&buf[..])) + } +} + parameter_types! { pub const Version: RuntimeVersion = VERSION; /// Block weights base values and limits. @@ -377,7 +397,7 @@ impl pallet_asset_registry::Config for Runtime { impl pallet_xyk::Config for Runtime { type Event = Event; type AssetRegistry = AssetRegistry; - type AssetPairAccountId = pallet_xyk::AssetPairAccountId; + type AssetPairAccountId = AssetPairAccountId; type Currency = Currencies; type NativeAssetId = NativeAssetId; type WeightInfo = weights::xyk::BasiliskWeight; @@ -402,7 +422,7 @@ impl pallet_lbp::Config for Runtime { type NativeAssetId = NativeAssetId; type CreatePoolOrigin = EnsureSuperMajorityTechCommitteeOrRoot; type LBPWeightFunction = pallet_lbp::LBPWeightFunction; - type AssetPairPoolId = pallet_lbp::AssetPairPoolId; + type AssetPairAccountId = AssetPairAccountId; type MinTradingLimit = MinTradingLimit; type MinPoolLiquidity = MinPoolLiquidity; type MaxInRatio = MaxInRatio; @@ -937,7 +957,7 @@ impl_runtime_apis! { } fn get_pool_id(asset_a: AssetId, asset_b: AssetId) -> AccountId{ - pallet_xyk::AssetPairAccountId::::from_assets(asset_a, asset_b) + XYK::pair_account_from_assets(asset_a, asset_b) } } @@ -947,7 +967,7 @@ impl_runtime_apis! { AssetId, > for Runtime { fn get_pool_id(asset_a: AssetId, asset_b: AssetId) -> AccountId{ - pallet_lbp::AssetPairPoolId::::from_assets(asset_a, asset_b) + LBP::pair_account_from_assets(asset_a, asset_b) } } diff --git a/runtime/testing-basilisk/src/lib.rs b/runtime/testing-basilisk/src/lib.rs index ad6747f0aa8..80d3ae53997 100644 --- a/runtime/testing-basilisk/src/lib.rs +++ b/runtime/testing-basilisk/src/lib.rs @@ -38,12 +38,14 @@ use sp_core::{ OpaqueMetadata, }; use sp_runtime::{ + app_crypto::sp_core::crypto::UncheckedFrom, create_runtime_str, generic, impl_opaque_keys, traits::{AccountIdConversion, BlakeTwo256, Block as BlockT, IdentityLookup}, transaction_validity::{TransactionSource, TransactionValidity}, ApplyExtrinsicResult, Perbill, }; use sp_std::convert::From; +use sp_std::marker::PhantomData; use sp_std::prelude::*; #[cfg(feature = "std")] use sp_version::NativeVersion; @@ -60,15 +62,14 @@ use frame_support::{ DispatchClass, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }, }; -use pallet_lbp::AssetPairPoolIdFor; use pallet_transaction_payment::TargetedFeeAdjustment; -use pallet_xyk::AssetPairAccountIdFor; +use primitives::traits::AssetPairAccountIdFor; pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; +mod adapter; #[allow(clippy::all)] mod weights; mod xcm; -mod adapter; use pallet_xyk_rpc_runtime_api as xyk_rpc; @@ -130,8 +131,8 @@ pub fn native_version() -> NativeVersion { } } -use smallvec::smallvec; use crate::adapter::OrmlTokensAdapter; +use smallvec::smallvec; pub struct WeightToFee; impl WeightToFeePolynomial for WeightToFee { @@ -160,6 +161,25 @@ impl WeightToFeePolynomial for WeightToFee { } } +pub struct AssetPairAccountId(PhantomData); +impl AssetPairAccountIdFor for AssetPairAccountId +where + T::AccountId: UncheckedFrom + AsRef<[u8]>, +{ + fn from_assets(asset_a: AssetId, asset_b: AssetId, identifier: &str) -> T::AccountId { + let mut buf: Vec = identifier.as_bytes().to_vec(); + + if asset_a < asset_b { + buf.extend_from_slice(&asset_a.to_le_bytes()); + buf.extend_from_slice(&asset_b.to_le_bytes()); + } else { + buf.extend_from_slice(&asset_b.to_le_bytes()); + buf.extend_from_slice(&asset_a.to_le_bytes()); + } + T::AccountId::unchecked_from(::hash(&buf[..])) + } +} + parameter_types! { pub const Version: RuntimeVersion = VERSION; /// Block weights base values and limits. @@ -344,7 +364,7 @@ impl pallet_asset_registry::Config for Runtime { impl pallet_xyk::Config for Runtime { type Event = Event; type AssetRegistry = AssetRegistry; - type AssetPairAccountId = pallet_xyk::AssetPairAccountId; + type AssetPairAccountId = AssetPairAccountId; type Currency = Currencies; type NativeAssetId = NativeAssetId; type WeightInfo = weights::xyk::BasiliskWeight; @@ -369,7 +389,7 @@ impl pallet_lbp::Config for Runtime { type NativeAssetId = NativeAssetId; type CreatePoolOrigin = EnsureSuperMajorityTechCommitteeOrRoot; type LBPWeightFunction = pallet_lbp::LBPWeightFunction; - type AssetPairPoolId = pallet_lbp::AssetPairPoolId; + type AssetPairAccountId = AssetPairAccountId; type MinTradingLimit = MinTradingLimit; type MinPoolLiquidity = MinPoolLiquidity; type MaxInRatio = MaxInRatio; @@ -896,7 +916,7 @@ impl_runtime_apis! { } fn get_pool_id(asset_a: AssetId, asset_b: AssetId) -> AccountId{ - pallet_xyk::AssetPairAccountId::::from_assets(asset_a, asset_b) + XYK::pair_account_from_assets(asset_a, asset_b) } } @@ -907,7 +927,7 @@ impl_runtime_apis! { AssetId, > for Runtime { fn get_pool_id(asset_a: AssetId, asset_b: AssetId) -> AccountId{ - pallet_lbp::AssetPairPoolId::::from_assets(asset_a, asset_b) + LBP::pair_account_from_assets(asset_a, asset_b) } }