Incentive module multiple reward denom tests (#788)

* types: multiple rewards

* supply-side reward keeper methods

* remove legacy comments

* update hard claim reward to coins type

* borrow-side reward keeper methods

* update claim payout to sdk.Coins

* make tests compile

* fix genesis validation for compile

* comment out failing tests

* fix ! found logic

* accumulate hard supply rewards (single)

* sync hard supply rewards (single)

* update hard borrow/supply index denoms

* accumulate hard borrow rewards (single)

* sync hard borrow rewards (single)

* sync hard delegator rewards

* payout hard claim (single)

* accumulate hard supply rewards (multiple)

* sync hard supply rewards (multiple)

* update hard supply index denoms (multiple)

* update hard borrow index denoms (multiple)

* accumulate hard borrow rewards (multiple)

* sync hard borrow rewards (multiple)

* fix denom

* remove comment

* fix merge

* payout hard reward (multiple)

Co-authored-by: karzak <kjydavis3@gmail.com>
This commit is contained in:
Denali Marsh 2021-02-03 23:23:53 +01:00 committed by GitHub
parent 0c1fa5d27b
commit 754c018f04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 1853 additions and 1352 deletions

View File

@ -107,6 +107,9 @@ func (k Keeper) ClaimHardReward(ctx sdk.Context, addr sdk.AccAddress, multiplier
} }
rewardCoins = append(rewardCoins, sdk.NewCoin(coin.Denom, rewardAmount)) rewardCoins = append(rewardCoins, sdk.NewCoin(coin.Denom, rewardAmount))
} }
if rewardCoins.IsZero() {
return types.ErrZeroClaim
}
length, err := k.GetPeriodLength(ctx, multiplier) length, err := k.GetPeriodLength(ctx, multiplier)
if err != nil { if err != nil {
return err return err

View File

@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/auth"
"github.com/kava-labs/kava/app" "github.com/kava-labs/kava/app"
cdptypes "github.com/kava-labs/kava/x/cdp/types" cdptypes "github.com/kava-labs/kava/x/cdp/types"
"github.com/kava-labs/kava/x/hard"
"github.com/kava-labs/kava/x/incentive/types" "github.com/kava-labs/kava/x/incentive/types"
"github.com/kava-labs/kava/x/kavadist" "github.com/kava-labs/kava/x/kavadist"
validatorvesting "github.com/kava-labs/kava/x/validator-vesting" validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
@ -153,179 +154,281 @@ func (suite *KeeperTestSuite) TestPayoutUSDXMintingClaim() {
} }
} }
// func (suite *KeeperTestSuite) TestPayoutHardLiquidityProviderClaim() { func (suite *KeeperTestSuite) TestPayoutHardLiquidityProviderClaim() {
// type args struct { type args struct {
// deposit sdk.Coins deposit sdk.Coins
// borrow sdk.Coins borrow sdk.Coins
// rewardsPerSecond sdk.Coin rewardsPerSecond sdk.Coins
// initialTime time.Time initialTime time.Time
// multipliers types.Multipliers multipliers types.Multipliers
// multiplier types.MultiplierName multiplier types.MultiplierName
// timeElapsed int64 timeElapsed int64
// expectedReward sdk.Coin expectedRewards sdk.Coins
// expectedPeriods vesting.Periods expectedPeriods vesting.Periods
// isPeriodicVestingAccount bool isPeriodicVestingAccount bool
// } }
// type errArgs struct { type errArgs struct {
// expectPass bool expectPass bool
// contains string contains string
// } }
// type test struct { type test struct {
// name string name string
// args args args args
// errArgs errArgs errArgs errArgs
// } }
// testCases := []test{ testCases := []test{
// { {
// "valid 1 day", "single reward denom: valid 1 day",
// args{ args{
// deposit: cs(c("bnb", 10000000000)), deposit: cs(c("bnb", 10000000000)),
// borrow: cs(c("bnb", 5000000000)), borrow: cs(c("bnb", 5000000000)),
// rewardsPerSecond: c("hard", 122354), rewardsPerSecond: cs(c("hard", 122354)),
// initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC), initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
// multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))}, multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
// multiplier: types.MultiplierName("large"), multiplier: types.MultiplierName("large"),
// timeElapsed: 86400, timeElapsed: 86400,
// expectedReward: c("hard", 21142771200), // 10571385600 (deposit reward) + 10571385600 (borrow reward) expectedRewards: cs(c("hard", 21142771200)), // 10571385600 (deposit reward) + 10571385600 (borrow reward)
// expectedPeriods: vesting.Periods{vesting.Period{Length: 31536000, Amount: cs(c("hard", 21142771200))}}, expectedPeriods: vesting.Periods{vesting.Period{Length: 32918400, Amount: cs(c("hard", 21142771200))}},
// isPeriodicVestingAccount: true, isPeriodicVestingAccount: true,
// }, },
// errArgs{ errArgs{
// expectPass: true, expectPass: true,
// contains: "", contains: "",
// }, },
// }, },
// { {
// "invalid zero rewards", "single reward denom: valid 10 days",
// args{ args{
// deposit: cs(c("bnb", 10000000000)), deposit: cs(c("bnb", 10000000000)),
// borrow: cs(c("bnb", 5000000000)), borrow: cs(c("bnb", 5000000000)),
// rewardsPerSecond: c("hard", 0), rewardsPerSecond: cs(c("hard", 122354)),
// initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC), initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
// multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))}, multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
// multiplier: types.MultiplierName("large"), multiplier: types.MultiplierName("large"),
// timeElapsed: 86400, timeElapsed: 864000,
// expectedReward: sdk.Coin{}, expectedRewards: cs(c("hard", 211427712000)), // 105713856000 (deposit reward) + 105713856000 (borrow reward)
// expectedPeriods: vesting.Periods{}, expectedPeriods: vesting.Periods{vesting.Period{Length: 32140800, Amount: cs(c("hard", 211427712000))}},
// isPeriodicVestingAccount: false, isPeriodicVestingAccount: true,
// }, },
// errArgs{ errArgs{
// expectPass: false, expectPass: true,
// contains: "claim amount rounds to zero", contains: "",
// }, },
// }, },
// } {
"invalid zero rewards",
args{
deposit: cs(c("bnb", 10000000000)),
borrow: cs(c("bnb", 5000000000)),
rewardsPerSecond: cs(c("hard", 0)),
initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
multiplier: types.MultiplierName("large"),
timeElapsed: 86400,
expectedRewards: cs(c("hard", 0)),
expectedPeriods: vesting.Periods{},
isPeriodicVestingAccount: false,
},
errArgs{
expectPass: false,
contains: "claim amount rounds to zero",
},
},
{
"multiple reward denoms: valid 1 day",
args{
deposit: cs(c("bnb", 10000000000)),
borrow: cs(c("bnb", 5000000000)),
rewardsPerSecond: cs(c("hard", 122354), c("ukava", 122354)),
initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
multiplier: types.MultiplierName("large"),
timeElapsed: 86400,
expectedRewards: cs(c("hard", 21142771200), c("ukava", 21142771200)), // 10571385600 (deposit reward) + 10571385600 (borrow reward)
expectedPeriods: vesting.Periods{vesting.Period{Length: 32918400, Amount: cs(c("hard", 21142771200), c("ukava", 21142771200))}},
isPeriodicVestingAccount: true,
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"multiple reward denoms: valid 10 days",
args{
deposit: cs(c("bnb", 10000000000)),
borrow: cs(c("bnb", 5000000000)),
rewardsPerSecond: cs(c("hard", 122354), c("ukava", 122354)),
initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
multiplier: types.MultiplierName("large"),
timeElapsed: 864000,
expectedRewards: cs(c("hard", 211427712000), c("ukava", 211427712000)), // 105713856000 (deposit reward) + 105713856000 (borrow reward)
expectedPeriods: vesting.Periods{vesting.Period{Length: 32140800, Amount: cs(c("hard", 211427712000), c("ukava", 211427712000))}},
isPeriodicVestingAccount: true,
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"multiple reward denoms with different rewards per second: valid 1 day",
args{
deposit: cs(c("bnb", 10000000000)),
borrow: cs(c("bnb", 5000000000)),
rewardsPerSecond: cs(c("hard", 122354), c("ukava", 222222)),
initialTime: time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC),
multipliers: types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
multiplier: types.MultiplierName("large"),
timeElapsed: 86400,
expectedRewards: cs(c("hard", 21142771200), c("ukava", 38399961600)),
expectedPeriods: vesting.Periods{vesting.Period{Length: 32918400, Amount: cs(c("hard", 21142771200), c("ukava", 38399961600))}},
isPeriodicVestingAccount: true,
},
errArgs{
expectPass: true,
contains: "",
},
},
}
// for _, tc := range testCases { for _, tc := range testCases {
// suite.Run(tc.name, func() { suite.Run(tc.name, func() {
// suite.SetupWithGenState() suite.SetupWithGenState()
// suite.ctx = suite.ctx.WithBlockTime(tc.args.initialTime) suite.ctx = suite.ctx.WithBlockTime(tc.args.initialTime)
// // setup kavadist state // setup kavadist state
// sk := suite.app.GetSupplyKeeper() sk := suite.app.GetSupplyKeeper()
// err := sk.MintCoins(suite.ctx, kavadist.ModuleName, cs(c("hard", 1000000000000))) err := sk.MintCoins(suite.ctx, kavadist.ModuleName, cs(c("hard", 1000000000000000000), c("ukava", 1000000000000000000)))
// suite.Require().NoError(err) suite.Require().NoError(err)
// // Set up generic reward periods // Set up generic reward periods
// var multiRewardPeriods types.MultiRewardPeriods var multiRewardPeriods types.MultiRewardPeriods
// var rewardPeriods types.RewardPeriods var rewardPeriods types.RewardPeriods
// for _, coin := range tc.args.deposit { for _, coin := range tc.args.deposit {
// rewardPeriod := types.NewRewardPeriod(true, coin.Denom, tc.args.initialTime, tc.args.initialTime.Add(time.Hour*24*365*4), tc.args.rewardsPerSecond) if len(tc.args.rewardsPerSecond) > 0 {
// rewardPeriods = append(rewardPeriods, rewardPeriod) rewardPeriod := types.NewRewardPeriod(true, coin.Denom, tc.args.initialTime, tc.args.initialTime.Add(time.Hour*24*365*4), tc.args.rewardsPerSecond[0])
// multiRewardPeriod := types.NewMultiRewardPeriod(true, coin.Denom, tc.args.initialTime, tc.args.initialTime.Add(time.Hour*24*365*4), cs(tc.args.rewardsPerSecond)) rewardPeriods = append(rewardPeriods, rewardPeriod)
// multiRewardPeriods = append(multiRewardPeriods, multiRewardPeriod) }
// } multiRewardPeriod := types.NewMultiRewardPeriod(true, coin.Denom, tc.args.initialTime, tc.args.initialTime.Add(time.Hour*24*365*4), tc.args.rewardsPerSecond)
multiRewardPeriods = append(multiRewardPeriods, multiRewardPeriod)
}
// // Set up incentive state // Set up generic reward periods
// params := types.NewParams( params := types.NewParams(
// rewardPeriods, multiRewardPeriods, multiRewardPeriods, rewardPeriods, rewardPeriods, multiRewardPeriods, multiRewardPeriods, rewardPeriods,
// tc.args.multipliers, types.Multipliers{types.NewMultiplier(types.MultiplierName("small"), 1, d("0.25")), types.NewMultiplier(types.MultiplierName("large"), 12, d("1.0"))},
// tc.args.initialTime.Add(time.Hour*24*365*5), tc.args.initialTime.Add(time.Hour*24*365*5),
// ) )
// suite.keeper.SetParams(suite.ctx, params) suite.keeper.SetParams(suite.ctx, params)
// // Set each denom's previous accrual time and supply reward factor // Set each denom's previous accrual time and supply reward factor
// for _, coin := range tc.args.deposit { if len(tc.args.rewardsPerSecond) > 0 {
// suite.keeper.SetPreviousHardSupplyRewardAccrualTime(suite.ctx, coin.Denom, tc.args.initialTime) for _, coin := range tc.args.deposit {
// defaultRewardIndexes := types.RewardIndexes{types.NewRewardIndex(types.HardLiquidityRewardDenom, sdk.ZeroDec())} suite.keeper.SetPreviousHardSupplyRewardAccrualTime(suite.ctx, coin.Denom, tc.args.initialTime)
// suite.keeper.SetHardSupplyRewardIndexes(suite.ctx, coin.Denom, defaultRewardIndexes) var rewardIndexes types.RewardIndexes
// } for _, rewardCoin := range tc.args.rewardsPerSecond {
// for _, coin := range tc.args.borrow { rewardIndex := types.NewRewardIndex(rewardCoin.Denom, sdk.ZeroDec())
// suite.keeper.SetPreviousHardBorrowRewardAccrualTime(suite.ctx, coin.Denom, tc.args.initialTime) rewardIndexes = append(rewardIndexes, rewardIndex)
// defaultRewardIndexes := types.RewardIndexes{types.NewRewardIndex(types.HardLiquidityRewardDenom, sdk.ZeroDec())} }
// suite.keeper.SetHardBorrowRewardIndexes(suite.ctx, coin.Denom, defaultRewardIndexes) suite.keeper.SetHardSupplyRewardIndexes(suite.ctx, coin.Denom, rewardIndexes)
// } }
}
// hardKeeper := suite.app.GetHardKeeper() // Set each denom's previous accrual time and borrow reward factor
// userAddr := suite.addrs[3] if len(tc.args.rewardsPerSecond) > 0 {
for _, coin := range tc.args.borrow {
suite.keeper.SetPreviousHardBorrowRewardAccrualTime(suite.ctx, coin.Denom, tc.args.initialTime)
var rewardIndexes types.RewardIndexes
for _, rewardCoin := range tc.args.rewardsPerSecond {
rewardIndex := types.NewRewardIndex(rewardCoin.Denom, sdk.ZeroDec())
rewardIndexes = append(rewardIndexes, rewardIndex)
}
suite.keeper.SetHardBorrowRewardIndexes(suite.ctx, coin.Denom, rewardIndexes)
}
}
// // User deposits hardKeeper := suite.app.GetHardKeeper()
// err = hardKeeper.Deposit(suite.ctx, userAddr, tc.args.deposit) userAddr := suite.addrs[3]
// suite.Require().NoError(err)
// // User borrows // User deposits and borrows
// err = hardKeeper.Borrow(suite.ctx, userAddr, tc.args.borrow) err = hardKeeper.Deposit(suite.ctx, userAddr, tc.args.deposit)
// suite.Require().NoError(err) suite.Require().NoError(err)
err = hardKeeper.Borrow(suite.ctx, userAddr, tc.args.borrow)
suite.Require().NoError(err)
// // Check that Hard hooks initialized a HardLiquidityProviderClaim that has 0 rewards // Check that Hard hooks initialized a HardLiquidityProviderClaim that has 0 rewards
// claim, found := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, suite.addrs[3]) claim, found := suite.keeper.GetHardLiquidityProviderClaim(suite.ctx, suite.addrs[3])
// suite.Require().True(found) suite.Require().True(found)
// for _, coin := range tc.args.deposit { for _, coin := range tc.args.deposit {
// suite.Require().Equal(sdk.ZeroInt(), claim.Reward.AmountOf(coin.Denom)) suite.Require().Equal(sdk.ZeroInt(), claim.Reward.AmountOf(coin.Denom))
// } }
// // Set up future runtime context // Set up future runtime context
// runAtTime := time.Unix(suite.ctx.BlockTime().Unix()+(tc.args.timeElapsed), 0) runAtTime := time.Unix(suite.ctx.BlockTime().Unix()+(tc.args.timeElapsed), 0)
// runCtx := suite.ctx.WithBlockTime(runAtTime) runCtx := suite.ctx.WithBlockTime(runAtTime)
// // Run Hard begin blocker // Run Hard begin blocker
// hard.BeginBlocker(runCtx, suite.hardKeeper) hard.BeginBlocker(runCtx, suite.hardKeeper)
// // Accumulate supply rewards for each deposit denom // Accumulate supply rewards for each deposit denom
// for _, coin := range tc.args.deposit { for _, coin := range tc.args.deposit {
// rewardPeriod, found := suite.keeper.GetHardSupplyRewardPeriods(runCtx, coin.Denom) rewardPeriod, found := suite.keeper.GetHardSupplyRewardPeriods(runCtx, coin.Denom)
// suite.Require().True(found) suite.Require().True(found)
// err = suite.keeper.AccumulateHardSupplyRewards(runCtx, rewardPeriod) err = suite.keeper.AccumulateHardSupplyRewards(runCtx, rewardPeriod)
// suite.Require().NoError(err) suite.Require().NoError(err)
// } }
// // Accumulate borrow rewards for each deposit denom // Accumulate borrow rewards for each deposit denom
// for _, coin := range tc.args.borrow { for _, coin := range tc.args.borrow {
// rewardPeriod, found := suite.keeper.GetHardBorrowRewardPeriods(runCtx, coin.Denom) rewardPeriod, found := suite.keeper.GetHardBorrowRewardPeriods(runCtx, coin.Denom)
// suite.Require().True(found) suite.Require().True(found)
// err = suite.keeper.AccumulateHardBorrowRewards(runCtx, rewardPeriod) err = suite.keeper.AccumulateHardBorrowRewards(runCtx, rewardPeriod)
// suite.Require().NoError(err) suite.Require().NoError(err)
// } }
// // Fetch pre-claim balances // Sync hard supply rewards
// ak := suite.app.GetAccountKeeper() deposit, found := suite.hardKeeper.GetDeposit(suite.ctx, suite.addrs[3])
// preClaimAcc := ak.GetAccount(runCtx, suite.addrs[3]) suite.Require().True(found)
suite.keeper.SynchronizeHardSupplyReward(suite.ctx, deposit)
// err = suite.keeper.ClaimHardReward(runCtx, suite.addrs[3], tc.args.multiplier) // Sync hard borrow rewards
// if tc.errArgs.expectPass { borrow, found := suite.hardKeeper.GetBorrow(suite.ctx, suite.addrs[3])
// suite.Require().NoError(err) suite.Require().True(found)
suite.keeper.SynchronizeHardBorrowReward(suite.ctx, borrow)
// // Check that user's balance has increased by expected reward amount // Fetch pre-claim balances
// postClaimAcc := ak.GetAccount(suite.ctx, suite.addrs[3]) ak := suite.app.GetAccountKeeper()
// suite.Require().Equal(preClaimAcc.GetCoins().Add(tc.args.expectedReward), postClaimAcc.GetCoins()) preClaimAcc := ak.GetAccount(runCtx, suite.addrs[3])
// if tc.args.isPeriodicVestingAccount { err = suite.keeper.ClaimHardReward(runCtx, suite.addrs[3], tc.args.multiplier)
// vacc, ok := postClaimAcc.(*vesting.PeriodicVestingAccount) if tc.errArgs.expectPass {
// suite.Require().True(ok) suite.Require().NoError(err)
// suite.Require().Equal(tc.args.expectedPeriods, vacc.VestingPeriods)
// }
// // Check that the claim's reward amount has been reset to 0 // Check that user's balance has increased by expected reward amount
// claim, found := suite.keeper.GetHardLiquidityProviderClaim(runCtx, suite.addrs[3]) postClaimAcc := ak.GetAccount(suite.ctx, suite.addrs[3])
// suite.Require().True(found) suite.Require().Equal(preClaimAcc.GetCoins().Add(tc.args.expectedRewards...), postClaimAcc.GetCoins())
// suite.Require().Equal(c("hard", 0), claim.Reward)
// } else { if tc.args.isPeriodicVestingAccount {
// suite.Require().Error(err) vacc, ok := postClaimAcc.(*vesting.PeriodicVestingAccount)
// suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains)) suite.Require().True(ok)
// } suite.Require().Equal(tc.args.expectedPeriods, vacc.VestingPeriods)
// }) }
// }
// } // Check that each claim reward coin's amount has been reset to 0
claim, found := suite.keeper.GetHardLiquidityProviderClaim(runCtx, suite.addrs[3])
suite.Require().True(found)
for _, claimRewardCoin := range claim.Reward {
suite.Require().Equal(c(claimRewardCoin.Denom, 0), claimRewardCoin)
}
} else {
suite.Require().Error(err)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}
func (suite *KeeperTestSuite) TestSendCoinsToPeriodicVestingAccount() { func (suite *KeeperTestSuite) TestSendCoinsToPeriodicVestingAccount() {
type accountArgs struct { type accountArgs struct {

View File

@ -301,16 +301,17 @@ func (k Keeper) SynchronizeHardSupplyReward(ctx sdk.Context, deposit hardtypes.D
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := claim.SupplyRewardIndexes.GetRewardIndexIndex(coin.Denom)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue
}
for _, globalRewardIndex := range globalRewardIndexes { for _, globalRewardIndex := range globalRewardIndexes {
userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType) userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndex { if !foundUserRewardIndex {
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue
}
globalRewardFactor := globalRewardIndex.RewardFactor globalRewardFactor := globalRewardIndex.RewardFactor
userRewardFactor := userRewardIndex.RewardFactor userRewardFactor := userRewardIndex.RewardFactor
@ -377,16 +378,17 @@ func (k Keeper) SynchronizeHardBorrowReward(ctx sdk.Context, borrow hardtypes.Bo
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := claim.BorrowRewardIndexes.GetRewardIndexIndex(coin.Denom)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue
}
for _, globalRewardIndex := range globalRewardIndexes { for _, globalRewardIndex := range globalRewardIndexes {
userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType) userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndex { if !foundUserRewardIndex {
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", coin.Denom) // TODO: remove before production
continue
}
globalRewardFactor := globalRewardIndex.RewardFactor globalRewardFactor := globalRewardIndex.RewardFactor
userRewardFactor := userRewardIndex.RewardFactor userRewardFactor := userRewardIndex.RewardFactor
@ -616,11 +618,11 @@ func (k Keeper) SynchronizeHardLiquidityProviderClaim(ctx sdk.Context, owner sdk
k.SynchronizeHardSupplyReward(ctx, deposit) k.SynchronizeHardSupplyReward(ctx, deposit)
} }
// // Synchronize any hard liquidity borrow-side rewards // Synchronize any hard liquidity borrow-side rewards
// borrow, foundBorrow := k.hardKeeper.GetBorrow(ctx, owner) borrow, foundBorrow := k.hardKeeper.GetBorrow(ctx, owner)
// if foundBorrow { if foundBorrow {
// k.SynchronizeHardBorrowReward(ctx, borrow) k.SynchronizeHardBorrowReward(ctx, borrow)
// } }
// Synchronize any hard delegator rewards // Synchronize any hard delegator rewards
k.SynchronizeHardDelegatorRewards(ctx, owner) k.SynchronizeHardDelegatorRewards(ctx, owner)
@ -668,16 +670,17 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := claim.SupplyRewardIndexes.GetRewardIndexIndex(ri.CollateralType)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", ri.CollateralType) // TODO: remove before production
continue
}
for _, globalRewardIndex := range globalRewardIndexes { for _, globalRewardIndex := range globalRewardIndexes {
userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType) userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndex { if !foundUserRewardIndex {
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", ri.CollateralType) // TODO: remove before production
continue
}
globalRewardFactor := globalRewardIndex.RewardFactor globalRewardFactor := globalRewardIndex.RewardFactor
userRewardFactor := userRewardIndex.RewardFactor userRewardFactor := userRewardIndex.RewardFactor
@ -717,16 +720,17 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := claim.BorrowRewardIndexes.GetRewardIndexIndex(ri.CollateralType)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", ri.CollateralType) // TODO: remove before production
continue
}
for _, globalRewardIndex := range globalRewardIndexes { for _, globalRewardIndex := range globalRewardIndexes {
userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType) userRewardIndex, foundUserRewardIndex := userRewardIndexes.RewardIndexes.GetRewardIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndex { if !foundUserRewardIndex {
continue continue
} }
userRewardIndexIndex, foundUserRewardIndexIndex := userRewardIndexes.RewardIndexes.GetFactorIndex(globalRewardIndex.CollateralType)
if !foundUserRewardIndexIndex {
fmt.Printf("\n[LOG]: factor index for %s should always be found", ri.CollateralType) // TODO: remove before production
continue
}
globalRewardFactor := globalRewardIndex.RewardFactor globalRewardFactor := globalRewardIndex.RewardFactor
userRewardFactor := userRewardIndex.RewardFactor userRewardFactor := userRewardIndex.RewardFactor
@ -748,7 +752,7 @@ func (k Keeper) SimulateHardSynchronization(ctx sdk.Context, claim types.HardLiq
fmt.Printf("[LOG]: factor index for %s should always be found", ri.CollateralType) // TODO: remove before production fmt.Printf("[LOG]: factor index for %s should always be found", ri.CollateralType) // TODO: remove before production
continue continue
} }
claim.SupplyRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor claim.BorrowRewardIndexes[userRewardIndexIndex].RewardIndexes[factorIndex].RewardFactor = globalRewardIndex.RewardFactor
newRewardsCoin := sdk.NewCoin(userRewardIndex.CollateralType, newRewardsAmount) newRewardsCoin := sdk.NewCoin(userRewardIndex.CollateralType, newRewardsAmount)
claim.Reward = claim.Reward.Add(newRewardsCoin) claim.Reward = claim.Reward.Add(newRewardsCoin)
} }

File diff suppressed because it is too large Load Diff

View File

@ -458,6 +458,16 @@ func (mris MultiRewardIndexes) GetRewardIndex(denom string) (MultiRewardIndex, b
return MultiRewardIndex{}, false return MultiRewardIndex{}, false
} }
// GetRewardIndexIndex fetches a specific reward index inside the array by its denom
func (mris MultiRewardIndexes) GetRewardIndexIndex(denom string) (int, bool) {
for i, ri := range mris {
if ri.CollateralType == denom {
return i, true
}
}
return -1, false
}
// Validate validation for reward indexes // Validate validation for reward indexes
func (mris MultiRewardIndexes) Validate() error { func (mris MultiRewardIndexes) Validate() error {
for _, mri := range mris { for _, mri := range mris {