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

Open
wants to merge 141 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
141 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
d232449
add fees to swappedevents from stabeswap share calculation
dmoka Nov 14, 2024
9895a68
formatting
dmoka Nov 14, 2024
efe4ceb
add missing fee info to swapped event when removing exact amount of l…
dmoka Nov 14, 2024
ed3852a
add hooks for utility batch calls so can have proper Swapped events
dmoka Nov 14, 2024
66fbe9c
revert changes in math as we can solve it simpler way without changin…
dmoka Nov 14, 2024
d4227a3
cleaning up
dmoka Nov 14, 2024
a122029
make clippy happy
dmoka Nov 14, 2024
458ae3a
formatting
dmoka Nov 14, 2024
a2f3ad3
rebenchmark utility pallet
dmoka Nov 14, 2024
04c1942
bump versions
dmoka Nov 15, 2024
2d829b6
add liquidty add and moreve to stableswap relevant swapped events
dmoka Dec 3, 2024
30cdafd
split up swapped in omnipool sell
dmoka Dec 6, 2024
8d98ceb
split up swapped in omnipool buy
dmoka Dec 6, 2024
5291f1c
cleaning up
dmoka Dec 6, 2024
64e0b9b
fix some tests
dmoka Dec 6, 2024
b31de5d
formatting
dmoka Dec 9, 2024
5202a70
fix not compiling tests
dmoka Dec 9, 2024
0ee69af
add missing assertion for swapped when ref code is used
dmoka Dec 9, 2024
2b14d21
make setting global
dmoka Dec 9, 2024
24e5423
clear execution stack on init
dmoka Dec 9, 2024
39a2d9a
remove hanging todo comment
dmoka Dec 9, 2024
a3991c3
Merge remote-tracking branch 'origin/master' into unified_trade_events
dmoka Dec 9, 2024
4a64061
add todo for stuff we discussed in a call about refactoring event dep…
dmoka Dec 10, 2024
e8d14dd
replace amm related types to amm pallet as it belongs there
dmoka Dec 12, 2024
64a1962
add test for nested batch
dmoka Dec 18, 2024
f1b11b5
use amm support pallet directlyn instead of via traits
dmoka Dec 18, 2024
5edd26e
remove unused trait
dmoka Dec 18, 2024
fd38cf0
remove unnecessary types
dmoka Dec 18, 2024
a8c33d0
use alias for executionIdStack
dmoka Dec 18, 2024
66a0a25
fix unit tests as we removed many traits from amm support pallet
dmoka Dec 18, 2024
1a00e1c
cleaning up
dmoka Dec 18, 2024
83104b3
use overflowing to prevent future issues
dmoka Dec 18, 2024
b887f62
cleaning up
dmoka Dec 18, 2024
0233d43
cleaning up
dmoka Dec 18, 2024
feb24dc
remove unnecessary generic types
dmoka Dec 18, 2024
6ea27c7
added asset type and using it in swapped event
dmoka Dec 19, 2024
c7fbb35
remove unnecessary getter
dmoka Dec 19, 2024
27f6b3f
remove unnecessary incremental id getter
dmoka Dec 19, 2024
34a0c73
add xcm echange event stack population
dmoka Dec 19, 2024
2ff4aee
add better doc
dmoka Dec 19, 2024
c7be65b
replace incremental id type from primites as primites tpyes only used…
dmoka Dec 19, 2024
c36165d
remove xcm exchange from filler type
dmoka Dec 20, 2024
b5d94bc
remove generic type
dmoka Dec 20, 2024
58c2c80
add schedule id to dca execution type so indexer can relate DCA used …
dmoka Dec 20, 2024
ce767c2
formatting
dmoka Dec 20, 2024
96f2c5c
add todo
dmoka Dec 20, 2024
8e2ba94
add custom send xcm
dmoka Dec 23, 2024
e5bd863
add unified even support for xcm with xcm hash
dmoka Dec 26, 2024
1acc07f
populate unified event stack with xcm and topic id
dmoka Dec 27, 2024
f19c091
make unified events storage operations more bullet proof as we dont w…
dmoka Dec 27, 2024
f1a8e40
merge master to unified_trade_events
dmoka Dec 27, 2024
4f56dd8
formatting
dmoka Dec 27, 2024
d59b9d5
bump versions
dmoka Dec 27, 2024
d71deca
bump versions
dmoka Dec 27, 2024
239b22f
make clippy happy
dmoka Dec 27, 2024
53e9b58
fix test
dmoka Dec 27, 2024
2ca6939
formatting
dmoka Dec 27, 2024
92151cb
renaming to express intent better
dmoka Dec 30, 2024
8ff8309
increase stack size to 16 to have more room for events
dmoka Dec 30, 2024
708a5ea
removed ICE as not used yet
dmoka Dec 30, 2024
fd60605
rename to support pallet
dmoka Dec 30, 2024
e43cba9
fix failing test
dmoka Dec 31, 2024
7f3b92d
fix failing test
dmoka Dec 31, 2024
ff7b18b
WIP - separate normal, staking and referral fee
dmoka Dec 31, 2024
20c3363
remove unnecessary test
dmoka Jan 2, 2025
9d72e42
fix otc filler/swapper of swapped event
dmoka Jan 2, 2025
5fb58c2
formatting
dmoka Jan 2, 2025
da1b526
merge master to unified_trade_events
dmoka Jan 2, 2025
9b93702
bump versions
dmoka Jan 2, 2025
c902d29
use patch4 release
dmoka Jan 2, 2025
868c360
fix xcm test by not asserting the specific topic id as it is dynamic
dmoka Jan 2, 2025
54128eb
renaming
dmoka Jan 2, 2025
c6834fa
refactoring
dmoka Jan 2, 2025
925cd27
formatting
dmoka Jan 2, 2025
4bd0aff
some formatting
dmoka Jan 3, 2025
8c8a9f5
remove done todo
dmoka Jan 3, 2025
68632a2
adjust doc comments
dmoka Jan 3, 2025
1e3e6ad
remove done todo
dmoka Jan 3, 2025
094c186
make clippy happy
dmoka Jan 3, 2025
2c7936d
formatting
dmoka Jan 3, 2025
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
621 changes: 327 additions & 294 deletions Cargo.lock

Large diffs are not rendered by default.

972 changes: 487 additions & 485 deletions Cargo.toml

Large diffs are not rendered by default.

4 changes: 3 additions & 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.25.1"
version = "1.26.0"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down Expand Up @@ -41,6 +41,7 @@ pallet-staking = { workspace = true }
pallet-lbp = { workspace = true }
pallet-xyk = { workspace = true }
pallet-evm-accounts = { workspace = true }
pallet-amm-support = { workspace = true }

pallet-treasury = { workspace = true }
pallet-democracy = { workspace = true }
Expand Down Expand Up @@ -217,6 +218,7 @@ std = [
"pallet-dynamic-evm-fee/std",
"precompile-utils/std",
"pallet-transaction-pause/std",
"pallet-amm-support/std",
]

# we don't include integration tests when benchmarking feature is enabled
Expand Down
194 changes: 192 additions & 2 deletions integration-tests/src/dca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod omnipool {
use super::*;
use frame_support::assert_ok;
use hydradx_runtime::{DCA, XYK};
use hydradx_traits::router::{PoolType, Trade};
use hydradx_traits::router::{ExecutionType, PoolType, Trade};
use hydradx_traits::AssetKind;
use sp_runtime::{FixedU128, TransactionOutcome};

Expand Down Expand Up @@ -201,6 +201,98 @@ mod omnipool {
});
}

#[test]
fn buy_schedule_execution_should_emit_swapped_events() {
TestNet::reset();
Hydra::execute_with(|| {
//Arrange
init_omnipool_with_oracle_for_block_10();

let dca_budget = 1000 * UNITS;
let amount_out = 100 * UNITS;
let schedule1 = schedule_fake_with_buy_order(PoolType::Omnipool, HDX, DAI, amount_out, dca_budget);
create_schedule(ALICE, schedule1);

//Act
run_to_block(11, 12);

//Assert
let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &get_last_swapped_events()[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactOut,
inputs: vec![(AssetType::Fungible(HDX), 140421094367051)],
outputs: vec![(AssetType::Fungible(LRNA), 70210545436437)],
fees: vec![Fee::new(LRNA, 35105272718, Omnipool::protocol_account()),],
dmoka marked this conversation as resolved.
Show resolved Hide resolved
operation_id: vec![
dmoka marked this conversation as resolved.
Show resolved Hide resolved
ExecutionType::DCA(0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
]
}),
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactOut,
inputs: vec![(AssetType::Fungible(LRNA), 70175440163719)],
outputs: vec![(AssetType::Fungible(DAI), amount_out)],
fees: vec![Fee::new(DAI, 250626566417, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
})
]
);

run_to_block(13, 17);

let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &get_last_swapped_events()[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactOut,
inputs: vec![(AssetType::Fungible(HDX), 140421107716515)],
outputs: vec![(AssetType::Fungible(LRNA), 70210548448729)],
fees: vec![Fee::new(LRNA, 35105274224, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(3),
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
}),
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactOut,
inputs: vec![(AssetType::Fungible(LRNA), 70175443174505)],
outputs: vec![(AssetType::Fungible(DAI), amount_out)],
fees: vec![Fee::new(DAI, 250626566417, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(3),
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
})
]
);
});
}

#[test]
fn buy_schedule_execution_should_work_without_route() {
TestNet::reset();
Expand Down Expand Up @@ -588,6 +680,105 @@ mod omnipool {
});
}

#[test]
fn sell_schedule_execution_should_emit_swapped_event() {
TestNet::reset();
Hydra::execute_with(|| {
//Arrange
init_omnipool_with_oracle_for_block_10();
let alice_init_hdx_balance = 5000 * UNITS;
assert_ok!(Balances::force_set_balance(
RuntimeOrigin::root(),
ALICE.into(),
alice_init_hdx_balance,
));

let dca_budget = 1100 * UNITS;
let amount_to_sell = 100 * UNITS;
let schedule1 =
schedule_fake_with_sell_order(ALICE, PoolType::Omnipool, dca_budget, HDX, DAI, amount_to_sell);
create_schedule(ALICE, schedule1);

//Act
run_to_block(11, 12);

//Assert
let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &get_last_swapped_events()[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactIn,
inputs: vec![(AssetType::Fungible(HDX), amount_to_sell)],
outputs: vec![(AssetType::Fungible(LRNA), 49999999159957)],
fees: vec![Fee::new(LRNA, 24999999579, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
}),
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactIn,
inputs: vec![(AssetType::Fungible(LRNA), 49974999160378)],
outputs: vec![(AssetType::Fungible(DAI), 71214372624126)],
fees: vec![Fee::new(DAI, 178482136903, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
})
]
);

run_to_block(13, 17);

let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &get_last_swapped_events()[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactIn,
inputs: vec![(AssetType::Fungible(HDX), amount_to_sell)],
outputs: vec![(AssetType::Fungible(LRNA), 49999997360044)],
fees: vec![Fee::new(LRNA, 24999998680, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(3),
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
}),
RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_amm_support::Filler::Omnipool,
operation: pallet_amm_support::TradeOperation::ExactIn,
inputs: vec![(AssetType::Fungible(LRNA), 49974997361364)],
outputs: vec![(AssetType::Fungible(DAI), 71214367826179)],
fees: vec![Fee::new(DAI, 178482124878, Omnipool::protocol_account()),],
operation_id: vec![
ExecutionType::DCA(3),
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
})
]
);
});
}

#[test]
fn sell_schedule_be_retried_when_route_is_invalid() {
TestNet::reset();
Expand Down Expand Up @@ -4032,7 +4223,6 @@ pub fn run_to_block(from: BlockNumber, to: BlockNumber) {
for b in from..=to {
do_trade_to_populate_oracle(DAI, HDX, UNITS);
set_relaychain_block_number(b);
do_trade_to_populate_oracle(DAI, HDX, UNITS);
}
}

Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ mod referrals;
mod router;
mod staking;
mod transact_call_filter;
mod utility;
pub mod utils;
mod vesting;
mod xyk;
Expand Down
22 changes: 20 additions & 2 deletions integration-tests/src/polkadot_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ use cumulus_primitives_core::ParaId;
use cumulus_test_relay_sproof_builder::RelayStateSproofBuilder;
pub use frame_system::RawOrigin;
use hex_literal::hex;
use hydradx_runtime::{evm::WETH_ASSET_LOCATION, Referrals, RuntimeOrigin};
pub use hydradx_traits::{evm::InspectEvmAccounts, registry::Mutate};
use hydradx_runtime::{evm::WETH_ASSET_LOCATION, Referrals, RuntimeEvent, RuntimeOrigin};
pub use hydradx_traits::{
evm::InspectEvmAccounts,
registry::Mutate,
router::{AssetType, ExecutionType, Fee, Filler, TradeOperation},
};
use pallet_referrals::{FeeDistribution, Level};
pub use polkadot_primitives::v7::{BlockNumber, MAX_CODE_SIZE, MAX_POV_SIZE};
use polkadot_runtime_parachains::configuration::HostConfiguration;
Expand Down Expand Up @@ -893,3 +897,17 @@ pub fn assert_xcm_message_processing_passed() {
hydradx_runtime::RuntimeEvent::MessageQueue(pallet_message_queue::Event::Processed { success: true, .. })
)));
}

pub fn get_last_swapped_events() -> Vec<RuntimeEvent> {
let last_events: Vec<RuntimeEvent> = last_hydra_events(1000);
let mut swapped_events = vec![];

for event in last_events {
let e = event.clone();
if let RuntimeEvent::AmmSupport(pallet_amm_support::Event::Swapped { .. }) = e {
swapped_events.push(e);
}
}

swapped_events
}
Loading
Loading