-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshots.go
118 lines (95 loc) · 2.69 KB
/
snapshots.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
package broker
import (
"context"
"encoding/json"
"fmt"
)
type Asset struct {
AssetId string `json:"assetId"`
AssetKey string `json:"assetKey,omitempty"`
ChainId string `json:"chainId"`
Icon string `json:"icon"`
Name string `json:"name"`
Symbol string `json:"symbol"`
CoinId uint `json:"coinId,omitempty"`
}
type SnapshotUser struct {
FoxId uint `json:"foxId,omitempty"`
MixinId string `json:"mixinId,omitempty"`
Avatar string `json:"avatar,omitempty"`
Fullname string `json:"fullname,omitempty"`
}
type Snapshot struct {
SnapshotId string `json:"snapshotId"`
TraceId string `json:"traceId"`
UserId string `json:"userId"`
CreatedAt int64 `json:"createdAt"`
Source string `json:"source"`
Amount string `json:"amount"`
InsideMixin bool `json:"insideMixin"`
Memo string `json:"memo"`
Sender string `json:"sender,omitempty"`
Receiver string `json:"receiver,omitempty"`
TransactionHash string `json:"transactionHash,omitempty"`
Asset Asset `json:"asset,omitempty"`
User SnapshotUser `json:"user,omitempty"`
Opponent SnapshotUser `json:"opponent,omitempty"`
ExtraData map[string]interface{} `json:"extraData,omitempty"`
}
type Snapshots []Snapshot
type SnapshotResponse struct {
Pagination OffsetPagination `json:"pagination"`
Snapshots Snapshots `json:"snapshots"`
}
type SingleSnapshotResponse struct {
Snapshot Snapshot `json:"snapshot"`
}
func (b Broker) PullSnapshots(ctx context.Context, userId, assetId, cursor string, asc bool, limit int) (*SnapshotResponse, error) {
paras := []interface{}{}
if len(userId) > 0 {
paras = append(paras, "userId", userId)
}
if len(assetId) > 0 {
paras = append(paras, "assetId", assetId)
}
if len(cursor) > 0 {
paras = append(paras, "cursor", cursor)
}
if asc {
paras = append(paras, "order", "ASC")
} else {
paras = append(paras, "order", "DESC")
}
if limit > 0 {
paras = append(paras, "limit", limit)
}
resp, err := b.do(ctx, "POST", "broker/snapshots", paras...)
if err != nil {
return nil, err
}
data, err := resp.MarshalJSON()
if err != nil {
return nil, err
}
snapResp := &SnapshotResponse{}
if err := json.Unmarshal(data, snapResp); err != nil {
return nil, err
}
return snapResp, nil
}
func (b Broker) GetSnapshot(ctx context.Context, snapshotID string) (*Snapshot, error) {
resp, err := b.do(ctx, "POST", fmt.Sprintf("broker/snapshot/%s", snapshotID))
if err != nil {
return nil, err
}
data, err := resp.MarshalJSON()
if err != nil {
return nil, err
}
fmt.Printf("%s\n", data)
sr := SingleSnapshotResponse{}
if err := json.Unmarshal(data, &sr); err != nil {
return nil, err
}
return &sr.Snapshot, nil
}