-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsnapshot.go
More file actions
180 lines (166 loc) · 4.85 KB
/
snapshot.go
File metadata and controls
180 lines (166 loc) · 4.85 KB
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
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"`
Destination string `json:"destination"`
Tag string `json:"tag"`
}
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"`
KernelAssetID string `json:"kernel_asset_id"`
Amount string `json:"amount"`
Memo string `json:"memo"`
RequestId string `json:"request_id"`
CreatedAt time.Time `json:"created_at"`
Level int64 `json:"level"`
Deposit *SafeDepositView `json:"deposit,omitempty"`
Withdrawal *SafeWithdrawalView `json:"withdrawal,omitempty"`
}
func SafeSnapshots(ctx context.Context, limit int, app, assetId, opponent, offset string, su *SafeUser) ([]*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("GET", path, "", su)
if err != nil {
return nil, err
}
return SafeSnapshotsByToken(ctx, limit, app, assetId, opponent, offset, 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, su *SafeUser) (*SafeSnapshot, error) {
path := "/safe/snapshots/" + snapshotId
token, err := SignAuthenticationToken("GET", path, "", su)
if err != nil {
return nil, err
}
return SafeSnapshotByToken(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
}
type MessageWithSession struct {
Type string `json:"type"`
RepresentativeId string `json:"representative_id"`
QuoteMessageId string `json:"quote_message_id"`
ConversationId string `json:"conversation_id"`
UserId string `json:"user_id"`
SessionId string `json:"session_id"`
MessageId string `json:"message_id"`
Category string `json:"category"`
Data string `json:"data"`
DataBase64 string `json:"data_base64"`
Status string `json:"status"`
Source string `json:"source"`
Silent bool `json:"silent"`
ExpireIn int64 `json:"expire_in"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func SafeNotifySnapshot(ctx context.Context, transactionHash string, outputIndex int64, receiverID string, su *SafeUser) (*MessageWithSession, error) {
data, err := json.Marshal(map[string]any{
"transaction_hash": transactionHash,
"output_index": outputIndex,
"receiver_id": receiverID,
})
if err != nil {
return nil, err
}
method, path := "POST", "/safe/snapshots/notifications"
token, err := SignAuthenticationToken(method, path, string(data), su)
if err != nil {
return nil, err
}
body, err := Request(ctx, method, path, data, token)
if err != nil {
return nil, err
}
var resp struct {
Data *MessageWithSession `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
}