Skip to content

Commit

Permalink
Merge branch 'unified_trade_events' into remove-imbalance
Browse files Browse the repository at this point in the history
  • Loading branch information
enthusiastmartin committed Jan 7, 2025
2 parents 863299b + fd331c1 commit 79037de
Show file tree
Hide file tree
Showing 33 changed files with 956 additions and 794 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

40 changes: 20 additions & 20 deletions integration-tests/src/dca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ mod omnipool {

//Assert
let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &get_last_swapped_events()[swapped_events.len() - 2..];
let last_two_swapped_events = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -237,8 +237,8 @@ mod omnipool {
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
]
}),
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
},
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -251,18 +251,18 @@ mod omnipool {
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..];
let last_two_swapped_events = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -275,8 +275,8 @@ mod omnipool {
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
}),
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
},
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -289,7 +289,7 @@ mod omnipool {
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
})
}
]
);
});
Expand Down Expand Up @@ -707,11 +707,11 @@ mod omnipool {

//Assert
let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &get_last_swapped_events()[swapped_events.len() - 2..];
let last_two_swapped_events = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -724,8 +724,8 @@ mod omnipool {
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
}),
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
},
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -738,18 +738,18 @@ mod omnipool {
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..];
let last_two_swapped_events = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -762,8 +762,8 @@ mod omnipool {
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
}),
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
},
pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
Expand All @@ -776,7 +776,7 @@ mod omnipool {
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
})
}
]
);
});
Expand Down
94 changes: 30 additions & 64 deletions integration-tests/src/exchange_asset.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(test)]

use crate::assert_operation_stack;
use crate::polkadot_test_net::*;
use frame_support::dispatch::GetDispatchInfo;
use frame_support::storage::with_transaction;
Expand All @@ -26,6 +27,7 @@ use sp_runtime::DispatchResult;
use sp_runtime::{FixedU128, Permill, TransactionOutcome};
use sp_std::sync::Arc;
use xcm_emulator::TestExt;

pub const SELL: bool = true;
pub const BUY: bool = false;

Expand Down Expand Up @@ -125,45 +127,28 @@ fn hydra_should_swap_assets_when_receiving_from_acala_with_sell() {
let received = 39_101 * UNITS + BOB_INITIAL_NATIVE_BALANCE + 207_131_554_396;
assert_eq!(hydradx_runtime::Balances::free_balance(AccountId::from(BOB)), received);

let last_swapped_events = get_last_swapped_events();
let last_swapped_events: Vec<pallet_support::Event<hydradx_runtime::Runtime>> = get_last_swapped_events();
let last_two_swapped_events = &last_swapped_events[last_swapped_events.len() - 2..];
let topic_id = [
180, 44, 225, 230, 61, 43, 219, 219, 222, 9, 44, 219, 86, 193, 219, 212, 120, 169, 197, 209, 39, 180, 219,
225, 224, 182, 134, 141, 41, 162, 96, 54,
];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
swapper: TempAccountForXcmAssetExchange::get(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
operation: pallet_support::types::TradeOperation::ExactIn,
inputs: vec![pallet_support::types::Asset::new(ACA, 50000000000000),],
outputs: vec![pallet_support::types::Asset::new(LRNA::get(), 49180327868852)],
fees: vec![Fee::new(LRNA::get(), 24590163934, Omnipool::protocol_account()),],
operation_stack: vec![
ExecutionType::Xcm(topic_id, 0),
ExecutionType::XcmExchange(1),
ExecutionType::Router(2),
ExecutionType::Omnipool(3)
],
}),
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
swapper: TempAccountForXcmAssetExchange::get(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
operation: pallet_support::types::TradeOperation::ExactIn,
inputs: vec![pallet_support::types::Asset::new(LRNA::get(), 49155737704918),],
outputs: vec![pallet_support::types::Asset::new(HDX, 39101207131554396)],
fees: vec![Fee::new(HDX, 97998012861039, Omnipool::protocol_account()),],
operation_stack: vec![
ExecutionType::Xcm(topic_id, 0),
ExecutionType::XcmExchange(1),
ExecutionType::Router(2),
ExecutionType::Omnipool(3)
],
})

let event1 = &last_two_swapped_events[0];
assert_operation_stack!(
event1,
[
ExecutionType::Xcm(_, 0),
ExecutionType::XcmExchange(1),
ExecutionType::Router(2),
ExecutionType::Omnipool(3)
]
);

let event2 = &last_two_swapped_events[0];
assert_operation_stack!(
event2,
[
ExecutionType::Xcm(_, 0),
ExecutionType::XcmExchange(1),
ExecutionType::Router(2),
ExecutionType::Omnipool(3)
]
);

Expand All @@ -177,33 +162,14 @@ fn hydra_should_swap_assets_when_receiving_from_acala_with_sell() {
vec![],
));

let last_swapped_events = get_last_swapped_events();
let last_swapped_events: Vec<pallet_support::Event<hydradx_runtime::Runtime>> = get_last_swapped_events();
let last_two_swapped_events = &last_swapped_events[last_swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
operation: pallet_support::types::TradeOperation::ExactIn,
inputs: vec![pallet_support::types::Asset::new(HDX, 1 * UNITS),],
outputs: vec![pallet_support::types::Asset::new(LRNA::get(), 1308673515)],
fees: vec![Fee::new(LRNA::get(), 654336, Omnipool::protocol_account()),],
operation_stack: vec![ExecutionType::Router(4), ExecutionType::Omnipool(5)],
}),
RuntimeEvent::AmmSupport(pallet_support::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_support::types::Filler::Omnipool,
operation: pallet_support::types::TradeOperation::ExactIn,
inputs: vec![pallet_support::types::Asset::new(LRNA::get(), 1308019179),],
outputs: vec![pallet_support::types::Asset::new(ACA, 1348602600)],
fees: vec![Fee::new(ACA, 3379957, Omnipool::protocol_account()),],
operation_stack: vec![ExecutionType::Router(4), ExecutionType::Omnipool(5)],
})
]
);

let event1 = &last_two_swapped_events[0];
assert_operation_stack!(event1, [ExecutionType::Router(4), ExecutionType::Omnipool(5)]);

let event2 = &last_two_swapped_events[0];
assert_operation_stack!(event2, [ExecutionType::Router(4), ExecutionType::Omnipool(5)]);
});
}

Expand Down
34 changes: 25 additions & 9 deletions integration-tests/src/polkadot_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,16 +894,32 @@ pub fn assert_xcm_message_processing_passed() {
)));
}

pub fn get_last_swapped_events() -> Vec<RuntimeEvent> {
pub fn get_last_swapped_events() -> Vec<pallet_support::Event<hydradx_runtime::Runtime>> {
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_support::Event::Swapped { .. }) = e {
swapped_events.push(e);
}
}
last_events
.into_iter()
.filter_map(|event| {
if let RuntimeEvent::AmmSupport(inner_event @ pallet_support::Event::Swapped { .. }) = event {
Some(inner_event)
} else {
None
}
})
.collect()
}

swapped_events
#[macro_export]
macro_rules! assert_operation_stack {
($event:expr, [$($pattern:pat),*]) => {
if let pallet_support::Event::Swapped { operation_stack, .. } = $event {
assert!(matches!(&operation_stack[..],
[
$($pattern),*
]
));
} else {
panic!("Expected Swapped event");
}
}
}
Loading

0 comments on commit 79037de

Please sign in to comment.