Skip to content

Commit

Permalink
update remaining reward calc method
Browse files Browse the repository at this point in the history
  • Loading branch information
zsystm committed Jul 25, 2023
1 parent d6ed3cf commit f74fd46
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
15 changes: 12 additions & 3 deletions x/liquidstaking/keeper/net_amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ func (k Keeper) GetNetAmountState(ctx sdk.Context) (nas types.NetAmountState) {
cachedCtx, _ := ctx.CacheContext()
endingPeriod := k.distributionKeeper.IncrementValidatorPeriod(cachedCtx, validator)
delRewards := k.distributionKeeper.CalculateDelegationRewards(cachedCtx, validator, delegation, endingPeriod)
// chunk's remaining reward is calculated by
// 1. rest = del_reward - insurance_commission
// 2. remaining = rest x (1 - module_fee_rate)
// 3. result = remaining x (1 - discount_rate)
delReward := delRewards.AmountOf(bondDenom)
insuranceCommission := delReward.Mul(pairedIns.FeeRate)
// insuranceCommission is not reward of module
pureReward := delReward.Sub(insuranceCommission)
totalRemainingRewards = totalRemainingRewards.Add(pureReward)
restReward := delReward.Sub(insuranceCommission)
moduleFeeRate, _ := k.CalcDynamicFeeRate(ctx)
remainingReward := restReward.Mul(sdk.OneDec().Sub(moduleFeeRate))
totalRemainingRewards = totalRemainingRewards.Add(
remainingReward.Mul(
sdk.OneDec().Sub(k.CalcDiscountRate(ctx)),
),
)
default:
k.stakingKeeper.IterateDelegatorUnbondingDelegations(ctx, chunk.DerivedAddress(), func(ubd stakingtypes.UnbondingDelegation) (stop bool) {
for _, entry := range ubd.Entries {
Expand Down
42 changes: 42 additions & 0 deletions x/liquidstaking/keeper/net_amount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper_test

import (
"github.com/Canto-Network/Canto/v6/x/liquidstaking/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (suite *KeeperTestSuite) TestGetNetAmountState() {
env := suite.setupLiquidStakeTestingEnv(testingEnvOptions{
desc: "",
numVals: 3,
fixedValFeeRate: TenPercentFeeRate,
valFeeRates: nil,
fixedPower: 1,
powers: nil,
numInsurances: 3,
fixedInsuranceFeeRate: TenPercentFeeRate,
insuranceFeeRates: nil,
numPairedChunks: 3,
fundingAccountBalance: types.ChunkSize.MulRaw(40),
})

suite.ctx = suite.advanceHeight(suite.ctx, 100, "delegation rewards are accumulated")
nas := suite.app.LiquidStakingKeeper.GetNetAmountState(suite.ctx)

cachedCtx, _ := suite.ctx.CacheContext()
suite.app.DistrKeeper.WithdrawDelegationRewards(cachedCtx, env.pairedChunks[0].DerivedAddress(), env.insurances[0].GetValidator())
delReward := suite.app.BankKeeper.GetBalance(cachedCtx, env.pairedChunks[0].DerivedAddress(), suite.denom)
totalDelReward := delReward.Amount.MulRaw(int64(len(env.pairedChunks)))
suite.Equal("8999964000143999250000", totalDelReward.String())

// Calc TotalRemainingRewards manually
rest := totalDelReward.ToDec().Mul(sdk.OneDec().Sub(TenPercentFeeRate))
remaining := rest.Mul(sdk.OneDec().Sub(nas.FeeRate))
result := remaining.Mul(sdk.OneDec().Sub(nas.DiscountRate))
suite.Equal("7595237306532985264014.570227833838100000", result.String())
suite.Equal(result.String(), nas.TotalRemainingRewards.String())
suite.True(
totalDelReward.GT(nas.TotalRemainingRewards.TruncateInt()),
"total del reward should be greater than total remaining rewards",
)
}

0 comments on commit f74fd46

Please sign in to comment.