-
Notifications
You must be signed in to change notification settings - Fork 8
/
candidates.go
50 lines (41 loc) 路 1.1 KB
/
candidates.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
package api
import (
"encoding/json"
"strconv"
)
type CandidatesResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID string `json:"id,omitempty"`
Result []*CandidateResult `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
// Returns list of candidates.
func (a *Api) Candidates(includeStakes bool) ([]*CandidateResult, error) {
return a.CandidatesAtHeight(LatestBlockHeight, includeStakes)
}
// Returns list of candidates.
func (a *Api) CandidatesAtHeight(height int, includeStakes bool) ([]*CandidateResult, error) {
params := make(map[string]string)
if includeStakes {
params["include_stakes"] = "true"
}
if height > 0 {
params["height"] = strconv.Itoa(height)
}
res, err := a.client.R().SetQueryParams(params).Get("/candidates")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, NewResponseError(res)
}
response := new(CandidatesResponse)
err = json.Unmarshal(res.Body(), response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}