From bdfec14214dcb8caa4bb3edf4712389fbf0fdd07 Mon Sep 17 00:00:00 2001 From: stanieltron Date: Tue, 22 Jun 2021 08:58:30 +0200 Subject: [PATCH 1/4] event test added --- pallets/assets-info/src/tests.rs | 1 + pallets/xyk/src/mock.rs | 19 +++++++++++++++---- pallets/xyk/src/tests.rs | 10 +++++++++- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pallets/assets-info/src/tests.rs b/pallets/assets-info/src/tests.rs index 72c45ca1c5831..770c339a46a15 100644 --- a/pallets/assets-info/src/tests.rs +++ b/pallets/assets-info/src/tests.rs @@ -2,6 +2,7 @@ use crate::{mock::*, AssetInfo, Error}; use frame_support::{assert_noop, assert_ok}; +use frame_system as system; #[test] fn set_info_and_retrieve_works_ok() { diff --git a/pallets/xyk/src/mock.rs b/pallets/xyk/src/mock.rs index 124e02194daa5..83ecd7311fa56 100644 --- a/pallets/xyk/src/mock.rs +++ b/pallets/xyk/src/mock.rs @@ -9,17 +9,27 @@ 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}; 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, + xyk, + orml_tokens, + } +} // 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. @@ -42,7 +52,7 @@ impl system::Trait for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = TestEvent; type BlockHashCount = BlockHashCount; type MaximumBlockWeight = MaximumBlockWeight; type DbWeight = (); @@ -60,7 +70,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; @@ -73,12 +83,13 @@ parameter_types! { } impl Trait for Test { - type Event = (); + type Event = TestEvent; type Currency = MultiTokenCurrencyAdapter; type NativeCurrencyId = NativeCurrencyId; } pub type XykStorage = Module; +pub type System = system::Module; impl Module { pub fn balance(id: TokenId, who: T::AccountId) -> Balance { diff --git a/pallets/xyk/src/tests.rs b/pallets/xyk/src/tests.rs index f6a8749a06f9e..ccdbaef023c22 100644 --- a/pallets/xyk/src/tests.rs +++ b/pallets/xyk/src/tests.rs @@ -2,7 +2,6 @@ #![allow(non_snake_case)] use super::*; -use crate::mock::Test; use crate::mock::*; use frame_support::assert_err; @@ -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); @@ -73,6 +73,13 @@ fn initialize() { 60000000000000000000, ) .unwrap(); + + let pool_created_event = + TestEvent::xyk(Event::PoolCreated(acc_id, 0, 40000000000000000000, 1, 60000000000000000000)); + + assert!(System::events() + .iter() + .any(|record| record.event == pool_created_event)); } fn initialize_buy_and_burn() { @@ -408,6 +415,7 @@ fn create_pool_W() { XykStorage::balance(1, XykStorage::account_id()), 60000000000000000000 ); // amount of asset 1 in vault acc after creating pool + }); } From 4111511927b34eb4ee29995f71d40aa1a735a7a3 Mon Sep 17 00:00:00 2001 From: Mateusz Nowakowski Date: Tue, 22 Jun 2021 09:15:29 +0200 Subject: [PATCH 2/4] compilation fix --- pallets/xyk/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/xyk/src/tests.rs b/pallets/xyk/src/tests.rs index ccdbaef023c22..eb9302c0d194d 100644 --- a/pallets/xyk/src/tests.rs +++ b/pallets/xyk/src/tests.rs @@ -75,7 +75,7 @@ fn initialize() { .unwrap(); let pool_created_event = - TestEvent::xyk(Event::PoolCreated(acc_id, 0, 40000000000000000000, 1, 60000000000000000000)); + TestEvent::xyk(Event::::PoolCreated(1, 0, 40000000000000000000, 1, 60000000000000000000)); assert!(System::events() .iter() From 88d6da557267c204d643d460820abe39c9383e06 Mon Sep 17 00:00:00 2001 From: stanieltron Date: Tue, 22 Jun 2021 09:46:13 +0200 Subject: [PATCH 3/4] added all events tests to working tests added all events tests to working tests modified invalid mint and burn event parameter in xyk lib --- pallets/xyk/src/lib.rs | 4 ++-- pallets/xyk/src/tests.rs | 24 +++++++++++++++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pallets/xyk/src/lib.rs b/pallets/xyk/src/lib.rs index 1079c8c3b6cdb..f679140fcdfef 100644 --- a/pallets/xyk/src/lib.rs +++ b/pallets/xyk/src/lib.rs @@ -1200,7 +1200,7 @@ impl XykFunctionsTrait for Module { second_asset_id, second_asset_amount, liquidity_asset_id, - second_asset_amount, + liquidity_assets_minted, )); Ok(()) @@ -1330,7 +1330,7 @@ impl XykFunctionsTrait for Module { second_asset_id, second_asset_amount, liquidity_asset_id, - second_asset_amount, + liquidity_asset_amount, )); Ok(()) diff --git a/pallets/xyk/src/tests.rs b/pallets/xyk/src/tests.rs index ccdbaef023c22..4ed87c44dd6a8 100644 --- a/pallets/xyk/src/tests.rs +++ b/pallets/xyk/src/tests.rs @@ -74,12 +74,9 @@ fn initialize() { ) .unwrap(); - let pool_created_event = - TestEvent::xyk(Event::PoolCreated(acc_id, 0, 40000000000000000000, 1, 60000000000000000000)); + let pool_created_event = TestEvent::xyk(Event::::PoolCreated(acc_id, 0, 40000000000000000000, 1, 60000000000000000000)); - assert!(System::events() - .iter() - .any(|record| record.event == pool_created_event)); + assert!(System::events().iter().any(|record| record.event == pool_created_event)); } fn initialize_buy_and_burn() { @@ -512,6 +509,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 @@ -529,6 +527,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::::AssetsSwapped(2, 0, 20000000000000000000, 1, 19959959959959959959)); + + assert!(System::events().iter().any(|record| record.event == assets_swapped_event)); }); } @@ -605,6 +607,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( @@ -629,6 +632,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::::AssetsSwapped(2, 0, 40120361083249749248, 1, 30000000000000000000)); + + assert!(System::events().iter().any(|record| record.event == assets_swapped_event)); }); } @@ -765,6 +772,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::::LiquidityMinted(2, 0, 20000000000000000000, 1, 30000000000000000001, 2, 50000000000000000000 )); + + assert!(System::events().iter().any(|record| record.event == liquidity_minted_event)); }); } @@ -887,6 +897,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::::LiquidityBurned(2, 0, 20000000000000000000, 1, 30000000000000000000, 2, 50000000000000000000)); + + assert!(System::events().iter().any(|record| record.event == liquidity_burned)); }); } From 9bfb5d6c15439d82f476d4d284f7f1242ce515e4 Mon Sep 17 00:00:00 2001 From: stanieltron Date: Wed, 23 Jun 2021 08:52:47 +0200 Subject: [PATCH 4/4] removed frame system from asset-info test --- pallets/assets-info/src/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/assets-info/src/tests.rs b/pallets/assets-info/src/tests.rs index 770c339a46a15..72c45ca1c5831 100644 --- a/pallets/assets-info/src/tests.rs +++ b/pallets/assets-info/src/tests.rs @@ -2,7 +2,6 @@ use crate::{mock::*, AssetInfo, Error}; use frame_support::{assert_noop, assert_ok}; -use frame_system as system; #[test] fn set_info_and_retrieve_works_ok() {