Skip to content

Commit

Permalink
Merge branch 'master' into xcm-topic-id
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jan 13, 2025
2 parents e4e5b92 + d24c275 commit 30ab2f4
Show file tree
Hide file tree
Showing 134 changed files with 9,012 additions and 3,117 deletions.
658 changes: 349 additions & 309 deletions Cargo.lock

Large diffs are not rendered by default.

898 changes: 450 additions & 448 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.26.0"
version = "1.28.0"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down Expand Up @@ -44,6 +44,7 @@ pallet-evm-accounts = { workspace = true }
pallet-xyk-liquidity-mining = { workspace = true }
pallet-transaction-pause = { workspace = true }
pallet-liquidation = { workspace = true }
pallet-broadcast = { workspace = true }

pallet-treasury = { workspace = true }
pallet-democracy = { workspace = true }
Expand Down Expand Up @@ -221,6 +222,7 @@ std = [
"precompile-utils/std",
"pallet-transaction-pause/std",
"pallet-liquidation/std",
"pallet-broadcast/std",
]

# we don't include integration tests when benchmarking feature is enabled
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/cross_chain_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn rococo_should_receive_asset_when_sent_from_hydra() {
Rococo::execute_with(|| {
assert_eq!(
hydradx_runtime::Balances::free_balance(AccountId::from(BOB)),
2_999_989_698_923 // 3 * HDX - fee
2_999_989_606_492 // 3 * HDX - fee
);
});
}
Expand Down
214 changes: 212 additions & 2 deletions integration-tests/src/dca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ use hydradx_traits::registry::{AssetKind, Create};
use hydradx_traits::router::AssetPair;
use hydradx_traits::router::PoolType;
use hydradx_traits::router::Trade;
use hydradx_traits::stableswap::AssetAmount;
use orml_traits::MultiCurrency;
use orml_traits::MultiReservableCurrency;
use pallet_broadcast::types::*;
use pallet_dca::types::{Order, Schedule};
use pallet_omnipool::types::Tradability;
use pallet_stableswap::types::AssetAmount;
use pallet_stableswap::MAX_ASSETS_IN_POOL;
use primitives::{AssetId, Balance};
use sp_runtime::traits::ConstU32;
Expand All @@ -39,6 +40,7 @@ mod omnipool {
use hydradx_runtime::{DCA, XYK};
use hydradx_traits::router::{PoolType, Trade};
use hydradx_traits::AssetKind;
use pallet_broadcast::types::Destination;
use sp_runtime::{FixedU128, TransactionOutcome};

#[test]
Expand Down Expand Up @@ -201,6 +203,107 @@ 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 schedule_id = 0;
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 = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
pallet_broadcast::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_broadcast::types::Filler::Omnipool,
operation: pallet_broadcast::types::TradeOperation::ExactOut,
inputs: vec![Asset::new(HDX, 140421094367051)],
outputs: vec![Asset::new(LRNA, 70210545436437)],
fees: vec![Fee::new(LRNA, 35105272718, Destination::Burned)],
operation_stack: vec![
ExecutionType::DCA(schedule_id, 0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
]
},
pallet_broadcast::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_broadcast::types::Filler::Omnipool,
operation: pallet_broadcast::types::TradeOperation::ExactOut,
inputs: vec![Asset::new(LRNA, 70175440163719)],
outputs: vec![Asset::new(DAI, amount_out)],
fees: vec![Fee::new(
DAI,
250626566417,
Destination::Account(Omnipool::protocol_account())
)],
operation_stack: vec![
ExecutionType::DCA(schedule_id, 0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
}
]
);

run_to_block(13, 17);

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

#[test]
fn buy_schedule_execution_should_work_without_route() {
TestNet::reset();
Expand Down Expand Up @@ -588,6 +691,114 @@ 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 schedule_id = 0;
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 = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
pallet_broadcast::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_broadcast::types::Filler::Omnipool,
operation: pallet_broadcast::types::TradeOperation::ExactIn,
inputs: vec![Asset::new(HDX, amount_to_sell)],
outputs: vec![Asset::new(LRNA, 49999999159957)],
fees: vec![Fee::new(LRNA, 24999999579, Destination::Burned)],
operation_stack: vec![
ExecutionType::DCA(schedule_id, 0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
},
pallet_broadcast::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_broadcast::types::Filler::Omnipool,
operation: pallet_broadcast::types::TradeOperation::ExactIn,
inputs: vec![Asset::new(LRNA, 49974999160378)],
outputs: vec![Asset::new(DAI, 71214372624126)],
fees: vec![Fee::new(
DAI,
178482136903,
Destination::Account(Omnipool::protocol_account())
)],
operation_stack: vec![
ExecutionType::DCA(schedule_id, 0),
ExecutionType::Router(1),
ExecutionType::Omnipool(2)
],
}
]
);

run_to_block(13, 17);

let swapped_events = get_last_swapped_events();
let last_two_swapped_events = &swapped_events[swapped_events.len() - 2..];
pretty_assertions::assert_eq!(
last_two_swapped_events,
vec![
pallet_broadcast::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_broadcast::types::Filler::Omnipool,
operation: pallet_broadcast::types::TradeOperation::ExactIn,
inputs: vec![Asset::new(HDX, amount_to_sell)],
outputs: vec![Asset::new(LRNA, 49999997360044)],
fees: vec![Fee::new(LRNA, 24999998680, Destination::Burned)],
operation_stack: vec![
ExecutionType::DCA(schedule_id, 3),
ExecutionType::Router(4),
ExecutionType::Omnipool(5)
],
},
pallet_broadcast::Event::Swapped {
swapper: ALICE.into(),
filler: Omnipool::protocol_account(),
filler_type: pallet_broadcast::types::Filler::Omnipool,
operation: pallet_broadcast::types::TradeOperation::ExactIn,
inputs: vec![Asset::new(LRNA, 49974997361364)],
outputs: vec![Asset::new(DAI, 71214367826179)],
fees: vec![Fee::new(
DAI,
178482124878,
Destination::Account(Omnipool::protocol_account())
)],
operation_stack: vec![
ExecutionType::DCA(schedule_id, 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 +4243,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
53 changes: 52 additions & 1 deletion integration-tests/src/exchange_asset.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#![cfg(test)]

use crate::assert_operation_stack;
use crate::polkadot_test_net::*;
use frame_support::dispatch::GetDispatchInfo;
use frame_support::storage::with_transaction;
use frame_support::traits::fungible::Balanced;
use frame_support::traits::tokens::Precision;
use frame_support::weights::Weight;
use frame_support::{assert_ok, pallet_prelude::*};
use hydradx_runtime::AssetRegistry;
use hydradx_runtime::Omnipool;
use hydradx_runtime::Router;
use hydradx_runtime::RuntimeEvent;
use hydradx_runtime::RuntimeOrigin;
use hydradx_runtime::TempAccountForXcmAssetExchange;
use hydradx_runtime::{AssetRegistry, LRNA};
use hydradx_traits::AssetKind;
use hydradx_traits::Create;
use orml_traits::currency::MultiCurrency;
Expand Down Expand Up @@ -120,6 +126,50 @@ fn hydra_should_swap_assets_when_receiving_from_acala_with_sell() {
// We receive about 39_101 HDX (HDX is super cheap in our test)
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: Vec<pallet_broadcast::Event<hydradx_runtime::Runtime>> = get_last_swapped_events();
let last_two_swapped_events = &last_swapped_events[last_swapped_events.len() - 2..];

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)
]
);

//We assert that another trade doesnt have the xcm exchange type on stack
assert_ok!(Router::sell(
RuntimeOrigin::signed(ALICE.into()),
HDX,
ACA,
1 * UNITS,
0,
vec![],
));

let last_swapped_events: Vec<pallet_broadcast::Event<hydradx_runtime::Runtime>> = get_last_swapped_events();
let last_two_swapped_events = &last_swapped_events[last_swapped_events.len() - 2..];

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 Expand Up @@ -1143,6 +1193,7 @@ fn half(asset: &Asset) -> Asset {
id: asset.clone().id,
}
}
use pallet_broadcast::types::{ExecutionType, Fee};
use rococo_runtime::xcm_config::BaseXcmWeight;
use xcm_builder::FixedWeightBounds;
use xcm_executor::traits::WeightBounds;
Expand Down
Loading

0 comments on commit 30ab2f4

Please sign in to comment.