Skip to content

Commit

Permalink
add pallet-amm-support tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak committed Oct 2, 2024
1 parent c24094e commit 4bca2b6
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pallets/amm-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ primitives = { workspace = true }
# Substrate dependencies
sp-std = { workspace = true }
sp-api = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }

Expand All @@ -32,6 +34,8 @@ std = [
"frame-system/std",
"sp-std/std",
"sp-api/std",
"sp-core/std",
"sp-io/std",
"primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
3 changes: 3 additions & 0 deletions pallets/amm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub use hydradx_traits::{
};
pub use primitives::IncrementalId as IncrementalIdType;

#[cfg(test)]
mod tests;

// Re-export pallet items so that they can be accessed from the crate namespace.
pub use pallet::*;

Expand Down
67 changes: 67 additions & 0 deletions pallets/amm-support/src/tests/incremental_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file is part of HydraDX-node.

// Copyright (C) 2020-2022 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use hydradx_traits::router::{Filler, TradeOperation};
use crate::tests::mock::*;
use crate::Event;

#[test]
fn event_id_should_be_incremented() {
ExtBuilder::default().build().execute_with(|| {

assert_eq!(AmmSupport::incremental_id(), 0);
assert_eq!(AmmSupport::next_incremental_id(), 0);

assert_eq!(AmmSupport::incremental_id(), 1);
assert_eq!(AmmSupport::next_incremental_id(), 1);

assert_eq!(AmmSupport::incremental_id(), 2);
assert_eq!(AmmSupport::next_incremental_id(), 2);
});
}

#[test]
fn event_should_be_deposited() {
ExtBuilder::default().build().execute_with(|| {
AmmSupport::deposit_trade_event(
ALICE,
BOB,
Filler::Omnipool,
TradeOperation::Sell,
HDX,
DOT,
1_000_000,
2_000_000,
vec![(HDX, 1_000, ALICE), (DOT, 2_000, BOB)],
Some(7),
);

expect_events(vec![Event::Swapped {
swapper: ALICE,
filler: BOB,
filler_type: Filler::Omnipool,
operation: TradeOperation::Sell,
asset_in: HDX,
asset_out: DOT,
amount_in: 1_000_000,
amount_out: 2_000_000,
fees: vec![(HDX, 1_000, ALICE), (DOT, 2_000, BOB)],
event_id: Some(7),
}
.into()]);
});
}
100 changes: 100 additions & 0 deletions pallets/amm-support/src/tests/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// This file is part of HydraDX.

// Copyright (C) 2020-2022 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate as pallet_amm_support;

use frame_support::{
construct_runtime,
sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
},
traits::{ConstU32, ConstU64, Everything},
};
use sp_core::H256;

type Block = frame_system::mocking::MockBlock<Test>;

pub type AccountId = u64;
pub type AssetId = u32;

pub const HDX: AssetId = 0;
pub const DOT: AssetId = 1;

pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;

construct_runtime!(
pub enum Test
{
System: frame_system,
AmmSupport: pallet_amm_support,
}
);

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type RuntimeTask = RuntimeTask;
type Nonce = u64;
type Block = Block;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type DbWeight = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

#[derive(Default)]
pub struct ExtBuilder {
}

impl ExtBuilder {
pub fn build(self) -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();

let mut r: sp_io::TestExternalities = t.into();

r.execute_with(|| {
System::set_block_number(1);
});

r
}
}

pub fn expect_events(e: Vec<RuntimeEvent>) {
e.into_iter().for_each(frame_system::Pallet::<Test>::assert_has_event);
}
2 changes: 2 additions & 0 deletions pallets/amm-support/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod mock;
mod incremental_id;

0 comments on commit 4bca2b6

Please sign in to comment.