forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redelegations.go
84 lines (74 loc) · 2.34 KB
/
redelegations.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
package common
import (
"context"
"sync"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/rs/zerolog/log"
"github.com/faddat/bdjuno/database"
"github.com/faddat/bdjuno/x/staking/types"
"github.com/faddat/bdjuno/x/utils"
)
// UpdateValidatorsRedelegations updates the redelegations for all the validators provided
func UpdateValidatorsRedelegations(
height int64, bondDenom string, validators []stakingtypes.Validator,
client stakingtypes.QueryClient, db *database.BigDipperDb,
) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating validators redelegations")
var wg sync.WaitGroup
for _, val := range validators {
wg.Add(1)
go getRedelegations(val.OperatorAddress, bondDenom, height, client, db, &wg)
}
wg.Wait()
}
func getRedelegations(
validatorAddress string, bondDenom string, height int64,
client stakingtypes.QueryClient, db *database.BigDipperDb, wg *sync.WaitGroup,
) {
defer wg.Done()
header := utils.GetHeightRequestHeader(height)
var nextKey []byte
var stop = false
for !stop {
res, err := client.Redelegations(
context.Background(),
&stakingtypes.QueryRedelegationsRequest{
SrcValidatorAddr: validatorAddress,
Pagination: &query.PageRequest{
Key: nextKey,
Limit: 100, // Query 100 unbonding delegations at a time
},
},
header,
)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while getting validators redelegations")
return
}
var delegations []types.Redelegation
for _, delegation := range res.RedelegationResponses {
for _, entry := range delegation.Entries {
delegations = append(delegations, types.NewRedelegation(
delegation.Redelegation.DelegatorAddress,
delegation.Redelegation.ValidatorSrcAddress,
delegation.Redelegation.ValidatorDstAddress,
sdk.NewCoin(bondDenom, entry.Balance),
entry.RedelegationEntry.CompletionTime,
entry.RedelegationEntry.CreationHeight,
))
}
}
err = db.SaveRedelegations(delegations)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while saving validators redelegations")
return
}
nextKey = res.Pagination.NextKey
stop = len(res.Pagination.NextKey) == 0
}
}