Skip to content

Commit

Permalink
Merge pull request #76 from mangata-finance/feature/xyk-test-events
Browse files Browse the repository at this point in the history
Feature/xyk test events
  • Loading branch information
gleb-urvanov committed Jun 24, 2021
2 parents 60a243f + 9bfb5d6 commit 92de842
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pallets/xyk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ impl<T: Trait> XykFunctionsTrait<T::AccountId> for Module<T> {
second_asset_id,
second_asset_amount,
liquidity_asset_id,
second_asset_amount,
liquidity_assets_minted,
));

Ok(())
Expand Down Expand Up @@ -1555,7 +1555,7 @@ impl<T: Trait> XykFunctionsTrait<T::AccountId> for Module<T> {
second_asset_id,
second_asset_amount,
liquidity_asset_id,
second_asset_amount,
liquidity_asset_amount,
));

Ok(())
Expand Down
19 changes: 15 additions & 4 deletions pallets/xyk/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ use sp_runtime::{
Perbill,
};

use frame_support::{impl_outer_origin, parameter_types, weights::Weight};
use frame_support::{impl_outer_origin, impl_outer_event, parameter_types, weights::Weight};
use frame_system as system;
use mangata_primitives::{Amount, Balance, TokenId};
use orml_tokens::{MultiTokenCurrency, MultiTokenCurrencyAdapter, MultiTokenCurrencyExtended};
use pallet_assets_info as assets_info;

pub const NATIVE_CURRENCY_ID: u32 = 0;
mod xyk {
pub use crate::Event;
}

impl_outer_origin! {
pub enum Origin for Test {}
}

impl_outer_event! {
pub enum TestEvent for Test {
frame_system<T>,
xyk<T>,
orml_tokens<T>,
}
}
// For testing the pallet, we construct most of a mock runtime. This means
// first constructing a configuration type (`Test`) which `impl`s each of the
// configuration traits of pallets we want to use.
Expand All @@ -43,7 +53,7 @@ impl system::Trait for Test {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = ();
type Event = TestEvent;
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
Expand All @@ -61,7 +71,7 @@ impl system::Trait for Test {
}

impl orml_tokens::Trait for Test {
type Event = ();
type Event = TestEvent;
type Balance = Balance;
type Amount = Amount;
type CurrencyId = TokenId;
Expand Down Expand Up @@ -96,12 +106,13 @@ parameter_types! {
}

impl Trait for Test {
type Event = ();
type Event = TestEvent;
type Currency = MultiTokenCurrencyAdapter<Test>;
type NativeCurrencyId = NativeCurrencyId;
}

pub type XykStorage = Module<Test>;
pub type System = system::Module<Test>;

impl<T: Trait> Module<T> {
pub fn balance(id: TokenId, who: T::AccountId) -> Balance {
Expand Down
24 changes: 23 additions & 1 deletion pallets/xyk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![allow(non_snake_case)]

use super::*;
use crate::mock::Test;
use crate::mock::*;
use frame_support::assert_err;

Expand Down Expand Up @@ -60,6 +59,7 @@ use frame_support::assert_err;

fn initialize() {
// creating asset with assetId 0 and minting to accountId 2
System::set_block_number(1);
let acc_id: u64 = 2;
let amount: u128 = 1000000000000000000000;
XykStorage::create_new_token(&acc_id, amount);
Expand All @@ -73,6 +73,10 @@ fn initialize() {
60000000000000000000,
)
.unwrap();

let pool_created_event = TestEvent::xyk(Event::<Test>::PoolCreated(acc_id, 0, 40000000000000000000, 1, 60000000000000000000));

assert!(System::events().iter().any(|record| record.event == pool_created_event));
}

fn initialize_buy_and_burn() {
Expand Down Expand Up @@ -438,6 +442,7 @@ fn create_pool_W() {
XykStorage::balance(1, XykStorage::account_id()),
60000000000000000000
); // amount of asset 1 in vault acc after creating pool

});
}

Expand Down Expand Up @@ -534,6 +539,7 @@ fn create_pool_N_zero_second_amount() {
#[test]
fn sell_W() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
initialize();
XykStorage::sell_asset(Origin::signed(2), 0, 1, 20000000000000000000, 0).unwrap(); // selling 20000000000000000000 assetId 0 of pool 0 1

Expand All @@ -551,6 +557,10 @@ fn sell_W() {
XykStorage::balance(1, XykStorage::account_id()),
40040040040040040041
); // amount of asset 1 in vault acc after creating pool

let assets_swapped_event = TestEvent::xyk(Event::<Test>::AssetsSwapped(2, 0, 20000000000000000000, 1, 19959959959959959959));

assert!(System::events().iter().any(|record| record.event == assets_swapped_event));
});
}

Expand Down Expand Up @@ -627,6 +637,7 @@ fn sell_N_zero_amount() {
#[test]
fn buy_W() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
initialize();
// buying 30000000000000000000 assetId 1 of pool 0 1
XykStorage::buy_asset(
Expand All @@ -651,6 +662,10 @@ fn buy_W() {
XykStorage::balance(1, XykStorage::account_id()),
30000000000000000000
); // amount of asset 1 in vault acc after creating pool

let assets_swapped_event = TestEvent::xyk(Event::<Test>::AssetsSwapped(2, 0, 40120361083249749248, 1, 30000000000000000000));

assert!(System::events().iter().any(|record| record.event == assets_swapped_event));
});
}

Expand Down Expand Up @@ -787,6 +802,9 @@ fn mint_W() {
XykStorage::balance(1, XykStorage::account_id()),
90000000000000000001
); // amount of asset 1 in vault acc after creating pool
let liquidity_minted_event = TestEvent::xyk(Event::<Test>::LiquidityMinted(2, 0, 20000000000000000000, 1, 30000000000000000001, 2, 50000000000000000000 ));

assert!(System::events().iter().any(|record| record.event == liquidity_minted_event));
});
}

Expand Down Expand Up @@ -908,6 +926,10 @@ fn burn_W() {
XykStorage::balance(1, XykStorage::account_id()),
30000000000000000000
); // amount of asset 1 in vault acc after creating pool

let liquidity_burned = TestEvent::xyk(Event::<Test>::LiquidityBurned(2, 0, 20000000000000000000, 1, 30000000000000000000, 2, 50000000000000000000));

assert!(System::events().iter().any(|record| record.event == liquidity_burned));
});
}

Expand Down

0 comments on commit 92de842

Please sign in to comment.