-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdelegatorsProcessor.go
More file actions
179 lines (153 loc) · 5.75 KB
/
delegatorsProcessor.go
File metadata and controls
179 lines (153 loc) · 5.75 KB
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
package logsevents
import (
"encoding/hex"
"math/big"
"strconv"
"time"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-es-indexer-go/data"
indexer "github.com/multiversx/mx-chain-es-indexer-go/process/dataindexer"
)
const (
minNumTopicsDelegators = 4
delegateFunc = "delegate"
unDelegateFunc = "unDelegate"
withdrawFunc = "withdraw"
reDelegateRewardsFunc = "reDelegateRewards"
claimRewardsFunc = "claimRewards"
removeDelegationFromSourceFunc = "removeDelegationFromSource"
moveDelegationToDestinationFunc = "moveDelegationToDestination"
minNumTopicsClaimRewards = 2
numTopicsClaimRewardsWithContractAddress = 3
)
type delegatorsProc struct {
balanceConverter indexer.BalanceConverter
pubkeyConverter core.PubkeyConverter
delegatorsOperations map[string]struct{}
}
func newDelegatorsProcessor(
pubkeyConverter core.PubkeyConverter,
balanceConverter indexer.BalanceConverter,
) *delegatorsProc {
return &delegatorsProc{
delegatorsOperations: map[string]struct{}{
delegateFunc: {},
unDelegateFunc: {},
withdrawFunc: {},
reDelegateRewardsFunc: {},
claimRewardsFunc: {},
removeDelegationFromSourceFunc: {},
moveDelegationToDestinationFunc: {},
},
pubkeyConverter: pubkeyConverter,
balanceConverter: balanceConverter,
}
}
func (dp *delegatorsProc) processEvent(args *argsProcessEvent) argOutputProcessEvent {
if args.selfShardID != core.MetachainShardId {
return argOutputProcessEvent{}
}
eventIdentifierStr := string(args.event.GetIdentifier())
_, ok := dp.delegatorsOperations[eventIdentifierStr]
if !ok {
return argOutputProcessEvent{}
}
if eventIdentifierStr == claimRewardsFunc {
return argOutputProcessEvent{
delegator: dp.getDelegatorFromClaimRewardsEvent(args),
processed: true,
}
}
topics := args.event.GetTopics()
if len(topics) < minNumTopicsDelegators {
return argOutputProcessEvent{
processed: true,
}
}
// for delegate / unDelegate / withdraw / reDelegateRewards
// topics slice contains:
// topics[0] = delegated value / unDelegated value / withdraw value / reDelegated value
// topics[1] = active stake
// topics[2] = num contract users
// topics[3] = total contract active stake
// topics[4] = true - if the delegator was deleted in case of withdrawal
// the contract address in case of delegate operations from staking v3.5 (makeNewContractFromValidatorData, mergeValidatorToDelegationSameOwner or mergeValidatorToDelegationWithWhitelist)
// unDelegate fund key in case of unDelegate operation
// topics[5:] = unDelegate fund keys in case of withdrawal
activeStake := big.NewInt(0).SetBytes(topics[1])
contractAddr := dp.pubkeyConverter.SilentEncode(args.logAddress, log)
if len(topics) >= minNumTopicsDelegators+1 && eventIdentifierStr == delegateFunc {
contractAddr = dp.pubkeyConverter.SilentEncode(topics[4], log)
}
encodedAddr := dp.pubkeyConverter.SilentEncode(args.event.GetAddress(), log)
activeStakeNum, err := dp.balanceConverter.ComputeBalanceAsFloat(activeStake)
if err != nil {
log.Warn("delegatorsProc.processEvent cannot compute active stake as num", "active stake", activeStake,
"hash", args.txHashHexEncoded, "error", err)
}
delegator := &data.Delegator{
Address: encodedAddr,
Contract: contractAddr,
ActiveStake: activeStake.String(),
ActiveStakeNum: activeStakeNum,
Timestamp: time.Duration(args.timestamp),
}
if eventIdentifierStr == withdrawFunc && len(topics) >= minNumTopicsDelegators+1 {
delegator.ShouldDelete = bytesToBool(topics[4])
withdrawFundIds := topics[5:]
delegator.WithdrawFundIDs = make([]string, 0, len(withdrawFundIds))
for _, id := range withdrawFundIds {
delegator.WithdrawFundIDs = append(delegator.WithdrawFundIDs, hex.EncodeToString(id))
}
}
if eventIdentifierStr == unDelegateFunc && len(topics) >= minNumTopicsDelegators+1 {
unDelegateValue := big.NewInt(0).SetBytes(topics[0])
unDelegatedValueNum, errUn := dp.balanceConverter.ComputeBalanceAsFloat(unDelegateValue)
if errUn != nil {
log.Warn("delegatorsProc.processEvent cannot compute undelegated value as num",
"undelegated value", unDelegateValue, "hash", args.txHashHexEncoded, "error", errUn)
}
delegator.UnDelegateInfo = &data.UnDelegate{
Timestamp: time.Duration(args.timestamp),
Value: unDelegateValue.String(),
ValueNum: unDelegatedValueNum,
ID: hex.EncodeToString(topics[4]),
}
}
return argOutputProcessEvent{
delegator: delegator,
processed: true,
}
}
func (dp *delegatorsProc) getDelegatorFromClaimRewardsEvent(args *argsProcessEvent) *data.Delegator {
topics := args.event.GetTopics()
// for claimRewards
// topics slice contains:
// topics[0] -- claimed rewards
// topics[1] -- true = if delegator was deleted
// topics[2] -- if is present will contain the contract address
if len(topics) < minNumTopicsClaimRewards {
return nil
}
shouldDelete := bytesToBool(topics[minNumTopicsClaimRewards-1])
if !shouldDelete {
return nil
}
encodedAddr := dp.pubkeyConverter.SilentEncode(args.event.GetAddress(), log)
encodedContractAddr := dp.pubkeyConverter.SilentEncode(args.logAddress, log)
if len(topics) == numTopicsClaimRewardsWithContractAddress {
encodedContractAddr = dp.pubkeyConverter.SilentEncode(topics[numTopicsClaimRewardsWithContractAddress-1], log)
}
return &data.Delegator{
Address: encodedAddr,
Contract: encodedContractAddr,
ShouldDelete: shouldDelete,
}
}
func bytesToBool(boolBytes []byte) bool {
b, err := strconv.ParseBool(string(boolBytes))
if err != nil {
log.Warn("delegatorsProc.bytesToBool", "error", err.Error())
}
return b
}