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 61 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
6cfc1cd
initial implementation
Roznovjak Sep 20, 2024
2935f90
revert removed comments about emitted events
Roznovjak Sep 20, 2024
ce87882
Merge branch 'master' into unified_trade_events
Roznovjak Sep 25, 2024
d01eab3
formatting
Roznovjak Sep 25, 2024
5bb6696
additional implementation
Roznovjak Sep 25, 2024
3ad68ab
rename batch_id to event_id
Roznovjak Sep 25, 2024
4ac8adc
rename pallet_trade_event to pallet_amm_support
Roznovjak Sep 25, 2024
1c83f9d
Merge branch 'master' into unified_trade_events
Roznovjak Sep 26, 2024
87bd948
fix some tests
Roznovjak Sep 27, 2024
ac008a3
update the event structure
Roznovjak Sep 27, 2024
111b99a
Merge branch 'master' into unified_trade_events
Roznovjak Sep 29, 2024
0f5c9a3
fix lbp tests
Roznovjak Sep 30, 2024
cb02e14
fix xyk tests
Roznovjak Sep 30, 2024
914b332
fix router tests
Roznovjak Oct 1, 2024
66272e8
fix omnipool tests
Roznovjak Oct 1, 2024
cd80ac6
formatting
Roznovjak Oct 1, 2024
065d0a7
fix stableswap tests
Roznovjak Oct 1, 2024
1d0a377
fix otc tests
Roznovjak Oct 1, 2024
81c4b6a
fix integration tests
Roznovjak Oct 2, 2024
a50ffb7
formatting
Roznovjak Oct 2, 2024
1e209bb
fix integration tests
Roznovjak Oct 2, 2024
d58de6c
satisfy clippy
Roznovjak Oct 2, 2024
2fe9d65
check event in otc-settlements test
Roznovjak Oct 2, 2024
c24094e
formatting
Roznovjak Oct 2, 2024
4bca2b6
add pallet-amm-support tests
Roznovjak Oct 2, 2024
d38504e
formatting
Roznovjak Oct 2, 2024
288705d
refactor incremental id computation
Roznovjak Oct 2, 2024
5f79fbb
satisfy clippy
Roznovjak Oct 2, 2024
682c88b
fix compilation error
dmoka Nov 4, 2024
1f8f7bd
stack
Roznovjak Nov 4, 2024
e2efb8a
revert old impl that is not needed anymore
Roznovjak Nov 5, 2024
c083ebf
formatting
Roznovjak Nov 5, 2024
5825368
fixed some compilation error
dmoka Nov 5, 2024
94bc0f9
fix compilation errors
dmoka Nov 5, 2024
66023cb
emit missing swapped events in omnipool
dmoka Nov 5, 2024
fc28af6
clean up in comments
dmoka Nov 5, 2024
62c10f4
Merge remote-tracking branch 'origin/master' into unified_trade_events
dmoka Nov 5, 2024
24721f7
update lock
dmoka Nov 5, 2024
2d5394e
fix compilation error
dmoka Nov 5, 2024
d220923
Merge remote-tracking branch 'origin/master' into unified_trade_events
dmoka Nov 5, 2024
b2d1ef5
formatting
dmoka Nov 5, 2024
ca4b6ba
update lock
dmoka Nov 6, 2024
2178b22
Merge remote-tracking branch 'origin/master' into unified_trade_events
dmoka Nov 6, 2024
7ccd1c1
update lock
dmoka Nov 6, 2024
0d6c6cb
fix compilation error
dmoka Nov 6, 2024
abd9ed2
raise swapped event in stableswap
dmoka Nov 6, 2024
e262a04
add HubAmountUpdated event deposits as these are extra info relating …
dmoka Nov 6, 2024
14b24ff
add comments explaining the why
dmoka Nov 6, 2024
25808e9
pass event id id to HubAmountUpdated event
dmoka Nov 6, 2024
f5a986e
formatting
dmoka Nov 6, 2024
8169f00
add todos
dmoka Nov 6, 2024
d318637
add unified event stack handling for DCA
dmoka Nov 7, 2024
b0f62f3
Merge remote-tracking branch 'origin/master' into unified_trade_events
dmoka Nov 7, 2024
0723194
formatting
dmoka Nov 7, 2024
2b81233
update lock
dmoka Nov 7, 2024
1045fc8
add stableswap swapped events
dmoka Nov 11, 2024
fb03a18
formatting
dmoka Nov 11, 2024
9e33836
remove unused run to block method
dmoka Nov 11, 2024
8dc92ed
simplify amm support dependencies
dmoka Nov 11, 2024
f17bcdd
add missing comments
dmoka Nov 11, 2024
e620966
add comments explaining thre why
dmoka Nov 11, 2024
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"]
56 changes: 49 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,7 @@ 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 `pallet_trade_event::Swapped` when successful.
#[pallet::call_index(4)]
#[pallet::weight(<T as Config>::WeightInfo::sell())]
pub fn sell(
Expand All @@ -729,7 +732,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 +757,7 @@ 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 `pallet_trade_event::Swapped` when successful.
Roznovjak marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::buy())]
pub fn buy(
Expand All @@ -759,7 +769,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 +940,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 +1110,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 +1127,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(),
jak-pan marked this conversation as resolved.
Show resolved Hide resolved
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 +1263,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 +1273,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,
dmoka marked this conversation as resolved.
Show resolved Hide resolved
);

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