Skip to content

Commit

Permalink
adds different asset decimals test, add bench test to CI run
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderian committed Nov 26, 2024
1 parent 3eeca72 commit 792d032
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/reusable-build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ jobs:
echo "RUSTC_WRAPPER=sccache" >> $GITHUB_ENV
echo "CARGO_INCREMENTAL=0" >> $GITHUB_ENV
- name: Run benchmarks tests
run: cargo test --release -j8 --features=runtime-benchmarks -p pallet-xyk -p pallet-issuance -p pallet-multipurpose-liquidity -p pallet-fee-lock
run: cargo test --release -j8 --features=runtime-benchmarks -p pallet-xyk -p pallet-issuance -p pallet-multipurpose-liquidity -p pallet-fee-lock -p pallet-market
- name: Run benchmarks tests
run: cargo test --release -j8 --features=runtime-benchmarks -p pallet-bootstrap
# NOTE: MGX-742
Expand Down
153 changes: 86 additions & 67 deletions rollup/runtime/integration-test/src/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::setup::*;
use pallet_market::{AtomicSwap, Event, PoolKind};
use sp_runtime::{traits::Zero, DispatchResult};

const ASSET_ID_1: u32 = 1;
const ASSET_ID_2: u32 = 2;
const ASSET_ID_3: u32 = 3;
const ASSET_ID_4_DISABLED: u32 = 4;
const ASSET_ID_5_UNREGISTERED: u32 = 5;
const POOL_ID_1: u32 = 6;
const POOL_ID_2: u32 = 7;
const POOL_ID_3: u32 = 8;
const ASSET_ID_1: u32 = NATIVE_ASSET_ID + 1;
const ASSET_ID_2: u32 = ASSET_ID_1 + 1;
const ASSET_ID_3: u32 = ASSET_ID_2 + 1;
const ASSET_ID_4_DISABLED: u32 = ASSET_ID_3 + 1;
const ASSET_ID_5: u32 = ASSET_ID_4_DISABLED + 1;
const ASSET_ID_6: u32 = ASSET_ID_5 + 1;
const POOL_ID_1: u32 = ASSET_ID_6 + 1;
const POOL_ID_2: u32 = POOL_ID_1 + 1;
const POOL_ID_3: u32 = POOL_ID_2 + 1;

fn test_env() -> TestExternalities {
ExtBuilder {
Expand All @@ -20,7 +21,8 @@ fn test_env() -> TestExternalities {
(AccountId::from(ALICE), ASSET_ID_2, 100 * UNIT),
(AccountId::from(ALICE), ASSET_ID_3, 100 * UNIT),
(AccountId::from(ALICE), ASSET_ID_4_DISABLED, 100 * UNIT),
(AccountId::from(ALICE), ASSET_ID_5_UNREGISTERED, 100 * UNIT),
(AccountId::from(ALICE), ASSET_ID_5, 100 * UNIT),
(AccountId::from(ALICE), ASSET_ID_6, 100 * UNIT),
],
assets: vec![
(
Expand Down Expand Up @@ -76,6 +78,26 @@ fn test_env() -> TestExternalities {
},
},
),
(
ASSET_ID_5,
AssetMetadataOf {
decimals: 10,
name: BoundedVec::truncate_from(b"Asset".to_vec()),
symbol: BoundedVec::truncate_from(b"Asset".to_vec()),
existential_deposit: Default::default(),
additional: Default::default(),
},
),
(
ASSET_ID_6,
AssetMetadataOf {
decimals: 12,
name: BoundedVec::truncate_from(b"Asset".to_vec()),
symbol: BoundedVec::truncate_from(b"Asset".to_vec()),
existential_deposit: Default::default(),
additional: Default::default(),
},
),
],
..ExtBuilder::default()
}
Expand Down Expand Up @@ -109,31 +131,31 @@ fn create_pool_works() {
assert_ok!(create_pool(PoolKind::Xyk, (NATIVE_ASSET_ID, ASSET_ID_1)));
System::assert_has_event(RuntimeEvent::Market(Event::PoolCreated {
creator: AccountId::from(ALICE),
pool_id: 6,
lp_token: 6,
pool_id: POOL_ID_1,
lp_token: POOL_ID_1,
assets: (0, 1),
}));
System::assert_has_event(RuntimeEvent::Market(Event::LiquidityMinted {
who: AccountId::from(ALICE),
pool_id: 6,
pool_id: POOL_ID_1,
amounts_provided: (10000000000000000000, 5000000000000000000),
lp_token: 6,
lp_token: POOL_ID_1,
lp_token_minted: 7500000000000000000,
total_supply: 7500000000000000000,
}));

assert_ok!(create_pool(PoolKind::StableSwap, (NATIVE_ASSET_ID, ASSET_ID_1)));
System::assert_has_event(RuntimeEvent::Market(Event::PoolCreated {
creator: AccountId::from(ALICE),
pool_id: 7,
lp_token: 7,
pool_id: POOL_ID_2,
lp_token: POOL_ID_2,
assets: (0, 1),
}));
System::assert_has_event(RuntimeEvent::Market(Event::LiquidityMinted {
who: AccountId::from(ALICE),
pool_id: 7,
pool_id: POOL_ID_2,
amounts_provided: (10000000000000000000, 5000000000000000000),
lp_token: 7,
lp_token: POOL_ID_2,
lp_token_minted: 14999063611862273044,
total_supply: 14999063611862273044,
}));
Expand All @@ -154,9 +176,9 @@ fn add_liquidity_works() {
assert_ok!(Market::mint_liquidity(origin(), POOL_ID_1, NATIVE_ASSET_ID, UNIT, 10 * UNIT));
System::assert_last_event(RuntimeEvent::Market(Event::LiquidityMinted {
who: AccountId::from(ALICE),
pool_id: 6,
pool_id: POOL_ID_1,
amounts_provided: (1000000000000000000, expected),
lp_token: 6,
lp_token: POOL_ID_1,
lp_token_minted: lp_expected,
total_supply: 8250000000000000000,
}));
Expand All @@ -169,9 +191,9 @@ fn add_liquidity_works() {
assert_ok!(Market::mint_liquidity(origin(), POOL_ID_2, NATIVE_ASSET_ID, UNIT, 10 * UNIT));
System::assert_last_event(RuntimeEvent::Market(Event::LiquidityMinted {
who: AccountId::from(ALICE),
pool_id: 7,
pool_id: POOL_ID_2,
amounts_provided: (1000000000000000000, expected),
lp_token: 7,
lp_token: POOL_ID_2,
lp_token_minted: lp_expected,
total_supply: 16998182477145509576,
}));
Expand All @@ -187,9 +209,9 @@ fn add_liquidity_fixed_works() {
assert_ok!(Market::mint_liquidity_fixed_amounts(origin(), POOL_ID_1, (UNIT, 0), 0));
System::assert_last_event(RuntimeEvent::Market(Event::LiquidityMinted {
who: AccountId::from(ALICE),
pool_id: 6,
pool_id: POOL_ID_1,
amounts_provided: (1000000000000000000, 0),
lp_token: 6,
lp_token: POOL_ID_1,
lp_token_minted: 365524961509654622,
total_supply: 7865524961509654622,
}));
Expand All @@ -198,9 +220,9 @@ fn add_liquidity_fixed_works() {
assert_ok!(Market::mint_liquidity_fixed_amounts(origin(), POOL_ID_2, (UNIT, 5 * UNIT), 0));
System::assert_last_event(RuntimeEvent::Market(Event::LiquidityMinted {
who: AccountId::from(ALICE),
pool_id: 7,
pool_id: POOL_ID_2,
amounts_provided: (1000000000000000000, 5000000000000000000),
lp_token: 7,
lp_token: POOL_ID_2,
lp_token_minted: expected,
total_supply: 20990943480975169792,
}));
Expand All @@ -216,7 +238,7 @@ fn remove_liquidity_works() {
assert_ok!(Market::burn_liquidity(origin(), POOL_ID_1, UNIT, 0, 0));
System::assert_last_event(RuntimeEvent::Market(Event::LiquidityBurned {
who: AccountId::from(ALICE),
pool_id: 6,
pool_id: POOL_ID_1,
amounts: (1333333333333333333, 666666666666666666),
burned_amount: 1000000000000000000,
total_supply: 6500000000000000000,
Expand All @@ -225,7 +247,7 @@ fn remove_liquidity_works() {
assert_ok!(Market::burn_liquidity(origin(), POOL_ID_2, UNIT, 0, 0));
System::assert_last_event(RuntimeEvent::Market(Event::LiquidityBurned {
who: AccountId::from(ALICE),
pool_id: 7,
pool_id: POOL_ID_2,
amounts: (666708286515387818, 333354143257693909),
burned_amount: 1000000000000000000,
total_supply: 13999063611862273044,
Expand Down Expand Up @@ -255,23 +277,23 @@ fn multiswap_should_work_xyk() {
who: AccountId::from(ALICE),
swaps: vec![
AtomicSwap {
pool_id: 6,
pool_id: POOL_ID_1,
kind: PoolKind::Xyk,
asset_in: 0,
asset_out: 1,
amount_in: 1000000000000000000,
amount_out: 453305446940074565,
},
AtomicSwap {
pool_id: 7,
pool_id: POOL_ID_2,
kind: PoolKind::Xyk,
asset_in: 1,
asset_out: 2,
amount_in: 453305446940074565,
amount_out: 216201629292906575,
},
AtomicSwap {
pool_id: 8,
pool_id: POOL_ID_3,
kind: PoolKind::Xyk,
asset_in: 2,
asset_out: 3,
Expand Down Expand Up @@ -317,23 +339,23 @@ fn multiswap_should_work_stable_swap_with_bnb() {
who: AccountId::from(ALICE),
swaps: vec![
AtomicSwap {
pool_id: 6,
pool_id: POOL_ID_1,
kind: PoolKind::StableSwap,
asset_in: 0,
asset_out: 1,
amount_in: 1000000000000000000,
amount_out: 995595345298031754,
},
AtomicSwap {
pool_id: 7,
pool_id: POOL_ID_2,
kind: PoolKind::StableSwap,
asset_in: 1,
asset_out: 2,
amount_in: 995595345298031754,
amount_out: 991212132384121611,
},
AtomicSwap {
pool_id: 8,
pool_id: POOL_ID_3,
kind: PoolKind::StableSwap,
asset_in: 2,
asset_out: 3,
Expand Down Expand Up @@ -367,23 +389,23 @@ fn multiswap_should_work_mixed() {
who: AccountId::from(ALICE),
swaps: vec![
AtomicSwap {
pool_id: 6,
pool_id: POOL_ID_1,
kind: PoolKind::Xyk,
asset_in: 0,
asset_out: 1,
amount_in: 1000000000000000000,
amount_out: 453305446940074565,
},
AtomicSwap {
pool_id: 7,
pool_id: POOL_ID_2,
kind: PoolKind::StableSwap,
asset_in: 1,
asset_out: 2,
amount_in: 453305446940074565,
amount_out: 451412806019623895,
},
AtomicSwap {
pool_id: 8,
pool_id: POOL_ID_3,
kind: PoolKind::Xyk,
asset_in: 2,
asset_out: 3,
Expand All @@ -396,19 +418,32 @@ fn multiswap_should_work_mixed() {
}

#[test]
fn multiswap_buy_should_work_mixed() {
fn test_diff_decimals_work() {
test_env().execute_with(|| {
assert_ok!(create_pool(PoolKind::Xyk, (NATIVE_ASSET_ID, ASSET_ID_1)));
assert_ok!(create_pool(PoolKind::StableSwap, (ASSET_ID_1, ASSET_ID_2)));
assert_ok!(create_pool(PoolKind::Xyk, (ASSET_ID_2, ASSET_ID_3)));
let unit10 = 10_000_000_000_u128;
let unit12 = 1_000_000_000_000_u128;
assert_ok!(Market::create_pool(
origin(),
PoolKind::StableSwap,
ASSET_ID_5,
100 * unit10,
ASSET_ID_6,
100 * unit12
));

let pool = Market::get_pools(Some(POOL_ID_1));
let price = Market::calculate_sell_price(POOL_ID_1, ASSET_ID_5, 1).unwrap();

println!("{:#?}", pool);
println!("{:#?}", price);

assert_ok!(Market::multiswap_asset_buy(
assert_ok!(Market::multiswap_asset(
origin(),
vec![POOL_ID_1, POOL_ID_2, POOL_ID_3],
ASSET_ID_3,
UNIT / 100,
NATIVE_ASSET_ID,
UNIT,
vec![POOL_ID_1],
ASSET_ID_5,
1,
ASSET_ID_6,
1,
));

println!("{:#?}", events());
Expand All @@ -417,28 +452,12 @@ fn multiswap_buy_should_work_mixed() {
who: AccountId::from(ALICE),
swaps: vec![
AtomicSwap {
pool_id: 6,
kind: PoolKind::Xyk,
asset_in: 0,
asset_out: 1,
amount_in: 40648650414565365,
amount_out: 20181563007698743,
},
AtomicSwap {
pool_id: 7,
pool_id: POOL_ID_1,
kind: PoolKind::StableSwap,
asset_in: 1,
asset_out: 2,
amount_in: 20181563007698743,
amount_out: 20100381304233342,
},
AtomicSwap {
pool_id: 8,
kind: PoolKind::Xyk,
asset_in: 2,
asset_out: 3,
amount_in: 20100381304233342,
amount_out: 10000000000000000,
asset_in: ASSET_ID_5,
asset_out: ASSET_ID_6,
amount_in: 1,
amount_out: 99,
},
],
}));
Expand Down

0 comments on commit 792d032

Please sign in to comment.