Skip to content

Commit 9ddea80

Browse files
authored
Merge pull request #1383 from galacticcouncil/fix/dca-atoken-rounding
fix: DCA slippage limit calculation
2 parents 266373e + 855a352 commit 9ddea80

7 files changed

Lines changed: 75 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "runtime-integration-tests"
3-
version = "1.74.1"
3+
version = "1.75.0"
44
description = "Integration tests"
55
authors = ["GalacticCouncil"]
66
edition = "2021"

integration-tests/src/dca.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,62 @@ mod omnipool {
21192119
assert_balance!(ALICE.into(), HDX, 0);
21202120
});
21212121
}
2122+
2123+
#[test]
2124+
fn sell_schedule_should_use_slippage_limit_when_min_amount_out_is_zero() {
2125+
TestNet::reset();
2126+
Hydra::execute_with(|| {
2127+
//Arrange
2128+
init_omnipool_with_oracle_for_block_10();
2129+
let alice_init_hdx_balance = 5000 * UNITS;
2130+
assert_ok!(Balances::force_set_balance(
2131+
RuntimeOrigin::root(),
2132+
ALICE.into(),
2133+
alice_init_hdx_balance,
2134+
));
2135+
2136+
let dca_budget = 1100 * UNITS;
2137+
let amount_to_sell = 100 * UNITS;
2138+
2139+
// Create sell schedule with min_amount_out = 0
2140+
// This means last_block_slippage_min_limit will be used as the effective limit
2141+
let schedule = Schedule {
2142+
owner: AccountId::from(ALICE),
2143+
period: 5u32,
2144+
total_amount: dca_budget,
2145+
max_retries: None,
2146+
stability_threshold: None,
2147+
slippage: Some(Permill::from_percent(5)),
2148+
order: Order::Sell {
2149+
asset_in: HDX,
2150+
asset_out: DAI,
2151+
amount_in: amount_to_sell,
2152+
min_amount_out: 0,
2153+
route: create_bounded_vec(vec![Trade {
2154+
pool: PoolType::Omnipool,
2155+
asset_in: HDX,
2156+
asset_out: DAI,
2157+
}]),
2158+
},
2159+
};
2160+
create_schedule(ALICE, schedule);
2161+
2162+
let alice_dai_before = Currencies::free_balance(DAI, &ALICE.into());
2163+
2164+
//Act
2165+
go_to_block(12);
2166+
2167+
//Assert - DCA executed successfully and schedule is still alive
2168+
let alice_dai_after = Currencies::free_balance(DAI, &ALICE.into());
2169+
assert!(
2170+
alice_dai_after > alice_dai_before,
2171+
"ALICE should have received DAI from the trade"
2172+
);
2173+
2174+
let schedule = DCA::schedules(0);
2175+
assert!(schedule.is_some(), "DCA schedule should still be alive after execution");
2176+
});
2177+
}
21222178
}
21232179

21242180
mod fee {

pallets/dca/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = 'pallet-dca'
3-
version = "1.16.1"
3+
version = "1.17.0"
44
description = 'A pallet to manage DCA scheduling'
55
authors = ['GalacticCouncil']
66
edition = '2021'

pallets/dca/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,16 +830,26 @@ impl<T: Config> Pallet<T> {
830830
let last_trade = trade_amounts.last().defensive_ok_or(Error::<T>::InvalidState)?;
831831
let amount_out = last_trade.amount_out;
832832

833-
if *min_amount_out > last_block_slippage_min_limit {
833+
let effective_min_limit = if *min_amount_out > last_block_slippage_min_limit {
834834
ensure!(amount_out >= *min_amount_out, Error::<T>::TradeLimitReached);
835+
amount_out
835836
} else {
836837
ensure!(
837838
amount_out >= last_block_slippage_min_limit,
838839
Error::<T>::SlippageLimitReached
839840
);
841+
// Use slippage limit to mainly absorb aToken rounding errors
842+
last_block_slippage_min_limit
840843
};
841844

842-
T::RouteExecutor::sell(origin, *asset_in, *asset_out, amount_to_sell, amount_out, route.clone())?;
845+
T::RouteExecutor::sell(
846+
origin,
847+
*asset_in,
848+
*asset_out,
849+
amount_to_sell,
850+
effective_min_limit,
851+
route.clone(),
852+
)?;
843853

844854
Ok(AmountInAndOut {
845855
amount_in: amount_to_sell,

runtime/hydradx/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hydradx-runtime"
3-
version = "403.0.0"
3+
version = "404.0.0"
44
authors = ["GalacticCouncil"]
55
edition = "2021"
66
license = "Apache 2.0"

runtime/hydradx/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
129129
spec_name: Cow::Borrowed("hydradx"),
130130
impl_name: Cow::Borrowed("hydradx"),
131131
authoring_version: 1,
132-
spec_version: 403,
132+
spec_version: 404,
133133
impl_version: 0,
134134
apis: RUNTIME_API_VERSIONS,
135135
transaction_version: 1,

0 commit comments

Comments
 (0)