Skip to content

Commit

Permalink
reset incentives.PendingMultiRewards and rewards.SharesAndWithdrawnRe…
Browse files Browse the repository at this point in the history
…wards for Pool aUSD-IBTC (#2357)

* reset incentives.PendingMultiRewards and rewards.SharesAndWithdrawnRewards for Pool aUSD-IBTC

* reset rewards.PoolInfos for Pool aUSD-IBTC

* fix clippy

* update
  • Loading branch information
wangjj9219 authored Sep 30, 2022
1 parent 8fd1743 commit 2290ca3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
98 changes: 97 additions & 1 deletion modules/incentives/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
use super::*;
use crate::log;
use frame_support::traits::OnRuntimeUpgrade;
use sp_std::marker::PhantomData;

/// Clear all DexSavingRewardRates storage
pub struct ClearDexSavingRewardRates<T>(sp_std::marker::PhantomData<T>);
pub struct ClearDexSavingRewardRates<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for ClearDexSavingRewardRates<T> {
fn on_runtime_upgrade() -> Weight {
log::info!(
Expand All @@ -47,3 +48,98 @@ impl<T: Config> OnRuntimeUpgrade for ClearDexSavingRewardRates<T> {
Ok(())
}
}

type WithdrawnRewards = BTreeMap<CurrencyId, Balance>;

/// Clear all PendingMultiRewards for specific Pool
pub struct ClearPendingMultiRewards<T, GetPoolId>(PhantomData<T>, PhantomData<GetPoolId>);
impl<T: Config, GetPoolId: Get<PoolId>> OnRuntimeUpgrade for ClearPendingMultiRewards<T, GetPoolId> {
fn on_runtime_upgrade() -> Weight {
let pool_id = GetPoolId::get();
log::info!(
target: "incentives",
"ClearPendingMultiRewards::on_runtime_upgrade execute, will clear Storage PendingMultiRewards for Pool {:?}",
pool_id,
);

// clear all PendingMultiRewards for specific pool
let _ = PendingMultiRewards::<T>::clear_prefix(pool_id, u32::max_value(), None);

0
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let pool_id = GetPoolId::get();
assert_eq!(PendingMultiRewards::<T>::iter_prefix(pool_id).count(), 0);

log::info!(
target: "incentives",
"ClearPendingMultiRewards for Pool {:?} done!",
pool_id,
);

Ok(())
}
}

/// Reset rewards record for storage rewards.SharesAndWithdrawnRewards and rewards.PoolInfos at
/// specific PoolId
pub struct ResetRewardsRecord<T, GetPoolId>(PhantomData<T>, PhantomData<GetPoolId>);
impl<T: Config, GetPoolId: Get<PoolId>> OnRuntimeUpgrade for ResetRewardsRecord<T, GetPoolId> {
fn on_runtime_upgrade() -> Weight {
let pool_id = GetPoolId::get();
log::info!(
target: "rewards",
"ResetRewardsRecord::on_runtime_upgrade execute, will reset Storage SharesAndWithdrawnRewards and PoolInfos for Pool {:?}",
pool_id
);

let mut total_share: Balance = Default::default();

// reset SharesAndWithdrawnRewards
for (who, (share, _)) in orml_rewards::SharesAndWithdrawnRewards::<T>::iter_prefix(&pool_id) {
orml_rewards::SharesAndWithdrawnRewards::<T>::mutate(&pool_id, &who, |(_, withdrawn_rewards)| {
*withdrawn_rewards = WithdrawnRewards::new();
});

total_share = total_share.saturating_add(share);
}

// reset PoolInfos
let pool_info = orml_rewards::PoolInfo::<Balance, Balance, CurrencyId> {
total_shares: total_share,
..Default::default()
};
orml_rewards::PoolInfos::<T>::insert(&pool_id, pool_info);

0
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let pool_id = GetPoolId::get();
let mut total_share = Balance::default();

for (_, (share, withdrawn_rewards)) in orml_rewards::SharesAndWithdrawnRewards::<T>::iter_prefix(&pool_id) {
assert_eq!(withdrawn_rewards, WithdrawnRewards::new());
total_share = total_share.saturating_add(share);
}

assert_eq!(
orml_rewards::PoolInfos::<T>::get(&pool_id),
orml_rewards::PoolInfo::<Balance, Balance, CurrencyId> {
total_shares: total_share,
..Default::default()
}
);

log::info!(
target: "rewards",
"ResetRewardsRecord for Pool {:?} done!",
pool_id,
);

Ok(())
}
}
15 changes: 12 additions & 3 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1772,7 +1778,10 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
module_incentives::migration::ClearDexSavingRewardRates<Runtime>,
(
module_incentives::migration::ResetRewardsRecord<Runtime, GetAusdIbtcPoolId>,
module_incentives::migration::ClearPendingMultiRewards<Runtime, GetAusdIbtcPoolId>,
),
>;

#[cfg(feature = "runtime-benchmarks")]
Expand Down
10 changes: 2 additions & 8 deletions runtime/mandala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1919,14 +1919,8 @@ pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
module_incentives::migration::ClearDexSavingRewardRates<Runtime>,
>;
pub type Executive =
frame_executive::Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllPalletsWithSystem, ()>;

construct_runtime!(
pub enum Runtime where
Expand Down

0 comments on commit 2290ca3

Please sign in to comment.