-
Notifications
You must be signed in to change notification settings - Fork 8
/
events.go
103 lines (85 loc) 路 2.25 KB
/
events.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
package api
import (
"encoding/json"
"errors"
"strconv"
)
type EventsResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID string `json:"id,omitempty"`
Result *EventsResult `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
type EventsResult struct {
Events []Event `json:"events"`
}
type Event struct {
Type string `json:"type"`
Value map[string]string `json:"value"`
}
// Converting event map data to the structure interface regarding event type
func (e *Event) ValueStruct() (interface{}, error) {
bytes, err := json.Marshal(e.Value)
if err != nil {
return nil, err
}
var value interface{}
switch e.Type {
case "minter/RewardEvent":
value = &RewardEventValue{}
case "minter/SlashEvent":
value = &SlashEventValue{}
case "minter/UnbondEvent":
value = &UnbondEventValue{}
default:
return nil, errors.New("unknown event type")
}
err = json.Unmarshal(bytes, value)
if err != nil {
return nil, err
}
return value, nil
}
type RewardEventValue struct {
Role string `json:"role"`
Address string `json:"address"`
Amount string `json:"amount"`
ValidatorPubKey string `json:"validator_pub_key"`
}
type SlashEventValue struct {
Address string `json:"address"`
Amount string `json:"amount"`
Coin string `json:"coin"`
ValidatorPubKey string `json:"validator_pub_key"`
}
type UnbondEventValue struct {
Address string `json:"address"`
Amount string `json:"amount"`
Coin string `json:"coin"`
ValidatorPubKey string `json:"validator_pub_key"`
}
// Returns events at given height.
func (a *Api) Events() (*EventsResult, error) {
return a.EventsAtHeight(LatestBlockHeight)
}
// Returns events at given height.
func (a *Api) EventsAtHeight(height int) (*EventsResult, error) {
params := make(map[string]string)
params["height"] = strconv.Itoa(height)
res, err := a.client.R().SetQueryParams(params).Get("/events")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, NewResponseError(res)
}
response := new(EventsResponse)
err = json.Unmarshal(res.Body(), response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}