/
staking.go
202 lines (182 loc) · 8.21 KB
/
staking.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package emitter
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/staking/exported"
types "github.com/cosmos/cosmos-sdk/x/staking/types"
)
var (
EventTypeCompleteUnbonding = types.EventTypeCompleteUnbonding
EventTypeCompleteRedelegation = types.EventTypeCompleteRedelegation
)
func (app *App) emitStakingModule() {
app.StakingKeeper.IterateValidators(app.DeliverContext, func(_ int64, val exported.ValidatorI) (stop bool) {
app.emitSetValidator(val.GetOperator())
return false
})
app.StakingKeeper.IterateAllDelegations(app.DeliverContext, func(delegation types.Delegation) (stop bool) {
app.emitDelegation(delegation.ValidatorAddress, delegation.DelegatorAddress)
return false
})
app.StakingKeeper.IterateRedelegations(app.DeliverContext, func(_ int64, red types.Redelegation) (stop bool) {
for _, entry := range red.Entries {
app.Write("NEW_REDELEGATION", JsDict{
"delegator_address": red.DelegatorAddress,
"operator_src_address": red.ValidatorSrcAddress,
"operator_dst_address": red.ValidatorDstAddress,
"completion_time": entry.CompletionTime.UnixNano(),
"amount": entry.SharesDst.String(),
})
}
return false
})
app.StakingKeeper.IterateUnbondingDelegations(app.DeliverContext, func(_ int64, ubd types.UnbondingDelegation) (stop bool) {
for _, entry := range ubd.Entries {
app.Write("NEW_UNBONDING_DELEGATION", JsDict{
"delegator_address": ubd.DelegatorAddress,
"operator_address": ubd.ValidatorAddress,
"completion_time": entry.CompletionTime.UnixNano(),
"amount": entry.Balance.String(),
})
}
return false
})
}
func (app *App) emitSetValidator(addr sdk.ValAddress) {
val, _ := app.StakingKeeper.GetValidator(app.DeliverContext, addr)
currentReward, currentRatio := app.getCurrentRewardAndCurrentRatio(addr)
accCommission, _ := app.DistrKeeper.GetValidatorAccumulatedCommission(app.DeliverContext, addr).TruncateDecimal()
app.Write("SET_VALIDATOR", JsDict{
"operator_address": addr.String(),
"delegator_address": sdk.AccAddress(addr).String(),
"consensus_address": sdk.ConsAddress(val.ConsPubKey.Address()).String(),
"consensus_pubkey": sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, val.ConsPubKey),
"moniker": val.Description.Moniker,
"identity": val.Description.Identity,
"website": val.Description.Website,
"details": val.Description.Details,
"commission_rate": val.Commission.Rate.String(),
"commission_max_rate": val.Commission.MaxRate.String(),
"commission_max_change": val.Commission.MaxChangeRate.String(),
"min_self_delegation": val.MinSelfDelegation.String(),
"tokens": val.Tokens.Uint64(),
"jailed": val.Jailed,
"delegator_shares": val.DelegatorShares.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
"accumulated_commission": accCommission.String(),
"last_update": app.DeliverContext.BlockTime().UnixNano(),
})
}
func (app *App) emitUpdateValidator(addr sdk.ValAddress) {
val, _ := app.StakingKeeper.GetValidator(app.DeliverContext, addr)
currentReward, currentRatio := app.getCurrentRewardAndCurrentRatio(addr)
app.Write("UPDATE_VALIDATOR", JsDict{
"operator_address": addr.String(),
"tokens": val.Tokens.Uint64(),
"delegator_shares": val.DelegatorShares.String(),
"current_reward": currentReward,
"current_ratio": currentRatio,
"last_update": app.DeliverContext.BlockTime().UnixNano(),
})
}
func (app *App) emitUpdateValidatorStatus(addr sdk.ValAddress) {
status := app.OracleKeeper.GetValidatorStatus(app.DeliverContext, addr)
app.Write("UPDATE_VALIDATOR", JsDict{
"operator_address": addr.String(),
"status": status.IsActive,
"status_since": status.Since.UnixNano(),
})
}
func (app *App) emitDelegationAfterWithdrawReward(operatorAddress sdk.ValAddress, delegatorAddress sdk.AccAddress) {
_, ratio := app.getCurrentRewardAndCurrentRatio(operatorAddress)
app.Write("UPDATE_DELEGATION", JsDict{
"delegator_address": delegatorAddress,
"operator_address": operatorAddress,
"last_ratio": ratio,
})
}
func (app *App) emitDelegation(operatorAddress sdk.ValAddress, delegatorAddress sdk.AccAddress) {
delegation, found := app.StakingKeeper.GetDelegation(app.DeliverContext, delegatorAddress, operatorAddress)
if found {
_, ratio := app.getCurrentRewardAndCurrentRatio(operatorAddress)
app.Write("SET_DELEGATION", JsDict{
"delegator_address": delegatorAddress,
"operator_address": operatorAddress,
"shares": delegation.Shares.String(),
"last_ratio": ratio,
})
} else {
app.Write("REMOVE_DELEGATION", JsDict{
"delegator_address": delegatorAddress,
"operator_address": operatorAddress,
})
}
}
// handleMsgCreateValidator implements emitter handler for MsgCreateValidator.
func (app *App) handleMsgCreateValidator(
txHash []byte, msg staking.MsgCreateValidator, evMap EvMap, extra JsDict,
) {
app.emitSetValidator(msg.ValidatorAddress)
app.emitDelegation(msg.ValidatorAddress, msg.DelegatorAddress)
}
// handleMsgEditValidator implements emitter handler for MsgEditValidator.
func (app *App) handleMsgEditValidator(
txHash []byte, msg staking.MsgEditValidator, evMap EvMap, extra JsDict,
) {
app.emitSetValidator(msg.ValidatorAddress)
}
func (app *App) emitUpdateValidatorAndDelegation(operatorAddress sdk.ValAddress, delegatorAddress sdk.AccAddress) {
app.emitUpdateValidator(operatorAddress)
app.emitDelegation(operatorAddress, delegatorAddress)
}
// handleMsgDelegate implements emitter handler for MsgDelegate
func (app *App) handleMsgDelegate(
txHash []byte, msg staking.MsgDelegate, evMap EvMap, extra JsDict,
) {
app.emitUpdateValidatorAndDelegation(msg.ValidatorAddress, msg.DelegatorAddress)
}
// handleMsgUndelegate implements emitter handler for MsgUndelegate
func (app *App) handleMsgUndelegate(
txHash []byte, msg staking.MsgUndelegate, evMap EvMap, extra JsDict,
) {
app.emitUpdateValidatorAndDelegation(msg.ValidatorAddress, msg.DelegatorAddress)
app.emitUnbondingDelegation(msg, evMap)
}
func (app *App) emitUnbondingDelegation(msg staking.MsgUndelegate, evMap EvMap) {
completeTime, _ := time.Parse(time.RFC3339, evMap[types.EventTypeUnbond+"."+types.AttributeKeyCompletionTime][0])
app.Write("NEW_UNBONDING_DELEGATION", JsDict{
"delegator_address": msg.DelegatorAddress,
"operator_address": msg.ValidatorAddress,
"creation_height": app.DeliverContext.BlockHeight(),
"completion_time": completeTime.UnixNano(),
"amount": evMap[types.EventTypeUnbond+"."+sdk.AttributeKeyAmount][0],
})
}
// handleMsgBeginRedelegate implements emitter handler for MsgBeginRedelegate
func (app *App) handleMsgBeginRedelegate(
txHash []byte, msg staking.MsgBeginRedelegate, evMap EvMap, extra JsDict,
) {
app.emitUpdateValidatorAndDelegation(msg.ValidatorSrcAddress, msg.DelegatorAddress)
app.emitUpdateValidatorAndDelegation(msg.ValidatorDstAddress, msg.DelegatorAddress)
app.emitUpdateRedelation(msg.ValidatorSrcAddress, msg.ValidatorDstAddress, msg.DelegatorAddress, evMap)
}
func (app *App) emitUpdateRedelation(operatorSrcAddress sdk.ValAddress, operatorDstAddress sdk.ValAddress, delegatorAddress sdk.AccAddress, evMap EvMap) {
completeTime, _ := time.Parse(time.RFC3339, evMap[types.EventTypeRedelegate+"."+types.AttributeKeyCompletionTime][0])
app.Write("NEW_REDELEGATION", JsDict{
"delegator_address": delegatorAddress.String(),
"operator_src_address": operatorSrcAddress.String(),
"operator_dst_address": operatorDstAddress.String(),
"completion_time": completeTime.UnixNano(),
"amount": evMap[types.EventTypeRedelegate+"."+sdk.AttributeKeyAmount][0],
})
}
func (app *App) handleEventTypeCompleteUnbonding(evMap EvMap) {
acc, _ := sdk.AccAddressFromBech32(evMap[types.EventTypeCompleteUnbonding+"."+types.AttributeKeyDelegator][0])
app.Write("REMOVE_UNBONDING", JsDict{"timestamp": app.DeliverContext.BlockTime().UnixNano()})
app.AddAccountsInBlock(acc)
}
func (app *App) handEventTypeCompleteRedelegation(evMap EvMap) {
app.Write("REMOVE_REDELEGATION", JsDict{"timestamp": app.DeliverContext.BlockTime().UnixNano()})
}