fix: set reward indexes in init genesis and zero them out on export genesis (#807)

This commit is contained in:
Kevin Davis 2021-02-08 17:32:54 -07:00 committed by GitHub
parent e9eb5ea20c
commit 58db05f8e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}