From ba9dd09007caff01d9765f80e84ddf10ef7b7068 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 18 Jun 2024 10:54:43 +0200 Subject: [PATCH 01/28] validate new route has oracle price data --- integration-tests/src/router.rs | 285 +++++++++++++++++++++--------- pallets/route-executor/src/lib.rs | 30 ++-- runtime/hydradx/src/assets.rs | 1 + 3 files changed, 215 insertions(+), 101 deletions(-) diff --git a/integration-tests/src/router.rs b/integration-tests/src/router.rs index 4c9c11b05..89b66d991 100644 --- a/integration-tests/src/router.rs +++ b/integration-tests/src/router.rs @@ -2951,8 +2951,11 @@ mod set_route { use sp_runtime::TransactionOutcome; mod when_prestored_route_is_invalid { - use super::*; + use frame_support::assert_ok; + use hydradx_runtime::EmaOracle; + use hydradx_traits::AssetKind; + use primitives::constants::chain::XYK_SOURCE; #[test] fn set_route_should_work_with_all_pools_involved() { @@ -2963,8 +2966,8 @@ mod set_route { let _ = with_transaction(|| { //Arrange let (pool_id, stable_asset_1, _) = init_stableswap_with_details( - 1_000_000_000_000_000_000u128, - 300_000_000_000_000_000u128, + 1_000_000_000_000_000u128, + 300_000_000_000_000u128, 18, ) .unwrap(); @@ -2975,7 +2978,7 @@ mod set_route { hydradx_runtime::RuntimeOrigin::root(), Omnipool::protocol_account(), pool_id, - 60000 * UNITS as i128, + 1000 * UNITS as i128, )); assert_ok!(hydradx_runtime::Omnipool::add_token( @@ -2986,11 +2989,8 @@ mod set_route { AccountId::from(BOB), )); - create_xyk_pool_with_amounts(DOT, 1000000 * UNITS, stable_asset_1, 20000 * UNITS); - - create_lbp_pool_with_amounts(DOT, 1000000 * UNITS, stable_asset_1, 20000 * UNITS); - //Start lbp campaign - set_relaychain_block_number(LBP_SALE_START + 15); + create_xyk_pool_with_amounts(DOT, 1000 * UNITS, stable_asset_1, 2000 * UNITS); + create_xyk_pool_with_amounts(HDX, 10000000 * UNITS, DOT, 10000 * UNITS); let route1 = vec![ Trade { @@ -3012,18 +3012,8 @@ mod set_route { let route2_cheaper = vec![ Trade { - pool: PoolType::Omnipool, + pool: PoolType::XYK, asset_in: HDX, - asset_out: pool_id, - }, - Trade { - pool: PoolType::Stableswap(pool_id), - asset_in: pool_id, - asset_out: stable_asset_1, - }, - Trade { - pool: PoolType::LBP, - asset_in: stable_asset_1, asset_out: DOT, }, ]; @@ -3031,7 +3021,7 @@ mod set_route { let asset_pair = Pair::new(HDX, DOT); //Verify if the cheaper route is indeed cheaper in both ways - let amount_to_sell = 100 * UNITS; + let amount_to_sell = 1 * UNITS; //Check for normal route let dot_amount_out = with_transaction::<_, _, _>(|| { @@ -3107,6 +3097,7 @@ mod set_route { assert!(amount_out_for_inverse_with_chaper_route > amount_out_for_inverse); //ACT AND ASSERT + populate_oracle(HDX, DOT, route1.clone(), Some(10), None); //We set first the more expensive route assert_ok!(Router::set_route( @@ -3116,7 +3107,9 @@ mod set_route { )); assert_eq!(Router::route(asset_pair).unwrap(), route1); - //We set the cheaper one so it should replace + + //We set the cheaper one so it should replace existing one + populate_oracle(HDX, DOT, route2_cheaper.clone(), Some(11), None); assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, @@ -3211,7 +3204,7 @@ mod set_route { let asset_pair = Pair::new(HDX, BTC); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, asset_in: HDX, @@ -3224,11 +3217,14 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route.clone(), None, None); + + //Act and assert assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, - route2 + route ),); }); } @@ -3272,6 +3268,9 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route1.clone(), None, None); + + let asset_pair = Pair::new(HDX, BTC); assert_ok!(Router::set_route( @@ -3286,7 +3285,7 @@ mod set_route { Tradability::FROZEN )); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::XYK, asset_in: HDX, @@ -3299,11 +3298,13 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route.clone(), Some(11), None); + //Act and assert assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, - route2 + route ),); }); } @@ -3335,7 +3336,7 @@ mod set_route { let asset_pair = Pair::new(HDX, BTC); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, asset_in: HDX, @@ -3348,6 +3349,8 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route.clone(), None, None); + assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state( hydradx_runtime::RuntimeOrigin::root(), DOT, @@ -3356,7 +3359,7 @@ mod set_route { //Act and assert assert_noop!( - Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route2), + Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route), pallet_omnipool::Error::::NotAllowed ); }); @@ -3389,7 +3392,7 @@ mod set_route { let asset_pair = Pair::new(HDX, BTC); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, asset_in: HDX, @@ -3402,6 +3405,9 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route.clone(), None, None); + + assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state( hydradx_runtime::RuntimeOrigin::root(), DOT, @@ -3410,7 +3416,7 @@ mod set_route { //Act and assert assert_noop!( - Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route2), + Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route), pallet_omnipool::Error::::NotAllowed ); }); @@ -3441,12 +3447,12 @@ mod set_route { create_xyk_pool_with_amounts(DOT, 10 * UNITS, BTC, 1000000 * UNITS); - let asset_pair = Pair::new(HDX, BTC); + let asset_pair = Pair::new(DAI, BTC); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, - asset_in: HDX, + asset_in: DAI, asset_out: DOT, }, Trade { @@ -3456,11 +3462,23 @@ mod set_route { }, ]; + + //To prevent ED error + assert_ok!(hydradx_runtime::Tokens::set_balance( + RawOrigin::Root.into(), + DAVE.into(), + BTC, + 1 * UNITS, + 0, + )); + + populate_oracle(DAI, BTC, route.clone(), None, Some(90 * UNITS)); + //Act and assert assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, - route2 + route ),); }); } @@ -3492,7 +3510,7 @@ mod set_route { let asset_pair = Pair::new(HDX, BTC); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, asset_in: HDX, @@ -3505,11 +3523,14 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route.clone(), None, None); + + //Act and assert assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, - route2 + route ),); }); } @@ -3536,6 +3557,8 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route1.clone(), None, None); + let asset_pair = Pair::new(HDX, BTC); assert_ok!(Router::set_route( @@ -3545,6 +3568,101 @@ mod set_route { )); }); } + + #[test] + fn set_route_should_not_work_when_route_has_insufficient_asset_without_oracle() { + { + TestNet::reset(); + + Hydra::execute_with(|| { + let _ = with_transaction(|| { + let name = b"INSUF1".to_vec(); + let insufficient_asset = AssetRegistry::register_insufficient_asset( + None, + Some(name.try_into().unwrap()), + AssetKind::External, + Some(1_000), + None, + None, + None, + None, + ) + .unwrap(); + + let route1 = vec![Trade { + pool: PoolType::XYK, + asset_in: DOT, + asset_out: insufficient_asset, + }]; + + create_xyk_pool_with_amounts(DOT, 10000 * UNITS, insufficient_asset, 10000 * UNITS); + + //Act + assert_noop!( + Router::set_route( + hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), + Pair::new(DOT, insufficient_asset), + route1.clone() + ), + pallet_route_executor::Error::::InvalidRoute + ); + + TransactionOutcome::Commit(DispatchResult::Ok(())) + }); + }); + } + } + + #[test] + fn set_route_should_work_when_route_has_insufficient_asset_with_oracle() { + { + TestNet::reset(); + + Hydra::execute_with(|| { + let _ = with_transaction(|| { + let name = b"INSUF1".to_vec(); + let insufficient_asset = AssetRegistry::register_insufficient_asset( + None, + Some(name.try_into().unwrap()), + AssetKind::External, + Some(1_000), + None, + None, + None, + None, + ) + .unwrap(); + + let route1 = vec![Trade { + pool: PoolType::XYK, + asset_in: DOT, + asset_out: insufficient_asset, + }]; + + create_xyk_pool_with_amounts(DOT, 10000 * UNITS, insufficient_asset, 10000 * UNITS); + + //Whitelist insufficient asset in oracle + EmaOracle::add_oracle( + hydradx_runtime::RuntimeOrigin::root(), + XYK_SOURCE, + (DOT, insufficient_asset), + ) + .unwrap(); + + populate_oracle(DOT, insufficient_asset, route1.clone(), None, Some(10 * UNITS)); + + //Act + assert_ok!(Router::set_route( + hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), + Pair::new(DOT, insufficient_asset), + route1.clone() + ),); + + TransactionOutcome::Commit(DispatchResult::Ok(())) + }); + }); + } + } } mod when_prestored_route_is_valid { @@ -3588,6 +3706,8 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route1.clone(), None, None); + let asset_pair = Pair::new(HDX, BTC); //Act and assert @@ -3661,6 +3781,8 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, route1.clone(), None, None); + let asset_pair = Pair::new(HDX, BTC); assert_ok!(Router::set_route( @@ -3743,6 +3865,8 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, prestored_route.clone(), None, None); + assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, @@ -3817,6 +3941,9 @@ mod set_route { }, ]; + populate_oracle(HDX, BTC, prestored_route.clone(), None, None); + + assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, @@ -3893,7 +4020,7 @@ mod set_route { let asset_pair = Pair::new(BTC, ETH); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, asset_in: BTC, @@ -3906,9 +4033,11 @@ mod set_route { }, ]; + populate_oracle(BTC, ETH, route.clone(), None, Some(UNITS / 1000000)); + //Validation is fine so no AMM error, but since the route is not better, it results in unsuccessfull route setting assert_noop!( - Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route2), + Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route), pallet_route_executor::Error::::RouteUpdateIsNotSuccessful ); }); @@ -3957,7 +4086,7 @@ mod set_route { let asset_pair = Pair::new(BTC, ETH); - let route2 = vec![ + let route = vec![ Trade { pool: PoolType::Omnipool, asset_in: BTC, @@ -3970,9 +4099,11 @@ mod set_route { }, ]; + populate_oracle(BTC, ETH, route.clone(), None, Some(UNITS / 100)); + //Validation is fine, but since the route is not better, it results in unsuccessfull route setting assert_noop!( - Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route2), + Router::set_route(hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, route), pallet_route_executor::Error::::RouteUpdateIsNotSuccessful ); }); @@ -4007,6 +4138,8 @@ mod set_route { asset_out: DOT, }]; + populate_oracle(HDX, DOT, route1.clone(), None, None); + let asset_pair = Pair::new(HDX, DOT); assert_noop!( @@ -4019,6 +4152,7 @@ mod set_route { } mod with_on_chain_and_default_route { + use frame_support::assert_ok; use super::*; #[test] @@ -4067,6 +4201,9 @@ mod with_on_chain_and_default_route { }, ]; + populate_oracle(HDX, DOT, route1.clone(), None, None); + + let asset_pair = Pair::new(HDX, DOT); let amount_to_buy = 100 * UNITS; @@ -4147,6 +4284,8 @@ mod with_on_chain_and_default_route { }, ]; + populate_oracle(HDX, DOT, route1.clone(), None, None); + let asset_pair = Pair::new(HDX, DOT); let amount_to_sell = 100 * UNITS; @@ -4220,6 +4359,8 @@ mod with_on_chain_and_default_route { }, ]; + populate_oracle(HDX, DOT, route1.clone(), None, None); + let asset_pair = Pair::new(HDX, DOT); let amount_to_sell = 100 * UNITS; @@ -4680,51 +4821,6 @@ fn create_lbp_pool(accumulated_asset: u32, distributed_asset: u32) { )); } -fn create_lbp_pool_with_amounts(accumulated_asset: u32, amount_a: u128, distributed_asset: u32, amount_b: u128) { - assert_ok!(Currencies::update_balance( - hydradx_runtime::RuntimeOrigin::root(), - DAVE.into(), - accumulated_asset, - amount_a as i128, - )); - assert_ok!(Currencies::update_balance( - hydradx_runtime::RuntimeOrigin::root(), - DAVE.into(), - distributed_asset, - amount_b as i128, - )); - - assert_ok!(LBP::create_pool( - RuntimeOrigin::root(), - DAVE.into(), - accumulated_asset, - amount_a, - distributed_asset, - amount_b, - 20_000_000, - 80_000_000, - WeightCurveType::Linear, - (2, 1_000), - CHARLIE.into(), - 0, - )); - - let account_id = get_lbp_pair_account_id(accumulated_asset, distributed_asset); - - assert_ok!(LBP::update_pool_data( - RuntimeOrigin::signed(DAVE.into()), - account_id, - None, - Some(LBP_SALE_START), - Some(LBP_SALE_END), - None, - None, - None, - None, - None, - )); -} - fn get_lbp_pair_account_id(asset_a: AssetId, asset_b: AssetId) -> AccountId { let asset_pair = pallet_lbp::AssetPair { asset_in: asset_a, @@ -4846,3 +4942,22 @@ pub fn init_stableswap_with_details( Ok((pool_id, asset_in, asset_out)) } + +fn populate_oracle(asset_in: AssetId, asset_out: AssetId, route: Vec>, block: Option, amount: Option) { + assert_ok!(hydradx_runtime::Tokens::set_balance( + RawOrigin::Root.into(), + DAVE.into(), + asset_in, + amount.unwrap_or(100 * UNITS), + 0, + )); + assert_ok!(Router::sell( + hydradx_runtime::RuntimeOrigin::signed(DAVE.into()), + asset_in, + asset_out, + amount.unwrap_or(1 * UNITS), + 0, + route.clone() + )); + set_relaychain_block_number(block.unwrap_or(10)); +} diff --git a/pallets/route-executor/src/lib.rs b/pallets/route-executor/src/lib.rs index e8c388e6a..3eb1e0811 100644 --- a/pallets/route-executor/src/lib.rs +++ b/pallets/route-executor/src/lib.rs @@ -60,6 +60,8 @@ pub mod pallet { use hydradx_traits::router::{ExecutorError, RefundEdCalculator}; use sp_runtime::traits::{AtLeast32BitUnsigned, CheckedDiv, Zero}; use sp_runtime::Saturating; + use hydra_dx_math::ema::EmaPrice; + use hydradx_traits::{OraclePeriod, PriceOracle}; #[pallet::pallet] pub struct Pallet(_); @@ -104,8 +106,12 @@ pub mod pallet { Error = DispatchError, >; + ///Calculate ED for tolerating currency balance difference type EdToRefundCalculator: RefundEdCalculator; + ///Oracle price provider to validate if new route has oracle price data + type OraclePriceProvider: PriceOracle; + /// Pool type used in the default route type DefaultRoutePoolType: Get>; @@ -365,7 +371,7 @@ pub mod pallet { let _ = ensure_signed(origin.clone())?; Self::ensure_route_size(new_route.len())?; Self::ensure_route_arguments(&asset_pair, &new_route)?; - Self::ensure_route_has_no_insufficient_asset(&new_route)?; + T::OraclePriceProvider::price(&new_route, OraclePeriod::TenMinutes).ok_or(Error::::InvalidRoute)?; if !asset_pair.is_ordered() { asset_pair = asset_pair.ordered_pair(); @@ -492,22 +498,7 @@ impl Pallet { Ok(()) } - fn ensure_route_has_no_insufficient_asset(new_route: &[Trade]) -> DispatchResult { - let mut unique_assets = sp_std::collections::btree_set::BTreeSet::new(); - for trade in new_route.iter() { - unique_assets.insert(trade.asset_in); - unique_assets.insert(trade.asset_out); - } - for asset in unique_assets.iter() { - ensure!( - T::InspectRegistry::is_sufficient(*asset), - Error::::InsufficientAssetNotSupported - ); - } - - Ok(()) - } fn ensure_that_user_received_asset_out_at_most( who: T::AccountId, @@ -626,6 +617,13 @@ impl Pallet { let Ok(who) = ensure_signed(origin.clone()) else { return TransactionOutcome::Rollback(Err(Error::::InvalidRoute.into())); }; + //NOTE: This is necessary so router's account can pay ED for insufficient assets in the + //route. Value is 10K to make sure we can pay ED for really long routes. + let _ = T::Currency::mint_into( + T::NativeAssetId::get(), + &Self::router_account(), + 10_000_000_000_000_000_u128.into(), + ); let _ = T::Currency::mint_into(asset_in, &Self::router_account(), amount_in); let sell_result = Self::sell(origin, asset_in, asset_out, amount_in, u128::MIN.into(), route.clone()); diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index f921580b2..55aaf9a4e 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1058,6 +1058,7 @@ impl pallet_route_executor::Config for Runtime { type InspectRegistry = AssetRegistry; type TechnicalOrigin = SuperMajorityTechCommittee; type EdToRefundCalculator = RefundAndLockedEdCalculator; + type OraclePriceProvider = OraclePriceProvider; } parameter_types! { From cc3fad854b9401ad876633ad651b58bc37b99ad4 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 18 Jun 2024 12:35:00 +0200 Subject: [PATCH 02/28] fix tests --- integration-tests/src/router.rs | 2 +- pallets/dca/src/tests/mock.rs | 27 ++++++++++++------------ pallets/route-executor/src/lib.rs | 4 ++-- pallets/route-executor/src/tests/mock.rs | 19 ++++++++++++++++- runtime/adapters/src/tests/mock.rs | 23 +++++++++++++++----- runtime/hydradx/src/assets.rs | 4 +++- 6 files changed, 56 insertions(+), 23 deletions(-) diff --git a/integration-tests/src/router.rs b/integration-tests/src/router.rs index 89b66d991..b7ce1ae31 100644 --- a/integration-tests/src/router.rs +++ b/integration-tests/src/router.rs @@ -3604,7 +3604,7 @@ mod set_route { Pair::new(DOT, insufficient_asset), route1.clone() ), - pallet_route_executor::Error::::InvalidRoute + pallet_route_executor::Error::::InsufficientAssetNotSupported ); TransactionOutcome::Commit(DispatchResult::Ok(())) diff --git a/pallets/dca/src/tests/mock.rs b/pallets/dca/src/tests/mock.rs index 72d992889..ad8c0c361 100644 --- a/pallets/dca/src/tests/mock.rs +++ b/pallets/dca/src/tests/mock.rs @@ -365,6 +365,7 @@ impl pallet_route_executor::Config for Test { type WeightInfo = (); type TechnicalOrigin = EnsureRoot; type EdToRefundCalculator = MockedEdCalculator; + type OraclePriceProvider = PriceProviderMock; } pub struct MockedEdCalculator; @@ -375,6 +376,19 @@ impl RefundEdCalculator for MockedEdCalculator { } } +pub struct PriceProviderMock {} + +impl PriceOracle for crate::tests::mock::PriceProviderMock { + type Price = Ratio; + + fn price(_: &[Trade], period: OraclePeriod) -> Option { + if period == OraclePeriod::Short { + return Some(Ratio::new(80, 100)); + } + Some(Ratio::new(88, 100)) + } +} + type OriginForRuntime = OriginFor; pub const INVALID_CALCULATION_AMOUNT: Balance = 999; pub const CALCULATED_AMOUNT_IN_FOR_OMNIPOOL_BUY: Balance = 10 * ONE; @@ -624,19 +638,6 @@ impl TradeExecution for Xyk { } } -pub struct PriceProviderMock {} - -impl PriceOracle for PriceProviderMock { - type Price = Ratio; - - fn price(_: &[Trade], period: OraclePeriod) -> Option { - if period == OraclePeriod::Short { - return Some(Ratio::new(80, 100)); - } - Some(Ratio::new(88, 100)) - } -} - parameter_types! { pub NativeCurrencyId: AssetId = HDX; pub MinBudgetInNativeCurrency: Balance= MIN_BUDGET.with(|v| *v.borrow()); diff --git a/pallets/route-executor/src/lib.rs b/pallets/route-executor/src/lib.rs index 3eb1e0811..c54bd06b5 100644 --- a/pallets/route-executor/src/lib.rs +++ b/pallets/route-executor/src/lib.rs @@ -152,7 +152,7 @@ pub mod pallet { InvalidRoute, ///The route update was not successful RouteUpdateIsNotSuccessful, - ///Insufficient asset is not supported for on chain routing + ///Insufficient asset without oracle is not supported for on chain routing InsufficientAssetNotSupported, ///The route execution failed in the underlying AMM InvalidRouteExecution, @@ -371,7 +371,7 @@ pub mod pallet { let _ = ensure_signed(origin.clone())?; Self::ensure_route_size(new_route.len())?; Self::ensure_route_arguments(&asset_pair, &new_route)?; - T::OraclePriceProvider::price(&new_route, OraclePeriod::TenMinutes).ok_or(Error::::InvalidRoute)?; + T::OraclePriceProvider::price(&new_route, OraclePeriod::TenMinutes).ok_or(Error::::InsufficientAssetNotSupported)?; if !asset_pair.is_ordered() { asset_pair = asset_pair.ordered_pair(); diff --git a/pallets/route-executor/src/tests/mock.rs b/pallets/route-executor/src/tests/mock.rs index 042303d95..11e311484 100644 --- a/pallets/route-executor/src/tests/mock.rs +++ b/pallets/route-executor/src/tests/mock.rs @@ -35,6 +35,7 @@ use sp_runtime::{ }; use std::cell::RefCell; use std::ops::Deref; +use hydra_dx_math::ratio::Ratio; type Block = frame_system::mocking::MockBlock; @@ -150,6 +151,7 @@ impl Config for Test { type InspectRegistry = MockedAssetRegistry; type AMM = Pools; type EdToRefundCalculator = MockedEdCalculator; + type OraclePriceProvider = PriceProviderMock; type DefaultRoutePoolType = DefaultRoutePoolType; type TechnicalOrigin = EnsureRoot; type WeightInfo = (); @@ -163,7 +165,22 @@ impl RefundEdCalculator for MockedEdCalculator { } } -use hydradx_traits::AssetKind; +pub struct PriceProviderMock {} + +impl PriceOracle for PriceProviderMock { + type Price = Ratio; + + fn price(route: &[Trade], _: OraclePeriod) -> Option { + let has_insufficient_asset = route.iter().any(|t| t.asset_in > 2000 || t.asset_out > 2000); + if has_insufficient_asset { + return None; + } + Some(Ratio::new(88, 100)) + } +} + + +use hydradx_traits::{AssetKind, OraclePeriod, PriceOracle}; pub struct MockedAssetRegistry; impl hydradx_traits::registry::Inspect for MockedAssetRegistry { diff --git a/runtime/adapters/src/tests/mock.rs b/runtime/adapters/src/tests/mock.rs index 3233bcaba..a33ea8551 100644 --- a/runtime/adapters/src/tests/mock.rs +++ b/runtime/adapters/src/tests/mock.rs @@ -31,11 +31,8 @@ use hydra_dx_math::ema::EmaPrice; use hydra_dx_math::support::rational::Rounding; use hydra_dx_math::to_u128_wrapper; use hydradx_traits::pools::DustRemovalAccountWhitelist; -use hydradx_traits::router::RefundEdCalculator; -use hydradx_traits::{ - router::PoolType, AssetKind, AssetPairAccountIdFor, CanCreatePool, Create as CreateRegistry, - Inspect as InspectRegistry, -}; +use hydradx_traits::router::{RefundEdCalculator, Trade}; +use hydradx_traits::{router::PoolType, AssetKind, AssetPairAccountIdFor, CanCreatePool, Create as CreateRegistry, Inspect as InspectRegistry, PriceOracle, OraclePeriod}; use orml_traits::{parameter_type_with_key, GetByKey}; use pallet_currencies::fungibles::FungibleCurrencies; use pallet_currencies::BasicCurrencyAdapter; @@ -52,6 +49,7 @@ use sp_runtime::{ use sp_runtime::{BoundedVec, Permill}; use std::cell::RefCell; use std::collections::HashMap; +use hydra_dx_math::ratio::Ratio; type Block = frame_system::mocking::MockBlock; @@ -328,6 +326,7 @@ impl pallet_route_executor::Config for Test { type InspectRegistry = DummyRegistry; type AMM = Pools; type EdToRefundCalculator = MockedEdCalculator; + type OraclePriceProvider = PriceProviderMock; type DefaultRoutePoolType = DefaultRoutePoolType; type TechnicalOrigin = EnsureRoot; type WeightInfo = (); @@ -341,6 +340,20 @@ impl RefundEdCalculator for MockedEdCalculator { } } +pub struct PriceProviderMock {} + +impl PriceOracle for PriceProviderMock { + type Price = Ratio; + + fn price(_: &[Trade], period: OraclePeriod) -> Option { + if period == OraclePeriod::Short { + return Some(Ratio::new(80, 100)); + } + Some(Ratio::new(88, 100)) + } +} + + pub struct ExtBuilder { endowed_accounts: Vec<(u64, AssetId, Balance)>, registered_assets: Vec, diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 55aaf9a4e..02ea61aa9 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1058,8 +1058,10 @@ impl pallet_route_executor::Config for Runtime { type InspectRegistry = AssetRegistry; type TechnicalOrigin = SuperMajorityTechCommittee; type EdToRefundCalculator = RefundAndLockedEdCalculator; + #[cfg(not(feature = "runtime-benchmarks"))] type OraclePriceProvider = OraclePriceProvider; -} + #[cfg(feature = "runtime-benchmarks")] + type OraclePriceProvider = DummyOraclePriceProvider;} parameter_types! { pub const ExistentialDepositMultiplier: u8 = 5; From caeb6b6872e09f87c989ba24c7543767e82ef0d4 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 18 Jun 2024 12:37:22 +0200 Subject: [PATCH 03/28] renaming --- integration-tests/src/router.rs | 2 +- pallets/route-executor/src/lib.rs | 6 +++--- pallets/route-executor/src/tests/set_route.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/integration-tests/src/router.rs b/integration-tests/src/router.rs index b7ce1ae31..890e408b9 100644 --- a/integration-tests/src/router.rs +++ b/integration-tests/src/router.rs @@ -3604,7 +3604,7 @@ mod set_route { Pair::new(DOT, insufficient_asset), route1.clone() ), - pallet_route_executor::Error::::InsufficientAssetNotSupported + pallet_route_executor::Error::::RouteHasNoOracle ); TransactionOutcome::Commit(DispatchResult::Ok(())) diff --git a/pallets/route-executor/src/lib.rs b/pallets/route-executor/src/lib.rs index c54bd06b5..6df70763d 100644 --- a/pallets/route-executor/src/lib.rs +++ b/pallets/route-executor/src/lib.rs @@ -152,8 +152,8 @@ pub mod pallet { InvalidRoute, ///The route update was not successful RouteUpdateIsNotSuccessful, - ///Insufficient asset without oracle is not supported for on chain routing - InsufficientAssetNotSupported, + ///Route contains assets that has no oracle data + RouteHasNoOracle, ///The route execution failed in the underlying AMM InvalidRouteExecution, /// Trading same assets is not allowed. @@ -371,7 +371,7 @@ pub mod pallet { let _ = ensure_signed(origin.clone())?; Self::ensure_route_size(new_route.len())?; Self::ensure_route_arguments(&asset_pair, &new_route)?; - T::OraclePriceProvider::price(&new_route, OraclePeriod::TenMinutes).ok_or(Error::::InsufficientAssetNotSupported)?; + T::OraclePriceProvider::price(&new_route, OraclePeriod::TenMinutes).ok_or(Error::::RouteHasNoOracle)?; if !asset_pair.is_ordered() { asset_pair = asset_pair.ordered_pair(); diff --git a/pallets/route-executor/src/tests/set_route.rs b/pallets/route-executor/src/tests/set_route.rs index 3e7cc8e69..ab0b9e485 100644 --- a/pallets/route-executor/src/tests/set_route.rs +++ b/pallets/route-executor/src/tests/set_route.rs @@ -450,7 +450,7 @@ fn set_route_should_fail_with_insufficient_asset() { //Act assert_noop!( Router::set_route(RuntimeOrigin::signed(ALICE), asset_pair, route), - Error::::InsufficientAssetNotSupported + Error::::RouteHasNoOracle ); }); } @@ -481,7 +481,7 @@ fn set_route_should_fail_with_insufficient_asset_as_intermediare() { //Act assert_noop!( Router::set_route(RuntimeOrigin::signed(ALICE), asset_pair, route), - Error::::InsufficientAssetNotSupported + Error::::RouteHasNoOracle ); }); } From 479d5696eef61e8872bc1f07cafff0bc4bb63998 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 18 Jun 2024 13:19:36 +0200 Subject: [PATCH 04/28] formatting --- integration-tests/src/router.rs | 77 +++++++++++------------- pallets/route-executor/src/lib.rs | 6 +- pallets/route-executor/src/tests/mock.rs | 3 +- runtime/adapters/src/tests/mock.rs | 8 ++- runtime/hydradx/src/assets.rs | 3 +- 5 files changed, 45 insertions(+), 52 deletions(-) diff --git a/integration-tests/src/router.rs b/integration-tests/src/router.rs index 890e408b9..7c11f95eb 100644 --- a/integration-tests/src/router.rs +++ b/integration-tests/src/router.rs @@ -2965,12 +2965,9 @@ mod set_route { Hydra::execute_with(|| { let _ = with_transaction(|| { //Arrange - let (pool_id, stable_asset_1, _) = init_stableswap_with_details( - 1_000_000_000_000_000u128, - 300_000_000_000_000u128, - 18, - ) - .unwrap(); + let (pool_id, stable_asset_1, _) = + init_stableswap_with_details(1_000_000_000_000_000u128, 300_000_000_000_000u128, 18) + .unwrap(); init_omnipool(); @@ -3010,13 +3007,11 @@ mod set_route { }, ]; - let route2_cheaper = vec![ - Trade { - pool: PoolType::XYK, - asset_in: HDX, - asset_out: DOT, - }, - ]; + let route2_cheaper = vec![Trade { + pool: PoolType::XYK, + asset_in: HDX, + asset_out: DOT, + }]; let asset_pair = Pair::new(HDX, DOT); @@ -3107,7 +3102,6 @@ mod set_route { )); assert_eq!(Router::route(asset_pair).unwrap(), route1); - //We set the cheaper one so it should replace existing one populate_oracle(HDX, DOT, route2_cheaper.clone(), Some(11), None); assert_ok!(Router::set_route( @@ -3219,7 +3213,6 @@ mod set_route { populate_oracle(HDX, BTC, route.clone(), None, None); - //Act and assert assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), @@ -3270,7 +3263,6 @@ mod set_route { populate_oracle(HDX, BTC, route1.clone(), None, None); - let asset_pair = Pair::new(HDX, BTC); assert_ok!(Router::set_route( @@ -3407,7 +3399,6 @@ mod set_route { populate_oracle(HDX, BTC, route.clone(), None, None); - assert_ok!(hydradx_runtime::Omnipool::set_asset_tradable_state( hydradx_runtime::RuntimeOrigin::root(), DOT, @@ -3462,15 +3453,14 @@ mod set_route { }, ]; - //To prevent ED error assert_ok!(hydradx_runtime::Tokens::set_balance( - RawOrigin::Root.into(), - DAVE.into(), - BTC, - 1 * UNITS, - 0, - )); + RawOrigin::Root.into(), + DAVE.into(), + BTC, + 1 * UNITS, + 0, + )); populate_oracle(DAI, BTC, route.clone(), None, Some(90 * UNITS)); @@ -3525,7 +3515,6 @@ mod set_route { populate_oracle(HDX, BTC, route.clone(), None, None); - //Act and assert assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), @@ -3943,7 +3932,6 @@ mod set_route { populate_oracle(HDX, BTC, prestored_route.clone(), None, None); - assert_ok!(Router::set_route( hydradx_runtime::RuntimeOrigin::signed(ALICE.into()), asset_pair, @@ -4152,8 +4140,8 @@ mod set_route { } mod with_on_chain_and_default_route { - use frame_support::assert_ok; use super::*; + use frame_support::assert_ok; #[test] fn buy_should_work_with_onchain_route() { @@ -4203,7 +4191,6 @@ mod with_on_chain_and_default_route { populate_oracle(HDX, DOT, route1.clone(), None, None); - let asset_pair = Pair::new(HDX, DOT); let amount_to_buy = 100 * UNITS; @@ -4943,21 +4930,27 @@ pub fn init_stableswap_with_details( Ok((pool_id, asset_in, asset_out)) } -fn populate_oracle(asset_in: AssetId, asset_out: AssetId, route: Vec>, block: Option, amount: Option) { +fn populate_oracle( + asset_in: AssetId, + asset_out: AssetId, + route: Vec>, + block: Option, + amount: Option, +) { assert_ok!(hydradx_runtime::Tokens::set_balance( - RawOrigin::Root.into(), - DAVE.into(), - asset_in, - amount.unwrap_or(100 * UNITS), - 0, - )); + RawOrigin::Root.into(), + DAVE.into(), + asset_in, + amount.unwrap_or(100 * UNITS), + 0, + )); assert_ok!(Router::sell( - hydradx_runtime::RuntimeOrigin::signed(DAVE.into()), - asset_in, - asset_out, - amount.unwrap_or(1 * UNITS), - 0, - route.clone() - )); + hydradx_runtime::RuntimeOrigin::signed(DAVE.into()), + asset_in, + asset_out, + amount.unwrap_or(1 * UNITS), + 0, + route.clone() + )); set_relaychain_block_number(block.unwrap_or(10)); } diff --git a/pallets/route-executor/src/lib.rs b/pallets/route-executor/src/lib.rs index 6df70763d..2dd3a4f98 100644 --- a/pallets/route-executor/src/lib.rs +++ b/pallets/route-executor/src/lib.rs @@ -57,11 +57,11 @@ pub mod pallet { use super::*; use frame_support::traits::fungibles::Mutate; use frame_system::pallet_prelude::OriginFor; + use hydra_dx_math::ema::EmaPrice; use hydradx_traits::router::{ExecutorError, RefundEdCalculator}; + use hydradx_traits::{OraclePeriod, PriceOracle}; use sp_runtime::traits::{AtLeast32BitUnsigned, CheckedDiv, Zero}; use sp_runtime::Saturating; - use hydra_dx_math::ema::EmaPrice; - use hydradx_traits::{OraclePeriod, PriceOracle}; #[pallet::pallet] pub struct Pallet(_); @@ -498,8 +498,6 @@ impl Pallet { Ok(()) } - - fn ensure_that_user_received_asset_out_at_most( who: T::AccountId, asset_in: T::AssetId, diff --git a/pallets/route-executor/src/tests/mock.rs b/pallets/route-executor/src/tests/mock.rs index 11e311484..b662d59b3 100644 --- a/pallets/route-executor/src/tests/mock.rs +++ b/pallets/route-executor/src/tests/mock.rs @@ -23,6 +23,7 @@ use frame_support::{ }; use frame_system::EnsureRoot; use frame_system::{ensure_signed, pallet_prelude::OriginFor}; +use hydra_dx_math::ratio::Ratio; use hydradx_traits::router::{ExecutorError, PoolType, RefundEdCalculator, TradeExecution}; use orml_traits::parameter_type_with_key; use pallet_currencies::{fungibles::FungibleCurrencies, BasicCurrencyAdapter}; @@ -35,7 +36,6 @@ use sp_runtime::{ }; use std::cell::RefCell; use std::ops::Deref; -use hydra_dx_math::ratio::Ratio; type Block = frame_system::mocking::MockBlock; @@ -179,7 +179,6 @@ impl PriceOracle for PriceProviderMock { } } - use hydradx_traits::{AssetKind, OraclePeriod, PriceOracle}; pub struct MockedAssetRegistry; diff --git a/runtime/adapters/src/tests/mock.rs b/runtime/adapters/src/tests/mock.rs index a33ea8551..4aa1acff8 100644 --- a/runtime/adapters/src/tests/mock.rs +++ b/runtime/adapters/src/tests/mock.rs @@ -28,11 +28,15 @@ use frame_support::{ }; use frame_system::EnsureRoot; use hydra_dx_math::ema::EmaPrice; +use hydra_dx_math::ratio::Ratio; use hydra_dx_math::support::rational::Rounding; use hydra_dx_math::to_u128_wrapper; use hydradx_traits::pools::DustRemovalAccountWhitelist; use hydradx_traits::router::{RefundEdCalculator, Trade}; -use hydradx_traits::{router::PoolType, AssetKind, AssetPairAccountIdFor, CanCreatePool, Create as CreateRegistry, Inspect as InspectRegistry, PriceOracle, OraclePeriod}; +use hydradx_traits::{ + router::PoolType, AssetKind, AssetPairAccountIdFor, CanCreatePool, Create as CreateRegistry, + Inspect as InspectRegistry, OraclePeriod, PriceOracle, +}; use orml_traits::{parameter_type_with_key, GetByKey}; use pallet_currencies::fungibles::FungibleCurrencies; use pallet_currencies::BasicCurrencyAdapter; @@ -49,7 +53,6 @@ use sp_runtime::{ use sp_runtime::{BoundedVec, Permill}; use std::cell::RefCell; use std::collections::HashMap; -use hydra_dx_math::ratio::Ratio; type Block = frame_system::mocking::MockBlock; @@ -353,7 +356,6 @@ impl PriceOracle for PriceProviderMock { } } - pub struct ExtBuilder { endowed_accounts: Vec<(u64, AssetId, Balance)>, registered_assets: Vec, diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 02ea61aa9..e9f785eda 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1061,7 +1061,8 @@ impl pallet_route_executor::Config for Runtime { #[cfg(not(feature = "runtime-benchmarks"))] type OraclePriceProvider = OraclePriceProvider; #[cfg(feature = "runtime-benchmarks")] - type OraclePriceProvider = DummyOraclePriceProvider;} + type OraclePriceProvider = DummyOraclePriceProvider; +} parameter_types! { pub const ExistentialDepositMultiplier: u8 = 5; From 911c713a7c711647870113bb67d802767da53ed3 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 18 Jun 2024 13:38:38 +0200 Subject: [PATCH 05/28] regnerate router weights --- runtime/hydradx/src/weights/route_executor.rs | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/runtime/hydradx/src/weights/route_executor.rs b/runtime/hydradx/src/weights/route_executor.rs index 10f017e21..769c3f777 100644 --- a/runtime/hydradx/src/weights/route_executor.rs +++ b/runtime/hydradx/src/weights/route_executor.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for `pallet_route_executor` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2024-03-18, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-06-18, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `bench-bot`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024 @@ -54,23 +54,23 @@ impl pallet_route_executor::weights::WeightInfo for Hyd /// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(108), added: 2583, mode: `MaxEncodedLen`) /// Storage: `System::Account` (r:3 w:1) /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + /// Storage: `AssetRegistry::Assets` (r:2 w:0) + /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// Storage: `Tokens::Locks` (r:1 w:1) /// Proof: `Tokens::Locks` (`max_values`: None, `max_size`: Some(1261), added: 3736, mode: `MaxEncodedLen`) /// Storage: `Duster::AccountBlacklist` (r:2 w:0) /// Proof: `Duster::AccountBlacklist` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`) /// Storage: `AssetRegistry::BannedAssets` (r:2 w:0) /// Proof: `AssetRegistry::BannedAssets` (`max_values`: None, `max_size`: Some(20), added: 2495, mode: `MaxEncodedLen`) - /// Storage: `AssetRegistry::Assets` (r:2 w:0) - /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// The range of component `c` is `[0, 1]`. fn calculate_and_execute_sell_in_lbp(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `3436` + // Measured: `3535` // Estimated: `13905` - // Minimum execution time: 344_173_000 picoseconds. - Weight::from_parts(347_378_169, 13905) - // Standard Error: 131_839 - .saturating_add(Weight::from_parts(49_213_080, 0).saturating_mul(c.into())) + // Minimum execution time: 331_742_000 picoseconds. + Weight::from_parts(334_752_338, 13905) + // Standard Error: 172_616 + .saturating_add(Weight::from_parts(53_146_599, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(16)) .saturating_add(T::DbWeight::get().writes(7)) } @@ -80,39 +80,39 @@ impl pallet_route_executor::weights::WeightInfo for Hyd /// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(108), added: 2583, mode: `MaxEncodedLen`) /// Storage: `System::Account` (r:3 w:1) /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) + /// Storage: `AssetRegistry::Assets` (r:2 w:0) + /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// Storage: `Tokens::Locks` (r:1 w:1) /// Proof: `Tokens::Locks` (`max_values`: None, `max_size`: Some(1261), added: 3736, mode: `MaxEncodedLen`) /// Storage: `Duster::AccountBlacklist` (r:2 w:0) /// Proof: `Duster::AccountBlacklist` (`max_values`: None, `max_size`: Some(48), added: 2523, mode: `MaxEncodedLen`) /// Storage: `AssetRegistry::BannedAssets` (r:2 w:0) /// Proof: `AssetRegistry::BannedAssets` (`max_values`: None, `max_size`: Some(20), added: 2495, mode: `MaxEncodedLen`) - /// Storage: `AssetRegistry::Assets` (r:2 w:0) - /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// The range of component `c` is `[1, 2]`. /// The range of component `b` is `[0, 1]`. fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `1604 + b * (1837 ±0)` - // Estimated: `6156 + b * (7749 ±251_795_645_551_580_832)` - // Minimum execution time: 75_692_000 picoseconds. - Weight::from_parts(76_409_000, 6156) - // Standard Error: 611_234 - .saturating_add(Weight::from_parts(2_382_471, 0).saturating_mul(c.into())) - // Standard Error: 1_341_833 - .saturating_add(Weight::from_parts(272_805_599, 0).saturating_mul(b.into())) + // Measured: `1637 + b * (1923 ±0)` + // Estimated: `6156 + b * (7749 ±99_524_913_928_918_768)` + // Minimum execution time: 74_280_000 picoseconds. + Weight::from_parts(74_850_000, 6156) + // Standard Error: 595_333 + .saturating_add(Weight::from_parts(2_341_575, 0).saturating_mul(c.into())) + // Standard Error: 1_306_926 + .saturating_add(Weight::from_parts(268_504_139, 0).saturating_mul(b.into())) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().reads((13_u64).saturating_mul(b.into()))) .saturating_add(T::DbWeight::get().writes((7_u64).saturating_mul(b.into()))) .saturating_add(Weight::from_parts(0, 7749).saturating_mul(b.into())) } - /// Storage: `AssetRegistry::Assets` (r:6 w:0) - /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// Storage: `Router::Routes` (r:1 w:1) /// Proof: `Router::Routes` (`max_values`: None, `max_size`: Some(90), added: 2565, mode: `MaxEncodedLen`) /// Storage: `System::Account` (r:7 w:0) /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) /// Storage: `Tokens::Accounts` (r:15 w:0) /// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(108), added: 2583, mode: `MaxEncodedLen`) + /// Storage: `AssetRegistry::Assets` (r:6 w:0) + /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// Storage: `XYK::ShareToken` (r:6 w:0) /// Proof: `XYK::ShareToken` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`) /// Storage: `Duster::AccountBlacklist` (r:7 w:0) @@ -129,10 +129,10 @@ impl pallet_route_executor::weights::WeightInfo for Hyd /// Proof: `Tokens::TotalIssuance` (`max_values`: None, `max_size`: Some(28), added: 2503, mode: `MaxEncodedLen`) fn set_route_for_xyk() -> Weight { // Proof Size summary in bytes: - // Measured: `6326` + // Measured: `6426` // Estimated: `39735` - // Minimum execution time: 2_263_303_000 picoseconds. - Weight::from_parts(2_275_623_000, 39735) + // Minimum execution time: 2_128_339_000 picoseconds. + Weight::from_parts(2_136_391_000, 39735) .saturating_add(T::DbWeight::get().reads(55)) .saturating_add(T::DbWeight::get().writes(1)) } @@ -142,8 +142,8 @@ impl pallet_route_executor::weights::WeightInfo for Hyd // Proof Size summary in bytes: // Measured: `1012` // Estimated: `0` - // Minimum execution time: 29_861_000 picoseconds. - Weight::from_parts(30_303_000, 0) + // Minimum execution time: 28_891_000 picoseconds. + Weight::from_parts(29_226_000, 0) .saturating_add(T::DbWeight::get().writes(1)) } } \ No newline at end of file From d52d739294d600c35a1c7bb814a194642e5d758c Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 18 Jun 2024 13:40:37 +0200 Subject: [PATCH 06/28] bump versions --- Cargo.lock | 10 +++++----- integration-tests/Cargo.toml | 2 +- pallets/dca/Cargo.toml | 2 +- pallets/route-executor/Cargo.toml | 2 +- runtime/adapters/Cargo.toml | 2 +- runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/lib.rs | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd31e2029..c47c4cf05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4621,7 +4621,7 @@ dependencies = [ [[package]] name = "hydradx-adapters" -version = "1.3.3" +version = "1.3.4" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", @@ -4671,7 +4671,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "241.0.0" +version = "242.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", @@ -7474,7 +7474,7 @@ dependencies = [ [[package]] name = "pallet-dca" -version = "1.4.7" +version = "1.4.8" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", @@ -8422,7 +8422,7 @@ dependencies = [ [[package]] name = "pallet-route-executor" -version = "2.3.2" +version = "2.4.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -11545,7 +11545,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.21.8" +version = "1.21.9" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-dmp-queue", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index a33f3996e..06301f314 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.21.8" +version = "1.21.9" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/pallets/dca/Cargo.toml b/pallets/dca/Cargo.toml index dfac51357..c7634b80c 100644 --- a/pallets/dca/Cargo.toml +++ b/pallets/dca/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-dca' -version = "1.4.7" +version = "1.4.8" description = 'A pallet to manage DCA scheduling' authors = ['GalacticCouncil'] edition = '2021' diff --git a/pallets/route-executor/Cargo.toml b/pallets/route-executor/Cargo.toml index ccf6826f9..62f609323 100644 --- a/pallets/route-executor/Cargo.toml +++ b/pallets/route-executor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-route-executor' -version = '2.3.2' +version = '2.4.0' description = 'A pallet to execute a route containing a sequence of trades' authors = ['GalacticCouncil'] edition = '2021' diff --git a/runtime/adapters/Cargo.toml b/runtime/adapters/Cargo.toml index 512d15490..05baabe02 100644 --- a/runtime/adapters/Cargo.toml +++ b/runtime/adapters/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-adapters" -version = "1.3.3" +version = "1.3.4" description = "Structs and other generic types for building runtimes." authors = ["GalacticCouncil"] edition = "2021" diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index 52353cede..3da4d4ac3 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "241.0.0" +version = "242.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index 9ed880600..1c95c24d0 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -109,7 +109,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 241, + spec_version: 242, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 4b20b62c0978e7bc52c54717df1a08d4da232bff Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 19 Jun 2024 10:27:04 +0200 Subject: [PATCH 07/28] update lock file --- Cargo.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ab21d65e..6254fb64f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4912,7 +4912,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "243.0.0" +version = "244.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", @@ -8886,7 +8886,7 @@ dependencies = [ [[package]] name = "pallet-route-executor" -version = "2.3.3" +version = "2.4.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -11915,7 +11915,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.22.4" +version = "1.22.5" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", From 262e5a84950bef75704c4d512290e37be02d4645 Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 19 Jun 2024 13:23:04 +0200 Subject: [PATCH 08/28] bump versions --- Cargo.lock | 4 ++-- pallets/dca/Cargo.toml | 2 +- runtime/adapters/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6254fb64f..716c2e300 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4862,7 +4862,7 @@ dependencies = [ [[package]] name = "hydradx-adapters" -version = "1.3.4" +version = "1.3.5" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", @@ -7920,7 +7920,7 @@ dependencies = [ [[package]] name = "pallet-dca" -version = "1.4.8" +version = "1.4.9" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", diff --git a/pallets/dca/Cargo.toml b/pallets/dca/Cargo.toml index c7634b80c..4d3427ce4 100644 --- a/pallets/dca/Cargo.toml +++ b/pallets/dca/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-dca' -version = "1.4.8" +version = "1.4.9" description = 'A pallet to manage DCA scheduling' authors = ['GalacticCouncil'] edition = '2021' diff --git a/runtime/adapters/Cargo.toml b/runtime/adapters/Cargo.toml index 05baabe02..d6ca4836a 100644 --- a/runtime/adapters/Cargo.toml +++ b/runtime/adapters/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-adapters" -version = "1.3.4" +version = "1.3.5" description = "Structs and other generic types for building runtimes." authors = ["GalacticCouncil"] edition = "2021" From 3e338102b2e7682ef4aaf60efd1ca771f5de6ff0 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Tue, 16 Jul 2024 14:43:11 +0200 Subject: [PATCH 09/28] bump crate versions --- Cargo.lock | 4 ++-- integration-tests/Cargo.toml | 2 +- pallets/dca/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1771af779..c73cec68e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7938,7 +7938,7 @@ dependencies = [ [[package]] name = "pallet-dca" -version = "1.4.9" +version = "1.4.10" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", @@ -11929,7 +11929,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.22.5" +version = "1.22.6" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 2524e59f9..0ad8c274a 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runtime-integration-tests" -version = "1.22.5" +version = "1.22.6" description = "Integration tests" authors = ["GalacticCouncil"] edition = "2021" diff --git a/pallets/dca/Cargo.toml b/pallets/dca/Cargo.toml index 4d3427ce4..9cd629442 100644 --- a/pallets/dca/Cargo.toml +++ b/pallets/dca/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-dca' -version = "1.4.9" +version = "1.4.10" description = 'A pallet to manage DCA scheduling' authors = ['GalacticCouncil'] edition = '2021' From a5935d2ae62a679086928f685b533249c04f91c5 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 16 Jul 2024 15:00:30 +0100 Subject: [PATCH 10/28] renaming --- integration-tests/src/router.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-tests/src/router.rs b/integration-tests/src/router.rs index 696222ace..943425782 100644 --- a/integration-tests/src/router.rs +++ b/integration-tests/src/router.rs @@ -2962,7 +2962,7 @@ mod set_route { use primitives::constants::chain::XYK_SOURCE; #[test] - fn set_route_should_work_with_all_pools_involved() { + fn set_route_should_work_with_omnipool_xyk_and_stable_pools() { { TestNet::reset(); From e8a3d8e8a5d7f52d22681ed37ebbaeffe0a221db Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Tue, 16 Jul 2024 17:37:14 +0200 Subject: [PATCH 11/28] bump runtime version --- Cargo.lock | 2 +- runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c8eabbbd5..e3171a964 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4938,7 +4938,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "247.0.0" +version = "248.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index 758161f11..f30b7aedf 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "247.0.0" +version = "248.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index 0543cc488..2e34909ec 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -113,7 +113,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 247, + spec_version: 248, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 599b7d9548fe639c070bfaf523a515fccdfba4c4 Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Wed, 17 Jul 2024 10:41:02 +0200 Subject: [PATCH 12/28] add missing config param to otc-settlements mock --- pallets/otc-settlements/src/mock.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pallets/otc-settlements/src/mock.rs b/pallets/otc-settlements/src/mock.rs index 4f1052f69..2ae890496 100644 --- a/pallets/otc-settlements/src/mock.rs +++ b/pallets/otc-settlements/src/mock.rs @@ -27,8 +27,11 @@ use frame_support::{ }, }; use frame_system::{EnsureRoot, EnsureSigned}; -use hydra_dx_math::ema::EmaPrice; -use hydradx_traits::router::{PoolType, RefundEdCalculator}; +use hydra_dx_math::{ema::EmaPrice, ratio::Ratio}; +use hydradx_traits::{ + router::{PoolType, RefundEdCalculator}, + OraclePeriod, PriceOracle, +}; use orml_traits::{parameter_type_with_key, GetByKey}; use pallet_currencies::{fungibles::FungibleCurrencies, BasicCurrencyAdapter}; use pallet_omnipool::traits::ExternalPriceProvider; @@ -125,18 +128,33 @@ impl RefundEdCalculator for MockedEdCalculator { } } +pub struct PriceProviderMock {} + +impl PriceOracle for PriceProviderMock { + type Price = Ratio; + + fn price(route: &[Trade], _: OraclePeriod) -> Option { + let has_insufficient_asset = route.iter().any(|t| t.asset_in > 2000 || t.asset_out > 2000); + if has_insufficient_asset { + return None; + } + Some(Ratio::new(88, 100)) + } +} + impl pallet_route_executor::Config for Test { type RuntimeEvent = RuntimeEvent; type AssetId = AssetId; type Balance = Balance; type NativeAssetId = HDXAssetId; type Currency = FungibleCurrencies; - type AMM = Omnipool; type InspectRegistry = AssetRegistry; + type AMM = Omnipool; + type EdToRefundCalculator = MockedEdCalculator; + type OraclePriceProvider = PriceProviderMock; type DefaultRoutePoolType = DefaultRoutePoolType; - type WeightInfo = (); type TechnicalOrigin = EnsureRoot; - type EdToRefundCalculator = MockedEdCalculator; + type WeightInfo = (); } parameter_types! { From 75e569da982bac9101fdf71154c7c9f03991d71a Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Wed, 17 Jul 2024 13:56:15 +0200 Subject: [PATCH 13/28] bump crate versions --- Cargo.lock | 4 ++-- pallets/otc-settlements/Cargo.toml | 2 +- pallets/otc/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e3171a964..ee297c953 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8746,7 +8746,7 @@ dependencies = [ [[package]] name = "pallet-otc" -version = "2.0.0" +version = "2.0.1" dependencies = [ "frame-benchmarking", "frame-support", @@ -8768,7 +8768,7 @@ dependencies = [ [[package]] name = "pallet-otc-settlements" -version = "1.0.1" +version = "1.0.2" dependencies = [ "frame-benchmarking", "frame-support", diff --git a/pallets/otc-settlements/Cargo.toml b/pallets/otc-settlements/Cargo.toml index eabeedcfd..bfc59eae6 100644 --- a/pallets/otc-settlements/Cargo.toml +++ b/pallets/otc-settlements/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-otc-settlements' -version = '1.0.1' +version = '1.0.2' description = 'A pallet with offchain worker closing OTC arbs' authors = ['GalacticCouncil'] edition = '2021' diff --git a/pallets/otc/Cargo.toml b/pallets/otc/Cargo.toml index 5c46fdfa6..97633fbdf 100644 --- a/pallets/otc/Cargo.toml +++ b/pallets/otc/Cargo.toml @@ -1,6 +1,6 @@ [package] name = 'pallet-otc' -version = '2.0.0' +version = '2.0.1' description = 'A pallet for trustless over-the-counter trading' authors = ['GalacticCouncil'] edition = '2021' From aabf1c07bb2266cc34ca5439a5bfa44a91887086 Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 14:15:19 +0200 Subject: [PATCH 14/28] include oracle to the benchmarking --- pallets/route-executor/src/weights.rs | 13 ++++ runtime/hydradx/src/assets.rs | 10 +-- .../src/benchmarking/route_executor.rs | 60 +++++++++++++++++- .../src/weights/pallet_route_executor.rs | 62 +++++++++++-------- 4 files changed, 113 insertions(+), 32 deletions(-) diff --git a/pallets/route-executor/src/weights.rs b/pallets/route-executor/src/weights.rs index e221631b1..abf9039fc 100644 --- a/pallets/route-executor/src/weights.rs +++ b/pallets/route-executor/src/weights.rs @@ -52,6 +52,7 @@ pub trait WeightInfo { fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight; fn set_route_for_xyk() -> Weight; fn force_insert_route() -> Weight; + fn get_oracle_price() -> Weight; fn get_route() -> Weight; fn calculate_spot_price_with_fee_in_lbp() -> Weight; } @@ -166,6 +167,18 @@ impl WeightInfo for () { Weight::from_parts(7_748_000, 3555) .saturating_add(RocksDbWeight::get().reads(1_u64)) } + + /// Storage: `EmaOracle::Oracles` (r:2 w:0) + /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) + fn get_oracle_price() -> Weight { + // Proof Size summary in bytes: + // Measured: `1589` + // Estimated: `6294` + // Minimum execution time: 24_019_000 picoseconds. + Weight::from_parts(24_870_000, 6294) + .saturating_add(RocksDbWeight::get().reads(2_u64)) + } + /// Storage: `LBP::PoolData` (r:1 w:0) /// Proof: `LBP::PoolData` (`max_values`: None, `max_size`: Some(163), added: 2638, mode: `MaxEncodedLen`) /// Storage: `Tokens::Locks` (r:1 w:0) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 4d4345e9b..3649a05b3 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1060,6 +1060,11 @@ impl AmmTradeWeights> for RouterWeightInfo { weight.saturating_accrue(amm_weight); } + //Add a sell weight as we do a dry-run sell as validation + let weight_of_get_oracle_price_for_2_assets = weights::pallet_route_executor::HydraWeight::::get_oracle_price(); + let weight_of_get_oracle_price_for_route = weight_of_get_oracle_price_for_2_assets.saturating_mul(route.len() as u64); + weight.saturating_accrue(weight_of_get_oracle_price_for_route); + weight } @@ -1108,10 +1113,7 @@ impl pallet_route_executor::Config for Runtime { type InspectRegistry = AssetRegistry; type TechnicalOrigin = SuperMajorityTechCommittee; type EdToRefundCalculator = RefundAndLockedEdCalculator; - #[cfg(not(feature = "runtime-benchmarks"))] - type OraclePriceProvider = OraclePriceProvider; - #[cfg(feature = "runtime-benchmarks")] - type OraclePriceProvider = DummyOraclePriceProvider; + type OraclePriceProvider = hydradx_adapters::OraclePriceProvider; } parameter_types! { diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index b23494c85..e7b235882 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -16,9 +16,7 @@ // limitations under the License. #![allow(clippy::result_large_err)] -use crate::{ - AccountId, AssetId, Balance, Currencies, InsufficientEDinHDX, Router, Runtime, RuntimeOrigin, System, LBP, XYK, -}; +use crate::{AccountId, AssetId, Balance, Currencies, InsufficientEDinHDX, Router, Runtime, RuntimeOrigin, System, LBP, XYK, EmaOracle}; use super::*; use crate::benchmarking::dca::{DAI, HDX}; @@ -33,6 +31,9 @@ use orml_benchmarking::runtime_benchmarks; use orml_traits::{MultiCurrency, MultiCurrencyExtended}; use primitives::constants::currency::UNITS; use sp_std::vec; +use primitives::BlockNumber; +use hydradx_traits::PriceOracle; +use pallet_ema_oracle::OraclePeriod; pub const INITIAL_BALANCE: Balance = 10_000_000 * UNITS; fn funded_account(name: &'static str, index: u32, assets: &[AssetId]) -> AccountId { @@ -132,6 +133,18 @@ fn create_xyk_pool(asset_a: u32, asset_b: u32) { asset_b, amount, )); + + let seller: AccountId = funded_account("caller", 0, &[asset_a]); + + assert_ok!(XYK::sell( + RuntimeOrigin::signed(seller), + asset_a, + asset_b, + 10 * UNITS, + u128::MIN, + false + )); + } runtime_benchmarks! { @@ -255,6 +268,9 @@ runtime_benchmarks! { asset_in: asset_5, asset_out: asset_6 }]; + + set_period(10); + Router::set_route( RawOrigin::Signed(caller.clone()).into(), AssetPair::new(HDX, asset_6), @@ -345,6 +361,27 @@ runtime_benchmarks! { Router::get_route(AssetPair::new(HDX, DAI)) } + get_oracle_price { + let asset_2 = register_asset(b"AS2".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; + + let caller: AccountId = funded_account("caller", 0, &[asset_2]); + create_xyk_pool(HDX, asset_2); + + let route = vec![Trade { + pool: PoolType::XYK, + asset_in: HDX, + asset_out: asset_2 + }]; + + set_period(10); + let mut price = None; + }: { + price = ::OraclePriceProvider::price(&route, OraclePeriod::TenMinutes); + } + verify { + assert!(price.is_some()); + } + // Calculates the weight of LBP spot price with fee calculation. Used in the calculation to determine the weight of the overhead. calculate_spot_price_with_fee_in_lbp { let asset_in = register_external_asset(b"FCA".to_vec()).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; @@ -414,3 +451,20 @@ mod tests { impl_benchmark_test_suite!(new_test_ext(),); } + +use frame_support::traits::OnInitialize; +use frame_support::traits::OnFinalize; + +fn set_period(to: u32) { + while System::block_number() < Into::::into(to) { + let b = System::block_number(); + + System::on_finalize(b); + EmaOracle::on_finalize(b); + + System::on_initialize(b + 1_u32); + EmaOracle::on_initialize(b + 1_u32); + + System::set_block_number(b + 1_u32); + } +} diff --git a/runtime/hydradx/src/weights/pallet_route_executor.rs b/runtime/hydradx/src/weights/pallet_route_executor.rs index 47258545f..48eda8f9e 100644 --- a/runtime/hydradx/src/weights/pallet_route_executor.rs +++ b/runtime/hydradx/src/weights/pallet_route_executor.rs @@ -19,9 +19,9 @@ //! Autogenerated weights for `pallet_route_executor` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0 -//! DATE: 2024-06-20, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-07-22, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `bench-bot`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` +//! HOSTNAME: `dmoka-msi-pc`, CPU: `AMD Ryzen 9 5900X 12-Core Processor` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024` // Executed Command: @@ -71,10 +71,10 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `3535` // Estimated: `13905` - // Minimum execution time: 354_159_000 picoseconds. - Weight::from_parts(358_572_573, 13905) - // Standard Error: 306_143 - .saturating_add(Weight::from_parts(70_834_738, 0).saturating_mul(c.into())) + // Minimum execution time: 272_559_000 picoseconds. + Weight::from_parts(276_963_801, 13905) + // Standard Error: 315_744 + .saturating_add(Weight::from_parts(50_972_261, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(16_u64)) .saturating_add(T::DbWeight::get().writes(7_u64)) } @@ -97,26 +97,28 @@ impl pallet_route_executor::WeightInfo for HydraWeight< fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `1637 + b * (1923 ±0)` - // Estimated: `6156 + b * (7749 ±228_644_766_292_339_040)` - // Minimum execution time: 94_614_000 picoseconds. - Weight::from_parts(96_046_000, 6156) - // Standard Error: 876_130 - .saturating_add(Weight::from_parts(3_658_596, 0).saturating_mul(c.into())) - // Standard Error: 1_923_356 - .saturating_add(Weight::from_parts(272_645_114, 0).saturating_mul(b.into())) + // Estimated: `6156 + b * (7749 ±245_709_589_663_843_264)` + // Minimum execution time: 68_840_000 picoseconds. + Weight::from_parts(69_610_000, 6156) + // Standard Error: 644_789 + .saturating_add(Weight::from_parts(2_620_118, 0).saturating_mul(c.into())) + // Standard Error: 1_415_496 + .saturating_add(Weight::from_parts(215_576_781, 0).saturating_mul(b.into())) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().reads((13_u64).saturating_mul(b.into()))) .saturating_add(T::DbWeight::get().writes((7_u64).saturating_mul(b.into()))) .saturating_add(Weight::from_parts(0, 7749).saturating_mul(b.into())) } - /// Storage: `AssetRegistry::Assets` (r:6 w:0) - /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) + /// Storage: `EmaOracle::Oracles` (r:2 w:0) + /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) /// Storage: `Router::Routes` (r:1 w:1) /// Proof: `Router::Routes` (`max_values`: None, `max_size`: Some(90), added: 2565, mode: `MaxEncodedLen`) /// Storage: `System::Account` (r:7 w:0) /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`) /// Storage: `Tokens::Accounts` (r:15 w:0) /// Proof: `Tokens::Accounts` (`max_values`: None, `max_size`: Some(108), added: 2583, mode: `MaxEncodedLen`) + /// Storage: `AssetRegistry::Assets` (r:6 w:0) + /// Proof: `AssetRegistry::Assets` (`max_values`: None, `max_size`: Some(125), added: 2600, mode: `MaxEncodedLen`) /// Storage: `XYK::ShareToken` (r:6 w:0) /// Proof: `XYK::ShareToken` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`) /// Storage: `Duster::AccountBlacklist` (r:7 w:0) @@ -133,11 +135,11 @@ impl pallet_route_executor::WeightInfo for HydraWeight< /// Proof: `Tokens::TotalIssuance` (`max_values`: None, `max_size`: Some(28), added: 2503, mode: `MaxEncodedLen`) fn set_route_for_xyk() -> Weight { // Proof Size summary in bytes: - // Measured: `6426` + // Measured: `7004` // Estimated: `39735` - // Minimum execution time: 1_917_993_000 picoseconds. - Weight::from_parts(1_930_602_000, 39735) - .saturating_add(T::DbWeight::get().reads(55_u64)) + // Minimum execution time: 1_672_083_000 picoseconds. + Weight::from_parts(1_684_994_000, 39735) + .saturating_add(T::DbWeight::get().reads(57_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: `Router::Routes` (r:0 w:1) @@ -146,8 +148,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1012` // Estimated: `0` - // Minimum execution time: 25_174_000 picoseconds. - Weight::from_parts(25_689_000, 0) + // Minimum execution time: 20_400_000 picoseconds. + Weight::from_parts(20_880_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: `Router::Routes` (r:1 w:0) @@ -156,10 +158,20 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `800` // Estimated: `3555` - // Minimum execution time: 7_562_000 picoseconds. - Weight::from_parts(7_748_000, 3555) + // Minimum execution time: 6_020_000 picoseconds. + Weight::from_parts(6_160_000, 3555) .saturating_add(T::DbWeight::get().reads(1_u64)) } + /// Storage: `EmaOracle::Oracles` (r:2 w:0) + /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) + fn get_oracle_price() -> Weight { + // Proof Size summary in bytes: + // Measured: `1589` + // Estimated: `6294` + // Minimum execution time: 24_019_000 picoseconds. + Weight::from_parts(24_870_000, 6294) + .saturating_add(T::DbWeight::get().reads(2_u64)) + } /// Storage: `LBP::PoolData` (r:1 w:0) /// Proof: `LBP::PoolData` (`max_values`: None, `max_size`: Some(163), added: 2638, mode: `MaxEncodedLen`) /// Storage: `Tokens::Locks` (r:1 w:0) @@ -170,8 +182,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1674` // Estimated: `6156` - // Minimum execution time: 36_086_000 picoseconds. - Weight::from_parts(36_646_000, 6156) + // Minimum execution time: 27_930_000 picoseconds. + Weight::from_parts(28_500_000, 6156) .saturating_add(T::DbWeight::get().reads(4_u64)) } } \ No newline at end of file From 0952798c633699c40a0ba114086bddf1ea129da3 Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 14:43:09 +0200 Subject: [PATCH 15/28] adjust set route weight with getting oracle price --- runtime/hydradx/src/assets.rs | 2 +- .../src/benchmarking/route_executor.rs | 1 + .../src/weights/pallet_route_executor.rs | 42 +++++++++---------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 3649a05b3..e95f1a599 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -850,7 +850,7 @@ impl RouterWeightInfo { set_route_overweight.saturating_sub(weights::pallet_xyk::HydraWeight::::router_execution_sell( number_of_times_calculate_sell_amounts_executed, number_of_times_execute_sell_amounts_executed, - )) + )).saturating_sub(weights::pallet_route_executor::HydraWeight::::get_oracle_price()) // we substract weight of getting oracle price as we add this back later based on the length of the route } pub fn calculate_spot_price_overweight() -> Weight { diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index e7b235882..89e5de7a2 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -15,6 +15,7 @@ // See the License for the specific language governing permissions and // limitations under the License. #![allow(clippy::result_large_err)] +#![allow(unused_assignments)] //Benchmark test leads to unused assignment, which is not true use crate::{AccountId, AssetId, Balance, Currencies, InsufficientEDinHDX, Router, Runtime, RuntimeOrigin, System, LBP, XYK, EmaOracle}; diff --git a/runtime/hydradx/src/weights/pallet_route_executor.rs b/runtime/hydradx/src/weights/pallet_route_executor.rs index 48eda8f9e..1eb327526 100644 --- a/runtime/hydradx/src/weights/pallet_route_executor.rs +++ b/runtime/hydradx/src/weights/pallet_route_executor.rs @@ -71,10 +71,10 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `3535` // Estimated: `13905` - // Minimum execution time: 272_559_000 picoseconds. - Weight::from_parts(276_963_801, 13905) - // Standard Error: 315_744 - .saturating_add(Weight::from_parts(50_972_261, 0).saturating_mul(c.into())) + // Minimum execution time: 276_059_000 picoseconds. + Weight::from_parts(280_206_250, 13905) + // Standard Error: 343_411 + .saturating_add(Weight::from_parts(53_426_562, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(16_u64)) .saturating_add(T::DbWeight::get().writes(7_u64)) } @@ -98,12 +98,12 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1637 + b * (1923 ±0)` // Estimated: `6156 + b * (7749 ±245_709_589_663_843_264)` - // Minimum execution time: 68_840_000 picoseconds. - Weight::from_parts(69_610_000, 6156) - // Standard Error: 644_789 - .saturating_add(Weight::from_parts(2_620_118, 0).saturating_mul(c.into())) - // Standard Error: 1_415_496 - .saturating_add(Weight::from_parts(215_576_781, 0).saturating_mul(b.into())) + // Minimum execution time: 68_440_000 picoseconds. + Weight::from_parts(69_699_000, 6156) + // Standard Error: 617_743 + .saturating_add(Weight::from_parts(2_520_114, 0).saturating_mul(c.into())) + // Standard Error: 1_356_122 + .saturating_add(Weight::from_parts(218_383_352, 0).saturating_mul(b.into())) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().reads((13_u64).saturating_mul(b.into()))) .saturating_add(T::DbWeight::get().writes((7_u64).saturating_mul(b.into()))) @@ -137,8 +137,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `7004` // Estimated: `39735` - // Minimum execution time: 1_672_083_000 picoseconds. - Weight::from_parts(1_684_994_000, 39735) + // Minimum execution time: 1_666_103_000 picoseconds. + Weight::from_parts(1_695_742_000, 39735) .saturating_add(T::DbWeight::get().reads(57_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -148,8 +148,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1012` // Estimated: `0` - // Minimum execution time: 20_400_000 picoseconds. - Weight::from_parts(20_880_000, 0) + // Minimum execution time: 20_250_000 picoseconds. + Weight::from_parts(20_950_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: `Router::Routes` (r:1 w:0) @@ -158,18 +158,18 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `800` // Estimated: `3555` - // Minimum execution time: 6_020_000 picoseconds. - Weight::from_parts(6_160_000, 3555) + // Minimum execution time: 6_050_000 picoseconds. + Weight::from_parts(6_270_000, 3555) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: `EmaOracle::Oracles` (r:2 w:0) /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) fn get_oracle_price() -> Weight { // Proof Size summary in bytes: - // Measured: `1589` + // Measured: `1452` // Estimated: `6294` - // Minimum execution time: 24_019_000 picoseconds. - Weight::from_parts(24_870_000, 6294) + // Minimum execution time: 21_710_000 picoseconds. + Weight::from_parts(22_040_000, 6294) .saturating_add(T::DbWeight::get().reads(2_u64)) } /// Storage: `LBP::PoolData` (r:1 w:0) @@ -182,8 +182,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1674` // Estimated: `6156` - // Minimum execution time: 27_930_000 picoseconds. - Weight::from_parts(28_500_000, 6156) + // Minimum execution time: 27_890_000 picoseconds. + Weight::from_parts(28_360_000, 6156) .saturating_add(T::DbWeight::get().reads(4_u64)) } } \ No newline at end of file From 5757dd7d35304fd6bb8e2c6037b1bcd9a751111b Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 14:44:55 +0200 Subject: [PATCH 16/28] update cargo lock --- Cargo.lock | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f6be7d76e..6c8235177 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4888,7 +4888,7 @@ dependencies = [ [[package]] name = "hydradx-adapters" -version = "1.3.4" +version = "1.3.5" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", @@ -4938,7 +4938,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "249.0.0" +version = "250.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", @@ -7938,7 +7938,7 @@ dependencies = [ [[package]] name = "pallet-dca" -version = "1.5.0" +version = "1.5.1" dependencies = [ "cumulus-pallet-parachain-system", "cumulus-primitives-core", @@ -8746,7 +8746,7 @@ dependencies = [ [[package]] name = "pallet-otc" -version = "2.0.0" +version = "2.0.1" dependencies = [ "frame-benchmarking", "frame-support", @@ -8768,7 +8768,7 @@ dependencies = [ [[package]] name = "pallet-otc-settlements" -version = "1.0.1" +version = "1.0.2" dependencies = [ "frame-benchmarking", "frame-support", @@ -8936,7 +8936,7 @@ dependencies = [ [[package]] name = "pallet-route-executor" -version = "2.4.1" +version = "2.5.0" dependencies = [ "frame-benchmarking", "frame-support", @@ -11929,7 +11929,7 @@ dependencies = [ [[package]] name = "runtime-integration-tests" -version = "1.23.0" +version = "1.23.1" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", From e74967d8bfe5be3871aa86d4c3aadc9c67395abd Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 14:50:16 +0200 Subject: [PATCH 17/28] add oracle period as pallet config of route executor --- pallets/dca/src/tests/mock.rs | 3 +++ pallets/otc-settlements/src/mock.rs | 2 ++ pallets/route-executor/src/lib.rs | 6 +++++- pallets/route-executor/src/tests/mock.rs | 3 +++ runtime/adapters/src/tests/mock.rs | 2 ++ runtime/hydradx/src/assets.rs | 3 +++ 6 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pallets/dca/src/tests/mock.rs b/pallets/dca/src/tests/mock.rs index 55cdc31d4..2092d36aa 100644 --- a/pallets/dca/src/tests/mock.rs +++ b/pallets/dca/src/tests/mock.rs @@ -350,6 +350,8 @@ pub const ASSET_PAIR_ACCOUNT: AccountId = 12; parameter_types! { pub MaxNumberOfTrades: u8 = 3; pub DefaultRoutePoolType: PoolType = PoolType::Omnipool; + pub const RouteValidationOraclePeriod: OraclePeriod = OraclePeriod::TenMinutes; + } type Pools = (OmniPool, Xyk); @@ -367,6 +369,7 @@ impl pallet_route_executor::Config for Test { type TechnicalOrigin = EnsureRoot; type EdToRefundCalculator = MockedEdCalculator; type OraclePriceProvider = PriceProviderMock; + type OraclePeriod = RouteValidationOraclePeriod; } pub struct MockedEdCalculator; diff --git a/pallets/otc-settlements/src/mock.rs b/pallets/otc-settlements/src/mock.rs index 2ae890496..50b7bc7c9 100644 --- a/pallets/otc-settlements/src/mock.rs +++ b/pallets/otc-settlements/src/mock.rs @@ -118,6 +118,7 @@ impl pallet_otc::Config for Test { parameter_types! { pub DefaultRoutePoolType: PoolType = PoolType::Omnipool; + pub const RouteValidationOraclePeriod: OraclePeriod = OraclePeriod::TenMinutes; } pub struct MockedEdCalculator; @@ -152,6 +153,7 @@ impl pallet_route_executor::Config for Test { type AMM = Omnipool; type EdToRefundCalculator = MockedEdCalculator; type OraclePriceProvider = PriceProviderMock; + type OraclePeriod = RouteValidationOraclePeriod; type DefaultRoutePoolType = DefaultRoutePoolType; type TechnicalOrigin = EnsureRoot; type WeightInfo = (); diff --git a/pallets/route-executor/src/lib.rs b/pallets/route-executor/src/lib.rs index dd265e33b..b393bf8b6 100644 --- a/pallets/route-executor/src/lib.rs +++ b/pallets/route-executor/src/lib.rs @@ -114,6 +114,10 @@ pub mod pallet { ///Oracle price provider to validate if new route has oracle price data type OraclePriceProvider: PriceOracle; + /// Oracle's price aggregation period. + #[pallet::constant] + type OraclePeriod: Get; + /// Pool type used in the default route type DefaultRoutePoolType: Get>; @@ -373,7 +377,7 @@ pub mod pallet { let _ = ensure_signed(origin.clone())?; Self::ensure_route_size(new_route.len())?; Self::ensure_route_arguments(&asset_pair, &new_route)?; - T::OraclePriceProvider::price(&new_route, OraclePeriod::TenMinutes).ok_or(Error::::RouteHasNoOracle)?; + T::OraclePriceProvider::price(&new_route, T::OraclePeriod::get()).ok_or(Error::::RouteHasNoOracle)?; if !asset_pair.is_ordered() { asset_pair = asset_pair.ordered_pair(); diff --git a/pallets/route-executor/src/tests/mock.rs b/pallets/route-executor/src/tests/mock.rs index a21cdfbe2..0363cf739 100644 --- a/pallets/route-executor/src/tests/mock.rs +++ b/pallets/route-executor/src/tests/mock.rs @@ -141,6 +141,8 @@ type Pools = (XYK, StableSwap, OmniPool, LBP); parameter_types! { pub NativeCurrencyId: AssetId = HDX; pub DefaultRoutePoolType: PoolType = PoolType::Omnipool; + pub const RouteValidationOraclePeriod: OraclePeriod = OraclePeriod::TenMinutes; + } impl Config for Test { @@ -153,6 +155,7 @@ impl Config for Test { type AMM = Pools; type EdToRefundCalculator = MockedEdCalculator; type OraclePriceProvider = PriceProviderMock; + type OraclePeriod = RouteValidationOraclePeriod; type DefaultRoutePoolType = DefaultRoutePoolType; type TechnicalOrigin = EnsureRoot; type WeightInfo = (); diff --git a/runtime/adapters/src/tests/mock.rs b/runtime/adapters/src/tests/mock.rs index 0b586d4cc..149ef9140 100644 --- a/runtime/adapters/src/tests/mock.rs +++ b/runtime/adapters/src/tests/mock.rs @@ -317,6 +317,7 @@ pub const ASSET_PAIR_ACCOUNT: AccountId = 12; parameter_types! { pub NativeCurrencyId: AssetId = HDX; pub DefaultRoutePoolType: PoolType = PoolType::Omnipool; + pub const RouteValidationOraclePeriod: OraclePeriod = OraclePeriod::TenMinutes; } type Pools = (Omnipool, XYK); @@ -333,6 +334,7 @@ impl pallet_route_executor::Config for Test { type OraclePriceProvider = PriceProviderMock; type DefaultRoutePoolType = DefaultRoutePoolType; type TechnicalOrigin = EnsureRoot; + type OraclePeriod = RouteValidationOraclePeriod; type WeightInfo = (); } diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 15151b2e5..0377dc7fc 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1105,6 +1105,8 @@ impl AmmTradeWeights> for RouterWeightInfo { parameter_types! { pub const DefaultRoutePoolType: PoolType = PoolType::Omnipool; + pub const RouteValidationOraclePeriod: OraclePeriod = OraclePeriod::TenMinutes; + } impl pallet_route_executor::Config for Runtime { @@ -1120,6 +1122,7 @@ impl pallet_route_executor::Config for Runtime { type TechnicalOrigin = SuperMajorityTechCommittee; type EdToRefundCalculator = RefundAndLockedEdCalculator; type OraclePriceProvider = hydradx_adapters::OraclePriceProvider; + type OraclePeriod = RouteValidationOraclePeriod; } parameter_types! { From c5fbf9a538a54113d2722c9d4e76447f480b0ae6 Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 14:54:23 +0200 Subject: [PATCH 18/28] formatting --- runtime/hydradx/src/assets.rs | 17 +++++++++++------ .../hydradx/src/benchmarking/route_executor.rs | 14 ++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 0377dc7fc..b46678eb3 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -853,10 +853,13 @@ impl RouterWeightInfo { let set_route_overweight = weights::pallet_route_executor::HydraWeight::::set_route_for_xyk(); - set_route_overweight.saturating_sub(weights::pallet_xyk::HydraWeight::::router_execution_sell( - number_of_times_calculate_sell_amounts_executed, - number_of_times_execute_sell_amounts_executed, - )).saturating_sub(weights::pallet_route_executor::HydraWeight::::get_oracle_price()) // we substract weight of getting oracle price as we add this back later based on the length of the route + set_route_overweight + .saturating_sub(weights::pallet_xyk::HydraWeight::::router_execution_sell( + number_of_times_calculate_sell_amounts_executed, + number_of_times_execute_sell_amounts_executed, + )) + .saturating_sub(weights::pallet_route_executor::HydraWeight::::get_oracle_price()) + // we substract weight of getting oracle price as we add this back later based on the length of the route } pub fn calculate_spot_price_overweight() -> Weight { @@ -1067,8 +1070,10 @@ impl AmmTradeWeights> for RouterWeightInfo { } //Add a sell weight as we do a dry-run sell as validation - let weight_of_get_oracle_price_for_2_assets = weights::pallet_route_executor::HydraWeight::::get_oracle_price(); - let weight_of_get_oracle_price_for_route = weight_of_get_oracle_price_for_2_assets.saturating_mul(route.len() as u64); + let weight_of_get_oracle_price_for_2_assets = + weights::pallet_route_executor::HydraWeight::::get_oracle_price(); + let weight_of_get_oracle_price_for_route = + weight_of_get_oracle_price_for_2_assets.saturating_mul(route.len() as u64); weight.saturating_accrue(weight_of_get_oracle_price_for_route); weight diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index 89e5de7a2..46bb44bb0 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -17,7 +17,10 @@ #![allow(clippy::result_large_err)] #![allow(unused_assignments)] //Benchmark test leads to unused assignment, which is not true -use crate::{AccountId, AssetId, Balance, Currencies, InsufficientEDinHDX, Router, Runtime, RuntimeOrigin, System, LBP, XYK, EmaOracle}; +use crate::{ + AccountId, AssetId, Balance, Currencies, EmaOracle, InsufficientEDinHDX, Router, Runtime, RuntimeOrigin, System, + LBP, XYK, +}; use super::*; use crate::benchmarking::dca::{DAI, HDX}; @@ -28,13 +31,13 @@ use frame_system::RawOrigin; use hydradx_traits::router::inverse_route; use hydradx_traits::router::{AssetPair, RouteProvider, RouteSpotPriceProvider}; use hydradx_traits::router::{PoolType, RouterT, Trade}; +use hydradx_traits::PriceOracle; use orml_benchmarking::runtime_benchmarks; use orml_traits::{MultiCurrency, MultiCurrencyExtended}; +use pallet_ema_oracle::OraclePeriod; use primitives::constants::currency::UNITS; -use sp_std::vec; use primitives::BlockNumber; -use hydradx_traits::PriceOracle; -use pallet_ema_oracle::OraclePeriod; +use sp_std::vec; pub const INITIAL_BALANCE: Balance = 10_000_000 * UNITS; fn funded_account(name: &'static str, index: u32, assets: &[AssetId]) -> AccountId { @@ -145,7 +148,6 @@ fn create_xyk_pool(asset_a: u32, asset_b: u32) { u128::MIN, false )); - } runtime_benchmarks! { @@ -453,8 +455,8 @@ mod tests { impl_benchmark_test_suite!(new_test_ext(),); } -use frame_support::traits::OnInitialize; use frame_support::traits::OnFinalize; +use frame_support::traits::OnInitialize; fn set_period(to: u32) { while System::block_number() < Into::::into(to) { From eab58096b0ef058c15187f3ee1f6d8d2a79c3da8 Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 15:05:51 +0200 Subject: [PATCH 19/28] adjust comment --- runtime/hydradx/src/assets.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index b46678eb3..97e334344 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -853,13 +853,13 @@ impl RouterWeightInfo { let set_route_overweight = weights::pallet_route_executor::HydraWeight::::set_route_for_xyk(); + // we substract weight of getting oracle price too as we add this back later based on the length of the route set_route_overweight .saturating_sub(weights::pallet_xyk::HydraWeight::::router_execution_sell( number_of_times_calculate_sell_amounts_executed, number_of_times_execute_sell_amounts_executed, )) .saturating_sub(weights::pallet_route_executor::HydraWeight::::get_oracle_price()) - // we substract weight of getting oracle price as we add this back later based on the length of the route } pub fn calculate_spot_price_overweight() -> Weight { From 9b732a96b9a064ecd4a2c4ed4ea05e89f8ddcff0 Mon Sep 17 00:00:00 2001 From: dmoka Date: Mon, 22 Jul 2024 15:23:56 +0200 Subject: [PATCH 20/28] regenerate benchmark on ref machine --- .../src/weights/pallet_route_executor.rs | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/runtime/hydradx/src/weights/pallet_route_executor.rs b/runtime/hydradx/src/weights/pallet_route_executor.rs index 1eb327526..71a379223 100644 --- a/runtime/hydradx/src/weights/pallet_route_executor.rs +++ b/runtime/hydradx/src/weights/pallet_route_executor.rs @@ -21,7 +21,7 @@ //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0 //! DATE: 2024-07-22, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` -//! HOSTNAME: `dmoka-msi-pc`, CPU: `AMD Ryzen 9 5900X 12-Core Processor` +//! HOSTNAME: `bench-bot`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024` // Executed Command: @@ -35,7 +35,7 @@ // --heap-pages=4096 // --template=scripts/pallet-weight-template.hbs // --pallet=pallet-route-executor -// --output=weights/router.rs +// --output=router.rs // --extrinsic=* #![cfg_attr(rustfmt, rustfmt_skip)] @@ -71,10 +71,10 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `3535` // Estimated: `13905` - // Minimum execution time: 276_059_000 picoseconds. - Weight::from_parts(280_206_250, 13905) - // Standard Error: 343_411 - .saturating_add(Weight::from_parts(53_426_562, 0).saturating_mul(c.into())) + // Minimum execution time: 345_896_000 picoseconds. + Weight::from_parts(348_809_433, 13905) + // Standard Error: 234_126 + .saturating_add(Weight::from_parts(80_768_066, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(16_u64)) .saturating_add(T::DbWeight::get().writes(7_u64)) } @@ -97,13 +97,13 @@ impl pallet_route_executor::WeightInfo for HydraWeight< fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `1637 + b * (1923 ±0)` - // Estimated: `6156 + b * (7749 ±245_709_589_663_843_264)` - // Minimum execution time: 68_440_000 picoseconds. - Weight::from_parts(69_699_000, 6156) - // Standard Error: 617_743 - .saturating_add(Weight::from_parts(2_520_114, 0).saturating_mul(c.into())) - // Standard Error: 1_356_122 - .saturating_add(Weight::from_parts(218_383_352, 0).saturating_mul(b.into())) + // Estimated: `6156 + b * (7749 ±228_644_766_292_339_040)` + // Minimum execution time: 93_611_000 picoseconds. + Weight::from_parts(94_339_000, 6156) + // Standard Error: 809_883 + .saturating_add(Weight::from_parts(3_194_585, 0).saturating_mul(c.into())) + // Standard Error: 1_777_925 + .saturating_add(Weight::from_parts(264_924_784, 0).saturating_mul(b.into())) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().reads((13_u64).saturating_mul(b.into()))) .saturating_add(T::DbWeight::get().writes((7_u64).saturating_mul(b.into()))) @@ -137,8 +137,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `7004` // Estimated: `39735` - // Minimum execution time: 1_666_103_000 picoseconds. - Weight::from_parts(1_695_742_000, 39735) + // Minimum execution time: 1_877_975_000 picoseconds. + Weight::from_parts(1_889_842_000, 39735) .saturating_add(T::DbWeight::get().reads(57_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -148,8 +148,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1012` // Estimated: `0` - // Minimum execution time: 20_250_000 picoseconds. - Weight::from_parts(20_950_000, 0) + // Minimum execution time: 25_643_000 picoseconds. + Weight::from_parts(26_239_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: `Router::Routes` (r:1 w:0) @@ -158,8 +158,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `800` // Estimated: `3555` - // Minimum execution time: 6_050_000 picoseconds. - Weight::from_parts(6_270_000, 3555) + // Minimum execution time: 7_713_000 picoseconds. + Weight::from_parts(7_908_000, 3555) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: `EmaOracle::Oracles` (r:2 w:0) @@ -168,8 +168,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1452` // Estimated: `6294` - // Minimum execution time: 21_710_000 picoseconds. - Weight::from_parts(22_040_000, 6294) + // Minimum execution time: 26_438_000 picoseconds. + Weight::from_parts(26_957_000, 6294) .saturating_add(T::DbWeight::get().reads(2_u64)) } /// Storage: `LBP::PoolData` (r:1 w:0) @@ -182,8 +182,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1674` // Estimated: `6156` - // Minimum execution time: 27_890_000 picoseconds. - Weight::from_parts(28_360_000, 6156) + // Minimum execution time: 36_073_000 picoseconds. + Weight::from_parts(36_488_000, 6156) .saturating_add(T::DbWeight::get().reads(4_u64)) } } \ No newline at end of file From 323db8c8332f13a4f7456f13866f2127b86ddcda Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 23 Jul 2024 16:05:06 +0200 Subject: [PATCH 21/28] include oracle price getting in the weights properly --- pallets/route-executor/src/weights.rs | 21 ++++++++++---- runtime/hydradx/src/assets.rs | 4 +-- .../src/benchmarking/route_executor.rs | 28 ++++++++++++++++++- .../src/weights/pallet_route_executor.rs | 19 ++++++++++--- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/pallets/route-executor/src/weights.rs b/pallets/route-executor/src/weights.rs index abf9039fc..018fd4a51 100644 --- a/pallets/route-executor/src/weights.rs +++ b/pallets/route-executor/src/weights.rs @@ -52,7 +52,8 @@ pub trait WeightInfo { fn calculate_and_execute_buy_in_lbp(c: u32, b: u32, ) -> Weight; fn set_route_for_xyk() -> Weight; fn force_insert_route() -> Weight; - fn get_oracle_price() -> Weight; + fn get_oracle_price_for_xyk() -> Weight; + fn get_oracle_price_for_omnipool() -> Weight; fn get_route() -> Weight; fn calculate_spot_price_with_fee_in_lbp() -> Weight; } @@ -170,14 +171,24 @@ impl WeightInfo for () { /// Storage: `EmaOracle::Oracles` (r:2 w:0) /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) - fn get_oracle_price() -> Weight { + fn get_oracle_price_for_xyk() -> Weight { // Proof Size summary in bytes: - // Measured: `1589` + // Measured: `1452` // Estimated: `6294` - // Minimum execution time: 24_019_000 picoseconds. - Weight::from_parts(24_870_000, 6294) + // Minimum execution time: 21_820_000 picoseconds. + Weight::from_parts(22_320_000, 6294) .saturating_add(RocksDbWeight::get().reads(2_u64)) } + /// Storage: `EmaOracle::Oracles` (r:4 w:0) + /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) + fn get_oracle_price_for_omnipool() -> Weight { + // Proof Size summary in bytes: + // Measured: `1814` + // Estimated: `11598` + // Minimum execution time: 35_720_000 picoseconds. + Weight::from_parts(36_110_000, 11598) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + } /// Storage: `LBP::PoolData` (r:1 w:0) /// Proof: `LBP::PoolData` (`max_values`: None, `max_size`: Some(163), added: 2638, mode: `MaxEncodedLen`) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index 97e334344..fcf554863 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -859,7 +859,7 @@ impl RouterWeightInfo { number_of_times_calculate_sell_amounts_executed, number_of_times_execute_sell_amounts_executed, )) - .saturating_sub(weights::pallet_route_executor::HydraWeight::::get_oracle_price()) + .saturating_sub(weights::pallet_route_executor::HydraWeight::::get_oracle_price_for_xyk()) } pub fn calculate_spot_price_overweight() -> Weight { @@ -1071,7 +1071,7 @@ impl AmmTradeWeights> for RouterWeightInfo { //Add a sell weight as we do a dry-run sell as validation let weight_of_get_oracle_price_for_2_assets = - weights::pallet_route_executor::HydraWeight::::get_oracle_price(); + weights::pallet_route_executor::HydraWeight::::get_oracle_price_for_omnipool(); let weight_of_get_oracle_price_for_route = weight_of_get_oracle_price_for_2_assets.saturating_mul(route.len() as u64); weight.saturating_accrue(weight_of_get_oracle_price_for_route); diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index 46bb44bb0..2ea6dd54d 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -38,6 +38,8 @@ use pallet_ema_oracle::OraclePeriod; use primitives::constants::currency::UNITS; use primitives::BlockNumber; use sp_std::vec; +use crate::Omnipool; + pub const INITIAL_BALANCE: Balance = 10_000_000 * UNITS; fn funded_account(name: &'static str, index: u32, assets: &[AssetId]) -> AccountId { @@ -364,7 +366,7 @@ runtime_benchmarks! { Router::get_route(AssetPair::new(HDX, DAI)) } - get_oracle_price { + get_oracle_price_for_xyk { let asset_2 = register_asset(b"AS2".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; let caller: AccountId = funded_account("caller", 0, &[asset_2]); @@ -385,6 +387,29 @@ runtime_benchmarks! { assert!(price.is_some()); } + //We use omnipool as it contains 2 reads when getting oracle price, so we can use it as worst case, and multiplying it with the length of the route + get_oracle_price_for_omnipool { + let asset_2 = register_asset(b"AS2".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; + + crate::benchmarking::omnipool::init()?; + let caller: AccountId = funded_account("caller", 0, &[HDX, DAI]); + Omnipool::sell(RawOrigin::Signed(caller).into(), HDX, DAI, 10 * UNITS, 0)?; + + set_period(10); + + let route = vec![Trade { + pool: PoolType::Omnipool, + asset_in: HDX, + asset_out: DAI + }]; + let mut price = None; + }: { + price = ::OraclePriceProvider::price(&route, OraclePeriod::TenMinutes); + } + verify { + assert!(price.is_some()); + } + // Calculates the weight of LBP spot price with fee calculation. Used in the calculation to determine the weight of the overhead. calculate_spot_price_with_fee_in_lbp { let asset_in = register_external_asset(b"FCA".to_vec()).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; @@ -457,6 +482,7 @@ mod tests { use frame_support::traits::OnFinalize; use frame_support::traits::OnInitialize; +use sp_runtime::FixedU128; fn set_period(to: u32) { while System::block_number() < Into::::into(to) { diff --git a/runtime/hydradx/src/weights/pallet_route_executor.rs b/runtime/hydradx/src/weights/pallet_route_executor.rs index 71a379223..9ceab0a46 100644 --- a/runtime/hydradx/src/weights/pallet_route_executor.rs +++ b/runtime/hydradx/src/weights/pallet_route_executor.rs @@ -164,14 +164,25 @@ impl pallet_route_executor::WeightInfo for HydraWeight< } /// Storage: `EmaOracle::Oracles` (r:2 w:0) /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) - fn get_oracle_price() -> Weight { + fn get_oracle_price_for_xyk() -> Weight { // Proof Size summary in bytes: // Measured: `1452` // Estimated: `6294` - // Minimum execution time: 26_438_000 picoseconds. - Weight::from_parts(26_957_000, 6294) - .saturating_add(T::DbWeight::get().reads(2_u64)) + // Minimum execution time: 21_820_000 picoseconds. + Weight::from_parts(22_320_000, 6294) + .saturating_add(RocksDbWeight::get().reads(2_u64)) } + /// Storage: `EmaOracle::Oracles` (r:4 w:0) + /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) + fn get_oracle_price_for_omnipool() -> Weight { + // Proof Size summary in bytes: + // Measured: `1814` + // Estimated: `11598` + // Minimum execution time: 35_720_000 picoseconds. + Weight::from_parts(36_110_000, 11598) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + } + /// Storage: `LBP::PoolData` (r:1 w:0) /// Proof: `LBP::PoolData` (`max_values`: None, `max_size`: Some(163), added: 2638, mode: `MaxEncodedLen`) /// Storage: `Tokens::Locks` (r:1 w:0) From 016b6fd7ba670727b7af91bb58eb7dcc82ddf869 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 23 Jul 2024 16:11:01 +0200 Subject: [PATCH 22/28] remove unused import --- runtime/hydradx/src/benchmarking/route_executor.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index 2ea6dd54d..0c9a88e9f 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -482,7 +482,6 @@ mod tests { use frame_support::traits::OnFinalize; use frame_support::traits::OnInitialize; -use sp_runtime::FixedU128; fn set_period(to: u32) { while System::block_number() < Into::::into(to) { From 1718990b9cb1b967d8b34399828b8fe5c36c5e87 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 23 Jul 2024 16:11:26 +0200 Subject: [PATCH 23/28] rebenchmark router on ref machine --- .../src/weights/pallet_route_executor.rs | 51 +++++++++---------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/runtime/hydradx/src/weights/pallet_route_executor.rs b/runtime/hydradx/src/weights/pallet_route_executor.rs index 9ceab0a46..c24ae0a0a 100644 --- a/runtime/hydradx/src/weights/pallet_route_executor.rs +++ b/runtime/hydradx/src/weights/pallet_route_executor.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for `pallet_route_executor` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 32.0.0 -//! DATE: 2024-07-22, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2024-07-23, STEPS: `10`, REPEAT: `30`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `bench-bot`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz` //! WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: `1024` @@ -71,10 +71,10 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `3535` // Estimated: `13905` - // Minimum execution time: 345_896_000 picoseconds. - Weight::from_parts(348_809_433, 13905) - // Standard Error: 234_126 - .saturating_add(Weight::from_parts(80_768_066, 0).saturating_mul(c.into())) + // Minimum execution time: 347_049_000 picoseconds. + Weight::from_parts(350_762_308, 13905) + // Standard Error: 214_404 + .saturating_add(Weight::from_parts(69_087_003, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(16_u64)) .saturating_add(T::DbWeight::get().writes(7_u64)) } @@ -98,12 +98,12 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1637 + b * (1923 ±0)` // Estimated: `6156 + b * (7749 ±228_644_766_292_339_040)` - // Minimum execution time: 93_611_000 picoseconds. - Weight::from_parts(94_339_000, 6156) - // Standard Error: 809_883 - .saturating_add(Weight::from_parts(3_194_585, 0).saturating_mul(c.into())) - // Standard Error: 1_777_925 - .saturating_add(Weight::from_parts(264_924_784, 0).saturating_mul(b.into())) + // Minimum execution time: 94_096_000 picoseconds. + Weight::from_parts(95_234_000, 6156) + // Standard Error: 881_744 + .saturating_add(Weight::from_parts(3_499_280, 0).saturating_mul(c.into())) + // Standard Error: 1_935_679 + .saturating_add(Weight::from_parts(266_101_120, 0).saturating_mul(b.into())) .saturating_add(T::DbWeight::get().reads(3_u64)) .saturating_add(T::DbWeight::get().reads((13_u64).saturating_mul(b.into()))) .saturating_add(T::DbWeight::get().writes((7_u64).saturating_mul(b.into()))) @@ -137,8 +137,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `7004` // Estimated: `39735` - // Minimum execution time: 1_877_975_000 picoseconds. - Weight::from_parts(1_889_842_000, 39735) + // Minimum execution time: 1_880_149_000 picoseconds. + Weight::from_parts(1_889_973_000, 39735) .saturating_add(T::DbWeight::get().reads(57_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } @@ -148,8 +148,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1012` // Estimated: `0` - // Minimum execution time: 25_643_000 picoseconds. - Weight::from_parts(26_239_000, 0) + // Minimum execution time: 25_566_000 picoseconds. + Weight::from_parts(26_013_000, 0) .saturating_add(T::DbWeight::get().writes(1_u64)) } /// Storage: `Router::Routes` (r:1 w:0) @@ -158,8 +158,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `800` // Estimated: `3555` - // Minimum execution time: 7_713_000 picoseconds. - Weight::from_parts(7_908_000, 3555) + // Minimum execution time: 7_753_000 picoseconds. + Weight::from_parts(7_998_000, 3555) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: `EmaOracle::Oracles` (r:2 w:0) @@ -168,9 +168,9 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1452` // Estimated: `6294` - // Minimum execution time: 21_820_000 picoseconds. - Weight::from_parts(22_320_000, 6294) - .saturating_add(RocksDbWeight::get().reads(2_u64)) + // Minimum execution time: 26_531_000 picoseconds. + Weight::from_parts(27_010_000, 6294) + .saturating_add(T::DbWeight::get().reads(2_u64)) } /// Storage: `EmaOracle::Oracles` (r:4 w:0) /// Proof: `EmaOracle::Oracles` (`max_values`: None, `max_size`: Some(177), added: 2652, mode: `MaxEncodedLen`) @@ -178,11 +178,10 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1814` // Estimated: `11598` - // Minimum execution time: 35_720_000 picoseconds. - Weight::from_parts(36_110_000, 11598) - .saturating_add(RocksDbWeight::get().reads(4_u64)) + // Minimum execution time: 40_726_000 picoseconds. + Weight::from_parts(41_280_000, 11598) + .saturating_add(T::DbWeight::get().reads(4_u64)) } - /// Storage: `LBP::PoolData` (r:1 w:0) /// Proof: `LBP::PoolData` (`max_values`: None, `max_size`: Some(163), added: 2638, mode: `MaxEncodedLen`) /// Storage: `Tokens::Locks` (r:1 w:0) @@ -193,8 +192,8 @@ impl pallet_route_executor::WeightInfo for HydraWeight< // Proof Size summary in bytes: // Measured: `1674` // Estimated: `6156` - // Minimum execution time: 36_073_000 picoseconds. - Weight::from_parts(36_488_000, 6156) + // Minimum execution time: 35_350_000 picoseconds. + Weight::from_parts(35_956_000, 6156) .saturating_add(T::DbWeight::get().reads(4_u64)) } } \ No newline at end of file From 45210176de504d15d2d7886ee57d30b9f3e214eb Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 23 Jul 2024 16:14:48 +0200 Subject: [PATCH 24/28] formatting --- runtime/hydradx/src/benchmarking/route_executor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index 0c9a88e9f..44c72ce4a 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -24,6 +24,7 @@ use crate::{ use super::*; use crate::benchmarking::dca::{DAI, HDX}; +use crate::Omnipool; use frame_benchmarking::{account, BenchmarkError}; use frame_support::dispatch::DispatchResult; use frame_support::{assert_ok, ensure}; @@ -38,7 +39,6 @@ use pallet_ema_oracle::OraclePeriod; use primitives::constants::currency::UNITS; use primitives::BlockNumber; use sp_std::vec; -use crate::Omnipool; pub const INITIAL_BALANCE: Balance = 10_000_000 * UNITS; From 868662fbfa6117b2ec3df6e83bf31256ac7491e7 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 23 Jul 2024 16:17:35 +0200 Subject: [PATCH 25/28] adjust comments --- runtime/hydradx/src/assets.rs | 2 +- runtime/hydradx/src/benchmarking/route_executor.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index fcf554863..de211e7d1 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1069,7 +1069,7 @@ impl AmmTradeWeights> for RouterWeightInfo { weight.saturating_accrue(amm_weight); } - //Add a sell weight as we do a dry-run sell as validation + //Include oracle price calculation. let weight_of_get_oracle_price_for_2_assets = weights::pallet_route_executor::HydraWeight::::get_oracle_price_for_omnipool(); let weight_of_get_oracle_price_for_route = diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index 44c72ce4a..efb38a020 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -387,7 +387,7 @@ runtime_benchmarks! { assert!(price.is_some()); } - //We use omnipool as it contains 2 reads when getting oracle price, so we can use it as worst case, and multiplying it with the length of the route + //We use omnipool as it contains 4 reads when getting oracle price, so we can use it as worst case, and multiplying it with the length of the route get_oracle_price_for_omnipool { let asset_2 = register_asset(b"AS2".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; From de4ade3ed5b401980a4bbbf710c4cd2101e82d05 Mon Sep 17 00:00:00 2001 From: dmoka Date: Tue, 23 Jul 2024 16:47:22 +0200 Subject: [PATCH 26/28] add comments for explaining why we use omnipool --- runtime/hydradx/src/assets.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/hydradx/src/assets.rs b/runtime/hydradx/src/assets.rs index de211e7d1..3d555f48f 100644 --- a/runtime/hydradx/src/assets.rs +++ b/runtime/hydradx/src/assets.rs @@ -1069,7 +1069,8 @@ impl AmmTradeWeights> for RouterWeightInfo { weight.saturating_accrue(amm_weight); } - //Include oracle price calculation. + // Incorporate oracle price calculation + // We use omnipool as reference as it is the worst case for calculating oracle price let weight_of_get_oracle_price_for_2_assets = weights::pallet_route_executor::HydraWeight::::get_oracle_price_for_omnipool(); let weight_of_get_oracle_price_for_route = From bdad11fea12432672eccc038797e263838bced1f Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 24 Jul 2024 07:45:38 +0200 Subject: [PATCH 27/28] add comment to describe bench test --- runtime/hydradx/src/benchmarking/route_executor.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/hydradx/src/benchmarking/route_executor.rs b/runtime/hydradx/src/benchmarking/route_executor.rs index efb38a020..d7cca10a9 100644 --- a/runtime/hydradx/src/benchmarking/route_executor.rs +++ b/runtime/hydradx/src/benchmarking/route_executor.rs @@ -366,6 +366,7 @@ runtime_benchmarks! { Router::get_route(AssetPair::new(HDX, DAI)) } + //To calculate the overweight poperly we use this to substract get_oracle_price_for_xyk { let asset_2 = register_asset(b"AS2".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?; From f9877b7e9f2215349e6111a3123a67023e9c072b Mon Sep 17 00:00:00 2001 From: dmoka Date: Wed, 24 Jul 2024 07:45:50 +0200 Subject: [PATCH 28/28] bump runtime version --- Cargo.lock | 2 +- runtime/hydradx/Cargo.toml | 2 +- runtime/hydradx/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c8235177..2e52285cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4938,7 +4938,7 @@ dependencies = [ [[package]] name = "hydradx-runtime" -version = "250.0.0" +version = "251.0.0" dependencies = [ "cumulus-pallet-aura-ext", "cumulus-pallet-parachain-system", diff --git a/runtime/hydradx/Cargo.toml b/runtime/hydradx/Cargo.toml index 58ef6475b..ec93adbf1 100644 --- a/runtime/hydradx/Cargo.toml +++ b/runtime/hydradx/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydradx-runtime" -version = "250.0.0" +version = "251.0.0" authors = ["GalacticCouncil"] edition = "2021" license = "Apache 2.0" diff --git a/runtime/hydradx/src/lib.rs b/runtime/hydradx/src/lib.rs index fc5b5e3f8..be0f65d6b 100644 --- a/runtime/hydradx/src/lib.rs +++ b/runtime/hydradx/src/lib.rs @@ -113,7 +113,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("hydradx"), impl_name: create_runtime_str!("hydradx"), authoring_version: 1, - spec_version: 250, + spec_version: 251, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,