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

feat: unified trade events #910

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
36 changes: 29 additions & 7 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ members = [
'pallets/evm-accounts',
'pallets/dynamic-evm-fee',
'pallets/xyk-liquidity-mining',
'pallets/trade-event',
'precompiles/call-permit',
'runtime-mock'
]
Expand Down Expand Up @@ -104,6 +105,7 @@ pallet-xyk-liquidity-mining = { path = "pallets/xyk-liquidity-mining", default-f
pallet-referrals = { path = "pallets/referrals", default-features = false}
pallet-evm-accounts = { path = "pallets/evm-accounts", default-features = false}
pallet-evm-accounts-rpc-runtime-api = { path = "pallets/evm-accounts/rpc/runtime-api", default-features = false}
pallet-trade-event = { path = "pallets/trade-event", default-features = false}

hydra-dx-build-script-utils = { path = "utils/build-script-utils", default-features = false }
scraper = { path = "scraper", default-features = false }
Expand Down
6 changes: 4 additions & 2 deletions pallets/lbp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-lbp"
version = "4.8.4"
version = "4.9.0"
description = "HydraDX Liquidity Bootstrapping Pool Pallet"
authors = ["GalacticCouncil"]
edition = "2021"
Expand All @@ -20,9 +20,10 @@ scale-info = { version = "2.3.1", default-features = false, features = ["derive"
primitive-types = { default-features = false, version = "0.12.0" }
serde = { features = ["derive"], optional = true, version = "1.0.136" }

## Local dependencies
# HydraDX dependencies
hydra-dx-math = { workspace = true }
hydradx-traits = { workspace = true }
pallet-trade-event = { workspace = true }

## ORML dependencies
orml-traits = { workspace = true }
Expand Down Expand Up @@ -66,5 +67,6 @@ std = [
'frame-benchmarking/std',
"scale-info/std",
"hydra-dx-math/std",
"pallet-trade-event/std",
]
try-runtime = ["frame-support/try-runtime"]
58 changes: 51 additions & 7 deletions pallets/lbp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use frame_system::ensure_signed;
use frame_system::pallet_prelude::BlockNumberFor;
use hydra_dx_math::types::LBPWeight;
use hydradx_traits::{AMMTransfer, AssetPairAccountIdFor, CanCreatePool, LockedBalance, AMM};
use pallet_trade_event::IncrementalIdType;
use orml_traits::{MultiCurrency, MultiCurrencyExtended, MultiLockableCurrency};

use scale_info::TypeInfo;
Expand Down Expand Up @@ -183,7 +184,7 @@ pub mod pallet {
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + pallet_trade_event::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// Multi currency for transfer of currencies
Expand Down Expand Up @@ -341,6 +342,7 @@ pub mod pallet {
},

/// Sale executed.
/// Deprecated. Replaced by pallet_trade_event::Swapped
SellExecuted {
who: T::AccountId,
asset_in: AssetId,
Expand All @@ -352,6 +354,7 @@ pub mod pallet {
},

/// Purchase executed.
/// Deprecated. Replaced by pallet_trade_event::Swapped
BuyExecuted {
who: T::AccountId,
asset_out: AssetId,
Expand Down Expand Up @@ -717,7 +720,8 @@ pub mod pallet {
/// - `amount`: The amount of `asset_in`
/// - `max_limit`: minimum amount of `asset_out` / amount of asset_out to be obtained from the pool in exchange for `asset_in`.
///
/// Emits `SellExecuted` when successful.
/// Emits `SellExecuted` when successful. Deprecated.
/// Emits `pallet_trade_event::Swapped` when successful.
#[pallet::call_index(4)]
#[pallet::weight(<T as Config>::WeightInfo::sell())]
pub fn sell(
Expand All @@ -729,7 +733,14 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

<Self as AMM<_, _, _, _>>::sell(&who, AssetPair { asset_in, asset_out }, amount, max_limit, false)?;
<Self as AMM<_, _, _, _, _>>::sell(
&who,
AssetPair { asset_in, asset_out },
amount,
max_limit,
false,
None,
)?;

Ok(())
}
Expand All @@ -747,7 +758,8 @@ pub mod pallet {
/// - `amount`: The amount of `asset_out`.
/// - `max_limit`: maximum amount of `asset_in` to be sold in exchange for `asset_out`.
///
/// Emits `BuyExecuted` when successful.
/// Emits `BuyExecuted` when successful. Deprecated.
/// Emits `pallet_trade_event::Swapped` when successful.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep both for a bit

#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::buy())]
pub fn buy(
Expand All @@ -759,7 +771,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

<Self as AMM<_, _, _, _>>::buy(&who, AssetPair { asset_in, asset_out }, amount, max_limit, false)?;
<Self as AMM<_, _, _, _, _>>::buy(&who, AssetPair { asset_in, asset_out }, amount, max_limit, false)?;

Ok(())
}
Expand Down Expand Up @@ -930,7 +942,9 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>> for Pallet<T> {
impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>, IncrementalIdType>
for Pallet<T>
{
fn exists(assets: AssetPair) -> bool {
let pair_account = Self::pair_account_from_assets(assets.asset_in, assets.asset_out);
<PoolData<T>>::contains_key(&pair_account)
Expand Down Expand Up @@ -1098,9 +1112,13 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>> for Pallet<T
}
}

fn execute_sell(transfer: &AMMTransfer<T::AccountId, AssetId, AssetPair, Balance>) -> DispatchResult {
fn execute_sell(
transfer: &AMMTransfer<T::AccountId, AssetId, AssetPair, Balance>,
batch_id: Option<IncrementalIdType>,
) -> DispatchResult {
Self::execute_trade(transfer)?;

// TODO: Deprecated, remove when ready
Self::deposit_event(Event::<T>::SellExecuted {
who: transfer.origin.clone(),
asset_in: transfer.assets.asset_in,
Expand All @@ -1111,6 +1129,18 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>> for Pallet<T
fee_amount: transfer.fee.1,
});

pallet_trade_event::Pallet::<T>::deposit_trade_event(
transfer.origin.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking to add second account because of the Intents... We might have 2 accounts trading directly. cc @enthusiastmartin wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also easier to track when if have multiple pools with same assets

pallet_trade_event::PoolType::LBP,
pallet_trade_event::TradeOperation::Sell,
transfer.assets.asset_in,
transfer.assets.asset_out,
transfer.amount,
transfer.amount_b,
vec![transfer.fee],
batch_id,
);

Ok(())
}

Expand Down Expand Up @@ -1235,6 +1265,7 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>> for Pallet<T
fn execute_buy(transfer: &AMMTransfer<T::AccountId, AssetId, AssetPair, BalanceOf<T>>) -> DispatchResult {
Self::execute_trade(transfer)?;

// TODO: Deprecated, remove when ready
Self::deposit_event(Event::<T>::BuyExecuted {
who: transfer.origin.clone(),
asset_out: transfer.assets.asset_out,
Expand All @@ -1244,6 +1275,19 @@ impl<T: Config> AMM<T::AccountId, AssetId, AssetPair, BalanceOf<T>> for Pallet<T
fee_asset: transfer.fee.0,
fee_amount: transfer.fee.1,
});

pallet_trade_event::Pallet::<T>::deposit_trade_event(
transfer.origin.clone(),
pallet_trade_event::PoolType::LBP,
pallet_trade_event::TradeOperation::Buy,
transfer.assets.asset_in,
transfer.assets.asset_out,
transfer.amount,
transfer.amount_b,
vec![transfer.fee],
None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

batch id?

);

Ok(())
}

Expand Down
5 changes: 5 additions & 0 deletions pallets/lbp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ frame_support::construct_runtime!(
System: frame_system,
LBPPallet: lbp,
Currency: orml_tokens,
TradeEvent: pallet_trade_event,
}

);
Expand Down Expand Up @@ -170,6 +171,10 @@ impl LockedBalance<AssetId, AccountId, Balance> for MultiLockedBalance {
}
}

impl pallet_trade_event::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type MultiCurrency = Currency;
Expand Down
20 changes: 18 additions & 2 deletions pallets/lbp/src/trade_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::*;
use hydradx_traits::router::{ExecutorError, PoolType, TradeExecution};
use hydradx_traits::AMM;
use orml_traits::MultiCurrency;
use pallet_trade_event::IncrementalIdType;
use sp_runtime::traits::BlockNumberProvider;
use sp_runtime::DispatchError::Corruption;
use sp_runtime::{ArithmeticError, DispatchError, FixedPointNumber, FixedU128};
impl<T: Config> TradeExecution<T::RuntimeOrigin, T::AccountId, AssetId, Balance> for Pallet<T> {

impl<T: Config> TradeExecution<T::RuntimeOrigin, T::AccountId, AssetId, Balance, IncrementalIdType> for Pallet<T> {
type Error = DispatchError;

fn calculate_sell(
Expand Down Expand Up @@ -110,12 +112,26 @@ impl<T: Config> TradeExecution<T::RuntimeOrigin, T::AccountId, AssetId, Balance>
asset_out: AssetId,
amount_in: Balance,
min_limit: Balance,
batch_id: Option<IncrementalIdType>,
) -> Result<(), ExecutorError<Self::Error>> {
if pool_type != PoolType::LBP {
return Err(ExecutorError::NotSupported);
}

Self::sell(who, asset_in, asset_out, amount_in, min_limit).map_err(ExecutorError::Error)
let who = crate::ensure_signed(who).map_err(|e| ExecutorError::Error(e.into()))?;

<Self as AMM<_, _, _, _, _>>::sell(
&who,
AssetPair { asset_in, asset_out },
amount_in,
min_limit,
false,
batch_id,
)
.map_err(ExecutorError::Error)?;

Ok(())
// Self::sell(who, asset_in, asset_out, amount_in, min_limit).map_err(ExecutorError::Error)
}

fn execute_buy(
Expand Down
Loading
Loading