-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_assets.go
353 lines (301 loc) · 8.57 KB
/
api_assets.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package blockfrost
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"sync"
)
const (
resourceAssets = "assets"
resourceAssetHistory = "history"
resourceAssetTransactions = "transactions"
resourceAssetAddresses = "addresses"
resourcePolicyAssets = "assets/policy"
)
// Contains metadata information about an asset.
type AssetMetadata struct {
Name string `json:"name"`
Description string `json:"description"`
Ticker string `json:"ticker"`
URL string `json:"url"`
Logo string `json:"logo"`
Decimals int `json:"decimals"`
}
// Assets contains information on an asset.
type Asset struct {
// Hex-encoded asset full name
Asset string `json:"asset"`
// Policy ID of the asset
PolicyId string `json:"policy_id"`
// Hex-encoded asset name of the asset
AssetName string `json:"asset_name"`
// CIP14 based user-facing fingerprint
Fingerprint string `json:"fingerprint"`
// Current asset quantity
Quantity string `json:"quantity"`
// ID of the initial minting transaction
InitialMintTxHash string `json:"initial_mint_tx_hash"`
// Count of mint and burn transactions
MintOrBurnCount int `json:"mint_or_burn_count"`
// On-chain metadata which SHOULD adhere to the valid standards, based on which we perform the look up and display the asset (best effort)
OnchainMetadata *interface{} `json:"onchain_metadata"`
// Enum: "CIP25v1" "CIP25v2" "CIP68v1"
// If on-chain metadata passes validation, we display the standard under which it is valid
OnchainMetadataStandard *string `json:"onchain_metadata_standard"`
// Arbitrary plutus data (CIP68).
OnchainMetadataExtra *string `json:"onchain_metadata_extra"`
// Off-chain metadata fetched from GitHub based on network.
Metadata *AssetMetadata `json:"metadata"`
}
// Assets minted under a specific policy.
type AssetByPolicy struct {
// Hex-encoded asset full name
Asset string `json:"asset"`
// Current asset quantity
Quantity string `json:"quantity"`
}
// AssetHistory contains history of an asset.
type AssetHistory struct {
// Hash of the transaction containing the asset actio
TxHash string `json:"tx_hash"`
// Action executed upon the asset policy.
// Enum: "minted" "burned"
Action string `json:"action"`
// Asset amount of the specific action
Amount string `json:"amount"`
}
// AssetTransaction contains information on transactions belonging
// to an asset.
type AssetTransaction struct {
// Hash of the transaction
TxHash string `json:"tx_hash"`
// Transaction index within the block
TxIndex int `json:"tx_index"`
// Block height
BlockHeight int `json:"block_height"`
// Block time
BlockTime int `json:"block_time"`
}
type AssetAddress struct {
// Address containing the specific asset
Address string `json:"address"`
// Asset quantity on the specific address
Quantity string `json:"quantity"`
}
type AssetResult struct {
Res []Asset
Err error
}
type AssetByPolicyResult struct {
Res []AssetByPolicy
Err error
}
type AssetAddressesAll struct {
Res []AssetAddress
Err error
}
// Assets returns a paginated list of assets.
func (c *apiClient) Assets(ctx context.Context, query APIQueryParams) (a []AssetByPolicy, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceAssets))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
v := req.URL.Query()
v = formatParams(v, query)
req.URL.RawQuery = v.Encode()
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&a); err != nil {
return
}
return a, nil
}
// AssetsAll returns all assets.
func (c *apiClient) AssetsAll(ctx context.Context) <-chan AssetByPolicyResult {
ch := make(chan AssetByPolicyResult, c.routines)
jobs := make(chan methodOptions, c.routines)
quit := make(chan bool, 1)
wg := sync.WaitGroup{}
for i := 0; i < c.routines; i++ {
wg.Add(1)
go func(jobs chan methodOptions, ch chan AssetByPolicyResult, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
assets, err := c.Assets(j.ctx, j.query)
if len(assets) != j.query.Count || err != nil {
select {
case quit <- true:
default:
}
}
res := AssetByPolicyResult{Res: assets, Err: err}
ch <- res
}
}(jobs, ch, &wg)
}
go func() {
defer close(ch)
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
}
close(jobs)
wg.Wait()
}()
return ch
}
// Asset returns information about a specific asset.
func (c *apiClient) Asset(ctx context.Context, asset string) (a Asset, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s", c.server, resourceAssets, asset))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&a); err != nil {
return
}
return a, nil
}
// AssetHistory returns history of a specific asset.
func (c *apiClient) AssetHistory(ctx context.Context, asset string) (hist []AssetHistory, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceAssets, asset, resourceAssetHistory))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&hist); err != nil {
return
}
return hist, nil
}
// AssetTransactions returns list of a specific asset transactions.
func (c *apiClient) AssetTransactions(ctx context.Context, asset string) (trs []AssetTransaction, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceAssets, asset, resourceAssetTransactions))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&trs); err != nil {
return
}
return trs, nil
}
func (c *apiClient) AssetAddresses(ctx context.Context, asset string, query APIQueryParams) (addrs []AssetAddress, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceAssets, asset, resourceAddresses))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
v := req.URL.Query()
v = formatParams(v, query)
req.URL.RawQuery = v.Encode()
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&addrs); err != nil {
return
}
return addrs, nil
}
// AssetAddresses returns list of a addresses containing a specific asset.
func (c *apiClient) AssetAddressesAll(ctx context.Context, asset string) <-chan AssetAddressesAll {
ch := make(chan AssetAddressesAll, c.routines)
jobs := make(chan methodOptions, c.routines)
quit := make(chan bool, 1)
wg := sync.WaitGroup{}
for i := 0; i < c.routines; i++ {
wg.Add(1)
go func(jobs chan methodOptions, ch chan AssetAddressesAll, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
ad, err := c.AssetAddresses(j.ctx, asset, j.query)
if len(ad) != j.query.Count || err != nil {
select {
case quit <- true:
default:
}
}
res := AssetAddressesAll{Res: ad, Err: err}
ch <- res
}
}(jobs, ch, &wg)
}
go func() {
defer close(ch)
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
}
close(jobs)
wg.Wait()
}()
return ch
}
// AssetsByPolicy returns list of assets minted under a specific policy.
func (c *apiClient) AssetsByPolicy(ctx context.Context, policyId string) (a []AssetByPolicy, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s", c.server, resourcePolicyAssets, policyId))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&a); err != nil {
return
}
return a, nil
}