1717// If you feel like getting in touch with us, you can do so at <hello@kilt.io>
1818use frame_benchmarking:: v2:: * ;
1919use frame_support:: traits:: fungibles:: roles:: Inspect as InspectRoles ;
20+ use scale_info:: prelude:: format;
2021use sp_core:: U256 ;
2122use sp_std:: {
2223 ops:: { AddAssign , BitOrAssign , ShlAssign } ,
@@ -27,9 +28,11 @@ use substrate_fixed::traits::{Fixed, FixedSigned, FixedUnsigned, ToFixed};
2728use crate :: {
2829 curves:: {
2930 lmsr:: { LMSRParameters , LMSRParametersInput } ,
31+ polynomial:: PolynomialParameters ,
3032 square_root:: { SquareRootParameters , SquareRootParametersInput } ,
3133 Curve , CurveInput ,
3234 } ,
35+ types:: BondedCurrenciesSettings ,
3336 Call , CollateralAssetIdOf , CollateralBalanceOf , Config , CurveParameterTypeOf , FungiblesAssetIdOf ,
3437 FungiblesBalanceOf , Pallet ,
3538} ;
6366 fn set_native_balance ( _account : & <T >:: AccountId , _amount : u128 ) { }
6467}
6568
69+ fn get_2nd_order_polynomial_curve < Float : FixedSigned > ( ) -> Curve < Float > {
70+ let m = Float :: from_num ( 0.01 ) ;
71+ let n = Float :: from_num ( 2 ) ;
72+ let o = Float :: from_num ( 3 ) ;
73+ Curve :: Polynomial ( PolynomialParameters { m, n, o } )
74+ }
75+
6676fn get_square_root_curve < Float : FixedSigned > ( ) -> Curve < Float > {
6777 let m = Float :: from_num ( 3 ) ;
6878 let n = Float :: from_num ( 2 ) ;
@@ -220,10 +230,13 @@ mod benchmarks {
220230 owner,
221231 state,
222232 collateral,
223- denomination,
224233 bonded_currencies : BoundedVec :: truncate_from ( bonded_coin_ids. clone ( ) ) ,
225- transferable : true ,
226- min_operation_balance : 1u128 . saturated_into ( ) ,
234+ currencies_settings : BondedCurrenciesSettings {
235+ denomination,
236+ transferable : true ,
237+ allow_reset_team : true ,
238+ min_operation_balance : 1u128 . saturated_into ( ) ,
239+ } ,
227240 deposit : Pallet :: < T > :: calculate_pool_deposit ( bonded_coin_ids. len ( ) ) ,
228241 } ;
229242 Pools :: < T > :: insert ( & pool_id, pool_details) ;
@@ -233,11 +246,11 @@ mod benchmarks {
233246
234247 fn generate_token_metadata < T : Config > ( c : u32 ) -> BoundedVec < TokenMetaOf < T > , T :: MaxCurrenciesPerPool > {
235248 let mut token_meta = Vec :: new ( ) ;
236- for _ in 1 ..=c {
249+ for i in 1 ..=c {
237250 token_meta. push ( TokenMetaOf :: < T > {
238251 min_balance : 1u128 . saturated_into ( ) ,
239- name : BoundedVec :: try_from ( b"BTC" . to_vec ( ) ) . expect ( "Failed to create BoundedVec" ) ,
240- symbol : BoundedVec :: try_from ( b"BTC" . to_vec ( ) ) . expect ( "Failed to create BoundedVec" ) ,
252+ name : BoundedVec :: try_from ( format ! ( "Coin_{}" , & i ) . into_bytes ( ) ) . expect ( "Failed to create BoundedVec" ) ,
253+ symbol : BoundedVec :: try_from ( format ! ( "BTC_{}" , & i ) . into_bytes ( ) ) . expect ( "Failed to create BoundedVec" ) ,
241254 } )
242255 }
243256 BoundedVec :: try_from ( token_meta) . expect ( "creating bounded Vec should not fail" )
@@ -263,9 +276,12 @@ mod benchmarks {
263276 curve,
264277 collateral_id,
265278 currencies,
266- 10 ,
267- true ,
268- 1 ,
279+ BondedCurrenciesSettings {
280+ denomination : 10 ,
281+ allow_reset_team : true ,
282+ transferable : true ,
283+ min_operation_balance : 1u128 . saturated_into ( ) ,
284+ } ,
269285 ) ;
270286
271287 // Verify
@@ -301,9 +317,12 @@ mod benchmarks {
301317 curve,
302318 collateral_id,
303319 currencies,
304- 10 ,
305- true ,
306- 1 ,
320+ BondedCurrenciesSettings {
321+ denomination : 10 ,
322+ allow_reset_team : true ,
323+ transferable : true ,
324+ min_operation_balance : 1u128 . saturated_into ( ) ,
325+ } ,
307326 ) ;
308327
309328 // Verify
@@ -313,7 +332,7 @@ mod benchmarks {
313332 Curve :: SquareRoot ( _) => {
314333 assert_eq ! ( id, expected_pool_id) ;
315334 }
316- _ => panic ! ( "pool.curve is not a Polynomial function" ) ,
335+ _ => panic ! ( "pool.curve is not a SquareRoot function" ) ,
317336 }
318337 }
319338
@@ -338,9 +357,12 @@ mod benchmarks {
338357 curve,
339358 collateral_id,
340359 currencies,
341- 10 ,
342- true ,
343- 1 ,
360+ BondedCurrenciesSettings {
361+ denomination : 10 ,
362+ allow_reset_team : true ,
363+ transferable : true ,
364+ min_operation_balance : 1u128 . saturated_into ( ) ,
365+ } ,
344366 ) ;
345367
346368 // Verify
@@ -350,30 +372,27 @@ mod benchmarks {
350372 Curve :: Lmsr ( _) => {
351373 assert_eq ! ( id, expected_pool_id) ;
352374 }
353- _ => panic ! ( "pool.curve is not a Polynomial function " ) ,
375+ _ => panic ! ( "pool.curve is not a LSMR curve! " ) ,
354376 }
355377 }
356378
357379 #[ benchmark]
358- fn reset_team ( ) {
380+ fn reset_team ( c : Linear < 1 , { T :: MaxCurrenciesPerPool :: get ( ) } > ) {
359381 let origin = T :: DefaultOrigin :: try_successful_origin ( ) . expect ( "creating origin should not fail" ) ;
360382 let account_origin = origin
361383 . clone ( )
362384 . into_signer ( )
363385 . expect ( "generating account_id from origin should not fail" ) ;
364386 make_free_for_deposit :: < T > ( & account_origin) ;
365387
366- let bonded_coin_id = T :: BenchmarkHelper :: calculate_bonded_asset_id ( 0 ) ;
367- create_bonded_asset :: < T > ( bonded_coin_id. clone ( ) ) ;
388+ let bonded_currencies = create_bonded_currencies_in_range :: < T > ( c, false ) ;
368389
369390 let curve = get_linear_bonding_curve :: < CurveParameterTypeOf < T > > ( ) ;
370- let pool_id = create_pool :: < T > (
371- curve,
372- [ bonded_coin_id. clone ( ) ] . to_vec ( ) ,
373- Some ( account_origin) ,
374- None ,
375- None ,
376- ) ;
391+ let pool_id = create_pool :: < T > ( curve, bonded_currencies. clone ( ) , Some ( account_origin) , None , None ) ;
392+
393+ // Although these would rarely happen in practice, for benchmarking we assume
394+ // the worst case where the owner must be changed as well
395+ assert ! ( T :: Fungibles :: owner( bonded_currencies[ 0 ] . clone( ) ) != Some ( pool_id. clone( ) . into( ) ) ) ;
377396
378397 let admin: AccountIdOf < T > = account ( "admin" , 0 , 0 ) ;
379398 let freezer: AccountIdOf < T > = account ( "freezer" , 0 , 0 ) ;
@@ -382,12 +401,24 @@ mod benchmarks {
382401 freezer : freezer. clone ( ) ,
383402 } ;
384403
404+ let pool_id_for_call = pool_id. clone ( ) ;
405+
406+ let max_currencies = T :: MaxCurrenciesPerPool :: get ( ) ;
385407 #[ extrinsic_call]
386- _ ( origin as T :: RuntimeOrigin , pool_id, fungibles_team, 0 ) ;
408+ _ (
409+ origin as T :: RuntimeOrigin ,
410+ pool_id_for_call,
411+ fungibles_team,
412+ max_currencies,
413+ ) ;
387414
388415 // Verify
389- assert_eq ! ( T :: Fungibles :: admin( bonded_coin_id. clone( ) ) , Some ( admin) ) ;
390- assert_eq ! ( T :: Fungibles :: freezer( bonded_coin_id) , Some ( freezer) ) ;
416+ bonded_currencies. iter ( ) . for_each ( |asset_id| {
417+ assert_eq ! ( T :: Fungibles :: admin( asset_id. clone( ) ) , Some ( admin. clone( ) ) ) ;
418+ assert_eq ! ( T :: Fungibles :: freezer( asset_id. clone( ) ) , Some ( freezer. clone( ) ) ) ;
419+ assert_eq ! ( T :: Fungibles :: owner( asset_id. clone( ) ) , Some ( pool_id. clone( ) . into( ) ) ) ;
420+ assert_eq ! ( T :: Fungibles :: issuer( asset_id. clone( ) ) , Some ( pool_id. clone( ) . into( ) ) ) ;
421+ } ) ;
391422 }
392423
393424 #[ benchmark]
@@ -475,7 +506,7 @@ mod benchmarks {
475506 make_free_for_deposit :: < T > ( & account_origin) ;
476507 set_collateral_balance :: < T > ( collateral_id. clone ( ) , & account_origin, 10000u128 ) ;
477508
478- let curve = get_linear_bonding_curve :: < CurveParameterTypeOf < T > > ( ) ;
509+ let curve = get_2nd_order_polynomial_curve :: < CurveParameterTypeOf < T > > ( ) ;
479510 let bonded_currencies = create_bonded_currencies_in_range :: < T > ( c, false ) ;
480511
481512 let pool_id = create_pool :: < T > ( curve, bonded_currencies. clone ( ) , None , None , Some ( 0 ) ) ;
@@ -605,7 +636,7 @@ mod benchmarks {
605636 let start_balance = 100u128 ;
606637 set_fungible_balance :: < T > ( target_asset_id. clone ( ) , & account_origin, start_balance) ;
607638
608- let curve = get_linear_bonding_curve :: < CurveParameterTypeOf < T > > ( ) ;
639+ let curve = get_2nd_order_polynomial_curve :: < CurveParameterTypeOf < T > > ( ) ;
609640
610641 let pool_id = create_pool :: < T > ( curve, bonded_currencies, None , None , Some ( 0 ) ) ;
611642 let pool_account = pool_id. clone ( ) . into ( ) ;
0 commit comments