diff --git a/modules/homa-lite/src/benchmarking.rs b/modules/homa-lite/src/benchmarking.rs index 3bb41fbcc..07b318a4d 100644 --- a/modules/homa-lite/src/benchmarking.rs +++ b/modules/homa-lite/src/benchmarking.rs @@ -30,23 +30,6 @@ pub struct Module(crate::Pallet); const SEED: u32 = 0; benchmarks! { - on_idle { - let amount = 1_000_000_000_000; - let caller: T::AccountId = account("caller", 0, SEED); - let caller1: T::AccountId = account("callera", 0, SEED); - let caller2: T::AccountId = account("callerb", 0, SEED); - let caller3: T::AccountId = account("callerc", 0, SEED); - ::Currency::deposit(T::LiquidCurrencyId::get(), &caller1, amount)?; - ::Currency::deposit(T::LiquidCurrencyId::get(), &caller2, amount)?; - ::Currency::deposit(T::LiquidCurrencyId::get(), &caller3, amount)?; - let _ = crate::Pallet::::request_redeem(RawOrigin::Signed(caller1).into(), amount, Permill::default()); - let _ = crate::Pallet::::request_redeem(RawOrigin::Signed(caller2.clone()).into(), amount, Permill::default()); - let _ = crate::Pallet::::request_redeem(RawOrigin::Signed(caller3.clone()).into(), amount, Permill::default()); - let _ = crate::Pallet::::schedule_unbond(RawOrigin::Root.into(), amount*2, ::BlockNumber::default()); - }: { - let _ = crate::Pallet::::on_idle(::BlockNumber::default(), 1_000_000_000); - } - mint { let amount = 1_000_000_000_000; let caller: T::AccountId = account("caller", 0, SEED); @@ -73,7 +56,11 @@ benchmarks! { set_total_staking_currency {}: _(RawOrigin::Root, 1_000_000_000_000) - adjust_total_staking_currency {}: _(RawOrigin::Root, AmountOf::::default()) + adjust_total_staking_currency {}: _(RawOrigin::Root, AmountOf::::max_value()) + + adjust_available_staking_balance_with_no_matches {}: { + let _ = crate::Pallet::::adjust_available_staking_balance(RawOrigin::Root.into(), AmountOf::::max_value(), 0); + } set_minting_cap { }: _(RawOrigin::Root, 1_000_000_000_000_000_000) @@ -90,6 +77,20 @@ benchmarks! { schedule_unbond {}: _(RawOrigin::Root, 1_000_000_000_000, ::BlockNumber::default()) replace_schedule_unbond {}: _(RawOrigin::Root, vec![(1_000_000, ::BlockNumber::default()), (1_000_000_000, ::BlockNumber::default())]) + + redeem_with_available_staking_balance { + let amount = 1_000_000_000_000_000; + let caller: T::AccountId = account("caller", 0, SEED); + ::Currency::deposit(T::LiquidCurrencyId::get(), &caller, amount)?; + let _ = crate::Pallet::::adjust_available_staking_balance(RawOrigin::Root.into(), AmountOf::::max_value(), 1); + let _ = crate::Pallet::::request_redeem(RawOrigin::Signed(caller).into(), amount, Permill::default()); + }: { + let _ = crate::Pallet::::process_redeem_requests_with_available_staking_balance(1); + } + + xcm_unbond {}: { + let _ = crate::Pallet::::process_scheduled_unbond(1_000_000_000_000_000); + } } #[cfg(test)] @@ -98,13 +99,6 @@ mod tests { use crate::mock::*; use frame_support::assert_ok; - #[test] - fn test_on_idle() { - ExtBuilder::default().build().execute_with(|| { - assert_ok!(Pallet::::test_benchmark_on_idle()); - }); - } - #[test] fn test_mint() { ExtBuilder::default().build().execute_with(|| { @@ -129,6 +123,13 @@ mod tests { assert_ok!(Pallet::::test_benchmark_adjust_total_staking_currency()); }); } + #[test] + fn test_adjust_available_staking_balance_with_no_matches() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Pallet::::test_benchmark_adjust_available_staking_balance_with_no_matches()); + }); + } + #[test] fn test_set_minting_cap() { ExtBuilder::default().build().execute_with(|| { @@ -159,4 +160,16 @@ mod tests { assert_ok!(Pallet::::test_benchmark_replace_schedule_unbond()); }); } + #[test] + fn test_redeem_with_available_staking_balance() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Pallet::::test_benchmark_redeem_with_available_staking_balance()); + }); + } + #[test] + fn test_xcm_unbond() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Pallet::::test_benchmark_xcm_unbond()); + }); + } } diff --git a/modules/homa-lite/src/lib.rs b/modules/homa-lite/src/lib.rs index 23c88683a..8e87285dc 100644 --- a/modules/homa-lite/src/lib.rs +++ b/modules/homa-lite/src/lib.rs @@ -199,6 +199,10 @@ pub mod module { /// The scheduled Unbond has been withdrew from the RelayChain. ///\[staking_amount_added\] ScheduledUnbondWithdrew(Balance), + + /// The amount of the staking currency available to be redeemed is set. + /// \[total_available_staking_balance\] + AvailableStakingBalanceSet(Balance), } /// The total amount of the staking currency on the relaychain. @@ -245,9 +249,9 @@ pub mod module { #[pallet::hooks] impl Hooks for Pallet { fn on_idle(_n: T::BlockNumber, remaining_weight: Weight) -> Weight { - let required_weight = ::WeightInfo::on_idle(); let mut current_weight = 0; - if remaining_weight > required_weight { + // If enough weight, process the next XCM unbond. + if remaining_weight > ::WeightInfo::xcm_unbond() { let mut scheduled_unbond = Self::scheduled_unbond(); if !scheduled_unbond.is_empty() { let (staking_amount, block_number) = scheduled_unbond[0]; @@ -257,7 +261,7 @@ pub mod module { debug_assert!(res.is_ok()); if res.is_ok() { - current_weight = required_weight; + current_weight = ::WeightInfo::xcm_unbond(); scheduled_unbond.remove(0); ScheduledUnbond::::put(scheduled_unbond); @@ -265,6 +269,22 @@ pub mod module { } } } + + // With remaining weight, calculate max number of redeems that can be matched + let num_redeem_matches = remaining_weight + .saturating_sub(current_weight) + .checked_div(::WeightInfo::redeem_with_available_staking_balance()) + .unwrap_or_default(); + + // Iterate through existing redeem_requests, and try to match them with `available_staking_balance` + let res = Self::process_redeem_requests_with_available_staking_balance(num_redeem_matches as u32); + debug_assert!(res.is_ok()); + if let Ok(count) = res { + current_weight = current_weight.saturating_add( + ::WeightInfo::redeem_with_available_staking_balance().saturating_mul(count as Weight), + ); + } + current_weight } } @@ -318,7 +338,6 @@ pub mod module { #[transactional] pub fn adjust_total_staking_currency(origin: OriginFor, by_amount: AmountOf) -> DispatchResult { T::GovernanceOrigin::ensure_origin(origin)?; - let mut current_staking_total = Self::total_staking_currency(); // Convert AmountOf into Balance safely. let by_amount_abs = if by_amount == AmountOf::::min_value() { @@ -329,19 +348,21 @@ pub mod module { let by_balance = TryInto::::try_into(by_amount_abs).map_err(|_| ArithmeticError::Overflow)?; - // Adjust the current total. - if by_amount.is_positive() { - current_staking_total = current_staking_total - .checked_add(by_balance) - .ok_or(ArithmeticError::Overflow)?; - } else { - current_staking_total = current_staking_total - .checked_sub(by_balance) - .ok_or(ArithmeticError::Underflow)?; - } + // ensure TotalStakingCurrency doesn't become 0 + ensure!( + by_amount.is_positive() || by_balance < Self::total_staking_currency(), + Error::::InvalidTotalStakingCurrency + ); - TotalStakingCurrency::::put(current_staking_total); - Self::deposit_event(Event::::TotalStakingCurrencySet(current_staking_total)); + // Adjust the current total. + TotalStakingCurrency::::mutate(|current| { + if by_amount.is_positive() { + *current = current.saturating_add(by_balance); + } else { + *current = current.saturating_sub(by_balance); + } + Self::deposit_event(Event::::TotalStakingCurrencySet(*current)); + }); Ok(()) } @@ -567,6 +588,53 @@ pub mod module { Ok(()) } + + /// Adjusts the AvailableStakingBalance by the given difference. + /// Also attempt to process queued redeem request with the new Staking Balance. + /// Requires `T::GovernanceOrigin` + /// + /// Parameters: + /// - `adjustment`: The difference in amount the AvailableStakingBalance should be adjusted + /// by. + /// + /// Weight: Weight(xcm unbond) + n * Weight(match redeem requests), where n is number of + /// redeem requests matched. + #[pallet::weight( + < T as Config >::WeightInfo::adjust_available_staking_balance_with_no_matches().saturating_add( + (*max_num_matches as Weight).saturating_mul(< T as Config >::WeightInfo::redeem_with_available_staking_balance()) + ) + )] + #[transactional] + pub fn adjust_available_staking_balance( + origin: OriginFor, + by_amount: AmountOf, + max_num_matches: u32, + ) -> DispatchResult { + T::GovernanceOrigin::ensure_origin(origin)?; + + // Convert AmountOf into Balance safely. + let by_amount_abs = if by_amount == AmountOf::::min_value() { + AmountOf::::max_value() + } else { + by_amount.abs() + }; + + let by_balance = TryInto::::try_into(by_amount_abs).map_err(|_| ArithmeticError::Overflow)?; + + // Adjust the current total. + AvailableStakingBalance::::mutate(|current| { + if by_amount.is_positive() { + *current = current.saturating_add(by_balance); + } else { + *current = current.saturating_sub(by_balance); + } + Self::deposit_event(Event::::AvailableStakingBalanceSet(*current)); + }); + + // With new staking balance available, process pending redeem requests. + let _ = Self::process_redeem_requests_with_available_staking_balance(max_num_matches)?; + Ok(()) + } } impl Pallet { @@ -775,25 +843,51 @@ pub mod module { Ok(()) } + /// Construct XCM message and sent it to the relaychain to withdraw_unbonded Staking + /// currency. The staking currency withdrew becomes available to be redeemed. + /// + /// params: + /// - `staking_amount_unbonded`: amount of staking currency to withdraw unbond via XCM #[transactional] - fn process_scheduled_unbond(staking_amount: Balance) -> DispatchResult { - let msg = Self::construct_xcm_unreserve_message(T::ParachainAccount::get(), staking_amount); + pub fn process_scheduled_unbond(staking_amount_unbonded: Balance) -> DispatchResult { + let msg = Self::construct_xcm_unreserve_message(T::ParachainAccount::get(), staking_amount_unbonded); let res = pallet_xcm::Pallet::::send_xcm(Here, Parent, msg); log::debug!("on_idle XCM result: {:?}", res); ensure!(res.is_ok(), Error::::XcmFailed); - // Now that there's available staking balance, automatically match existing - // redeem_requests. + // Update storage with the new available amount + AvailableStakingBalance::::mutate(|current| { + *current = current.saturating_add(staking_amount_unbonded); + }); + + Self::deposit_event(Event::::ScheduledUnbondWithdrew(staking_amount_unbonded)); + Ok(()) + } + + /// Iterate through all redeem requests, then match them with available_staking_balance. + /// This should be called when new available_staking_balance becomes available. + /// + /// params: + /// - `max_num_matches`: Maximum number of redeem requests to be matched. + /// + /// return: + /// Result: The number of redeem reqeusts actually matched. + #[transactional] + pub fn process_redeem_requests_with_available_staking_balance( + max_num_matches: u32, + ) -> Result { + if max_num_matches.is_zero() { + return Ok(0); + } + let mut available_staking_balance = Self::available_staking_balance(); + if available_staking_balance < T::MinimumMintThreshold::get() { + return Ok(0); + } + let mut new_balances: Vec<(T::AccountId, Balance, Permill)> = vec![]; - let mut available_staking_balance = Self::available_staking_balance() - .checked_add(staking_amount) - .ok_or(ArithmeticError::Overflow)?; + let mut num_matched = 0u32; for (redeemer, (request_amount, extra_fee)) in RedeemRequests::::iter() { - // If all the currencies are minted, return. - if available_staking_balance.is_zero() { - break; - } let actual_liquid_amount = min( request_amount, Self::convert_staking_to_liquid(available_staking_balance)?, @@ -821,20 +915,26 @@ pub mod module { actual_staking_amount, actual_liquid_amount, )); + num_matched += 1u32; + + // If all the currencies are minted, return. + if available_staking_balance < T::MinimumMintThreshold::get() || num_matched >= max_num_matches { + break; + } } // Update storage to the new balances. Remove Redeem requests that have been filled. Self::update_redeem_requests(&new_balances); AvailableStakingBalance::::put(available_staking_balance); - Self::deposit_event(Event::::ScheduledUnbondWithdrew(staking_amount)); - Ok(()) + Ok(num_matched) } + /// Update the RedeemRequests storage with the new balances. + /// Remove Redeem requests that are dust, or have been filled. #[allow(clippy::ptr_arg)] fn update_redeem_requests(new_balances: &Vec<(T::AccountId, Balance, Permill)>) { - // Update storage with the new balances. Remove Redeem requests that have been filled. for (redeemer, new_balance, extra_fee) in new_balances { if Self::liquid_amount_is_above_minimum_threshold(*new_balance) { RedeemRequests::::insert(&redeemer, (*new_balance, *extra_fee)); diff --git a/modules/homa-lite/src/mock.rs b/modules/homa-lite/src/mock.rs index cb047ce9a..6e7cca5be 100644 --- a/modules/homa-lite/src/mock.rs +++ b/modules/homa-lite/src/mock.rs @@ -254,8 +254,8 @@ impl module_currencies::Config for Runtime { parameter_types! { pub const StakingCurrencyId: CurrencyId = KSM; pub const LiquidCurrencyId: CurrencyId = LKSM; - pub MinimumMintThreshold: Balance = millicent(1); - pub MinimumRedeemThreshold: Balance = millicent(1); + pub MinimumMintThreshold: Balance = millicent(1000); + pub MinimumRedeemThreshold: Balance = millicent(1000); pub const MockXcmDestination: MultiLocation = MOCK_XCM_DESTINATION; pub const MockXcmAccountId: AccountId = MOCK_XCM_ACCOUNTID; pub DefaultExchangeRate: ExchangeRate = ExchangeRate::saturating_from_rational(1, 10); diff --git a/modules/homa-lite/src/tests.rs b/modules/homa-lite/src/tests.rs index a3c8a3b44..3f3fcdd3f 100644 --- a/modules/homa-lite/src/tests.rs +++ b/modules/homa-lite/src/tests.rs @@ -200,33 +200,88 @@ fn can_adjust_total_staking_currency() { assert_eq!(HomaLite::total_staking_currency(), 1); assert_noop!( - HomaLite::adjust_total_staking_currency(Origin::signed(ALICE), 5000), + HomaLite::adjust_total_staking_currency(Origin::signed(ALICE), 5000i128), BadOrigin ); // Can adjust total_staking_currency with ROOT. - assert_ok!(HomaLite::adjust_total_staking_currency(Origin::root(), 5000)); - + assert_ok!(HomaLite::adjust_total_staking_currency(Origin::root(), 5000i128)); assert_eq!(HomaLite::total_staking_currency(), 5001); System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(5001))); - // Underflow / overflow causes error - assert_noop!( - HomaLite::adjust_total_staking_currency(Origin::root(), -5002), - ArithmeticError::Underflow - ); - - assert_eq!(HomaLite::total_staking_currency(), 5001); + // Can decrease total_staking_currency. + assert_ok!(HomaLite::adjust_total_staking_currency(Origin::root(), -5000i128)); + assert_eq!(HomaLite::total_staking_currency(), 1); + System::assert_last_event(Event::HomaLite(crate::Event::TotalStakingCurrencySet(1))); + // overflow can be handled assert_ok!(HomaLite::set_total_staking_currency( Origin::root(), Balance::max_value() )); + assert_ok!(HomaLite::adjust_total_staking_currency(Origin::root(), 1i128)); + assert_eq!(HomaLite::total_staking_currency(), Balance::max_value()); + + // Do not allow TotalStakingCurrency to become 0 + assert_ok!(HomaLite::set_total_staking_currency(Origin::root(), 5000)); assert_noop!( - HomaLite::adjust_total_staking_currency(Origin::root(), 1), - ArithmeticError::Overflow + HomaLite::adjust_total_staking_currency(Origin::root(), -5000i128), + Error::::InvalidTotalStakingCurrency ); + assert_eq!(HomaLite::total_staking_currency(), 5000); + + // TotalStakingCurrency must be atleast 1 + assert_ok!(HomaLite::adjust_total_staking_currency(Origin::root(), -4999i128)); + }); +} + +#[test] +fn can_adjust_available_staking_balance_with_no_matches() { + ExtBuilder::default().build().execute_with(|| { + assert_noop!( + HomaLite::adjust_available_staking_balance(Origin::signed(ALICE), 5000i128, 10), + BadOrigin + ); + + // Can adjust available_staking_balance with ROOT. + assert_ok!(HomaLite::adjust_available_staking_balance(Origin::root(), 5001i128, 10)); + assert_eq!(HomaLite::available_staking_balance(), 5001); + System::assert_last_event(Event::HomaLite(crate::Event::AvailableStakingBalanceSet(5001))); + + // Can decrease available_staking_balance. + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + -5001i128, + 10 + )); + assert_eq!(HomaLite::total_staking_currency(), 0); + System::assert_last_event(Event::HomaLite(crate::Event::AvailableStakingBalanceSet(0))); + + // Underflow / overflow can be handled due to the use of saturating arithmetic + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + -10_000i128, + 10 + )); + assert_eq!(HomaLite::available_staking_balance(), 0); + + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + i128::max_value(), + 10 + )); + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + i128::max_value(), + 10 + )); + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + i128::max_value(), + 10 + )); + assert_eq!(HomaLite::available_staking_balance(), Balance::max_value()); }); } @@ -407,20 +462,50 @@ fn new_available_staking_currency_can_handle_redeem_requests() { Some((dollar(989), Permill::zero())) ); - // Add more staking currency to fully satify the last redeem request - assert_ok!(HomaLite::replace_schedule_unbond( + // Add more redeem request + assert_ok!(Currencies::update_balance( Origin::root(), - vec![(dollar(150), 2)], + ALICE, + LKSM, + dollar(1_000) as i128 + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(ALICE), + dollar(1_000), + Permill::zero() + )); + // 1000 - withdraw_fee = 999 + assert_eq!( + RedeemRequests::::get(&ALICE), + Some((dollar(999), Permill::zero())) + ); + // Add more staking currency by adjust_available_staking_balance also + // automatically fullfill pending redeem request. + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + dollar(200) as i128, + 10 )); - MockRelayBlockNumberProvider::set(2); - HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); - // The last request is redeemed, the leftover is stored. - assert_eq!(AvailableStakingBalance::::get(), 51_100_000_000_000); - assert_eq!(Currencies::free_balance(KSM, &ROOT), 1_096_900_000_000_000); - assert_eq!(Currencies::free_balance(LKSM, &ROOT), dollar(989_000)); - assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), dollar(0)); + // The 2 remaining requests are redeemed, the leftover is stored. + // available_staking_remain = 200 - 98.9 - 99.9 = 1.2 + assert_eq!(AvailableStakingBalance::::get(), dollar(12) / 10); + + assert_eq!(RedeemRequests::::get(&ALICE), None); + assert_eq!(HomaLite::get_exchange_rate(), Ratio::saturating_from_rational(1, 10)); + // staking_gained = 99.9 - 1 (xcm_fee) = 98.9 + assert_eq!( + Currencies::free_balance(KSM, &ALICE), + dollar(INITIAL_BALANCE) + dollar(989) / 10 + ); + assert_eq!(Currencies::free_balance(LKSM, &ALICE), 0); + assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), 0); + assert_eq!(RedeemRequests::::get(&ROOT), None); + // staking = 999(first redeem) + 98.9(this redeem) - 1(xcm_fee) = 1096.9 + assert_eq!(Currencies::free_balance(KSM, &ROOT), dollar(10_969) / 10); + assert_eq!(Currencies::free_balance(LKSM, &ROOT), dollar(989_000)); + assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), 0); }); } @@ -463,12 +548,12 @@ fn on_idle_can_handle_changes_in_exchange_rate() { #[test] fn request_redeem_works() { ExtBuilder::default().build().execute_with(|| { - assert_ok!(HomaLite::replace_schedule_unbond( + assert_ok!(HomaLite::adjust_available_staking_balance( Origin::root(), - vec![(dollar(50_000), 1)], + 50_000_000_000_000_000, + 10 )); - MockRelayBlockNumberProvider::set(1); - HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); + assert_eq!(AvailableStakingBalance::::get(), dollar(50_000)); // Redeem amount has to be above a threshold. @@ -526,12 +611,12 @@ fn request_redeem_works() { #[test] fn request_redeem_can_handle_dust_redeem_requests() { ExtBuilder::default().build().execute_with(|| { - assert_ok!(HomaLite::replace_schedule_unbond( + assert_ok!(HomaLite::adjust_available_staking_balance( Origin::root(), - vec![(dollar(50_000), 1)], + 50_000_000_000_000_000, + 10 )); - MockRelayBlockNumberProvider::set(1); - HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); + assert_eq!(AvailableStakingBalance::::get(), dollar(50_000)); // Remaining `dollar(1)` is below the xcm_unbond_fee, therefore returned and requests filled. @@ -576,22 +661,80 @@ fn on_idle_can_handle_dust_redeem_requests() { // mint can handle dust redeem requests #[test] fn mint_can_handle_dust_redeem_requests() { - ExtBuilder::default().build().execute_with(|| { + ExtBuilder::empty().build().execute_with(|| { assert_ok!(HomaLite::set_minting_cap(Origin::root(), dollar(INITIAL_BALANCE))); + assert_ok!(Currencies::update_balance( + Origin::root(), + ALICE, + LKSM, + 1_001_001_101_101_101 as i128 + )); + assert_ok!(Currencies::update_balance( + Origin::root(), + BOB, + KSM, + dollar(101) as i128 + )); - // Test that on_idle doesn't add dust redeem requests into the queue. + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + Currencies::total_issuance(LKSM) / 10 + )); + + // Redeem enough for 100 KSM with dust remaining assert_ok!(HomaLite::request_redeem( - Origin::signed(ROOT), - dollar(500_010), + Origin::signed(ALICE), + 1_001_001_101_101_101, Permill::zero() )); + assert_eq!( + RedeemRequests::::get(&ALICE), + Some((1_000_000_100_000_000, Permill::zero())) + ); + assert_eq!(Currencies::free_balance(LKSM, &ALICE), 0); + assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), 1_000_000_100_000_000); + + let mint_amount = HomaLite::convert_liquid_to_staking(1_000_000_000_000_000).unwrap(); + assert_eq!(mint_amount, 100_100_100_100_099); + // Mint 100 KSM, remaning dust should be returned to the redeemer. + assert_ok!(HomaLite::mint(Origin::signed(BOB), mint_amount)); + + // some dust due to rounding error left + assert_eq!(Currencies::free_balance(KSM, &BOB), 899_899_899_902); + // Minted appox. $1000 LKSM + assert_eq!(Currencies::free_balance(LKSM, &BOB), 999_999_999_999_990); + + // Redeemed $100 KSM for ALICE, with rounding error + assert_eq!(Currencies::free_balance(KSM, &ALICE), 100_100_100_100_098); + // Dust LKSM is returned to the redeemer. + assert_eq!(Currencies::free_balance(LKSM, &ALICE), 100_000_010); + assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), 0); + assert_eq!(RedeemRequests::::get(&ALICE), None); - assert_ok!(HomaLite::mint(Origin::signed(ALICE), dollar(50_000))); - - assert_eq!(Currencies::free_balance(KSM, &ROOT), 49_950_999_000_000_000); - assert_eq!(Currencies::free_balance(LKSM, &ROOT), dollar(499_990)); - assert_eq!(Currencies::reserved_balance(LKSM, &ROOT), 0); - assert_eq!(RedeemRequests::::get(&ROOT), None); + let events = System::events(); + assert_eq!( + events[events.len() - 4].event, + Event::Currencies(module_currencies::Event::Transferred( + KSM, + BOB, + ALICE, + 100_100_100_100_098 + )) + ); + assert_eq!( + events[events.len() - 3].event, + Event::HomaLite(crate::Event::Redeemed(ALICE, 100_100_100_100_098, 999_999_999_999_990)) + ); + // Dust returned to redeemer + assert_eq!( + events[events.len() - 2].event, + Event::Tokens(orml_tokens::Event::Unreserved(LKSM, ALICE, 100_000_010)) + ); + // total amount minted, with rounding error + assert_eq!( + events[events.len() - 1].event, + Event::HomaLite(crate::Event::Minted(BOB, 100_100_100_100_099, 999_999_999_999_990)) + ); }); } @@ -799,9 +942,11 @@ fn redeem_can_handle_dust_available_staking_currency() { ExtBuilder::default().build().execute_with(|| { // If AvailableStakingBalance is not enough to pay for the unbonding fee, ignore it. // pub XcmUnbondFee: Balance = dollar(1); - assert_ok!(HomaLite::schedule_unbond(Origin::root(), 999_000_000, 0)); - MockRelayBlockNumberProvider::set(0); - HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + 999_000_000, + 10 + )); assert_eq!(AvailableStakingBalance::::get(), 999_000_000); @@ -942,3 +1087,305 @@ fn not_overcharge_redeem_fee() { assert_eq!(Currencies::reserved_balance(LKSM, &ALICE), dollar(20) - fee * 2); }); } + +#[test] +fn on_idle_matches_redeem_based_on_weights() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Currencies::update_balance( + Origin::root(), + ALICE, + LKSM, + dollar(INITIAL_BALANCE) as i128 + )); + + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + Currencies::total_issuance(LKSM) / 10 + )); + + // Schedule an unbond. + assert_ok!(HomaLite::schedule_unbond(Origin::root(), dollar(1_000_000), 0)); + MockRelayBlockNumberProvider::set(0); + + assert_ok!(HomaLite::request_redeem( + Origin::signed(ROOT), + dollar(1_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(ALICE), + dollar(1_000), + Permill::zero() + )); + + // Get the currently benchmarked weight. + let xcm_weight = ::WeightInfo::xcm_unbond(); + let redeem = ::WeightInfo::redeem_with_available_staking_balance(); + + // on_idle does nothing with insufficient weight + assert_eq!(HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 0), 0); + assert_eq!(ScheduledUnbond::::get(), vec![(dollar(1_000_000), 0)]); + assert_eq!( + RedeemRequests::::get(ROOT), + Some((dollar(999), Permill::zero())) + ); + assert_eq!( + RedeemRequests::::get(ALICE), + Some((dollar(999), Permill::zero())) + ); + + // on_idle only perform XCM unbond with sufficient weight + assert_eq!( + HomaLite::on_idle(MockRelayBlockNumberProvider::get(), xcm_weight + 1), + xcm_weight + ); + assert_eq!(ScheduledUnbond::::get(), vec![]); + assert_eq!( + RedeemRequests::::get(ROOT), + Some((dollar(999), Permill::zero())) + ); + assert_eq!( + RedeemRequests::::get(ALICE), + Some((dollar(999), Permill::zero())) + ); + + // on_idle has weights to match only one redeem + assert_ok!(HomaLite::schedule_unbond(Origin::root(), dollar(1_000_000), 0)); + assert_eq!(ScheduledUnbond::::get(), vec![(dollar(1_000_000), 0)]); + assert_eq!( + HomaLite::on_idle(MockRelayBlockNumberProvider::get(), xcm_weight + redeem + 1), + xcm_weight + redeem + ); + assert_eq!(ScheduledUnbond::::get(), vec![]); + assert_eq!( + RedeemRequests::::get(ROOT), + Some((dollar(999), Permill::zero())) + ); + assert_eq!(RedeemRequests::::get(ALICE), None); + + // on_idle will match the remaining redeem request, even with no scheduled unbond. + assert_ok!(HomaLite::schedule_unbond(Origin::root(), dollar(1_000_000), 10)); + assert_eq!(ScheduledUnbond::::get(), vec![(dollar(1_000_000), 10)]); + assert_eq!( + HomaLite::on_idle(MockRelayBlockNumberProvider::get(), redeem + 1), + redeem + ); + assert_eq!(ScheduledUnbond::::get(), vec![(dollar(1_000_000), 10)]); + assert_eq!(RedeemRequests::::get(ROOT), None); + assert_eq!(RedeemRequests::::get(ALICE), None); + }); +} + +#[test] +fn adjust_available_staking_balance_matches_redeem_based_on_input() { + ExtBuilder::default().build().execute_with(|| { + assert_ok!(Currencies::update_balance( + Origin::root(), + ALICE, + LKSM, + dollar(INITIAL_BALANCE) as i128 + )); + + assert_ok!(Currencies::update_balance( + Origin::root(), + BOB, + LKSM, + dollar(INITIAL_BALANCE) as i128 + )); + + assert_ok!(HomaLite::request_redeem( + Origin::signed(ROOT), + dollar(1_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(ALICE), + dollar(1_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(BOB), + dollar(1_000), + Permill::zero() + )); + + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + Currencies::total_issuance(LKSM) / 10 + )); + + // match no redeem requests + assert_ok!(HomaLite::adjust_available_staking_balance( + Origin::root(), + dollar(1_000_000) as i128, + 0 + )); + assert_eq!(AvailableStakingBalance::::get(), dollar(1_000_000)); + + // match only one request + assert_ok!(HomaLite::adjust_available_staking_balance(Origin::root(), 1i128, 1)); + assert_eq!( + RedeemRequests::::get(ROOT), + Some((dollar(999), Permill::zero())) + ); + assert_eq!(RedeemRequests::::get(ALICE), None); + assert_eq!( + RedeemRequests::::get(BOB), + Some((dollar(999), Permill::zero())) + ); + + // match the remaining requests + assert_ok!(HomaLite::adjust_available_staking_balance(Origin::root(), 1, 10)); + assert_eq!(RedeemRequests::::get(ROOT), None); + assert_eq!(RedeemRequests::::get(ALICE), None); + assert_eq!(RedeemRequests::::get(BOB), None); + }); +} + +#[test] +fn available_staking_balances_can_handle_rounding_error_dust() { + ExtBuilder::empty().build().execute_with(|| { + assert_ok!(Currencies::update_balance( + Origin::root(), + ALICE, + LKSM, + dollar(5_000) as i128 + )); + assert_ok!(Currencies::update_balance( + Origin::root(), + BOB, + LKSM, + dollar(2_000) as i128 + )); + assert_ok!(Currencies::update_balance( + Origin::root(), + ROOT, + LKSM, + dollar(3_000) as i128 + )); + + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + 1_000_237_000_000_000 + )); + let staking_amount = 999_999_999_999; + let liquid_amount = HomaLite::convert_staking_to_liquid(staking_amount).unwrap(); + let staking_amount2 = HomaLite::convert_liquid_to_staking(liquid_amount).unwrap(); + assert_ne!(staking_amount, staking_amount2); + + assert_ok!(HomaLite::request_redeem( + Origin::signed(ALICE), + dollar(5_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(BOB), + dollar(2_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(ROOT), + dollar(3_000), + Permill::zero() + )); + assert_ok!(HomaLite::replace_schedule_unbond( + Origin::root(), + vec![(999_999_999_999, 1)], + )); + MockRelayBlockNumberProvider::set(1); + + HomaLite::on_idle(MockRelayBlockNumberProvider::get(), 5_000_000_000); + + // Dust AvailableStakingBalance remains + assert_eq!(HomaLite::available_staking_balance(), 1); + let events = System::events(); + assert_eq!( + events[events.len() - 3].event, + Event::HomaLite(crate::Event::ScheduledUnbondWithdrew(999_999_999_999)) + ); + assert_eq!( + events[events.len() - 2].event, + Event::Tokens(orml_tokens::Event::Unreserved(LKSM, ALICE, 9987632930985)) + ); + assert_eq!( + events[events.len() - 1].event, + Event::HomaLite(crate::Event::Redeemed(ALICE, 999999999998, 9987632930985)) + ); + }); +} + +#[test] +fn mint_can_handle_rounding_error_dust() { + ExtBuilder::empty().build().execute_with(|| { + assert_ok!(Currencies::update_balance( + Origin::root(), + ALICE, + LKSM, + dollar(5_000) as i128 + )); + assert_ok!(Currencies::update_balance( + Origin::root(), + BOB, + LKSM, + dollar(2_000) as i128 + )); + assert_ok!(Currencies::update_balance( + Origin::root(), + ROOT, + LKSM, + dollar(3_000) as i128 + )); + assert_ok!(Currencies::update_balance( + Origin::root(), + ROOT, + KSM, + 1_999_999_999_999 as i128 + )); + + assert_ok!(HomaLite::set_total_staking_currency( + Origin::root(), + 1_000_237_000_000_000 + )); + let staking_amount = 999_999_999_999; + let liquid_amount = HomaLite::convert_staking_to_liquid(staking_amount).unwrap(); + let staking_amount2 = HomaLite::convert_liquid_to_staking(liquid_amount).unwrap(); + assert_ne!(staking_amount, staking_amount2); + + assert_ok!(HomaLite::request_redeem( + Origin::signed(ALICE), + dollar(5_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(BOB), + dollar(2_000), + Permill::zero() + )); + assert_ok!(HomaLite::request_redeem( + Origin::signed(ROOT), + dollar(3_000), + Permill::zero() + )); + assert_ok!(HomaLite::mint(Origin::signed(ROOT), 999_999_999_999,)); + + // Dust is un-transferred from minter + assert_eq!(Currencies::free_balance(KSM, &ROOT), 1000000000001); + assert_eq!(Currencies::free_balance(LKSM, &ROOT), 9_987_632_930_985); + + let events = System::events(); + assert_eq!( + events[events.len() - 3].event, + Event::Currencies(module_currencies::Event::Transferred(KSM, ROOT, ALICE, 999999999998)) + ); + // actual staking transfered is off due to rounding error + assert_eq!( + events[events.len() - 2].event, + Event::HomaLite(crate::Event::Redeemed(ALICE, 999999999998, 9_987_632_930_985)) + ); + // total amount minted includes dust caused by rounding error + assert_eq!( + events[events.len() - 1].event, + Event::HomaLite(crate::Event::Minted(ROOT, 999999999999, 9_987_632_930_985)) + ); + }); +} diff --git a/modules/homa-lite/src/weights.rs b/modules/homa-lite/src/weights.rs index 2ded7545d..a027b88e6 100644 --- a/modules/homa-lite/src/weights.rs +++ b/modules/homa-lite/src/weights.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-03, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-11-01, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -47,38 +47,35 @@ use sp_std::marker::PhantomData; /// Weight functions needed for module_homa_lite. pub trait WeightInfo { - fn on_idle() -> Weight; fn mint() -> Weight; fn mint_for_requests() -> Weight; fn set_total_staking_currency() -> Weight; fn adjust_total_staking_currency() -> Weight; + fn adjust_available_staking_balance_with_no_matches() -> Weight; fn set_minting_cap() -> Weight; fn set_xcm_dest_weight() -> Weight; fn request_redeem() -> Weight; fn schedule_unbond() -> Weight; fn replace_schedule_unbond() -> Weight; + fn redeem_with_available_staking_balance() -> Weight; + fn xcm_unbond() -> Weight; } /// Weights for module_homa_lite using the Acala node and recommended hardware. pub struct AcalaWeight(PhantomData); impl WeightInfo for AcalaWeight { - fn on_idle() -> Weight { - (34_000_000 as Weight) - .saturating_add(T::DbWeight::get().reads(8 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) - } fn mint() -> Weight { - (128_000_000 as Weight) + (138_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (338_000_000 as Weight) + (348_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { @@ -86,49 +83,59 @@ impl WeightInfo for AcalaWeight { .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn adjust_available_staking_balance_with_no_matches() -> Weight { + (17_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } fn set_minting_cap() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (76_000_000 as Weight) + (82_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(3 as Weight)) + .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (12_000_000 as Weight) + (13_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn redeem_with_available_staking_balance() -> Weight { + (6_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn xcm_unbond() -> Weight { + (24_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(5 as Weight)) + .saturating_add(T::DbWeight::get().writes(2 as Weight)) + } } // For backwards compatibility and tests impl WeightInfo for () { - fn on_idle() -> Weight { - (34_000_000 as Weight) - .saturating_add(RocksDbWeight::get().reads(8 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) - } fn mint() -> Weight { - (128_000_000 as Weight) + (138_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(14 as Weight)) .saturating_add(RocksDbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (338_000_000 as Weight) + (348_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(30 as Weight)) .saturating_add(RocksDbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { @@ -136,26 +143,41 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } + fn adjust_available_staking_balance_with_no_matches() -> Weight { + (17_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } fn set_minting_cap() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (76_000_000 as Weight) + (82_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(7 as Weight)) - .saturating_add(RocksDbWeight::get().writes(3 as Weight)) + .saturating_add(RocksDbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (12_000_000 as Weight) + (13_000_000 as Weight) .saturating_add(RocksDbWeight::get().reads(1 as Weight)) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (10_000_000 as Weight) + (11_000_000 as Weight) .saturating_add(RocksDbWeight::get().writes(1 as Weight)) } + fn redeem_with_available_staking_balance() -> Weight { + (6_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(2 as Weight)) + .saturating_add(RocksDbWeight::get().writes(1 as Weight)) + } + fn xcm_unbond() -> Weight { + (24_000_000 as Weight) + .saturating_add(RocksDbWeight::get().reads(5 as Weight)) + .saturating_add(RocksDbWeight::get().writes(2 as Weight)) + } } diff --git a/runtime/acala/src/weights/module_homa_lite.rs b/runtime/acala/src/weights/module_homa_lite.rs index 580c88af4..592f8c022 100644 --- a/runtime/acala/src/weights/module_homa_lite.rs +++ b/runtime/acala/src/weights/module_homa_lite.rs @@ -19,16 +19,16 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` -//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-latest"), DB CACHE: 128 +//! DATE: 2021-10-22, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("acala-dev"), DB CACHE: 128 // Executed Command: // target/release/acala // benchmark -// --chain=acala-latest +// --chain=acala-dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module-homa-lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -47,50 +47,58 @@ use sp_std::marker::PhantomData; /// Weight functions for module_homa_lite. pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { - fn on_idle() -> Weight { - (466_241_000 as Weight) - .saturating_add(T::DbWeight::get().reads(26 as Weight)) - .saturating_add(T::DbWeight::get().writes(19 as Weight)) - } fn mint() -> Weight { - (253_155_000 as Weight) - .saturating_add(T::DbWeight::get().reads(17 as Weight)) - .saturating_add(T::DbWeight::get().writes(8 as Weight)) + (134_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(14 as Weight)) + .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (611_461_000 as Weight) - .saturating_add(T::DbWeight::get().reads(33 as Weight)) - .saturating_add(T::DbWeight::get().writes(22 as Weight)) + (342_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(30 as Weight)) + .saturating_add(T::DbWeight::get().writes(21 as Weight)) } fn set_total_staking_currency() -> Weight { - (19_082_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (21_737_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn adjust_available_staking_balance_with_no_matches() -> Weight { + (17_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } fn set_minting_cap() -> Weight { - (19_547_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_772_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (144_128_000 as Weight) + (74_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) - .saturating_add(T::DbWeight::get().writes(4 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn schedule_unbond() -> Weight { - (23_150_000 as Weight) + (13_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (20_238_000 as Weight) + (11_000_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn redeem_with_available_staking_balance() -> Weight { + (21_316_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn xcm_unbond() -> Weight { + (21_316_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } } diff --git a/runtime/karura/src/weights/module_homa_lite.rs b/runtime/karura/src/weights/module_homa_lite.rs index 211fdafbb..300ec0565 100644 --- a/runtime/karura/src/weights/module_homa_lite.rs +++ b/runtime/karura/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-11-01, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("karura-dev"), DB CACHE: 128 // Executed Command: @@ -28,7 +28,7 @@ // --chain=karura-dev // --steps=50 // --repeat=20 -// --pallet=* +// --pallet=module-homa-lite // --extrinsic=* // --execution=wasm // --wasm-execution=compiled @@ -47,50 +47,60 @@ use sp_std::marker::PhantomData; /// Weight functions for module_homa_lite. pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { - fn on_idle() -> Weight { - (73_715_000 as Weight) - .saturating_add(T::DbWeight::get().reads(11 as Weight)) - .saturating_add(T::DbWeight::get().writes(4 as Weight)) - } fn mint() -> Weight { - (248_178_000 as Weight) + (144_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(17 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn mint_for_requests() -> Weight { - (261_398_000 as Weight) + (153_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(19 as Weight)) .saturating_add(T::DbWeight::get().writes(8 as Weight)) } fn set_total_staking_currency() -> Weight { - (18_585_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (20_912_000 as Weight) + (12_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(1 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn adjust_available_staking_balance_with_no_matches() -> Weight { + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_minting_cap() -> Weight { - (18_755_000 as Weight) + (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn set_xcm_dest_weight() -> Weight { - (18_336_000 as Weight) + (10_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (137_569_000 as Weight) + (81_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(4 as Weight)) } fn schedule_unbond() -> Weight { - (22_566_000 as Weight) + (12_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn replace_schedule_unbond() -> Weight { - (19_728_000 as Weight) + (11_000_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn redeem_with_available_staking_balance() -> Weight { + (6_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn xcm_unbond() -> Weight { + (34_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(8 as Weight)) + .saturating_add(T::DbWeight::get().writes(3 as Weight)) + } } diff --git a/runtime/mandala/src/weights/module_homa_lite.rs b/runtime/mandala/src/weights/module_homa_lite.rs index db4bcf179..ec32809a0 100644 --- a/runtime/mandala/src/weights/module_homa_lite.rs +++ b/runtime/mandala/src/weights/module_homa_lite.rs @@ -19,7 +19,7 @@ //! Autogenerated weights for module_homa_lite //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-10-03, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-22, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -47,16 +47,13 @@ use sp_std::marker::PhantomData; /// Weight functions for module_homa_lite. pub struct WeightInfo(PhantomData); impl module_homa_lite::WeightInfo for WeightInfo { - fn on_idle() -> Weight { - (0 as Weight) - } fn mint() -> Weight { - (131_000_000 as Weight) + (137_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(14 as Weight)) .saturating_add(T::DbWeight::get().writes(7 as Weight)) } fn mint_for_requests() -> Weight { - (339_000_000 as Weight) + (345_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(30 as Weight)) .saturating_add(T::DbWeight::get().writes(21 as Weight)) } @@ -65,10 +62,15 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn adjust_total_staking_currency() -> Weight { - (12_000_000 as Weight) + (13_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn adjust_available_staking_balance_with_no_matches() -> Weight { + (18_000_000 as Weight) + .saturating_add(T::DbWeight::get().reads(2 as Weight)) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } fn set_minting_cap() -> Weight { (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) @@ -78,12 +80,12 @@ impl module_homa_lite::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(1 as Weight)) } fn request_redeem() -> Weight { - (76_000_000 as Weight) + (77_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(7 as Weight)) .saturating_add(T::DbWeight::get().writes(3 as Weight)) } fn schedule_unbond() -> Weight { - (12_000_000 as Weight) + (14_000_000 as Weight) .saturating_add(T::DbWeight::get().reads(1 as Weight)) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } @@ -91,4 +93,12 @@ impl module_homa_lite::WeightInfo for WeightInfo { (11_000_000 as Weight) .saturating_add(T::DbWeight::get().writes(1 as Weight)) } + fn redeem_with_available_staking_balance() -> Weight { + (21_316_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } + fn xcm_unbond() -> Weight { + (21_316_000 as Weight) + .saturating_add(T::DbWeight::get().writes(1 as Weight)) + } }