Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions modules/incentives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,6 @@ pub mod module {
pub type IncentiveRewardAmounts<T: Config> =
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<T: Config> = StorageMap<_, Twox64Concat, PoolId, Rate, ValueQuery>;

/// Mapping from pool to its claim reward deduction rate.
///
/// ClaimRewardDeductionRates: map Pool => DeductionRate
Expand Down
78 changes: 70 additions & 8 deletions modules/incentives/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,91 @@
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>);
impl<T: Config> OnRuntimeUpgrade for ClearDexSavingRewardRates<T> {
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",
"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::<T>::clear(u32::max_value(), None);
// 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> {
assert_eq!(DexSavingRewardRates::<T>::iter().count(), 0);
let pool_id = GetPoolId::get();
assert_eq!(PendingMultiRewards::<T>::iter_prefix(pool_id).count(), 0);

log::info!(
target: "incentives",
"ClearDexSavingRewardRates done!",
"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);
zjb0807 marked this conversation as resolved.
Show resolved Hide resolved
}

// 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).total_shares, total_share);
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved

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

Ok(())
Expand Down
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/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1799,14 +1799,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, ()>;
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "runtime-benchmarks")]
#[macro_use]
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