This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
event.go
178 lines (162 loc) · 3.69 KB
/
event.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package tzstats
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/tezos"
)
type Event struct {
// table API only
RowId uint64 `json:"row_id"`
AccountId uint64 `json:"account_id"`
Height int64 `json:"height"`
OpId uint64 `json:"op_id"`
// table and explorer API
Contract tezos.Address `json:"contract"`
Type micheline.Prim `json:"type"`
Payload micheline.Prim `json:"payload"`
Tag string `json:"tag"`
TypeHash string `json:"type_hash"`
columns []string `json:"-"`
}
type EventList struct {
Rows []*Event
columns []string
}
func (l EventList) Len() int {
return len(l.Rows)
}
func (l EventList) Cursor() uint64 {
if len(l.Rows) == 0 {
return 0
}
return l.Rows[len(l.Rows)-1].RowId
}
func (l *EventList) UnmarshalJSON(data []byte) error {
if len(data) == 0 || bytes.Equal(data, null) {
return nil
}
if data[0] != '[' {
return fmt.Errorf("EventList: expected JSON array")
}
array := make([]json.RawMessage, 0)
if err := json.Unmarshal(data, &array); err != nil {
return err
}
for _, v := range array {
r := &Event{
columns: l.columns,
}
if err := r.UnmarshalJSON(v); err != nil {
return err
}
r.columns = nil
l.Rows = append(l.Rows, r)
}
return nil
}
func (a *Event) UnmarshalJSON(data []byte) error {
if len(data) == 0 || bytes.Equal(data, null) {
return nil
}
if len(data) == 2 {
return nil
}
if data[0] == '[' {
return a.UnmarshalJSONBrief(data)
}
type Alias *Event
return json.Unmarshal(data, Alias(a))
}
func (e *Event) UnmarshalJSONBrief(data []byte) error {
ev := Event{}
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
unpacked := make([]interface{}, 0)
err := dec.Decode(&unpacked)
if err != nil {
return err
}
for i, v := range e.columns {
f := unpacked[i]
if f == nil {
continue
}
switch v {
case "row_id":
ev.RowId, err = strconv.ParseUint(f.(json.Number).String(), 10, 64)
case "account_id":
ev.AccountId, err = strconv.ParseUint(f.(json.Number).String(), 10, 64)
case "height":
ev.Height, err = strconv.ParseInt(f.(json.Number).String(), 10, 64)
case "op_id":
ev.OpId, err = strconv.ParseUint(f.(json.Number).String(), 10, 64)
case "contract":
ev.Contract, err = tezos.ParseAddress(f.(string))
case "type":
var buf []byte
if buf, err = hex.DecodeString(f.(string)); err == nil && len(buf) > 0 {
err = ev.Type.UnmarshalBinary(buf)
}
case "payload":
var buf []byte
if buf, err = hex.DecodeString(f.(string)); err == nil && len(buf) > 0 {
err = ev.Payload.UnmarshalBinary(buf)
}
case "tag":
ev.Tag = f.(string)
case "type_hash":
ev.TypeHash = f.(string)
}
if err != nil {
return err
}
}
*e = ev
return nil
}
type EventQuery struct {
tableQuery
}
func (c *Client) NewEventQuery() EventQuery {
tinfo, err := GetTypeInfo(&Event{})
if err != nil {
panic(err)
}
q := tableQuery{
client: c,
Params: c.params.Copy(),
Table: "event",
Format: FormatJSON,
Limit: DefaultLimit,
Order: OrderAsc,
Columns: tinfo.Aliases(),
Filter: make(FilterList, 0),
}
return EventQuery{q}
}
func (q EventQuery) Run(ctx context.Context) (*EventList, error) {
result := &EventList{
columns: q.Columns,
}
if err := q.client.QueryTable(ctx, &q.tableQuery, result); err != nil {
return nil, err
}
return result, nil
}
func (c *Client) QueryEvents(ctx context.Context, filter FilterList, cols []string) (*EventList, error) {
q := c.NewEventQuery()
if len(cols) > 0 {
q.Columns = cols
}
if len(filter) > 0 {
q.Filter = filter
}
return q.Run(ctx)
}