From 58db05f8e2ef7fbb9c356077c7b37ee708a3fdca Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Mon, 8 Feb 2021 17:32:54 -0700 Subject: [PATCH] fix: set reward indexes in init genesis and zero them out on export genesis (#807) --- x/incentive/genesis.go | 90 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/x/incentive/genesis.go b/x/incentive/genesis.go index 857bad03..d4fdc755 100644 --- a/x/incentive/genesis.go +++ b/x/incentive/genesis.go @@ -27,12 +27,33 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, supplyKeeper types.SupplyKeep if !found { panic(fmt.Sprintf("usdx minting collateral type %s not found in cdp collateral types", rp.CollateralType)) } + k.SetUSDXMintingRewardFactor(ctx, rp.CollateralType, sdk.ZeroDec()) + } + + for _, mrp := range gs.Params.HardSupplyRewardPeriods { + newRewardIndexes := types.RewardIndexes{} + for _, rc := range mrp.RewardsPerSecond { + ri := types.NewRewardIndex(rc.Denom, sdk.ZeroDec()) + newRewardIndexes = append(newRewardIndexes, ri) + } + k.SetHardSupplyRewardIndexes(ctx, mrp.CollateralType, newRewardIndexes) + } + + for _, mrp := range gs.Params.HardBorrowRewardPeriods { + newRewardIndexes := types.RewardIndexes{} + for _, rc := range mrp.RewardsPerSecond { + ri := types.NewRewardIndex(rc.Denom, sdk.ZeroDec()) + newRewardIndexes = append(newRewardIndexes, ri) + } + k.SetHardBorrowRewardIndexes(ctx, mrp.CollateralType, newRewardIndexes) + } + + for _, rp := range gs.Params.HardDelegatorRewardPeriods { + k.SetHardDelegatorRewardFactor(ctx, rp.CollateralType, sdk.ZeroDec()) } k.SetParams(ctx, gs.Params) - // TODO: previous hard module accrual times/indexes should be set here - for _, gat := range gs.USDXAccumulationTimes { k.SetPreviousUSDXMintingAccrualTime(ctx, gat.CollateralType, gat.PreviousAccumulationTime) k.SetUSDXMintingRewardFactor(ctx, gat.CollateralType, gat.RewardFactor) @@ -51,9 +72,36 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, supplyKeeper types.SupplyKeep } for _, claim := range gs.USDXMintingClaims { + for _, ri := range claim.RewardIndexes { + if ri.RewardFactor != sdk.ZeroDec() { + ri.RewardFactor = sdk.ZeroDec() + } + } k.SetUSDXMintingClaim(ctx, claim) } + for _, claim := range gs.HardLiquidityProviderClaims { + for _, mri := range claim.SupplyRewardIndexes { + for _, ri := range mri.RewardIndexes { + if ri.RewardFactor != sdk.ZeroDec() { + ri.RewardFactor = sdk.ZeroDec() + } + } + } + for _, mri := range claim.BorrowRewardIndexes { + for _, ri := range mri.RewardIndexes { + if ri.RewardFactor != sdk.ZeroDec() { + ri.RewardFactor = sdk.ZeroDec() + } + } + } + for _, ri := range claim.DelegatorRewardIndexes { + if ri.RewardFactor != sdk.ZeroDec() { + ri.RewardFactor = sdk.ZeroDec() + } + } + k.SetHardLiquidityProviderClaim(ctx, claim) + } } // ExportGenesis export genesis state for incentive module @@ -63,6 +111,42 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { usdxClaims := k.GetAllUSDXMintingClaims(ctx) hardClaims := k.GetAllHardLiquidityProviderClaims(ctx) + synchronizedUsdxClaims := types.USDXMintingClaims{} + synchronizedHardClaims := types.HardLiquidityProviderClaims{} + + for _, usdxClaim := range usdxClaims { + claim, err := k.SynchronizeUSDXMintingClaim(ctx, usdxClaim) + if err != nil { + panic(err) + } + for _, ri := range claim.RewardIndexes { + ri.RewardFactor = sdk.ZeroDec() + } + synchronizedUsdxClaims = append(synchronizedUsdxClaims, claim) + } + + for _, hardClaim := range hardClaims { + k.SynchronizeHardLiquidityProviderClaim(ctx, hardClaim.Owner) + claim, found := k.GetHardLiquidityProviderClaim(ctx, hardClaim.Owner) + if !found { + panic("hard liquidity provider claim should always be found after synchronization") + } + for _, bri := range claim.BorrowRewardIndexes { + for _, ri := range bri.RewardIndexes { + ri.RewardFactor = sdk.ZeroDec() + } + } + for _, sri := range claim.SupplyRewardIndexes { + for _, ri := range sri.RewardIndexes { + ri.RewardFactor = sdk.ZeroDec() + } + } + for _, dri := range claim.DelegatorRewardIndexes { + dri.RewardFactor = sdk.ZeroDec() + } + synchronizedHardClaims = append(synchronizedHardClaims, claim) + } + var gats GenesisAccumulationTimes for _, rp := range params.USDXMintingRewardPeriods { @@ -78,5 +162,5 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState { gats = append(gats, gat) } - return types.NewGenesisState(params, gats, DefaultGenesisAccumulationTimes, DefaultGenesisAccumulationTimes, DefaultGenesisAccumulationTimes, usdxClaims, hardClaims) + return types.NewGenesisState(params, gats, DefaultGenesisAccumulationTimes, DefaultGenesisAccumulationTimes, DefaultGenesisAccumulationTimes, synchronizedUsdxClaims, synchronizedHardClaims) }