From 5d34cdbb70e5c2713bfaf3c1c454f385ebcc5a1e Mon Sep 17 00:00:00 2001 From: martinfridrich Date: Tue, 24 Oct 2023 14:14:30 +0200 Subject: [PATCH] pallet-xyk: remove dep on primitives --- Cargo.lock | 6 +-- integration-tests/Cargo.toml | 2 +- integration-tests/src/xyk.rs | 2 +- pallets/xyk/Cargo.toml | 2 +- pallets/xyk/src/impls.rs | 3 +- pallets/xyk/src/lib.rs | 18 +++---- pallets/xyk/src/tests/amm_position.rs | 2 +- pallets/xyk/src/tests/creation.rs | 2 +- pallets/xyk/src/tests/fees.rs | 2 +- pallets/xyk/src/tests/liquidity.rs | 4 +- pallets/xyk/src/tests/mock.rs | 6 ++- pallets/xyk/src/tests/spot_price.rs | 3 +- pallets/xyk/src/tests/trades.rs | 2 +- pallets/xyk/src/trade_execution.rs | 3 +- pallets/xyk/src/types.rs | 68 +++++++++++++++++++++++++++ runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/assets.rs | 2 + runtime/hydradx/src/lib.rs | 2 +- 18 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 pallets/xyk/src/types.rs diff --git a/Cargo.lock b/Cargo.lock index c27a34c6f..b9d94998b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3831,7 +3831,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "183.0.0" +version = "184.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", @@ -7779,7 +7779,7 @@ dependencies = [ [[package]] name = "pallet-xyk" -version = "6.2.10" +version = "6.3.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -10220,7 +10220,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.13.0" +version = "1.14.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 427cb6e53..dcc8170dd 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.13.0" +version = "1.14.0" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/integration-tests/src/xyk.rs b/integration-tests/src/xyk.rs index 882427e27..ed737e062 100644 --- a/integration-tests/src/xyk.rs +++ b/integration-tests/src/xyk.rs @@ -4,7 +4,7 @@ use crate::polkadot_test_net::*; use hydradx_runtime::{DustRemovalWhitelist, RuntimeOrigin, XYK}; use hydradx_traits::AMM; -use primitives::{asset::AssetPair, AssetId}; +use pallet_xyk::types::AssetPair; use xcm_emulator::TestExt; use frame_support::{assert_ok, traits::Contains}; diff --git a/pallets/xyk/Cargo.toml b/pallets/xyk/Cargo.toml index 4c88f9acc..0bb2fac5d 100644 --- a/pallets/xyk/Cargo.toml +++ b/pallets/xyk/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-xyk' -version = "6.2.10" +version = "6.3.0" description = 'XYK automated market maker' authors = ['GalacticCouncil'] edition = '2021' diff --git a/pallets/xyk/src/impls.rs b/pallets/xyk/src/impls.rs index 8bba9f9a6..41f8a3ca0 100644 --- a/pallets/xyk/src/impls.rs +++ b/pallets/xyk/src/impls.rs @@ -1,8 +1,7 @@ +use crate::types::{AssetId, AssetPair, Price}; use hydradx_traits::pools::SpotPriceProvider; use hydradx_traits::AMM; use orml_traits::MultiCurrency; -use primitives::asset::AssetPair; -use primitives::{AssetId, Price}; use sp_runtime::FixedPointNumber; use sp_std::marker::PhantomData; diff --git a/pallets/xyk/src/lib.rs b/pallets/xyk/src/lib.rs index 9e442e140..a53812719 100644 --- a/pallets/xyk/src/lib.rs +++ b/pallets/xyk/src/lib.rs @@ -35,9 +35,9 @@ use hydradx_traits::{ AMMPosition, AMMTransfer, AssetPairAccountIdFor, CanCreatePool, OnCreatePoolHandler, OnLiquidityChangedHandler, OnTradeHandler, Source, AMM, }; -use primitives::{asset::AssetPair, AssetId, Balance}; use sp_std::{vec, vec::Vec}; +use crate::types::{AssetId, AssetPair, Balance}; use hydra_dx_math::ratio::Ratio; use orml_traits::{MultiCurrency, MultiCurrencyExtended}; use primitives::Amount; @@ -49,15 +49,13 @@ mod benchmarking; mod impls; mod trade_execution; +pub mod types; pub mod weights; pub use impls::XYKSpotPrice; use weights::WeightInfo; -/// Oracle source identifier for this pallet. -pub const SOURCE: Source = *b"hydraxyk"; - // Re-export pallet items so that they can be accessed from the crate namespace. pub use pallet::*; @@ -114,6 +112,10 @@ pub mod pallet { #[pallet::constant] type MaxOutRatio: Get; + /// Oracle source identifier for this pallet. + #[pallet::constant] + type OracleSource: Get; + /// Called to ensure that pool can be created type CanCreatePool: CanCreatePool; @@ -461,7 +463,7 @@ pub mod pallet { let liquidity_a = T::Currency::total_balance(asset_a, &pair_account); let liquidity_b = T::Currency::total_balance(asset_b, &pair_account); T::AMMHandler::on_liquidity_changed( - SOURCE, + T::OracleSource::get(), asset_a, asset_b, amount_a, @@ -567,7 +569,7 @@ pub mod pallet { let liquidity_a = T::Currency::total_balance(asset_a, &pair_account); let liquidity_b = T::Currency::total_balance(asset_b, &pair_account); T::AMMHandler::on_liquidity_changed( - SOURCE, + T::OracleSource::get(), asset_a, asset_b, remove_amount_a, @@ -870,7 +872,7 @@ impl AMM for Pallet { let liquidity_in = T::Currency::total_balance(transfer.assets.asset_in, &pair_account); let liquidity_out = T::Currency::total_balance(transfer.assets.asset_out, &pair_account); T::AMMHandler::on_trade( - SOURCE, + T::OracleSource::get(), transfer.assets.asset_in, transfer.assets.asset_out, transfer.amount, @@ -1032,7 +1034,7 @@ impl AMM for Pallet { let liquidity_in = T::Currency::total_balance(transfer.assets.asset_in, &pair_account); let liquidity_out = T::Currency::total_balance(transfer.assets.asset_out, &pair_account); T::AMMHandler::on_trade( - SOURCE, + T::OracleSource::get(), transfer.assets.asset_in, transfer.assets.asset_out, transfer.amount, diff --git a/pallets/xyk/src/tests/amm_position.rs b/pallets/xyk/src/tests/amm_position.rs index b04b73da1..65196d4d3 100644 --- a/pallets/xyk/src/tests/amm_position.rs +++ b/pallets/xyk/src/tests/amm_position.rs @@ -1,7 +1,7 @@ use super::mock::*; +use crate::types::AssetPair; use crate::*; use frame_support::assert_ok; -use primitives::asset::AssetPair; #[test] fn get_liquidity_behind_shares_should_return_both_assets_value_when_pool_exists() { diff --git a/pallets/xyk/src/tests/creation.rs b/pallets/xyk/src/tests/creation.rs index fb4e56ba2..30e2a67fa 100644 --- a/pallets/xyk/src/tests/creation.rs +++ b/pallets/xyk/src/tests/creation.rs @@ -7,7 +7,7 @@ use orml_traits::MultiCurrency; use pallet_asset_registry::AssetType; use sp_std::convert::TryInto; -use primitives::asset::AssetPair; +use crate::types::AssetPair; #[test] fn create_pool_should_work() { diff --git a/pallets/xyk/src/tests/fees.rs b/pallets/xyk/src/tests/fees.rs index 07477e8f4..49937e3a8 100644 --- a/pallets/xyk/src/tests/fees.rs +++ b/pallets/xyk/src/tests/fees.rs @@ -4,7 +4,7 @@ use frame_support::{assert_noop, assert_ok}; use hydradx_traits::AMM as AmmPool; use orml_traits::MultiCurrency; -use primitives::asset::AssetPair; +use crate::types::AssetPair; #[test] fn fee_calculation() { diff --git a/pallets/xyk/src/tests/liquidity.rs b/pallets/xyk/src/tests/liquidity.rs index 7a5438387..2e2ac8e40 100644 --- a/pallets/xyk/src/tests/liquidity.rs +++ b/pallets/xyk/src/tests/liquidity.rs @@ -1,12 +1,10 @@ pub use super::mock::*; +use crate::types::{AssetPair, Balance}; use crate::{Error, Event}; use frame_support::{assert_noop, assert_ok}; use hydradx_traits::AMM as AmmPool; use orml_traits::MultiCurrency; -use primitives::asset::AssetPair; -use primitives::Balance; - #[test] fn add_liquidity_should_work() { new_test_ext().execute_with(|| { diff --git a/pallets/xyk/src/tests/mock.rs b/pallets/xyk/src/tests/mock.rs index a376fa2aa..9823f1e67 100644 --- a/pallets/xyk/src/tests/mock.rs +++ b/pallets/xyk/src/tests/mock.rs @@ -27,9 +27,9 @@ use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup, One}, }; +use crate::types::{AssetId, Balance}; use frame_support::traits::{Everything, GenesisBuild, Get, Nothing}; -use hydradx_traits::{AssetPairAccountIdFor, CanCreatePool}; -use primitives::{AssetId, Balance}; +use hydradx_traits::{AssetPairAccountIdFor, CanCreatePool, Source}; use frame_system::EnsureSigned; use hydradx_traits::pools::DustRemovalAccountWhitelist; @@ -181,6 +181,7 @@ parameter_types! { pub MaxOutRatio: u128 = MaximumOutRatio::get(); pub ExchangeFeeRate: (u32, u32) = ExchangeFee::get(); pub DiscountedFeeRate: (u32, u32) = DiscountedFee::get(); + pub const OracleSourceIdentifier: Source = *b"hydraxyk"; } pub struct Disallow10_10Pool(); @@ -207,6 +208,7 @@ impl Config for Test { type AMMHandler = (); type DiscountedFee = DiscountedFeeRate; type NonDustableWhitelistHandler = Whitelist; + type OracleSource = OracleSourceIdentifier; } pub struct ExtBuilder { diff --git a/pallets/xyk/src/tests/spot_price.rs b/pallets/xyk/src/tests/spot_price.rs index 66bf37eba..b24eea24e 100644 --- a/pallets/xyk/src/tests/spot_price.rs +++ b/pallets/xyk/src/tests/spot_price.rs @@ -1,11 +1,10 @@ use super::mock::*; +use crate::types::{AssetPair, Price}; use crate::XYKSpotPrice; use crate::*; use frame_support::assert_ok; use frame_support::dispatch::RawOrigin; use hydradx_traits::pools::SpotPriceProvider; -use primitives::asset::AssetPair; -use primitives::Price; #[test] fn spot_price_provider_should_return_correct_price_when_pool_exists() { diff --git a/pallets/xyk/src/tests/trades.rs b/pallets/xyk/src/tests/trades.rs index 5c51c2d9b..55c05d925 100644 --- a/pallets/xyk/src/tests/trades.rs +++ b/pallets/xyk/src/tests/trades.rs @@ -4,7 +4,7 @@ use frame_support::{assert_noop, assert_ok}; use hydradx_traits::AMM as AmmPool; use orml_traits::MultiCurrency; -use primitives::asset::AssetPair; +use crate::types::AssetPair; #[test] fn sell_test() { diff --git a/pallets/xyk/src/trade_execution.rs b/pallets/xyk/src/trade_execution.rs index 6722ed711..c0d77218e 100644 --- a/pallets/xyk/src/trade_execution.rs +++ b/pallets/xyk/src/trade_execution.rs @@ -1,11 +1,10 @@ +use crate::types::{AssetId, AssetPair, Balance}; use crate::{Config, Error, Pallet}; use frame_support::ensure; use frame_support::traits::Get; use hydradx_traits::router::{ExecutorError, PoolType, TradeExecution}; use hydradx_traits::AMM; use orml_traits::MultiCurrency; -use primitives::asset::AssetPair; -use primitives::{AssetId, Balance}; use sp_runtime::DispatchError; impl TradeExecution for Pallet { diff --git a/pallets/xyk/src/types.rs b/pallets/xyk/src/types.rs new file mode 100644 index 000000000..acae283c6 --- /dev/null +++ b/pallets/xyk/src/types.rs @@ -0,0 +1,68 @@ +// This file is part of Basilisk-node. + +// Copyright (C) 2020-2022 Intergalactic, Limited (GIB). +// SPDX-License-Identifier: Apache-2.0 + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub type AssetId = u32; +pub type Balance = u128; +pub type Price = FixedU128; + +use codec::{Decode, Encode}; +use scale_info::TypeInfo; +use sp_runtime::FixedU128; +use sp_std::vec::Vec; + +#[cfg(feature = "std")] +use serde::{Deserialize, Serialize}; + +/// Asset Pair representation for AMM trades +/// ( asset_a, asset_b ) combination where asset_a is meant to be exchanged for asset_b +/// +/// asset_in represents asset coming into the pool +/// asset_out represents asset coming out of the pool +#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] +#[derive(Debug, Encode, Decode, Copy, Clone, PartialEq, Eq, Default, TypeInfo)] +pub struct AssetPair { + pub asset_in: AssetId, + pub asset_out: AssetId, +} + +impl AssetPair { + pub fn new(asset_in: AssetId, asset_out: AssetId) -> Self { + Self { asset_in, asset_out } + } + + /// Return ordered asset tuple (A,B) where A < B + /// Used in storage + pub fn ordered_pair(&self) -> (AssetId, AssetId) { + match self.asset_in <= self.asset_out { + true => (self.asset_in, self.asset_out), + false => (self.asset_out, self.asset_in), + } + } + + /// Return share token name + pub fn name(&self) -> Vec { + let mut buf: Vec = Vec::new(); + + let (asset_a, asset_b) = self.ordered_pair(); + + buf.extend_from_slice(&asset_a.to_le_bytes()); + buf.extend_from_slice(b"HDT"); + buf.extend_from_slice(&asset_b.to_le_bytes()); + + buf + } +} diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index 4d36357fe..bcbe3e3a5 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "183.0.0" +version = "184.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index f8c922620..ee1a6fd47 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -906,6 +906,7 @@ impl pallet_lbp::Config for Runtime { parameter_types! { pub XYKExchangeFee: (u32, u32) = (3, 1_000); pub const DiscountedFee: (u32, u32) = (7, 10_000); + pub const XYKOracleSourceIdentifier: Source = *b"hydraxyk"; } impl pallet_xyk::Config for Runtime { @@ -924,4 +925,5 @@ impl pallet_xyk::Config for Runtime { type AMMHandler = pallet_ema_oracle::OnActivityHandler; type DiscountedFee = DiscountedFee; type NonDustableWhitelistHandler = Duster; + type OracleSource = XYKOracleSourceIdentifier; } diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index c7ffe6dd6..50436df0c 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -94,7 +94,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 183, + spec_version: 184, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,