Skip to content

Commit

Permalink
Fix rewards compute
Browse files Browse the repository at this point in the history
  • Loading branch information
chengwenxi committed Mar 19, 2019
1 parent 4da9f9b commit dcf30c9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions client/distribution/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func GetRewards(distrStoreName string, stakeStoreName string, cliCtx context.CLI
totalWithdraw := types.DecCoins{}
rewardsOutput := RewardsOutput{}

var selfVdi types.ValidatorDistInfo
selfValidator := GetValidator(stakeStoreName, cliCtx, sdk.ValAddress(account))
if selfValidator.OperatorAddr.Equals(sdk.ValAddress(account)) {
selfVdi = GetValidatorDistInfo(distrStoreName, cliCtx, selfValidator.GetOperator())
}

// get all delegator rewards
res, err := cliCtx.QuerySubspace(stakeKeeper.GetDelegationsKey(account), stakeStoreName)
if err != nil {
Expand All @@ -59,6 +65,7 @@ func GetRewards(distrStoreName string, stakeStoreName string, cliCtx context.CLI

feePool := GetFeePool(distrStoreName, cliCtx)
chainHeight, err := tendermint.GetChainHeight(cliCtx)

for _, re := range res {
del := stakeTypes.MustUnmarshalDelegation(cliCtx.Codec, re.Key, re.Value)
valAddr := del.GetValidatorAddr()
Expand All @@ -69,18 +76,19 @@ func GetRewards(distrStoreName string, stakeStoreName string, cliCtx context.CLI
vdi := GetValidatorDistInfo(distrStoreName, cliCtx, valAddr)
ddi := GetDelegationDistInfo(distrStoreName, cliCtx, del.DelegatorAddr, del.ValidatorAddr)
wc := GetWithdrawContext(stakeStoreName, cliCtx, feePool, chainHeight, validator)
_, _, _, diWithdraw := ddi.WithdrawRewards(log.NewNopLogger(), wc, vdi, validator.GetDelegatorShares(), del.GetShares())
_, vdi, _, diWithdraw := ddi.WithdrawRewards(log.NewNopLogger(), wc, vdi, validator.GetDelegatorShares(), del.GetShares())
totalWithdraw = totalWithdraw.Plus(diWithdraw)
rewardTruncate, _ := diWithdraw.TruncateDecimal()
rewardsOutput.Delegations = append(rewardsOutput.Delegations, DelegationsReward{valAddr, rewardTruncate})
if vdi.OperatorAddr.Equals(selfValidator.OperatorAddr) {
selfVdi = vdi
}
}

// get all validator rewards
validator := GetValidator(stakeStoreName, cliCtx, sdk.ValAddress(account))
if validator.OperatorAddr.Equals(sdk.ValAddress(account)) {
wc := GetWithdrawContext(stakeStoreName, cliCtx, feePool, chainHeight, validator)
valInfo := GetValidatorDistInfo(distrStoreName, cliCtx, validator.GetOperator())
valInfo, _, commission := valInfo.WithdrawCommission(log.NewNopLogger(), wc)
if selfVdi.OperatorAddr.Equals(sdk.ValAddress(account)) {
wc := GetWithdrawContext(stakeStoreName, cliCtx, feePool, chainHeight, selfValidator)
_, _, commission := selfVdi.WithdrawCommission(log.NewNopLogger(), wc)
totalWithdraw = totalWithdraw.Plus(commission)
rewardTruncate, _ := commission.TruncateDecimal()
rewardsOutput.Commission = rewardTruncate
Expand Down

0 comments on commit dcf30c9

Please sign in to comment.