Skip to content

Commit 43024b0

Browse files
authored
Merge pull request #1452 from galacticcouncil/fix/extrinsic-index
fix: account weight for trade with no route
2 parents 6761ff5 + fdb195f commit 43024b0

7 files changed

Lines changed: 159 additions & 24 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 4 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.82.0"
3+
version = "1.83.0"
44
description = "Integration tests"
55
authors = ["GalacticCouncil"]
66
edition = "2021"

integration-tests/src/router.rs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5740,3 +5740,140 @@ fn populate_oracle(
57405740
));
57415741
go_to_block(block.unwrap_or(10));
57425742
}
5743+
5744+
mod weight_uses_resolved_route {
5745+
use super::*;
5746+
use frame_support::dispatch::GetDispatchInfo;
5747+
5748+
fn stored_multi_hop_route() -> Vec<Trade<AssetId>> {
5749+
vec![
5750+
Trade {
5751+
pool: PoolType::Omnipool,
5752+
asset_in: HDX,
5753+
asset_out: DAI,
5754+
},
5755+
Trade {
5756+
pool: PoolType::XYK,
5757+
asset_in: DAI,
5758+
asset_out: DOT,
5759+
},
5760+
Trade {
5761+
pool: PoolType::Omnipool,
5762+
asset_in: DOT,
5763+
asset_out: ETH,
5764+
},
5765+
]
5766+
}
5767+
5768+
#[test]
5769+
fn sell_weight_should_reflect_stored_route_when_input_route_is_empty() {
5770+
TestNet::reset();
5771+
Hydra::execute_with(|| {
5772+
let stored = stored_multi_hop_route();
5773+
assert_ok!(Router::force_insert_route(
5774+
RuntimeOrigin::root(),
5775+
Pair::new(HDX, ETH),
5776+
BoundedVec::truncate_from(stored.clone()),
5777+
));
5778+
5779+
let call = pallet_route_executor::Call::<Runtime>::sell {
5780+
asset_in: HDX,
5781+
asset_out: ETH,
5782+
amount_in: UNITS,
5783+
min_amount_out: 0,
5784+
route: BoundedVec::new(),
5785+
};
5786+
5787+
let charged = call.get_dispatch_info().call_weight;
5788+
let expected = RouterWeightInfo::sell_weight(&stored);
5789+
assert_eq!(charged, expected);
5790+
});
5791+
}
5792+
5793+
#[test]
5794+
fn buy_weight_should_reflect_stored_route_when_input_route_is_empty() {
5795+
TestNet::reset();
5796+
Hydra::execute_with(|| {
5797+
let stored = stored_multi_hop_route();
5798+
assert_ok!(Router::force_insert_route(
5799+
RuntimeOrigin::root(),
5800+
Pair::new(HDX, ETH),
5801+
BoundedVec::truncate_from(stored.clone()),
5802+
));
5803+
5804+
let call = pallet_route_executor::Call::<Runtime>::buy {
5805+
asset_in: HDX,
5806+
asset_out: ETH,
5807+
amount_out: UNITS,
5808+
max_amount_in: u128::MAX,
5809+
route: BoundedVec::new(),
5810+
};
5811+
5812+
let charged = call.get_dispatch_info().call_weight;
5813+
let expected = RouterWeightInfo::buy_weight(&stored);
5814+
assert_eq!(charged, expected);
5815+
});
5816+
}
5817+
5818+
#[test]
5819+
fn sell_all_weight_should_reflect_stored_route_when_input_route_is_empty() {
5820+
TestNet::reset();
5821+
Hydra::execute_with(|| {
5822+
let stored = stored_multi_hop_route();
5823+
assert_ok!(Router::force_insert_route(
5824+
RuntimeOrigin::root(),
5825+
Pair::new(HDX, ETH),
5826+
BoundedVec::truncate_from(stored.clone()),
5827+
));
5828+
5829+
let call = pallet_route_executor::Call::<Runtime>::sell_all {
5830+
asset_in: HDX,
5831+
asset_out: ETH,
5832+
min_amount_out: 0,
5833+
route: BoundedVec::new(),
5834+
};
5835+
5836+
let charged = call.get_dispatch_info().call_weight;
5837+
let expected = RouterWeightInfo::sell_weight(&stored);
5838+
assert_eq!(charged, expected);
5839+
});
5840+
}
5841+
5842+
#[test]
5843+
fn sell_weight_should_be_higher_with_stored_multi_hop_route_than_without() {
5844+
TestNet::reset();
5845+
Hydra::execute_with(|| {
5846+
let call_without_stored = pallet_route_executor::Call::<Runtime>::sell {
5847+
asset_in: HDX,
5848+
asset_out: ETH,
5849+
amount_in: UNITS,
5850+
min_amount_out: 0,
5851+
route: BoundedVec::new(),
5852+
};
5853+
let weight_without_stored = call_without_stored.get_dispatch_info().call_weight;
5854+
5855+
assert_ok!(Router::force_insert_route(
5856+
RuntimeOrigin::root(),
5857+
Pair::new(HDX, ETH),
5858+
BoundedVec::truncate_from(stored_multi_hop_route()),
5859+
));
5860+
5861+
let call_with_stored = pallet_route_executor::Call::<Runtime>::sell {
5862+
asset_in: HDX,
5863+
asset_out: ETH,
5864+
amount_in: UNITS,
5865+
min_amount_out: 0,
5866+
route: BoundedVec::new(),
5867+
};
5868+
let weight_with_stored = call_with_stored.get_dispatch_info().call_weight;
5869+
5870+
assert!(
5871+
weight_with_stored.ref_time() > weight_without_stored.ref_time(),
5872+
"expected stored-route call to charge more ref_time than no-stored-route call \
5873+
(got {} vs {})",
5874+
weight_with_stored.ref_time(),
5875+
weight_without_stored.ref_time(),
5876+
);
5877+
});
5878+
}
5879+
}

pallets/route-executor/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pallet-route-executor"
3-
version = "2.12.0"
3+
version = "2.12.1"
44
description = "A pallet to execute a route containing a sequence of trades"
55
authors = ["GalacticCouncil"]
66
edition = "2021"
@@ -35,7 +35,6 @@ sp-io = { workspace = true }
3535
pretty_assertions = { workspace = true }
3636
orml-tokens = { workspace = true }
3737
pallet-currencies = { workspace = true }
38-
hydradx-adapters = { workspace = true }
3938
test-utils = { workspace = true }
4039

4140
[features]
@@ -54,9 +53,9 @@ std = [
5453
"frame-system/std",
5554
"orml-tokens/std",
5655
"orml-traits/std",
57-
"hydradx-adapters/std",
5856
"pallet-balances/std",
5957
"pallet-broadcast/std",
58+
"pallet-currencies/std",
6059
"hydradx-traits/std",
6160
"hydra-dx-math/std",
6261
"frame-benchmarking/std",

pallets/route-executor/src/lib.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub mod pallet {
189189
///
190190
/// Emits `RouteExecuted` when successful.
191191
#[pallet::call_index(0)]
192-
#[pallet::weight(T::WeightInfo::sell_weight(route))]
192+
#[pallet::weight(T::WeightInfo::sell_weight(&Pallet::<T>::get_route_or_default(*asset_in, *asset_out, route)))]
193193
#[transactional]
194194
pub fn sell(
195195
origin: OriginFor<T>,
@@ -216,7 +216,7 @@ pub mod pallet {
216216
///
217217
/// Emits `RouteExecuted` when successful.
218218
#[pallet::call_index(1)]
219-
#[pallet::weight(T::WeightInfo::buy_weight(route))]
219+
#[pallet::weight(T::WeightInfo::buy_weight(&Pallet::<T>::get_route_or_default(*asset_in, *asset_out, route)))]
220220
#[transactional]
221221
pub fn buy(
222222
origin: OriginFor<T>,
@@ -232,7 +232,7 @@ pub mod pallet {
232232
Self::ensure_route_size(route.len())?;
233233

234234
let asset_pair = AssetPair::new(asset_in, asset_out);
235-
let route = Self::get_route_or_default(route, asset_pair)?;
235+
let route = Self::get_route_or_default(asset_in, asset_out, &route);
236236
Self::ensure_route_arguments(&asset_pair, &route)?;
237237

238238
let trade_amounts = Self::calculate_buy_trade_amounts(&route, amount_out)?;
@@ -423,7 +423,7 @@ pub mod pallet {
423423
/// Emits `RouteExecuted` when successful.
424424
///
425425
#[pallet::call_index(4)]
426-
#[pallet::weight(T::WeightInfo::sell_weight(route))]
426+
#[pallet::weight(T::WeightInfo::sell_weight(&Pallet::<T>::get_route_or_default(*asset_in, *asset_out, route)))]
427427
#[transactional]
428428
pub fn sell_all(
429429
origin: OriginFor<T>,
@@ -460,7 +460,7 @@ impl<T: Config> Pallet<T> {
460460
Self::ensure_route_size(route.len())?;
461461

462462
let asset_pair = AssetPair::new(asset_in, asset_out);
463-
let route = Self::get_route_or_default(route, asset_pair)?;
463+
let route = Self::get_route_or_default(asset_in, asset_out, &route);
464464
Self::ensure_route_arguments(&asset_pair, &route)?;
465465

466466
let trader_account = Self::router_account();
@@ -558,16 +558,16 @@ impl<T: Config> Pallet<T> {
558558
Ok(())
559559
}
560560

561-
fn get_route_or_default(
562-
route: Route<T::AssetId>,
563-
asset_pair: AssetPair<T::AssetId>,
564-
) -> Result<Route<T::AssetId>, DispatchError> {
565-
let route = if !route.is_empty() {
566-
route
561+
pub fn get_route_or_default(
562+
asset_in: T::AssetId,
563+
asset_out: T::AssetId,
564+
route: &Route<T::AssetId>,
565+
) -> Route<T::AssetId> {
566+
if !route.is_empty() {
567+
route.clone()
567568
} else {
568-
<Pallet<T> as RouteProvider<T::AssetId>>::get_route(asset_pair)
569-
};
570-
Ok(route)
569+
<Self as RouteProvider<T::AssetId>>::get_route(AssetPair::new(asset_in, asset_out))
570+
}
571571
}
572572

573573
fn validate_route(route: &Route<T::AssetId>) -> Result<(T::Balance, T::Balance), DispatchError> {

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 = "417.0.0"
3+
version = "418.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
@@ -128,7 +128,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
128128
spec_name: Cow::Borrowed("hydradx"),
129129
impl_name: Cow::Borrowed("hydradx"),
130130
authoring_version: 1,
131-
spec_version: 417,
131+
spec_version: 418,
132132
impl_version: 0,
133133
apis: RUNTIME_API_VERSIONS,
134134
transaction_version: 1,

0 commit comments

Comments
 (0)