-
Notifications
You must be signed in to change notification settings - Fork 1
/
proposals.go
72 lines (61 loc) · 1.79 KB
/
proposals.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// Define struct to match JSON structure
type ProposalsData struct {
Proposals []Proposal `json:"proposals"`
Pagination Pagination `json:"pagination"`
}
type Proposal struct {
ID string `json:"id"`
Messages interface{} `json:"messages"`
Status string `json:"status"`
FinalTallyResult interface{} `json:"final_tally_result"`
SubmitTime string `json:"submit_time"`
DepositEndTime string `json:"deposit_end_time"`
TotalDeposit interface{} `json:"total_deposit"`
VotingStartTime string `json:"voting_start_time"`
VotingEndTime string `json:"voting_end_time"`
Metadata string `json:"metadata"`
Title string `json:"title"`
Summary string `json:"summary"`
Proposer string `json:"proposer"`
}
type Pagination struct {
NextKey *json.RawMessage `json:"next_key"`
Total string `json:"total"`
}
func fetchProposalsData(cosmosEndpoint string) (*ProposalsData, error) {
url := cosmosEndpoint
url += "/cosmos/gov/v1/proposals"
url += "?pagination.limit=1&pagination.count_total=true&pagination.reverse=true"
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var proposals ProposalsData
err = json.Unmarshal(body, &proposals)
if err != nil {
return nil, err
}
return &proposals, nil
}
func getLatestProposal(cosmosEndpoint string) (*Proposal, error) {
proposalsData, err := fetchProposalsData(cosmosEndpoint)
if err != nil {
return nil, err
}
if len(proposalsData.Proposals) == 0 {
return nil, fmt.Errorf("no proposals found")
}
return &proposalsData.Proposals[0], nil
}