diff --git a/Cargo.lock b/Cargo.lock index b9214a886..67960f501 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7479,6 +7479,8 @@ dependencies = [ "primitives", "scale-info", "sp-api", + "sp-core", + "sp-io", "sp-std 14.0.0 (git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.7.2)", ] diff --git a/pallets/amm-support/Cargo.toml b/pallets/amm-support/Cargo.toml index a851f8144..f03340955 100644 --- a/pallets/amm-support/Cargo.toml +++ b/pallets/amm-support/Cargo.toml @@ -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 } @@ -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"] diff --git a/pallets/amm-support/src/lib.rs b/pallets/amm-support/src/lib.rs index a30430b3c..ae7bb9cf6 100644 --- a/pallets/amm-support/src/lib.rs +++ b/pallets/amm-support/src/lib.rs @@ -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::*; diff --git a/pallets/amm-support/src/tests/incremental_id.rs b/pallets/amm-support/src/tests/incremental_id.rs new file mode 100644 index 000000000..b750b8fb7 --- /dev/null +++ b/pallets/amm-support/src/tests/incremental_id.rs @@ -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()]); + }); +} \ No newline at end of file diff --git a/pallets/amm-support/src/tests/mock.rs b/pallets/amm-support/src/tests/mock.rs new file mode 100644 index 000000000..0d859ec48 --- /dev/null +++ b/pallets/amm-support/src/tests/mock.rs @@ -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; + +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; + 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::::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) { + e.into_iter().for_each(frame_system::Pallet::::assert_has_event); +} diff --git a/pallets/amm-support/src/tests/mod.rs b/pallets/amm-support/src/tests/mod.rs new file mode 100644 index 000000000..9394fa916 --- /dev/null +++ b/pallets/amm-support/src/tests/mod.rs @@ -0,0 +1,2 @@ +pub mod mock; +mod incremental_id;