/
distribution.go
72 lines (61 loc) · 2.91 KB
/
distribution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package emitter
import (
sdk "github.com/cosmos/cosmos-sdk/types"
dist "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/bandprotocol/chain/hooks/common"
)
func (h *Hook) getCurrentRewardAndCurrentRatio(ctx sdk.Context, addr sdk.ValAddress) (string, string) {
currentReward := "0"
currentRatio := "0"
reward := h.distrKeeper.GetValidatorCurrentRewards(ctx, addr)
latestReward := h.distrKeeper.GetValidatorHistoricalRewards(ctx, addr, reward.Period-1)
if !reward.Rewards.IsZero() {
currentReward = reward.Rewards[0].Amount.String()
}
if !latestReward.CumulativeRewardRatio.IsZero() {
currentRatio = latestReward.CumulativeRewardRatio[0].Amount.String()
}
return currentReward, currentRatio
}
func (h *Hook) emitUpdateValidatorRewardAndAccumulatedCommission(ctx sdk.Context, addr sdk.ValAddress) {
currentReward, currentRatio := h.getCurrentRewardAndCurrentRatio(ctx, addr)
accCommission, _ := h.distrKeeper.GetValidatorAccumulatedCommission(ctx, addr).TruncateDecimal()
h.Write("UPDATE_VALIDATOR", common.JsDict{
"operator_address": addr.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
"accumulated_commission": accCommission.String(),
})
}
func (h *Hook) emitUpdateValidatorReward(ctx sdk.Context, addr sdk.ValAddress) {
currentReward, currentRatio := h.getCurrentRewardAndCurrentRatio(ctx, addr)
h.Write("UPDATE_VALIDATOR", common.JsDict{
"operator_address": addr.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
})
}
// handleMsgWithdrawDelegatorReward implements emitter handler for MsgWithdrawDelegatorReward.
func (h *Hook) handleMsgWithdrawDelegatorReward(
ctx sdk.Context, msg dist.MsgWithdrawDelegatorReward, evMap common.EvMap, extra common.JsDict,
) {
withdrawAddr := h.distrKeeper.GetDelegatorWithdrawAddr(ctx, msg.DelegatorAddress)
h.AddAccountsInTx(withdrawAddr)
h.emitUpdateValidatorReward(ctx, msg.ValidatorAddress)
h.emitDelegationAfterWithdrawReward(ctx, msg.ValidatorAddress, withdrawAddr)
extra["reward_amount"] = evMap[dist.EventTypeWithdrawRewards+"."+sdk.AttributeKeyAmount][0]
}
// handleMsgSetWithdrawAddress implements emitter handler for MsgSetWithdrawAddress.
func (h *Hook) handleMsgSetWithdrawAddress(msg dist.MsgSetWithdrawAddress) {
h.AddAccountsInTx(msg.WithdrawAddress)
}
// handleMsgWithdrawValidatorCommission implements emitter handler for MsgWithdrawValidatorCommission.
func (h *Hook) handleMsgWithdrawValidatorCommission(
ctx sdk.Context, msg dist.MsgWithdrawValidatorCommission, evMap common.EvMap, extra common.JsDict,
) {
withdrawAddr := h.distrKeeper.GetDelegatorWithdrawAddr(ctx, sdk.AccAddress(msg.ValidatorAddress))
h.AddAccountsInTx(withdrawAddr)
h.emitUpdateValidatorRewardAndAccumulatedCommission(ctx, msg.ValidatorAddress)
extra["commission_amount"] = evMap[types.EventTypeWithdrawCommission+"."+sdk.AttributeKeyAmount][0]
}