-
Notifications
You must be signed in to change notification settings - Fork 25
/
snapshot_legacy.go
263 lines (241 loc) · 6.98 KB
/
snapshot_legacy.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
package bot
import (
"context"
"encoding/json"
"net/url"
"strconv"
"time"
)
type LegacySnapshot struct {
Type string `json:"type"`
SnapshotId string `json:"snapshot_id"`
AssetId string `json:"asset_id"`
Amount string `json:"amount"`
OpeningBalance string `json:"opening_balance"`
ClosingBalance string `json:"closing_balance"`
TransactionHash string `json:"transaction_hash,omitempty"`
SnapshotHash string `json:"snapshot_hash,omitempty"`
SnapshotAt time.Time `json:"snapshot_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
// deposit & withdrawal
OutputIndex int64 `json:"output_index,omitempty"` // deposit
Sender string `json:"sender,omitempty"` // deposit
OpponentId string `json:"opponent_id,omitempty"` // transfer
TraceId string `json:"trace_id,omitempty"` // transfer & raw & withdrawal
Memo string `json:"memo,omitempty"` // transfer & raw & withdrawal
OpponentKey string `json:"opponent_key"` // raw
OpponentMultisigReceivers []string `json:"opponent_receivers"` // raw
OpponentMultisigThreshold int64 `json:"opponent_threshold"` // raw
State string `json:"state"` // raw & withdrawal
// withdrawal
Receiver string `json:"receiver,omitempty"`
Confirmations int64 `json:"confirmations,omitempty"`
Fee struct {
Amount string `json:"amount"`
AssetId string `json:"asset_id"`
} `json:"fee,omitempty"`
}
type LegacySnapshotShort struct {
Type string `json:"type"`
SnapshotId string `json:"snapshot_id"`
Source string `json:"source"`
Amount string `json:"amount"`
Asset struct {
Type string `json:"type"`
AssetId string `json:"asset_id"`
ChainId string `json:"chain_id"`
MixinId string `json:"mixin_id"`
Symbol string `json:"symbol"`
Name string `json:"name"`
AssetKey string `json:"asset_key"`
IconUrl string `json:"icon_url"`
} `json:"asset"`
State string `json:"state"`
SnapshotHash string `json:"snapshot_hash"`
CreatedAt time.Time `json:"created_at"`
TraceId string `json:"trace_id"`
OpponentId string `json:"opponent_id"`
Memo string `json:"data"`
}
func Snapshots(ctx context.Context, limit int, offset, assetId, order, uid, sid, sessionKey string) ([]*LegacySnapshot, error) {
v := url.Values{}
v.Set("limit", strconv.Itoa(limit))
if offset != "" {
v.Set("offset", offset)
}
if assetId != "" {
v.Set("asset", assetId)
}
if order != "" {
v.Set("order", order)
}
path := "/snapshots?" + v.Encode()
su := &SafeUser{
UserId: uid,
SessionId: sid,
SessionPrivateKey: sessionKey,
}
token, err := SignAuthenticationToken("GET", path, "", su)
if err != nil {
return nil, err
}
return SnapshotsByToken(ctx, limit, offset, assetId, order, token)
}
func SnapshotsByToken(ctx context.Context, limit int, offset, assetId, order, accessToken string) ([]*LegacySnapshot, error) {
v := url.Values{}
v.Set("limit", strconv.Itoa(limit))
if offset != "" {
v.Set("offset", offset)
}
if assetId != "" {
v.Set("asset", assetId)
}
if order != "" {
v.Set("order", order)
}
path := "/snapshots?" + v.Encode()
body, err := Request(ctx, "GET", path, nil, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data []*LegacySnapshot `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 SnapshotById(ctx context.Context, snapshotId string, uid, sid, sessionKey string) (*LegacySnapshot, error) {
su := &SafeUser{
UserId: uid,
SessionId: sid,
SessionPrivateKey: sessionKey,
}
path := "/snapshots/" + snapshotId
token, err := SignAuthenticationToken("GET", path, "", su)
if err != nil {
return nil, err
}
return SnapshotByToken(ctx, snapshotId, token)
}
func SnapshotByTraceId(ctx context.Context, traceId string, uid, sid, sessionKey string) (*LegacySnapshot, error) {
su := &SafeUser{
UserId: uid,
SessionId: sid,
SessionPrivateKey: sessionKey,
}
path := "/snapshots/trace/" + traceId
token, err := SignAuthenticationToken("GET", path, "", su)
if err != nil {
return nil, err
}
body, err := Request(ctx, "GET", path, nil, token)
if err != nil {
return nil, err
}
var resp struct {
Data *LegacySnapshot `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 SnapshotByToken(ctx context.Context, snapshotId string, accessToken string) (*LegacySnapshot, error) {
path := "/snapshots/" + snapshotId
body, err := Request(ctx, "GET", path, nil, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data *LegacySnapshot `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 NetworkSnapshot(ctx context.Context, snapshotId string) (*LegacySnapshot, error) {
return NetworkSnapshotByToken(ctx, snapshotId, "")
}
func NetworkSnapshotByToken(ctx context.Context, snapshotId, accessToken string) (*LegacySnapshot, error) {
path := "/network/snapshots/" + snapshotId
body, err := Request(ctx, "GET", path, nil, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data *LegacySnapshot `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 NetworkSnapshots(ctx context.Context, limit int, offset, assetId, order string) ([]*LegacySnapshotShort, error) {
return NetworkSnapshotsByToken(ctx, limit, offset, assetId, order, "", "", "")
}
func NetworkSnapshotsByToken(ctx context.Context, limit int, offset, assetId, order, uid, sid, sessionKey string) ([]*LegacySnapshotShort, error) {
v := url.Values{}
v.Set("limit", strconv.Itoa(limit))
if offset != "" {
v.Set("offset", offset)
}
if assetId != "" {
v.Set("asset", assetId)
}
if order == "ASC" || order == "DESC" {
v.Set("order", order)
}
path := "/network/snapshots?" + v.Encode()
accessToken := ""
if sessionKey != "" {
var err error
su := &SafeUser{
UserId: uid,
SessionId: sid,
SessionPrivateKey: sessionKey,
}
accessToken, err = SignAuthenticationToken("GET", path, "", su)
if err != nil {
return nil, err
}
}
body, err := Request(ctx, "GET", path, nil, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data []*LegacySnapshotShort `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
}