-
Notifications
You must be signed in to change notification settings - Fork 25
/
safe_snapshot.go
123 lines (112 loc) · 3.07 KB
/
safe_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
package bot
import (
"context"
"encoding/json"
"net/url"
"strconv"
"time"
)
type SafeDepositView struct {
DepositHash string `json:"deposit_hash"`
DepositIndex int64 `json:"deposit_index"`
Sender string `json:"sender"`
}
type SafeWithdrawalView struct {
WithdrawalHash string `json:"withdrawal_hash"`
Receiver string `json:"receiver"`
}
type SafeSnapshot struct {
Type string `json:"type"`
SnapshotID string `json:"snapshot_id"`
UserID string `json:"user_id"`
OpponentID string `json:"opponent_id"`
TransactionHash string `json:"transaction_hash"`
AssetID string `json:"asset_id"`
Amount string `json:"amount"`
Memo string `json:"memo"`
CreatedAt time.Time `json:"created_at"`
Deposit *SafeDepositView `json:"deposit,omitempty"`
Withdrawal *SafeWithdrawalView `json:"withdrawal,omitempty"`
}
func SafeSnapshots(ctx context.Context, limit int, app, assetId, opponent, offset, uid, sid, sessionKey string) ([]*SafeSnapshot, error) {
v := url.Values{}
v.Set("limit", strconv.Itoa(limit))
if app != "" {
v.Set("app", app)
}
if assetId != "" {
v.Set("asset", assetId)
}
if offset != "" {
v.Set("offset", offset)
}
if opponent != "" {
v.Set("opponent", opponent)
}
path := "/safe/snapshots?" + v.Encode()
token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "")
if err != nil {
return nil, err
}
return SafeSnapshotsByToken(ctx, limit, app, assetId, offset, opponent, token)
}
func SafeSnapshotsByToken(ctx context.Context, limit int, app, assetId, opponent, offset, accessToken string) ([]*SafeSnapshot, error) {
v := url.Values{}
v.Set("limit", strconv.Itoa(limit))
if app != "" {
v.Set("app", app)
}
if assetId != "" {
v.Set("asset", assetId)
}
if offset != "" {
v.Set("offset", offset)
}
if opponent != "" {
v.Set("opponent", opponent)
}
path := "/safe/snapshots?" + v.Encode()
body, err := Request(ctx, "GET", path, nil, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data []*SafeSnapshot `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 SafeSnapshotById(ctx context.Context, snapshotId string, uid, sid, sessionKey string) (*Snapshot, error) {
path := "/safe/snapshots/" + snapshotId
token, err := SignAuthenticationToken(uid, sid, sessionKey, "GET", path, "")
if err != nil {
return nil, err
}
return SnapshotByToken(ctx, snapshotId, token)
}
func SafeSnapshotByToken(ctx context.Context, snapshotId string, accessToken string) (*SafeSnapshot, error) {
path := "/safe/snapshots/" + snapshotId
body, err := Request(ctx, "GET", path, nil, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data *SafeSnapshot `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
}