-
Notifications
You must be signed in to change notification settings - Fork 0
/
planetmint_client.go
78 lines (63 loc) · 2.06 KB
/
planetmint_client.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
package service
import (
"context"
"github.com/cosmos/cosmos-sdk/codec"
stakingTypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type IPlanetmintClient interface {
GetValidatorAddresses() (addresses []string, err error)
GetValidatorDelegationAddresses(validatorAddress string) (addresses []string, err error)
}
type PlanetmintClient struct {
host string
}
func NewPlanetmintClient(host string) *PlanetmintClient {
return &PlanetmintClient{host: host}
}
func (pmc *PlanetmintClient) GetValidatorAddresses() (addresses []string, err error) {
client, err := pmc.getStakingQueryClient()
if err != nil {
return nil, err
}
validatorsRes, err := client.Validators(context.Background(), &stakingTypes.QueryValidatorsRequest{Status: stakingTypes.BondStatusBonded})
if err != nil {
return nil, err
}
for _, validator := range validatorsRes.GetValidators() {
addresses = append(addresses, validator.OperatorAddress)
}
return
}
func (pmc *PlanetmintClient) GetValidatorDelegationAddresses(validatorAddress string) (addresses []string, err error) {
client, err := pmc.getStakingQueryClient()
if err != nil {
return nil, err
}
delegationRes, err := client.ValidatorDelegations(context.Background(), &stakingTypes.QueryValidatorDelegationsRequest{
ValidatorAddr: validatorAddress,
})
if err != nil {
return nil, err
}
for _, delegation := range delegationRes.GetDelegationResponses() {
addresses = append(addresses, delegation.Delegation.DelegatorAddress)
}
return
}
func (pmc *PlanetmintClient) getStakingQueryClient() (stakingClient stakingTypes.QueryClient, err error) {
grpcConn, err := pmc.getGRPCConnection()
if err != nil {
return nil, err
}
stakingClient = stakingTypes.NewQueryClient(grpcConn)
return
}
func (pmc *PlanetmintClient) getGRPCConnection() (grpcConn *grpc.ClientConn, err error) {
return grpc.Dial(
pmc.host,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.ForceCodec(codec.NewProtoCodec(nil).GRPCCodec())),
)
}