This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
distribution.go
72 lines (62 loc) · 2.89 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"
)
func (app *App) getCurrentRewardAndCurrentRatio(addr sdk.ValAddress) (string, string) {
currentReward := "0"
currentRatio := "0"
reward := app.DistrKeeper.GetValidatorCurrentRewards(app.DeliverContext, addr)
latestReward := app.DistrKeeper.GetValidatorHistoricalRewards(app.DeliverContext, 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 (app *App) emitUpdateValidatorRewardAndAccumulatedCommission(addr sdk.ValAddress) {
currentReward, currentRatio := app.getCurrentRewardAndCurrentRatio(addr)
accCommission, _ := app.DistrKeeper.GetValidatorAccumulatedCommission(app.DeliverContext, addr).TruncateDecimal()
app.Write("UPDATE_VALIDATOR", JsDict{
"operator_address": addr.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
"accumulated_commission": accCommission.String(),
})
}
func (app *App) emitUpdateValidatorReward(addr sdk.ValAddress) {
currentReward, currentRatio := app.getCurrentRewardAndCurrentRatio(addr)
app.Write("UPDATE_VALIDATOR", JsDict{
"operator_address": addr.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
})
}
// handleMsgWithdrawDelegatorReward implements emitter handler for MsgWithdrawDelegatorReward.
func (app *App) handleMsgWithdrawDelegatorReward(
txHash []byte, msg dist.MsgWithdrawDelegatorReward, evMap EvMap, extra JsDict,
) {
withdrawAddr := app.DistrKeeper.GetDelegatorWithdrawAddr(app.DeliverContext, msg.DelegatorAddress)
app.AddAccountsInTx(withdrawAddr)
app.emitUpdateValidatorReward(msg.ValidatorAddress)
app.emitDelegationAfterWithdrawReward(msg.ValidatorAddress, withdrawAddr)
extra["reward_amount"] = evMap[dist.EventTypeWithdrawRewards+"."+sdk.AttributeKeyAmount][0]
}
// handleMsgSetWithdrawAddress implements emitter handler for MsgSetWithdrawAddress.
func (app *App) handleMsgSetWithdrawAddress(
txHash []byte, msg dist.MsgSetWithdrawAddress, evMap EvMap, extra JsDict,
) {
app.AddAccountsInTx(msg.WithdrawAddress)
}
// handleMsgWithdrawValidatorCommission implements emitter handler for MsgWithdrawValidatorCommission.
func (app *App) handleMsgWithdrawValidatorCommission(
txHash []byte, msg dist.MsgWithdrawValidatorCommission, evMap EvMap, extra JsDict,
) {
withdrawAddr := app.DistrKeeper.GetDelegatorWithdrawAddr(app.DeliverContext, sdk.AccAddress(msg.ValidatorAddress))
app.AddAccountsInTx(withdrawAddr)
app.emitUpdateValidatorRewardAndAccumulatedCommission(msg.ValidatorAddress)
extra["commission_amount"] = evMap[types.EventTypeWithdrawCommission+"."+sdk.AttributeKeyAmount][0]
}