-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigmap.go
176 lines (143 loc) · 3.75 KB
/
bigmap.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
package tzkt
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
type BigmapUpdate struct {
ID uint64 `json:"id"`
Level uint64 `json:"level"`
Timestamp time.Time `json:"timestamp"`
Bigmap uint64 `json:"bigmap"`
Contract Account `json:"contract"`
Path string `json:"path"`
Action string `json:"action"`
Content Content `json:"content"`
}
type Content struct {
Hash string `json:"hash"`
Key string `json:"key"`
Value BigmapValue `json:"value"`
}
type BigmapValue struct {
TokenID string `json:"token_id"`
}
// GetBigMapValueByPointer returns the value of a key in a bigmap.
func (c *TZKT) GetBigMapValueByPointer(pointer int, key string) ([]byte, error) {
u := url.URL{
Scheme: "https",
Host: c.endpoint,
Path: fmt.Sprintf("/v1/bigmaps/%d/keys", pointer),
RawQuery: url.Values{
"select": []string{"value"},
"key": []string{key},
}.Encode(),
}
var results []json.RawMessage
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
if err := c.request(req, &results); err != nil {
return nil, err
}
if len(results) == 0 {
return nil, fmt.Errorf("error key not found")
}
return results[0], nil
}
// GetBigMapPointersByContract returns a list of big map pointer for a contract.
// This call accepts tags and an option.
func (c *TZKT) GetBigMapPointersByContract(contract string, tags ...string) ([]int, error) {
query := url.Values{
"contract": []string{contract},
"select": []string{"ptr"},
}
if len(tags) > 0 {
query["tags.any"] = []string{strings.Join(tags, ",")}
}
u := url.URL{
Scheme: "https",
Host: c.endpoint,
Path: "/v1/bigmaps",
RawQuery: query.Encode(),
}
var pointer []int
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
if err := c.request(req, &pointer); err != nil {
return nil, err
}
return pointer, nil
}
// GetBigMapsByContractAndPath get BitMap of contract
func (c *TZKT) GetBigMapsByContractAndPath(contract string, path string) (int, error) {
u := url.URL{
Scheme: "https",
Host: c.endpoint,
Path: "/v1/bigmaps",
RawQuery: url.Values{
"contract": []string{contract},
"select": []string{"ptr"},
"path": []string{path},
}.Encode(),
}
var pointer []int
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return 0, err
}
if err := c.request(req, &pointer); err != nil {
return 0, err
}
if len(pointer) == 0 {
return 0, fmt.Errorf("no pointer")
}
return pointer[0], nil
}
// GetBigMapPointerForContractTokenMetadata returns the bigmap pointer of token_metadata
// for a specific contract
func (c *TZKT) GetBigMapPointerForContractTokenMetadata(contract string) (int, error) {
pointers, err := c.GetBigMapPointersByContract(contract, "token_metadata")
if err != nil {
return 0, err
}
if len(pointers) == 0 {
return 0, fmt.Errorf("no pointer")
}
return pointers[0], nil
}
// GetBigmapUpdatesByLevel returns the bigmap updates of given tags
func (c *TZKT) GetTokenMetadataBigmapUpdatesByLevel(level string, offset, limit int) ([]BigmapUpdate, error) {
if limit == 0 {
limit = 100
}
v := url.Values{
"level.eq": []string{level},
"sort": []string{"level"},
"offset": []string{fmt.Sprint(offset)},
"limit": []string{fmt.Sprint(limit)},
"tags.any": []string{"token_metadata"},
"action.ni": []string{"add_key", "allocate"},
}
u := url.URL{
Scheme: "https",
Host: c.endpoint,
Path: "/v1/bigmaps/updates",
RawQuery: v.Encode(),
}
var bigmaps []BigmapUpdate
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
if err := c.request(req, &bigmaps); err != nil {
return nil, err
}
return bigmaps, nil
}