Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subdex: Intoduce additional errors, tests: initialize_exchange full coverage #44

Merged
merged 4 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn staging_test_net(id: ParaId) -> ChainSpec {
fn testnet_genesis(
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
id: ParaId,
_id: ParaId,
) -> GenesisConfig {
GenesisConfig {
frame_system: Some(SystemConfig {
Expand Down
17 changes: 12 additions & 5 deletions pallets/pallet-subdex-xcmp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Get<ParaId> for FirstParaId {
}

// Used to get min parachain asset amount, based on its type size, set on node runtime level
const fn get_min_parachain_asset_amount() -> Balance {
pub const fn get_min_parachain_asset_amount() -> Balance {
match core::mem::size_of::<Balance>() {
size if size <= 64 => 1000,
// cosider 112 instead
Expand All @@ -88,7 +88,7 @@ const fn get_min_parachain_asset_amount() -> Balance {
}

// Used to get min main network asset amount, based on its type size, set on node runtime level
const fn get_min_main_network_asset_amount() -> Balance {
pub const fn get_min_main_network_asset_amount() -> Balance {
match core::mem::size_of::<Balance>() {
size if size <= 64 => 10_000,
// cosider 112 instead
Expand All @@ -97,7 +97,7 @@ const fn get_min_main_network_asset_amount() -> Balance {
}
}

#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, Eq, Debug, PartialEq)]
pub struct Test;
parameter_types! {
pub const ExistentialDeposit: Balance = 100;
Expand Down Expand Up @@ -280,12 +280,19 @@ pub fn with_test_externalities<R, F: FnOnce() -> R>(f: F) -> R {
ExtBuilder::build().execute_with(func)
}

type RawTestEvent = RawEvent<AccountId, Balance, Option<AssetId>, AssetId>;
type SubDexXcmpRawTestEvent = RawEvent<AccountId, Balance, Option<AssetId>, AssetId>;

pub fn get_test_event(raw_event: RawTestEvent) -> TestEvent {
type SubdexRawTestEvent =
pallet_subdex::RawEvent<AccountId, Asset<AssetId>, Balance, Balance, Option<Balance>>;

pub fn get_subdex_xcmp_test_event(raw_event: SubDexXcmpRawTestEvent) -> TestEvent {
TestEvent::subdex_xcmp(raw_event)
}

pub fn get_subdex_test_event(raw_event: SubdexRawTestEvent) -> TestEvent {
TestEvent::pallet_subdex(raw_event)
}

pub fn assert_event_success(tested_event: TestEvent, number_of_events_after_call: usize) {
// Ensure runtime events length is equal to expected number of events after call
assert_eq!(System::events().len(), number_of_events_after_call);
Expand Down
65 changes: 65 additions & 0 deletions pallets/pallet-subdex-xcmp/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,82 @@
mod handle_downward_message;
mod handle_xcmp_message;
mod initialize_exchange;
mod invest_liquidity;
mod transfer_balance_to_parachain_chain;
mod transfer_balance_to_relay_chain;

pub use super::*;
pub use crate::mock::*;
use pallet_subdex::Exchange;

// Receive provided amounts for both main network curency and parachain assets through xcmp and use them to initialize exchange
pub fn initialize_simple_exchange(
account_id: AccountId,
main_network_currency_transfer_amount: Balance,
para_asset_id: Option<AssetId>,
para_asset_transfer_amount: Balance,
) {
// Transfer both main network currency and custom parachain assets to dex parachain.

let asset_id = get_next_asset_id();

// Emulate xcmp message
emulate_xcmp_message(
FirstParaId::get(),
account_id.clone(),
para_asset_transfer_amount,
para_asset_id,
);

// Emulate downward message
emulate_downward_message(account_id.clone(), main_network_currency_transfer_amount);

// Initialize new exchange
assert_ok!(initialize_new_exchange(
account_id.clone(),
// previosuly mapped parachain asset representation
Asset::ParachainAsset(asset_id),
para_asset_transfer_amount,
Asset::MainNetworkCurrency,
main_network_currency_transfer_amount
));
}

// Subdex

pub fn asset_balances(account_id: AccountId, asset_id: AssetId) -> Balance {
SubDex::asset_balances(account_id, asset_id)
}

pub fn dex_exchanges(first_asset: Asset<AssetId>, second_asset: Asset<AssetId>) -> Exchange<Test> {
SubDex::exchanges(first_asset, second_asset)
}

pub fn initialize_new_exchange(
origin: AccountId,
first_asset: Asset<AssetId>,
first_asset_amount: Balance,
second_asset: Asset<AssetId>,
second_asset_amount: Balance,
) -> DispatchResult {
SubDex::initialize_exchange(
Origin::signed(origin),
first_asset,
first_asset_amount,
second_asset,
second_asset_amount,
)
}

pub fn emulate_invest_liquidity(
origin: AccountId,
first_asset: Asset<AssetId>,
second_asset: Asset<AssetId>,
shares: Balance,
) -> DispatchResult {
SubDex::invest_liquidity(Origin::signed(origin), first_asset, second_asset, shares)
}

// Subdex Xcmp

pub fn asset_id_exists(para_id: ParaId, asset_id: Option<AssetId>) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn handle_downward_message() {
transfer_amount
);

let transferred_tokens_from_relay_chain_event = get_test_event(
let transferred_tokens_from_relay_chain_event = get_subdex_xcmp_test_event(
RawEvent::TransferredTokensFromRelayChain(FirstAccountId::get(), transfer_amount),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn handle_xcmp_message() {
assert_eq!(next_asset_id + 1, get_next_asset_id());

let transferred_tokens_from_relay_chain_event =
get_test_event(RawEvent::DepositAssetViaXCMP(
get_subdex_xcmp_test_event(RawEvent::DepositAssetViaXCMP(
FirstParaId::get(),
para_asset_id,
FirstAccountId::get(),
Expand Down
Loading