-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
119 lines (96 loc) · 2.65 KB
/
api.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
package cosmos
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
"github.com/cosmos/cosmos-sdk/client/rpc"
)
const (
BaseAPIUrlFormat = "https://cosmos.directory/%s"
)
type ChainInfo struct {
ChainID string `json:"chain_id"`
RPCNode string
}
func GetChainInfo(chain, rpcNode string) (ChainInfo, error) {
var chainInfo ChainInfo
url := fmt.Sprintf(BaseAPIUrlFormat, chain)
resp, err := http.Get(url)
if err != nil {
return chainInfo, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return chainInfo, fmt.Errorf("error fetching chain info: status code %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return chainInfo, err
}
err = json.Unmarshal(body, &chainInfo)
if err != nil {
return chainInfo, err
}
chainInfo.RPCNode = rpcNode
return chainInfo, nil
}
func GetOpenProposals(rpcNode string) ([]types.Proposal, error) {
client, err := rpc.New(rpcNode, "/websocket")
if err != nil {
return nil, err
}
res, _, err := client.QueryWithData(context.Background(), "/custom/gov/proposals", query.NewPageRequest())
if err != nil {
return nil, err
}
var proposalsResponse types.QueryProposalsResponse
if err := client.Codec.UnmarshalJSON(res, &proposalsResponse); err != nil {
return nil, err
}
return proposalsResponse.GetProposals(), nil
}
func HasVoted(rpcNode, walletName string, proposalID uint64) (bool, error) {
client, err := rpc.New(rpcNode, "/websocket")
if err != nil {
return false, err
}
params := types.NewQueryVoteRequest(walletName, proposalID)
req, err := client.Codec.MarshalJSON(params)
if err != nil {
return false, err
}
_, _, err = client.QueryWithData(context.Background(), "/custom/gov/vote", req)
if err != nil {
if err.Error() == "rpc error: code = NotFound desc = vote not found" {
return false, nil
}
return false, err
}
return true, nil
}
func SubmitVote(rpcNode, walletName, password string, proposalID uint64, voteOption, gasPrices string) (string, error) {
client, err := rpc.New(rpcNode, "/websocket")
if err != nil {
return "", err
}
voteOptionParsed, err := types.VoteOptionFromString(voteOption)
if err != nil {
return "", err
}
msg := types.NewMsgVote(walletName, proposalID, voteOptionParsed)
tx, err := cli.BuildUnsignedTx(msg, walletName, password, gasPrices)
if err != nil {
return "", err
}
res, err := client.BroadcastTxCommit(context.Background(), tx)
if err != nil {
return "", err
}
return res.Hash.String(), nil
}