-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlcd.go
63 lines (50 loc) · 1.52 KB
/
lcd.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
package sacco
import (
"encoding/json"
"fmt"
"net/http"
)
// Retrieve the account data related to the given wallet address, like
// account number and sequence number.
func getAccountData(lcdEndpoint, address string) (AccountData, error) {
endpoint := fmt.Sprintf("%s/auth/accounts/%s", lcdEndpoint, address)
resp, err := http.Get(endpoint)
if err != nil {
return AccountData{}, err
}
jdec := json.NewDecoder(resp.Body)
if resp.StatusCode != http.StatusOK {
// we had an error, deserialize it and return
var jsonError Error
err := jdec.Decode(&jsonError)
if err != nil {
return AccountData{}, fmt.Errorf("error deserializing account data JSON error: %w", err)
}
return AccountData{}, fmt.Errorf("error during get account data: %s", jsonError.Error)
}
var accountData AccountData
errCdc := jdec.Decode(&accountData)
if errCdc != nil {
return AccountData{}, fmt.Errorf("could not unmarshal node response: %w", errCdc)
}
if accountData.Result.Value.Address == "" {
return AccountData{}, fmt.Errorf("account with address %s is not online", address)
}
return accountData, nil
}
// Return useful information of the full node, like the Network
// (chain) name.
func getNodeInfo(lcdEndpoint string) (NodeInfo, error) {
endpoint := fmt.Sprintf("%s/node_info", lcdEndpoint)
resp, err := http.Get(endpoint)
if err != nil {
return NodeInfo{}, err
}
var nodeInfo NodeInfo
jdec := json.NewDecoder(resp.Body)
err = jdec.Decode(&nodeInfo)
if err != nil {
return NodeInfo{}, err
}
return nodeInfo, nil
}