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

feat: add sell_all for route executor #888

Merged
merged 13 commits into from
Sep 5, 2024
6 changes: 3 additions & 3 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.23.2"
version = "1.23.3"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
138 changes: 137 additions & 1 deletion integration-tests/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ mod omnipool_router_tests {
}

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

Hydra::execute_with(|| {
Expand Down Expand Up @@ -5293,6 +5293,142 @@ mod route_spot_price {
}
}

mod sell_all {
use super::*;
use hydradx_runtime::Currencies;
use hydradx_traits::router::PoolType;

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

let limit = 0;
let amount_out = 26577363534770086553;

Hydra::execute_with(|| {
let bob_hdx_balance = Currencies::free_balance(HDX, &BOB.into());

//Arrange
init_omnipool();

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

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

//Assert
assert_balance!(BOB.into(), HDX, 0);

expect_hydra_last_events(vec![pallet_route_executor::Event::Executed {
asset_in: HDX,
asset_out: DAI,
amount_in: bob_hdx_balance,
amount_out,
}
.into()]);
});
}

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

let limit = 0;
let amount_out = 35227901268414708;

Hydra::execute_with(|| {
let bob_nonnative_balance = Currencies::free_balance(DAI, &BOB.into());

//Arrange
init_omnipool();

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

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

//Assert
assert_balance!(BOB.into(), DAI, 0);

expect_hydra_last_events(vec![pallet_route_executor::Event::Executed {
asset_in: DAI,
asset_out: HDX,
amount_in: bob_nonnative_balance,
amount_out,
}
.into()]);
});
}

#[test]
fn sell_all_should_work_when_selling_all_nonnative_in_stableswap() {
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_all(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
stable_asset_1,
pool_id,
0,
trades
));

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

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

fn create_lbp_pool(accumulated_asset: u32, distributed_asset: u32) {
assert_ok!(Currencies::update_balance(
hydradx_runtime::RuntimeOrigin::root(),
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.5.1'
version = '2.6.0'
description = 'A pallet to execute a route containing a sequence of trades'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
4 changes: 3 additions & 1 deletion pallets/route-executor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The comparison happens by calculating sell amount_outs for the routes, but also

The route is stored in an ordered manner, based on the oder of the ids in the asset pair.

If the route is set successfully, then the fee is payed back.
If the route is set successfully, then the fee is paid back.

If the route setting fails, it emits event `RouteUpdateIsNotSuccessful`

Expand All @@ -36,5 +36,7 @@ If not on-chain is present, then omnipool is used as default

Both buy and sell trades are supported.

There is also a `sell_all` extrinsic, which sells all the reducible `asset_in` balance of the user.

### Weight calculation
The extrinsic weights are calculated based on the size of the route.
Loading
Loading