-
Notifications
You must be signed in to change notification settings - Fork 13
/
snapshot.go
138 lines (110 loc) · 3.83 KB
/
snapshot.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
package mixin
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/shopspring/decimal"
)
type Snapshot struct {
SnapshotID string `json:"snapshot_id"`
CreatedAt time.Time `json:"created_at,omitempty"`
TraceID string `json:"trace_id,omitempty"`
UserID string `json:"user_id,omitempty"`
AssetID string `json:"asset_id,omitempty"`
ChainID string `json:"chain_id,omitempty"`
OpponentID string `json:"opponent_id,omitempty"`
Source string `json:"source,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
OpeningBalance decimal.Decimal `json:"opening_balance,omitempty"`
ClosingBalance decimal.Decimal `json:"closing_balance,omitempty"`
Memo string `json:"memo,omitempty"`
Type string `json:"type,omitempty"`
Sender string `json:"sender,omitempty"`
Receiver string `json:"receiver,omitempty"`
TransactionHash string `json:"transaction_hash,omitempty"`
Asset *Asset `json:"asset,omitempty"`
}
type (
snapshotJSON Snapshot
snapshotJSONWithData struct {
snapshotJSON
Data string `json:"data,omitempty"`
}
)
func (s *Snapshot) UnmarshalJSON(b []byte) error {
var sj snapshotJSONWithData
if err := json.Unmarshal(b, &sj); err != nil {
return err
}
if sj.Memo == "" {
sj.Memo = sj.Data
}
if sj.AssetID == "" && sj.Asset != nil {
sj.AssetID = sj.Asset.AssetID
}
*s = (Snapshot)(sj.snapshotJSON)
return nil
}
// ReadSnapshots return a list of snapshots
// order must be `ASC` or `DESC`
func (c *Client) ReadSnapshots(ctx context.Context, assetID string, offset time.Time, order string, limit int) ([]*Snapshot, error) {
var snapshots []*Snapshot
params := buildReadSnapshotsParams(assetID, offset, order, limit)
if err := c.Get(ctx, "/snapshots", params, &snapshots); err != nil {
return nil, err
}
return snapshots, nil
}
// ReadSnapshots by accessToken, scope SNAPSHOTS:READ required
func ReadSnapshots(ctx context.Context, accessToken string, assetID string, offset time.Time, order string, limit int) ([]*Snapshot, error) {
return NewFromAccessToken(accessToken).ReadSnapshots(ctx, assetID, offset, order, limit)
}
func (c *Client) ReadNetworkSnapshots(ctx context.Context, assetID string, offset time.Time, order string, limit int) ([]*Snapshot, error) {
var snapshots []*Snapshot
params := buildReadSnapshotsParams(assetID, offset, order, limit)
if err := c.Get(ctx, "/network/snapshots", params, &snapshots); err != nil {
return nil, err
}
return snapshots, nil
}
func (c *Client) ReadSnapshot(ctx context.Context, snapshotID string) (*Snapshot, error) {
uri := fmt.Sprintf("/snapshots/%s", snapshotID)
var snapshot Snapshot
if err := c.Get(ctx, uri, nil, &snapshot); err != nil {
return nil, err
}
return &snapshot, nil
}
// ReadSnapshot by accessToken, scope SNAPSHOTS:READ required
func ReadSnapshot(ctx context.Context, accessToken, snapshotID string) (*Snapshot, error) {
return NewFromAccessToken(accessToken).ReadSnapshot(ctx, snapshotID)
}
func (c *Client) ReadNetworkSnapshot(ctx context.Context, snapshotID string) (*Snapshot, error) {
uri := fmt.Sprintf("/network/snapshots/%s", snapshotID)
var snapshot Snapshot
if err := c.Get(ctx, uri, nil, &snapshot); err != nil {
return nil, err
}
return &snapshot, nil
}
func buildReadSnapshotsParams(assetID string, offset time.Time, order string, limit int) map[string]string {
params := make(map[string]string)
if assetID != "" {
params["asset"] = assetID
}
if !offset.IsZero() {
params["offset"] = offset.UTC().Format(time.RFC3339Nano)
}
switch order {
case "ASC", "DESC":
default:
order = "DESC"
}
params["order"] = order
if limit > 0 {
params["limit"] = strconv.Itoa(limit)
}
return params
}