Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from paritytech/giles-fix-tests
Browse files Browse the repository at this point in the history
fix tests post merge.
  • Loading branch information
Squirrel authored Nov 22, 2022
2 parents eac18b3 + f406417 commit a969f85
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
19 changes: 11 additions & 8 deletions frame/dex/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ use frame_support::{
construct_runtime,
instances::{Instance1, Instance2},
parameter_types,
traits::{ConstU32, ConstU64},
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64},
PalletId,
};
use frame_system::EnsureSigned;
use sp_core::H256;
use sp_keystore::{testing::KeyStore, KeystoreExt};
use sp_runtime::{
Expand Down Expand Up @@ -56,16 +57,16 @@ impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type Origin = Origin;
type Call = Call;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type DbWeight = ();
type Version = ();
Expand All @@ -82,7 +83,7 @@ impl frame_system::Config for Test {
impl pallet_balances::Config for Test {
type Balance = u64;
type DustRemoval = ();
type Event = Event;
type RuntimeEvent = RuntimeEvent;
type ExistentialDeposit = ConstU64<1>;
type AccountStore = System;
type WeightInfo = ();
Expand All @@ -92,11 +93,12 @@ impl pallet_balances::Config for Test {
}

impl pallet_assets::Config<Instance1> for Test {
type Event = Event;
type RuntimeEvent = RuntimeEvent;
type Balance = u64;
type AssetId = u32;
type Currency = Balances;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<u64>>;
type AssetDeposit = ConstU64<1>;
type AssetAccountDeposit = ConstU64<10>;
type MetadataDepositBase = ConstU64<1>;
Expand All @@ -110,11 +112,12 @@ impl pallet_assets::Config<Instance1> for Test {

//TODO: limit creation only to dex pallet
impl pallet_assets::Config<Instance2> for Test {
type Event = Event;
type RuntimeEvent = RuntimeEvent;
type Balance = u64;
type AssetId = u32;
type Currency = Balances;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<u64>>;
type AssetDeposit = ConstU64<0>;
type AssetAccountDeposit = ConstU64<0>;
type MetadataDepositBase = ConstU64<0>;
Expand All @@ -131,7 +134,7 @@ parameter_types! {
}

impl Config for Test {
type Event = Event;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type AssetBalance = <Self as pallet_balances::Config>::Balance;
type Assets = Assets;
Expand Down
63 changes: 36 additions & 27 deletions frame/dex/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn events() -> Vec<Event<Test>> {
let result = System::events()
.into_iter()
.map(|r| r.event)
.filter_map(|e| if let mock::Event::Dex(inner) = e { Some(inner) } else { None })
.filter_map(|e| if let mock::RuntimeEvent::Dex(inner) = e { Some(inner) } else { None })
.collect();

System::reset_events();
Expand All @@ -43,22 +43,22 @@ fn pools() -> Vec<PoolIdOf<Test>> {
fn assets() -> Vec<u32> {
// if the storage would be public:
// let mut s: Vec<_> = pallet_assets::pallet::Asset::<Test>::iter().map(|x| x.0).collect();
let mut s: Vec<_> = <<Test as Config>::Assets>::assets().collect();
let mut s: Vec<_> = <<Test as Config>::Assets>::asset_ids().collect();
s.sort();
s
}

fn pool_assets() -> Vec<u32> {
// if the storage would be public:
// let mut s: Vec<_> = pallet_assets::pallet::PoolAsset::<Test>::iter().map(|x| x.0).collect();
let mut s: Vec<_> = <<Test as Config>::PoolAssets>::assets().collect();
let mut s: Vec<_> = <<Test as Config>::PoolAssets>::asset_ids().collect();
s.sort();
s
}

fn create_tokens(owner: u64, tokens: Vec<u32>) {
for token_id in tokens {
assert_ok!(Assets::force_create(Origin::root(), token_id, owner, true, 1));
assert_ok!(Assets::force_create(RuntimeOrigin::root(), token_id, owner, true, 1));
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ fn create_pool_should_work() {

create_tokens(user, vec![token_1, token_2]);

assert_ok!(Dex::create_pool(Origin::signed(user), token_2, token_1, lp_token));
assert_ok!(Dex::create_pool(RuntimeOrigin::signed(user), token_2, token_1, lp_token));

assert_eq!(events(), [Event::<Test>::PoolCreated { creator: user, pool_id, lp_token }]);
assert_eq!(pools(), vec![pool_id]);
Expand All @@ -107,13 +107,13 @@ fn add_liquidity_should_work() {
topup_pallet();

create_tokens(user, vec![token_1, token_2]);
assert_ok!(Dex::create_pool(Origin::signed(user), token_1, token_2, lp_token));
assert_ok!(Dex::create_pool(RuntimeOrigin::signed(user), token_1, token_2, lp_token));

assert_ok!(Assets::mint(Origin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(Origin::signed(user), token_2, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_2, user, 1000));

assert_ok!(Dex::add_liquidity(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_1,
token_2,
10,
Expand Down Expand Up @@ -152,13 +152,13 @@ fn remove_liquidity_should_work() {
topup_pallet();

create_tokens(user, vec![token_1, token_2]);
assert_ok!(Dex::create_pool(Origin::signed(user), token_1, token_2, lp_token));
assert_ok!(Dex::create_pool(RuntimeOrigin::signed(user), token_1, token_2, lp_token));

assert_ok!(Assets::mint(Origin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(Origin::signed(user), token_2, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_2, user, 1000));

assert_ok!(Dex::add_liquidity(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_1,
token_2,
10,
Expand All @@ -169,7 +169,16 @@ fn remove_liquidity_should_work() {
2
));

assert_ok!(Dex::remove_liquidity(Origin::signed(user), token_1, token_2, 9, 0, 0, user, 2));
assert_ok!(Dex::remove_liquidity(
RuntimeOrigin::signed(user),
token_1,
token_2,
9,
0,
0,
user,
2
));

assert!(events().contains(&Event::<Test>::LiquidityRemoved {
who: user,
Expand Down Expand Up @@ -202,13 +211,13 @@ fn quote_price_should_work() {
topup_pallet();

create_tokens(user, vec![token_1, token_2]);
assert_ok!(Dex::create_pool(Origin::signed(user), token_1, token_2, lp_token));
assert_ok!(Dex::create_pool(RuntimeOrigin::signed(user), token_1, token_2, lp_token));

assert_ok!(Assets::mint(Origin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(Origin::signed(user), token_2, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_2, user, 1000));

assert_ok!(Dex::add_liquidity(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_1,
token_2,
1000,
Expand All @@ -233,15 +242,15 @@ fn swap_should_work() {
topup_pallet();

create_tokens(user, vec![token_1, token_2]);
assert_ok!(Dex::create_pool(Origin::signed(user), token_1, token_2, lp_token));
assert_ok!(Dex::create_pool(RuntimeOrigin::signed(user), token_1, token_2, lp_token));

assert_ok!(Assets::mint(Origin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(Origin::signed(user), token_2, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_2, user, 1000));

let liquidity1 = 1000;
let liquidity2 = 20;
assert_ok!(Dex::add_liquidity(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_1,
token_2,
liquidity1,
Expand All @@ -256,7 +265,7 @@ fn swap_should_work() {

let exchange_amount = 10;
assert_ok!(Dex::swap_exact_tokens_for_tokens(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_2,
token_1,
exchange_amount,
Expand Down Expand Up @@ -284,17 +293,17 @@ fn same_asset_swap_should_fail() {

create_tokens(user, vec![token_1]);
assert_noop!(
Dex::create_pool(Origin::signed(user), token_1, token_1, lp_token),
Dex::create_pool(RuntimeOrigin::signed(user), token_1, token_1, lp_token),
Error::<Test>::EqualAssets
);

assert_ok!(Assets::mint(Origin::signed(user), token_1, user, 1000));
assert_ok!(Assets::mint(RuntimeOrigin::signed(user), token_1, user, 1000));

let liquidity1 = 1000;
let liquidity2 = 20;
assert_noop!(
Dex::add_liquidity(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_1,
token_1,
liquidity1,
Expand All @@ -310,7 +319,7 @@ fn same_asset_swap_should_fail() {
let exchange_amount = 10;
assert_noop!(
Dex::swap_exact_tokens_for_tokens(
Origin::signed(user),
RuntimeOrigin::signed(user),
token_1,
token_1,
exchange_amount,
Expand Down

0 comments on commit a969f85

Please sign in to comment.