-
Notifications
You must be signed in to change notification settings - Fork 14
/
client.go
140 lines (126 loc) · 3.21 KB
/
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
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
package findaccount
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/types/bech32"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
staketypes "github.com/cosmos/cosmos-sdk/x/staking/types"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"regexp"
"strings"
)
type ChainInfo struct {
Apis struct {
Rpc []Rpc `json:"rpc"`
} `json:"apis"`
Explorers []Explorer `json:"explorers"`
}
type Rpc struct {
Address string `json:"address"`
}
type Explorer struct {
Url string `json:"url"`
}
var portRex = regexp.MustCompile(`.*:\d+$`)
var protoRex = regexp.MustCompile(`^\w+://`)
func getClient(info *ChainInfo, chain string) (*rpchttp.HTTP, error) {
client := &rpchttp.HTTP{}
var err error
ok := false
for i := range info.Apis.Rpc {
endpoint := info.Apis.Rpc[len(info.Apis.Rpc)-1-i]
endpoint.Address = strings.TrimRight(endpoint.Address, "/")
var unknown bool
if !portRex.MatchString(endpoint.Address) {
switch protoRex.FindString(endpoint.Address) {
case "https://":
endpoint.Address = endpoint.Address + ":443"
case "http://":
endpoint.Address = endpoint.Address + ":80"
case "tcp://":
endpoint.Address = endpoint.Address + ":26657"
default:
unknown = true
}
}
if unknown {
continue
}
client, err = rpchttp.NewWithTimeout(endpoint.Address, "/websocket", 10)
if err != nil {
continue
}
status, e := client.Status(context.Background())
if e != nil || status.SyncInfo.CatchingUp {
continue
}
ok = true
break
}
if !ok {
err = fmt.Errorf("could not connect to any endpoints for %s", chain)
}
return client, err
}
func IsValidator(info *ChainInfo, chain, account string) (validator string, err error) {
client, err := getClient(info, chain)
if err != nil {
return
}
// Check if the account is also a validator
_, b64, err := bech32.DecodeAndConvert(account)
if err != nil {
return
}
accountsMux.Lock()
prefix := Prefixes[chain]
accountsMux.Unlock()
addr, _ := bech32.ConvertAndEncode(prefix+"valoper", b64)
valQ := staketypes.QueryValidatorRequest{ValidatorAddr: addr}
valQuery, err := valQ.Marshal()
if err != nil {
return
}
valResult, err := client.ABCIQuery(context.Background(), "/cosmos.staking.v1beta1.Query/Validator", valQuery)
if err != nil {
return
}
if len(valResult.Response.Value) > 0 {
valResp := staketypes.QueryValidatorResponse{}
err = valResp.Unmarshal(valResult.Response.Value)
if err != nil {
return
}
validator = valResp.Validator.GetMoniker()
//fmt.Println(valResp)
}
return
}
func QueryAccount(info *ChainInfo, chain, account string) (hasBalance bool, balances string, err error) {
client, err := getClient(info, chain)
if err != nil {
return false, "", err
}
q := banktypes.QueryBalanceRequest{Address: account}
var query []byte
query, err = q.Marshal()
if err != nil {
return
}
result, err := client.ABCIQuery(context.Background(), "/cosmos.bank.v1beta1.Query/AllBalances", query)
if err != nil {
return
}
if len(result.Response.Value) > 0 {
balResp := banktypes.QueryBalanceResponse{}
err = balResp.Unmarshal(result.Response.Value)
if err != nil {
return
}
if balResp.Balance != nil {
balances = balResp.Balance.String()
hasBalance = true
}
}
return
}