-
Notifications
You must be signed in to change notification settings - Fork 25
/
asset.go
102 lines (95 loc) · 2.8 KB
/
asset.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
package bot
import (
"context"
"encoding/json"
)
type DepositEntry struct {
Destination string `json:"destination"`
Tag string `json:"tag"`
Properties []string `json:"properties"`
}
type Asset struct {
AssetId string `json:"asset_id"`
ChainId string `json:"chain_id"`
FeeAssetId string `json:"fee_asset_id"`
Symbol string `json:"symbol"`
Name string `json:"name"`
IconURL string `json:"icon_url"`
PriceBTC string `json:"price_btc"`
PriceUSD string `json:"price_usd"`
Balance string `json:"balance"`
Destination string `json:"destination"`
Tag string `json:"tag"`
Confirmations int `json:"confirmations"`
DepositEntries []DepositEntry `json:"deposit_entries"`
}
func AssetListWithRequestID(ctx context.Context, accessToken, requestID string) ([]*Asset, error) {
body, err := RequestWithId(ctx, "GET", "/assets", nil, accessToken, requestID)
if err != nil {
return nil, err
}
var resp struct {
Data []*Asset `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
if resp.Error.Code == 401 {
return nil, AuthorizationError(ctx)
} else if resp.Error.Code == 403 {
return nil, ForbiddenError(ctx)
}
return nil, ServerError(ctx, resp.Error)
}
return resp.Data, nil
}
func AssetList(ctx context.Context, accessToken string) ([]*Asset, error) {
return AssetListWithRequestID(ctx, accessToken, UuidNewV4().String())
}
func AssetShowWithRequestID(ctx context.Context, assetId string, accessToken, requestID string) (*Asset, error) {
body, err := RequestWithId(ctx, "GET", "/assets/"+assetId, nil, accessToken, requestID)
if err != nil {
return nil, err
}
var resp struct {
Data Asset `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error.Code > 0 {
if resp.Error.Code == 401 {
return nil, AuthorizationError(ctx)
} else if resp.Error.Code == 403 {
return nil, ForbiddenError(ctx)
}
return nil, resp.Error
}
return &resp.Data, nil
}
func AssetShow(ctx context.Context, assetId string, accessToken string) (*Asset, error) {
return AssetShowWithRequestID(ctx, assetId, accessToken, UuidNewV4().String())
}
func AssetSearch(ctx context.Context, name string) ([]*Asset, error) {
body, err := Request(ctx, "GET", "/network/assets/search/"+name, nil, "")
if err != nil {
return nil, err
}
var resp struct {
Data []*Asset `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, err
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp.Data, nil
}