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

fix: adjust router for insufficient asset #765

Merged
merged 35 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
34700d8
fix bug with ed vs router when nonnative balances falls below ED afte…
dmoka Feb 14, 2024
ca326bd
fix router ed issue for sell for both native and nonnative
dmoka Feb 19, 2024
83dd796
Merge remote-tracking branch 'origin/master' into fix/router-with-ed
dmoka Feb 19, 2024
21eef4d
simplify the ED handling in router
dmoka Feb 20, 2024
ccc9125
remove unnecessary variables
dmoka Feb 20, 2024
4af2218
remove unnecessary variables
dmoka Feb 20, 2024
421acf2
remove unnecessary variables
dmoka Feb 20, 2024
ccee548
simlify balance check
dmoka Feb 20, 2024
23de687
buy should work for the case when the user has less then ED afre the …
dmoka Feb 20, 2024
5149c54
bump versions
dmoka Feb 20, 2024
d98fbc9
Merge remote-tracking branch 'origin/master' into fix/router-with-ed
dmoka Feb 20, 2024
c698510
make clippy happy
dmoka Feb 20, 2024
b197e80
bump versions
dmoka Feb 20, 2024
3db469d
use asset registry to get ED
dmoka Feb 21, 2024
4cbbc7b
remove mistakenlty plced balancecheck
dmoka Feb 21, 2024
06a41ac
Merge remote-tracking branch 'origin/master' into fix/router-with-ed
dmoka Feb 21, 2024
a377f73
fix compilation error
dmoka Feb 21, 2024
8c82f5a
bump versions
dmoka Feb 21, 2024
e4f2013
fix test involving stableswap
dmoka Feb 21, 2024
0112322
remove balance check as it is problematic for insufficient assets so …
dmoka Feb 21, 2024
7567012
disable setting onchain route for insufficient asset
dmoka Feb 21, 2024
e289574
make clippy happy
dmoka Feb 21, 2024
02769d8
remove unused error
dmoka Feb 21, 2024
bcbb272
Merge remote-tracking branch 'origin/master' into fix/router-with-ed
dmoka Feb 21, 2024
6790d51
bump versions
dmoka Feb 21, 2024
ad88392
Merge branch 'master' into fix/router-with-ed
mrq1911 Feb 22, 2024
b1bbde0
lock
mrq1911 Feb 22, 2024
f55a925
wip - fix benchmarking
dmoka Feb 22, 2024
09885fb
fix bencmhark tests
dmoka Feb 22, 2024
4a01291
Merge remote-tracking branch 'origin/master' into fix/router-with-ed
dmoka Feb 22, 2024
e0349e3
rebenchmark pallet
dmoka Feb 22, 2024
10ac36b
remove unsed method
dmoka Feb 22, 2024
44cced4
bump runtime
dmoka Feb 22, 2024
f91af68
fix compilation error
dmoka Feb 22, 2024
3ad3686
bump patch version
dmoka Feb 22, 2024
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
8 changes: 4 additions & 4 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.18.1"
version = "1.18.2"
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.0.1"
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
2 changes: 1 addition & 1 deletion pallets/route-executor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-route-executor'
version = '2.0.1'
version = '2.0.2'
description = 'A pallet to execute a route containing a sequence of trades'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
Loading
Loading