From 8c1acf340d63c2f5e7776c8adcf76b6b003e271a Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Mon, 26 Sep 2022 18:53:28 +0800 Subject: [PATCH] reset incentives.PendingMultiRewards and rewards.SharesAndWithdrawnRewards for Pool aUSD-IBTC --- modules/incentives/src/lib.rs | 8 ---- modules/incentives/src/migration.rs | 61 +++++++++++++++++++++++++---- runtime/acala/src/lib.rs | 15 +++++-- runtime/karura/src/lib.rs | 10 +---- runtime/mandala/src/lib.rs | 10 +---- 5 files changed, 69 insertions(+), 35 deletions(-) diff --git a/modules/incentives/src/lib.rs b/modules/incentives/src/lib.rs index 42f3c23fc3..0f32075d6d 100644 --- a/modules/incentives/src/lib.rs +++ b/modules/incentives/src/lib.rs @@ -165,14 +165,6 @@ pub mod module { pub type IncentiveRewardAmounts = StorageDoubleMap<_, Twox64Concat, PoolId, Twox64Concat, CurrencyId, Balance, ValueQuery>; - /// NOTE: already deprecated, need remove it after next runtime upgrade - /// Mapping from pool to its fixed reward rate per period. - /// - /// DexSavingRewardRates: map Pool => SavingRatePerPeriod - #[pallet::storage] - #[pallet::getter(fn dex_saving_reward_rates)] - pub type DexSavingRewardRates = StorageMap<_, Twox64Concat, PoolId, Rate, ValueQuery>; - /// Mapping from pool to its claim reward deduction rate. /// /// ClaimRewardDeductionRates: map Pool => DeductionRate diff --git a/modules/incentives/src/migration.rs b/modules/incentives/src/migration.rs index 47d533417e..32dfd0082d 100644 --- a/modules/incentives/src/migration.rs +++ b/modules/incentives/src/migration.rs @@ -19,29 +19,74 @@ use super::*; use crate::log; use frame_support::traits::OnRuntimeUpgrade; +use sp_std::marker::PhantomData; -/// Clear all DexSavingRewardRates storage -pub struct ClearDexSavingRewardRates(sp_std::marker::PhantomData); -impl OnRuntimeUpgrade for ClearDexSavingRewardRates { +type WithdrawnRewards = BTreeMap; + +/// Clear all PendingMultiRewards for specific Pool +pub struct ClearPendingMultiRewards(PhantomData, PhantomData); +impl> OnRuntimeUpgrade for ClearPendingMultiRewards { fn on_runtime_upgrade() -> Weight { + let pool_id = GetPoolId::get(); log::info!( target: "incentives", - "ClearDexSavingRewardRates::on_runtime_upgrade execute, will clear Storage DexSavingRewardRates", + "ClearPendingMultiRewards::on_runtime_upgrade execute, will clear Storage PendingMultiRewards for Pool {:?}", + pool_id, ); - // clear storage DexSavingRewardRates, - let _ = DexSavingRewardRates::::clear(u32::max_value(), None); + // clear all PendingMultiRewards for specific pool + let _ = PendingMultiRewards::::clear_prefix(pool_id, u32::max_value(), None); 0 } #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { - assert_eq!(DexSavingRewardRates::::iter().count(), 0); + let pool_id = GetPoolId::get(); + assert_eq!(PendingMultiRewards::::iter_prefix(pool_id).count(), 0); log::info!( target: "incentives", - "ClearDexSavingRewardRates done!", + "ClearPendingMultiRewards for Pool {:?} done!", + pool_id, + ); + + Ok(()) + } +} + +/// Reset WithdrawnRewards records for rewards.SharesAndWithdrawnRewards at specific PoolId +pub struct ResetSharesAndWithdrawnRewards(PhantomData, PhantomData); +impl> OnRuntimeUpgrade for ResetSharesAndWithdrawnRewards { + fn on_runtime_upgrade() -> Weight { + let pool_id = GetPoolId::get(); + log::info!( + target: "rewards", + "ResetSharesAndWithdrawnRewards::on_runtime_upgrade execute, will reset Storage SharesAndWithdrawnRewards for Pool {:?}", + pool_id + ); + + for (who, (_, _)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { + orml_rewards::SharesAndWithdrawnRewards::::mutate(&pool_id, &who, |(_, withdrawn_rewards)| { + *withdrawn_rewards = WithdrawnRewards::new(); + }); + } + + 0 + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade() -> Result<(), &'static str> { + let pool_id = GetPoolId::get(); + + for (_, (_, withdrawn_rewards)) in orml_rewards::SharesAndWithdrawnRewards::::iter_prefix(&pool_id) { + assert_eq!(withdrawn_rewards, WithdrawnRewards::new()); + } + + log::info!( + target: "rewards", + "ResetSharesAndWithdrawnRewards for Pool {:?} done!", + pool_id, ); Ok(()) diff --git a/runtime/acala/src/lib.rs b/runtime/acala/src/lib.rs index a7368503ec..73fb2476ef 100644 --- a/runtime/acala/src/lib.rs +++ b/runtime/acala/src/lib.rs @@ -94,8 +94,8 @@ pub use primitives::{ task::TaskResult, unchecked_extrinsic::AcalaUncheckedExtrinsic, AccountId, AccountIndex, Address, Amount, AuctionId, AuthoritysOriginId, Balance, BlockNumber, CurrencyId, - DataProviderId, EraIndex, Hash, Lease, Moment, Multiplier, Nonce, ReserveIdentifier, Share, Signature, TokenSymbol, - TradingPair, + DataProviderId, DexShare, EraIndex, Hash, Lease, Moment, Multiplier, Nonce, ReserveIdentifier, Share, Signature, + TokenSymbol, TradingPair, }; pub use runtime_common::{ cent, dollar, microcent, millicent, AcalaDropAssets, AllPrecompiles, CheckRelayNumber, @@ -1244,6 +1244,12 @@ impl orml_rewards::Config for Runtime { parameter_types! { pub const AccumulatePeriod: BlockNumber = MINUTES; pub const EarnShareBooster: Permill = Permill::from_percent(30); + pub const GetAusdIbtcPoolId: PoolId = PoolId::Dex( + CurrencyId::DexShare( + DexShare::Token(TokenSymbol::AUSD), + DexShare::ForeignAsset(3) + ) + ); } impl module_incentives::Config for Runtime { @@ -1772,7 +1778,10 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - module_incentives::migration::ClearDexSavingRewardRates, + ( + module_incentives::migration::ResetSharesAndWithdrawnRewards, + module_incentives::migration::ClearPendingMultiRewards, + ), >; #[cfg(feature = "runtime-benchmarks")] diff --git a/runtime/karura/src/lib.rs b/runtime/karura/src/lib.rs index 0d6fea5366..e8186c1d24 100644 --- a/runtime/karura/src/lib.rs +++ b/runtime/karura/src/lib.rs @@ -1799,14 +1799,8 @@ pub type SignedPayload = generic::SignedPayload; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. -pub type Executive = frame_executive::Executive< - Runtime, - Block, - frame_system::ChainContext, - Runtime, - AllPalletsWithSystem, - module_incentives::migration::ClearDexSavingRewardRates, ->; +pub type Executive = + frame_executive::Executive, Runtime, AllPalletsWithSystem, ()>; #[cfg(feature = "runtime-benchmarks")] #[macro_use] diff --git a/runtime/mandala/src/lib.rs b/runtime/mandala/src/lib.rs index aa9697e21a..2af3a97250 100644 --- a/runtime/mandala/src/lib.rs +++ b/runtime/mandala/src/lib.rs @@ -1919,14 +1919,8 @@ pub type SignedPayload = generic::SignedPayload; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. -pub type Executive = frame_executive::Executive< - Runtime, - Block, - frame_system::ChainContext, - Runtime, - AllPalletsWithSystem, - module_incentives::migration::ClearDexSavingRewardRates, ->; +pub type Executive = + frame_executive::Executive, Runtime, AllPalletsWithSystem, ()>; construct_runtime!( pub enum Runtime where