-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsafe_snapshot.go
95 lines (76 loc) · 2.87 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
package mixin
import (
"context"
"strconv"
"time"
"github.com/fox-one/mixin-sdk-go/v2/mixinnet"
"github.com/shopspring/decimal"
)
type (
SafeSnapshotDeposit struct {
DepositHash string `json:"deposit_hash,omitempty"`
DepositIndex uint64 `json:"deposit_index,omitempty"`
Sender string `json:"sender,omitempty"`
}
SafeSnapshot struct {
SnapshotID string `json:"snapshot_id,omitempty"`
RequestID string `json:"request_id,omitempty"`
UserID string `json:"user_id,omitempty"`
OpponentID string `json:"opponent_id,omitempty"`
TransactionHash *mixinnet.Hash `json:"transaction_hash,omitempty"`
AssetID string `json:"asset_id,omitempty"`
KernelAssetID string `json:"kernel_asset_id,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
Memo string `json:"memo,omitempty"`
CreatedAt time.Time `json:"created_at"`
Deposit *SafeSnapshotDeposit `json:"deposit,omitempty"`
}
)
func (c *Client) ReadSafeSnapshot(ctx context.Context, snapshotID string) (*SafeSnapshot, error) {
var snapshot SafeSnapshot
if err := c.Get(ctx, "/safe/snapshots/"+snapshotID, nil, &snapshot); err != nil {
return nil, err
}
return &snapshot, nil
}
func ReadSafeSnapshot(ctx context.Context, accessToken, snapshotID string) (*SafeSnapshot, error) {
return NewFromAccessToken(accessToken).ReadSafeSnapshot(ctx, snapshotID)
}
func (c *Client) ReadSafeSnapshots(ctx context.Context, assetID string, offset time.Time, order string, limit int) ([]*SafeSnapshot, error) {
params := buildReadSafeSnapshotsParams(assetID, offset, order, limit)
var snapshots []*SafeSnapshot
if err := c.Get(ctx, "/safe/snapshots", params, &snapshots); err != nil {
return nil, err
}
return snapshots, nil
}
func ReadSafeSnapshots(ctx context.Context, accessToken string, assetID string, offset time.Time, order string, limit int) ([]*SafeSnapshot, error) {
return NewFromAccessToken(accessToken).ReadSafeSnapshots(ctx, assetID, offset, order, limit)
}
func buildReadSafeSnapshotsParams(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
}
func (c *Client) SafeNotifySnapshot(ctx context.Context, receiverID string, hash mixinnet.Hash, index uint8) error {
const uri = "/safe/snapshots/notifications"
return c.Post(ctx, uri, map[string]interface{}{
"transaction_hash": hash.String(),
"output_index": index,
"receiver_id": receiverID,
}, nil)
}