-
Notifications
You must be signed in to change notification settings - Fork 25
/
asset.go
186 lines (169 loc) · 4.91 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
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
package bot
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/gofrs/uuid/v5"
)
const (
BTC = "c6d0c728-2624-429b-8e0d-d9d19b6592fa"
ETH = "43d61dcd-e413-450d-80b8-101d5e903357"
USDT_ERC20 = "4d8c508b-91c5-375b-92b0-ee702ed2dac5"
USDC_ERC20 = "9b180ab6-6abe-3dc0-a13f-04169eb34bfa"
USDT_TRC20 = "b91e18ff-a9ae-3dc7-8679-e935d9a4b34b"
)
type Asset struct {
Type string `json:"type"`
AssetID string `json:"asset_id"`
ChainID string `json:"chain_id"`
AssetKey string `json:"asset_key"`
Precision int `json:"precision"`
KernelAssetId string `json:"kernel_asset_id"`
Symbol string `json:"symbol"`
Name string `json:"name"`
IconURL string `json:"icon_url"`
Amount string `json:"amount"`
PriceBTC string `json:"price_btc"`
PriceUSD string `json:"price_usd"`
ChangeBTC string `json:"change_btc"`
ChangeUSD string `json:"change_usd"`
Confirmations int64 `json:"confirmations"`
Fee string `json:"fee"`
Reserve string `json:"reserve"`
SnapshotsCount int64 `json:"snapshots_count"`
Capitalization float64 `json:"capitalization"`
Liquidity string `json:"liquidity"`
CollectionHash string `json:"collection_hash,omitempty"`
}
type AssetTicker struct {
Type string `json:"type"`
PriceBTC string `json:"price_btc"`
PriceUSD string `json:"price_usd"`
}
type AssetFee struct {
Type string `json:"type"`
AssetID string `json:"asset_id"`
Amount string `json:"amount"`
}
func ReadAsset(ctx context.Context, name string) (*Asset, error) {
body, err := Request(ctx, "GET", "/network/assets/"+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
}
func ReadAssetTicker(ctx context.Context, assetId string) (*AssetTicker, error) {
body, err := Request(ctx, "GET", "/network/ticker?asset="+assetId, nil, "")
if err != nil {
return nil, err
}
var resp struct {
Data *AssetTicker `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
}
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
}
func AssetBalance(ctx context.Context, assetId, uid, sid, sessionKey string) (common.Integer, error) {
su := &SafeUser{
UserId: uid,
SessionId: sid,
SessionPrivateKey: sessionKey,
}
if id, _ := uuid.FromString(assetId); assetId == id.String() {
assetId = crypto.Sha256Hash([]byte(assetId)).String()
}
return AssetBalanceWithSafeUser(ctx, assetId, su)
}
func AssetBalanceWithSafeUser(ctx context.Context, kernelAssetId string, su *SafeUser) (common.Integer, error) {
membersHash := HashMembers([]string{su.UserId})
outputs, err := ListUnspentOutputs(ctx, membersHash, 1, kernelAssetId, su)
if err != nil {
return common.Zero, err
}
var total common.Integer
for _, o := range outputs {
amt := common.NewIntegerFromString(o.Amount)
total = total.Add(amt)
}
return total, nil
}
func UserAssetBalance(ctx context.Context, userID, assetID, accessToken string) (common.Integer, error) {
if id, _ := uuid.FromString(assetID); assetID == id.String() {
assetID = crypto.Sha256Hash([]byte(assetID)).String()
}
membersHash := HashMembers([]string{userID})
outputs, err := ListUnspentOutputsByToken(ctx, membersHash, 1, assetID, accessToken)
if err != nil {
return common.Zero, err
}
var total common.Integer
for _, o := range outputs {
amt := common.NewIntegerFromString(o.Amount)
total = total.Add(amt)
}
return total, nil
}
func ReadAssetFee(ctx context.Context, assetId, destination string, su *SafeUser) ([]*AssetFee, error) {
params := url.Values{}
params.Set("destination", destination)
method, path := "GET", fmt.Sprintf("/safe/assets/%s/fees?%s", assetId, params.Encode())
token, err := SignAuthenticationToken(method, path, "", su)
if err != nil {
return nil, err
}
body, err := Request(ctx, method, path, nil, token)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp struct {
Data []*AssetFee `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp.Data, nil
}