-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgrpc_wrapper.go
More file actions
125 lines (104 loc) · 3.36 KB
/
grpc_wrapper.go
File metadata and controls
125 lines (104 loc) · 3.36 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
package main
import (
"context"
"math"
"strconv"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
cosmostypes "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
type GrpcWrapper struct {
nodeAddress string
grpcConn *grpc.ClientConn
}
func InitGrpcWrapper(nodeAddress string) *GrpcWrapper {
grpcConn, err := grpc.Dial(
NodeAddress,
grpc.WithInsecure(),
)
if err != nil {
log.Fatal().Err(err).Msg("Cannot connect to gRPC node")
}
return &GrpcWrapper{
nodeAddress: nodeAddress,
grpcConn: grpcConn,
}
}
func (w *GrpcWrapper) CloseConnection() {
w.grpcConn.Close()
}
func (w *GrpcWrapper) getValidator(address string) (stakingtypes.Validator, error) {
stakingClient := stakingtypes.NewQueryClient(w.grpcConn)
validatorResponse, err := stakingClient.Validator(
context.Background(),
&stakingtypes.QueryValidatorRequest{ValidatorAddr: address},
)
return validatorResponse.Validator, err
}
func (w *GrpcWrapper) getValidatorCommissionAtBlock(address string, block int64) (cosmostypes.DecCoins, error) {
distributionClient := distributiontypes.NewQueryClient(w.grpcConn)
response, err := distributionClient.ValidatorCommission(
metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, strconv.FormatInt(block, 10)),
&distributiontypes.QueryValidatorCommissionRequest{ValidatorAddress: address},
)
if err != nil {
return nil, err
}
return response.Commission.Commission, nil
}
func (w *GrpcWrapper) getDelegatorRewardsAtBlock(validator string, delegator string, block int64) (cosmostypes.DecCoins, error) {
distributionClient := distributiontypes.NewQueryClient(w.grpcConn)
response, err := distributionClient.DelegationRewards(
metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, strconv.FormatInt(block, 10)),
&distributiontypes.QueryDelegationRewardsRequest{
ValidatorAddress: validator,
DelegatorAddress: delegator,
},
)
if err != nil {
return nil, err
}
return response.Rewards, nil
}
func (w *GrpcWrapper) setDenom() {
// if --denom and --denom-coefficient are both provided, use them
// instead of fetching them via gRPC. Can be useful for networks like osmosis.
if Denom != "" && DenomCoefficient != 0 {
log.Info().
Str("denom", Denom).
Float64("coefficient", DenomCoefficient).
Msg("Using provided denom and coefficient.")
return
}
bankClient := banktypes.NewQueryClient(w.grpcConn)
denoms, err := bankClient.DenomsMetadata(
context.Background(),
&banktypes.QueryDenomsMetadataRequest{},
)
if err != nil {
log.Fatal().Err(err).Msg("Error querying denom")
}
metadata := denoms.Metadatas[0] // always using the first one
if Denom == "" { // using display currency
Denom = metadata.Display
}
for _, unit := range metadata.DenomUnits {
log.Debug().
Str("denom", unit.Denom).
Uint32("exponent", unit.Exponent).
Msg("Denom info")
if unit.Denom == Denom {
DenomCoefficient = math.Pow10(int(unit.Exponent))
log.Info().
Str("denom", Denom).
Float64("coefficient", DenomCoefficient).
Msg("Got denom info")
return
}
}
log.Fatal().Msg("Could not find the denom info")
}