-
Notifications
You must be signed in to change notification settings - Fork 14
/
account.go
98 lines (82 loc) · 1.99 KB
/
account.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
package findaccount
import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/bech32"
"log"
"sort"
"sync"
)
var accountsMux sync.Mutex
type Result struct {
Chain string `json:"chain"`
Address string `json:"address"`
Validator string `json:"is_validator"`
HasBalance bool `json:"hasBalance"`
Coins string `json:"coins"`
Error string `json:"error"`
Link string `json:"link"`
}
func (r Result) CsvHeader() string {
return "chain,address,validator,has balance,coins,error"
}
func (r Result) ToCsv() string {
return fmt.Sprintf("%s,%s,%q,%v,%s,%s", r.Chain, r.Address, r.Validator, r.HasBalance, r.Coins, r.Error)
}
// SearchAccounts is the entrypoint for performing a search
func SearchAccounts(account string) ([]Result, error) {
results := make([]Result, 0)
addrMap, err := ConvertToAccounts(account)
if err != nil {
return results, err
}
wg := &sync.WaitGroup{}
wg.Add(len(infos))
for k, v := range infos {
var link string
accountsMux.Lock()
chain, rpcs := k, v
addr := addrMap[k]
if len(infos[k].Explorers) > 0 {
link = infos[k].Explorers[0].Url
}
accountsMux.Unlock()
go func() {
bal, coins, e := QueryAccount(rpcs, chain, addr)
errStr := "ok"
if e != nil {
errStr = e.Error()
}
val, _ := IsValidator(rpcs, chain, addr)
results = append(results, Result{
Chain: chain,
Address: addr,
Validator: val,
HasBalance: bal,
Coins: coins,
Error: errStr,
Link: link,
})
wg.Done()
}()
}
wg.Wait()
sort.Slice(results, func(i, j int) bool {
return sort.StringsAreSorted([]string{results[i].Chain, results[j].Chain})
})
return results, err
}
func ConvertToAccounts(s string) (map[string]string, error) {
accounts := make(map[string]string)
_, b64, err := bech32.DecodeAndConvert(s)
if err != nil {
return nil, err
}
for k, v := range Prefixes {
addr, e := bech32.ConvertAndEncode(v, b64)
if e != nil {
log.Println(k, e)
}
accounts[k] = addr
}
return accounts, nil
}