Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove dependency on primitives from pallet-xyk #693

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.14.2"
version = "1.15.0"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/xyk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 1 addition & 2 deletions pallets/xyk/src/impls.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
18 changes: 10 additions & 8 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::*;

Expand Down Expand Up @@ -114,6 +112,10 @@ pub mod pallet {
#[pallet::constant]
type MaxOutRatio: Get<u128>;

/// Oracle source identifier for this pallet.
#[pallet::constant]
type OracleSource: Get<Source>;

/// Called to ensure that pool can be created
type CanCreatePool: CanCreatePool<AssetId>;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -870,7 +872,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
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,
Expand Down Expand Up @@ -1032,7 +1034,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, Balance> for Pallet<T> {
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,
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/src/tests/amm_position.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/src/tests/creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/src/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions pallets/xyk/src/tests/liquidity.rs
Original file line number Diff line number Diff line change
@@ -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(|| {
Expand Down
6 changes: 4 additions & 2 deletions pallets/xyk/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -207,6 +208,7 @@ impl Config for Test {
type AMMHandler = ();
type DiscountedFee = DiscountedFeeRate;
type NonDustableWhitelistHandler = Whitelist;
type OracleSource = OracleSourceIdentifier;
}

pub struct ExtBuilder {
Expand Down
3 changes: 1 addition & 2 deletions pallets/xyk/src/tests/spot_price.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/src/tests/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions pallets/xyk/src/trade_execution.rs
Original file line number Diff line number Diff line change
@@ -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<T: Config> TradeExecution<T::RuntimeOrigin, T::AccountId, AssetId, Balance> for Pallet<T> {
Expand Down
68 changes: 68 additions & 0 deletions pallets/xyk/src/types.rs
Original file line number Diff line number Diff line change
@@ -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<u8> {
let mut buf: Vec<u8> = 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
}
}
2 changes: 1 addition & 1 deletion runtime/hydradx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydradx-runtime"
version = "185.0.0"
version = "186.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
license = "Apache 2.0"
Expand Down
2 changes: 2 additions & 0 deletions runtime/hydradx/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,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 {
Expand All @@ -928,4 +929,5 @@ impl pallet_xyk::Config for Runtime {
type AMMHandler = pallet_ema_oracle::OnActivityHandler<Runtime>;
type DiscountedFee = DiscountedFee;
type NonDustableWhitelistHandler = Duster;
type OracleSource = XYKOracleSourceIdentifier;
}
2 changes: 1 addition & 1 deletion runtime/hydradx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: 185,
spec_version: 186,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading