Skip to content

Commit

Permalink
Merge pull request #150 from galacticcouncil/feat/create_pool_event
Browse files Browse the repository at this point in the history
feat: update xyk events
  • Loading branch information
Roznovjak authored Sep 2, 2021
2 parents 7a038d8 + f1ab709 commit 55f8e7d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 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.

11 changes: 9 additions & 2 deletions pallets/exchange/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,21 @@ fn initialize_pool(asset_a: u32, asset_b: u32, user: u64, amount: u128, price: P
price.checked_mul_int(amount).unwrap()
};

expect_event(xyk::Event::PoolCreated(user, asset_a, asset_b, shares));

let pair_account = XYKPallet::get_pair_id(AssetPair {
asset_in: asset_a,
asset_out: asset_b,
});
let share_token = XYKPallet::share_token(pair_account);

expect_event(xyk::Event::PoolCreated(
user,
asset_a,
asset_b,
shares,
share_token,
pair_account,
));

let amount_b = price.saturating_mul_int(amount);

// Check users state
Expand Down
2 changes: 1 addition & 1 deletion pallets/xyk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-xyk'
version = '1.3.0'
version = '1.4.0'
description = 'XYK automated market maker'
authors = ['GalacticCouncil']
edition = '2018'
Expand Down
12 changes: 6 additions & 6 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ pub mod pallet {
/// Liquidity was removed from the pool. [who, asset a, asset b, shares]
LiquidityRemoved(T::AccountId, AssetId, AssetId, Balance),

/// Pool was created. [who, asset a, asset b, initial shares amount]
PoolCreated(T::AccountId, AssetId, AssetId, Balance),
/// Pool was created. [who, asset a, asset b, initial shares amount, share token, pool account id]
PoolCreated(T::AccountId, AssetId, AssetId, Balance, AssetId, T::AccountId),

/// Pool was destroyed. [who, asset a, asset b]
PoolDestroyed(T::AccountId, AssetId, AssetId),
/// Pool was destroyed. [who, asset a, asset b, share token, pool account id]
PoolDestroyed(T::AccountId, AssetId, AssetId, AssetId, T::AccountId),

/// Asset sale executed. [who, asset in, asset out, amount, sale price, fee asset, fee amount]
SellExecuted(T::AccountId, AssetId, AssetId, Balance, Balance, AssetId, Balance),
Expand Down Expand Up @@ -274,7 +274,7 @@ pub mod pallet {

<TotalLiquidity<T>>::insert(&pair_account, shares_added);

Self::deposit_event(Event::PoolCreated(who, asset_a, asset_b, shares_added));
Self::deposit_event(Event::PoolCreated(who, asset_a, asset_b, shares_added, share_token, pair_account));

Ok(().into())
}
Expand Down Expand Up @@ -453,7 +453,7 @@ pub mod pallet {
<ShareToken<T>>::remove(&pair_account);
<PoolAssets<T>>::remove(&pair_account);

Self::deposit_event(Event::PoolDestroyed(who, asset_a, asset_b));
Self::deposit_event(Event::PoolDestroyed(who, asset_a, asset_b, share_token, pair_account));
}

Ok(().into())
Expand Down
59 changes: 39 additions & 20 deletions pallets/xyk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn create_pool_should_work() {
assert_eq!(Currency::free_balance(share_token, &ALICE), 100000000000000);
assert_eq!(XYK::total_liquidity(&pair_account), 100000000000000);

expect_events(vec![Event::PoolCreated(ALICE, asset_a, asset_b, 100000000000000).into()]);
expect_events(vec![Event::PoolCreated(ALICE, asset_a, asset_b, 100000000000000, share_token, pair_account).into()]);
});
}

Expand All @@ -92,7 +92,14 @@ fn create_same_pool_should_not_work() {
XYK::create_pool(Origin::signed(user), asset_b, asset_a, 1000, Price::from(2)),
Error::<Test>::TokenPoolAlreadyExists
);
expect_events(vec![Event::PoolCreated(ALICE, asset_b, asset_a, 2000).into()]);

let pair_account = XYK::get_pair_id(AssetPair {
asset_in: asset_a,
asset_out: asset_b,
});
let share_token = XYK::share_token(pair_account);

expect_events(vec![Event::PoolCreated(ALICE, asset_b, asset_a, 2000, share_token, pair_account).into()]);
});
}

Expand Down Expand Up @@ -182,7 +189,7 @@ fn add_liquidity_should_work() {
assert_eq!(XYK::total_liquidity(&pair_account), 1004000000000);

expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 1000000000000).into(),
Event::PoolCreated(ALICE, asset_a, asset_b, 1000000000000, share_token, pair_account).into(),
Event::LiquidityAdded(ALICE, asset_a, asset_b, 400000, 4000000000).into(),
]);
});
Expand Down Expand Up @@ -238,7 +245,7 @@ fn add_liquidity_as_another_user_should_work() {
assert_eq!(XYK::total_liquidity(&pair_account), 1014000000000);

expect_events(vec![
Event::PoolCreated(ALICE, asset_b, asset_a, 1000000000000).into(),
Event::PoolCreated(ALICE, asset_b, asset_a, 1000000000000, share_token, pair_account).into(),
Event::LiquidityAdded(ALICE, asset_b, asset_a, 400000, 4000000000).into(),
orml_tokens::Event::Endowed(0, 2, 10000000000).into(),
Event::LiquidityAdded(BOB, asset_b, asset_a, 1000000, 10000000000).into(),
Expand Down Expand Up @@ -281,7 +288,7 @@ fn remove_liquidity_should_work() {
assert_eq!(XYK::total_liquidity(&pair_account), 99645000);

expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000).into(),
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000, share_token, pair_account).into(),
Event::LiquidityRemoved(ALICE, asset_a, asset_b, 355_000).into(),
]);
});
Expand Down Expand Up @@ -317,7 +324,7 @@ fn remove_liquidity_without_shares_should_not_work() {
);

expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000).into(),
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000, share_token, pair_account).into(),
orml_tokens::Event::Endowed(share_token, BOB, shares).into(),
orml_tokens::Event::Transfer(share_token, ALICE, BOB, shares).into(),
]);
Expand Down Expand Up @@ -377,8 +384,15 @@ fn remove_liquidity_from_reduced_pool_should_not_work() {
XYK::remove_liquidity(Origin::signed(user), asset_a, asset_b, 200_000_000),
Error::<Test>::InsufficientAssetBalance
);

let pair_account = XYK::get_pair_id(AssetPair {
asset_in: asset_a,
asset_out: asset_b,
});
let share_token = XYK::share_token(pair_account);

expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000).into(),
Event::PoolCreated(ALICE, asset_a, asset_b, 100000000, share_token, pair_account).into(),
orml_tokens::Event::Transfer(asset_a, pair_account, BOB, 90_000_000).into(),
orml_tokens::Event::Transfer(asset_a, BOB, pair_account, 90_000_000).into(),
orml_tokens::Event::Transfer(asset_b, pair_account, BOB, 90_000_000).into(),
Expand Down Expand Up @@ -530,7 +544,7 @@ fn sell_test() {
assert_eq!(Currency::free_balance(asset_b, &pair_account), 598636516408213);

expect_events(vec![
Event::PoolCreated(ALICE, asset_a, asset_b, 600000000000000).into(),
Event::PoolCreated(ALICE, asset_a, asset_b, 600000000000000, share_token, pair_account).into(),
Event::SellExecuted(ALICE, asset_a, asset_b, 456444678, 1363483591787, asset_b, 2732432047).into(),
]);
});
Expand Down Expand Up @@ -697,7 +711,7 @@ fn work_flow_happy_path_should_work() {
assert_eq!(XYK::total_liquidity(&pair_account), 649_999_962_000);

expect_events(vec![
Event::PoolCreated(user_1, asset_a, asset_b, 350_000_000_000).into(),
Event::PoolCreated(user_1, asset_a, asset_b, 350_000_000_000, share_token, pair_account).into(),
orml_tokens::Event::Endowed(0, 2, 300000000000).into(),
Event::LiquidityAdded(user_2, asset_a, asset_b, 300_000_000_000, 12_000_000_000_000).into(),
Event::SellExecuted(
Expand Down Expand Up @@ -790,7 +804,7 @@ fn sell_with_correct_fees_should_work() {
assert_eq!(Currency::free_balance(asset_a, &user_1), 999999989900000);
assert_eq!(Currency::free_balance(asset_b, &user_1), 999998019762377);
expect_events(vec![
Event::PoolCreated(user_1, asset_a, asset_b, 2000000000).into(),
Event::PoolCreated(user_1, asset_a, asset_b, 2000000000, share_token, pair_account).into(),
Event::SellExecuted(user_1, asset_a, asset_b, 100_000, 19_762_377, asset_b, 39_603).into(),
]);
});
Expand Down Expand Up @@ -860,14 +874,17 @@ fn discount_sell_fees_should_work() {
let name: Vec<u8> = vec![208, 7, 0, 0, 72, 68, 84, 184, 11, 0, 0];
let bounded_name: BoundedVec<u8, <Test as pallet_asset_registry::Config>::StringLimit> = name.try_into().unwrap();

let share_token = XYK::share_token(pair_account);
let share_token_native = XYK::share_token(native_pair_account);

expect_events(vec![
Event::PoolCreated(user_1, asset_a, HDX, 10_000).into(),
Event::PoolCreated(user_1, asset_a, HDX, 10_000, share_token_native, native_pair_account).into(),
pallet_asset_registry::Event::Registered(1, bounded_name, AssetType::PoolShare(asset_a, asset_b)).into(),
frame_system::Event::NewAccount(pair_account).into(),
orml_tokens::Event::Endowed(asset_a, pair_account, 30000).into(),
orml_tokens::Event::Endowed(asset_b, pair_account, 60000).into(),
orml_tokens::Event::Endowed(1, 1, 60000).into(),
Event::PoolCreated(user_1, asset_a, asset_b, 60_000).into(),
Event::PoolCreated(user_1, asset_a, asset_b, 60_000, share_token, pair_account).into(),
Event::SellExecuted(user_1, asset_a, asset_b, 10_000, 14_990, asset_b, 10).into(),
]);
});
Expand Down Expand Up @@ -1029,7 +1046,7 @@ fn single_buy_should_work() {
assert_eq!(Currency::free_balance(asset_b, &pair_account), 960_639_995_191);

expect_events(vec![
Event::PoolCreated(user_1, asset_a, asset_b, 640_000_000_000).into(),
Event::PoolCreated(user_1, asset_a, asset_b, 640_000_000_000, share_token, pair_account).into(),
Event::BuyExecuted(
user_1,
asset_a,
Expand Down Expand Up @@ -1077,6 +1094,7 @@ fn single_buy_with_discount_should_work() {
asset_out: asset_b,
});
let share_token = XYK::share_token(pair_account);
let share_token_native = XYK::share_token(native_pair_account);

assert_eq!(Currency::free_balance(asset_a, &user_1), 999_949_800_000_000);
assert_eq!(Currency::free_balance(asset_b, &user_1), 999_360_000_000_000);
Expand Down Expand Up @@ -1110,13 +1128,13 @@ fn single_buy_with_discount_should_work() {
let bounded_name: BoundedVec<u8, <Test as pallet_asset_registry::Config>::StringLimit> = name.try_into().unwrap();

expect_events(vec![
Event::PoolCreated(user_1, asset_a, asset_b, 640_000_000_000).into(),
Event::PoolCreated(user_1, asset_a, asset_b, 640_000_000_000, share_token, pair_account).into(),
pallet_asset_registry::Event::Registered(1, bounded_name, AssetType::PoolShare(asset_a, HDX)).into(),
frame_system::Event::NewAccount(native_pair_account).into(),
orml_tokens::Event::Endowed(asset_a, 1003000, 50000000000).into(),
orml_tokens::Event::Endowed(1000, 1003000, 100000000000).into(),
orml_tokens::Event::Endowed(1, 1, 100000000000).into(),
Event::PoolCreated(user_1, asset_a, HDX, 100_000_000_000).into(),
Event::PoolCreated(user_1, asset_a, HDX, 100_000_000_000, share_token_native, native_pair_account).into(),
Event::BuyExecuted(
user_1,
asset_a,
Expand Down Expand Up @@ -1254,7 +1272,7 @@ fn create_pool_small_fixed_point_amount_should_work() {
assert_eq!(Currency::free_balance(share_token, &ALICE), 100000000000000);
assert_eq!(XYK::total_liquidity(&pair_account), 100000000000000);

expect_events(vec![Event::PoolCreated(ALICE, asset_a, asset_b, 100000000000000).into()]);
expect_events(vec![Event::PoolCreated(ALICE, asset_a, asset_b, 100000000000000, share_token, pair_account).into()]);
});
}

Expand Down Expand Up @@ -1284,7 +1302,7 @@ fn create_pool_fixed_point_amount_should_work() {
assert_eq!(Currency::free_balance(share_token, &ALICE), 100000000000);
assert_eq!(XYK::total_liquidity(&pair_account), 100000000000);

expect_events(vec![Event::PoolCreated(ALICE, asset_a, asset_b, 100000000000).into()]);
expect_events(vec![Event::PoolCreated(ALICE, asset_a, asset_b, 100000000000, share_token, pair_account).into()]);
});
}

Expand All @@ -1309,6 +1327,7 @@ fn destroy_pool_on_remove_liquidity_and_recreate_should_work() {
};

let pair_account = XYK::get_pair_id(asset_pair);
let share_token = XYK::share_token(pair_account);

assert_eq!(XYK::exists(asset_pair), true);

Expand All @@ -1334,15 +1353,15 @@ fn destroy_pool_on_remove_liquidity_and_recreate_should_work() {
));

expect_events(vec![
Event::PoolCreated(user, asset_a, asset_b, 100_000_000).into(),
Event::PoolCreated(user, asset_a, asset_b, 100_000_000, share_token, pair_account).into(),
frame_system::Event::KilledAccount(pair_account).into(),
Event::LiquidityRemoved(user, asset_a, asset_b, 100_000_000).into(),
Event::PoolDestroyed(user, asset_a, asset_b).into(),
Event::PoolDestroyed(user, asset_a, asset_b, share_token, pair_account).into(),
frame_system::Event::NewAccount(pair_account).into(),
orml_tokens::Event::Endowed(asset_a, pair_account, 100000000).into(),
orml_tokens::Event::Endowed(asset_b, pair_account, 1000000000000).into(),
orml_tokens::Event::Endowed(0, 1, 100000000).into(),
Event::PoolCreated(user, asset_a, asset_b, 100_000_000).into(),
Event::PoolCreated(user, asset_a, asset_b, 100_000_000, share_token, pair_account).into(),
]);
});
}
Expand Down

0 comments on commit 55f8e7d

Please sign in to comment.