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