Skip to content

Commit

Permalink
Merge pull request #765 from galacticcouncil/fix/router-with-ed
Browse files Browse the repository at this point in the history
fix: adjust router for insufficient asset
  • Loading branch information
dmoka authored Feb 22, 2024
2 parents dd578de + 3ad3686 commit 5bba8e4
Show file tree
Hide file tree
Showing 19 changed files with 590 additions and 177 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.19.0"
version = "1.19.1"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
244 changes: 243 additions & 1 deletion integration-tests/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,9 @@ mod router_different_pools_tests {

mod omnipool_router_tests {
use super::*;
use frame_support::assert_noop;
use hydradx_traits::router::PoolType;
use hydradx_traits::AssetKind;

#[test]
fn sell_should_work_when_route_contains_single_trade() {
Expand Down Expand Up @@ -750,6 +753,213 @@ mod omnipool_router_tests {
});
}

#[test]
fn sell_should_work_when_user_has_left_less_than_existential_in_nonnative() {
TestNet::reset();

Hydra::execute_with(|| {
let _ = with_transaction(|| {
//Arrange
let (pool_id, stable_asset_1, _) = init_stableswap().unwrap();

init_omnipool();

let init_balance = 3000 * UNITS + 1;
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
ALICE.into(),
stable_asset_1,
init_balance as i128,
));

let trades = vec![Trade {
pool: PoolType::Stableswap(pool_id),
asset_in: stable_asset_1,
asset_out: pool_id,
}];

assert_balance!(ALICE.into(), pool_id, 0);

//Act
let amount_to_sell = 3000 * UNITS;
assert_ok!(Router::sell(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
stable_asset_1,
pool_id,
amount_to_sell,
0,
trades
));

//Assert
assert_eq!(
hydradx_runtime::Currencies::free_balance(stable_asset_1, &AccountId::from(ALICE)),
0
);

TransactionOutcome::Commit(DispatchResult::Ok(()))
});
});
}

#[test]
fn sell_should_fail_when_all_asset_in_spent_for_shitcoin() {
TestNet::reset();

Hydra::execute_with(|| {
let _ = with_transaction(|| {
//Arrange
let name = b"SHITCO".to_vec();
let shitcoin = AssetRegistry::register_insufficient_asset(
None,
Some(name.try_into().unwrap()),
AssetKind::External,
Some(1_000),
None,
None,
None,
None,
)
.unwrap();
assert_ok!(Currencies::deposit(shitcoin, &DAVE.into(), 100000 * UNITS,));
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
DAVE.into(),
HDX,
100000 * UNITS as i128,
));

assert_ok!(XYK::create_pool(
RuntimeOrigin::signed(DAVE.into()),
HDX,
100000 * UNITS,
shitcoin,
100000 * UNITS,
));

let trades = vec![Trade {
pool: PoolType::XYK,
asset_in: HDX,
asset_out: shitcoin,
}];

//Act
let amount_to_sell = ALICE_INITIAL_NATIVE_BALANCE;
assert_noop!(
Router::sell(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
HDX,
shitcoin,
amount_to_sell,
0,
trades
),
orml_tokens::Error::<Runtime>::ExistentialDeposit
);

TransactionOutcome::Commit(DispatchResult::Ok(()))
});
});
}

#[test]
fn sell_should_pass_when_user_has_asset_in_covering_the_fee_for_shitcoin() {
TestNet::reset();

Hydra::execute_with(|| {
let _ = with_transaction(|| {
//Arrange
let name = b"SHITCO".to_vec();
let shitcoin = AssetRegistry::register_insufficient_asset(
None,
Some(name.try_into().unwrap()),
AssetKind::External,
Some(1_000),
None,
None,
None,
None,
)
.unwrap();

assert_ok!(Currencies::deposit(shitcoin, &DAVE.into(), 100000 * UNITS,));
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
DAVE.into(),
HDX,
100000 * UNITS as i128,
));

assert_ok!(XYK::create_pool(
RuntimeOrigin::signed(DAVE.into()),
HDX,
100000 * UNITS,
shitcoin,
100000 * UNITS,
));

let trades = vec![Trade {
pool: PoolType::XYK,
asset_in: HDX,
asset_out: shitcoin,
}];

//Act
let amount_to_sell = ALICE_INITIAL_NATIVE_BALANCE - 20 * UNITS;
assert_ok!(Router::sell(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
HDX,
shitcoin,
amount_to_sell,
0,
trades
));

TransactionOutcome::Commit(DispatchResult::Ok(()))
});
});
}

#[test]
fn sell_should_work_when_user_has_left_less_than_existential_in_native() {
TestNet::reset();

Hydra::execute_with(|| {
//Arrange
init_omnipool();

assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
ALICE.into(),
HDX,
1_i128,
));

let trades = vec![Trade {
pool: PoolType::Omnipool,
asset_in: HDX,
asset_out: DAI,
}];

//Act and assert
let amount_to_sell = ALICE_INITIAL_NATIVE_BALANCE;
assert_ok!(Router::sell(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
HDX,
DAI,
amount_to_sell,
0,
trades
));

//Assert
assert_eq!(
hydradx_runtime::Currencies::free_balance(HDX, &AccountId::from(ALICE)),
0
);
});
}

#[test]
fn sell_hub_asset_should_work_when_route_contains_single_trade() {
TestNet::reset();
Expand Down Expand Up @@ -910,6 +1120,38 @@ mod omnipool_router_tests {
});
}

#[test]
fn buy_should_work_when_after_trade_reamining_balance_is_less_than_existential_deposit() {
TestNet::reset();

Hydra::execute_with(|| {
//Arrange
init_omnipool();

let amount_to_buy = 26559360000000000000u128;

let limit = ALICE_INITIAL_NATIVE_BALANCE;
let trades = vec![Trade {
pool: PoolType::Omnipool,
asset_in: HDX,
asset_out: DAI,
}];

//Act
assert_ok!(Router::buy(
RuntimeOrigin::signed(BOB.into()),
HDX,
DAI,
amount_to_buy,
limit,
trades
));

//Assert
assert_balance!(BOB.into(), DAI, BOB_INITIAL_DAI_BALANCE + amount_to_buy);
});
}

#[test]
fn buy_hub_asset_should_not_work() {
TestNet::reset();
Expand Down Expand Up @@ -3424,7 +3666,7 @@ pub fn init_stableswap_with_liquidity(
None,
Some(name.try_into().unwrap()),
AssetKind::Token,
1u128,
1000u128,
Some(b"xDUM".to_vec().try_into().unwrap()),
Some(18u8),
None,
Expand Down
2 changes: 1 addition & 1 deletion pallets/asset-registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-asset-registry"
version = "3.0.0"
version = "3.1.0"
description = "Pallet for asset registry management"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion pallets/dca/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-dca'
version = "1.3.4"
version = "1.3.5"
description = 'A pallet to manage DCA scheduling'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
36 changes: 36 additions & 0 deletions pallets/dca/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,49 @@ parameter_types! {

type Pools = (OmniPool, Xyk);

pub struct MockedAssetRegistry;

impl hydradx_traits::registry::Inspect for MockedAssetRegistry {
type AssetId = AssetId;
type Location = ();

fn is_sufficient(_id: Self::AssetId) -> bool {
unimplemented!()
}

fn exists(_id: Self::AssetId) -> bool {
unimplemented!()
}

fn decimals(_id: Self::AssetId) -> Option<u8> {
unimplemented!()
}

fn asset_type(_id: Self::AssetId) -> Option<AssetKind> {
unimplemented!()
}

fn is_banned(_id: Self::AssetId) -> bool {
unimplemented!()
}

fn asset_name(_id: Self::AssetId) -> Option<Vec<u8>> {
unimplemented!()
}

fn asset_symbol(_id: Self::AssetId) -> Option<Vec<u8>> {
unimplemented!()
}
}

impl pallet_route_executor::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AssetId = AssetId;
type Balance = Balance;
type NativeAssetId = NativeCurrencyId;
type Currency = FungibleCurrencies<Test>;
type AMM = Pools;
type InspectRegistry = MockedAssetRegistry;
type DefaultRoutePoolType = DefaultRoutePoolType;
type WeightInfo = ();
}
Expand Down
Loading

0 comments on commit 5bba8e4

Please sign in to comment.