-
Notifications
You must be signed in to change notification settings - Fork 13
/
safe_utxo.go
121 lines (102 loc) · 3.43 KB
/
safe_utxo.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
package mixin
import (
"context"
"errors"
"fmt"
"strconv"
"time"
"github.com/fox-one/mixin-sdk-go/v2/mixinnet"
"github.com/shopspring/decimal"
)
const (
SafeUtxoStateUnspent SafeUtxoState = "unspent"
SafeUtxoStateSigned SafeUtxoState = "signed"
SafeUtxoStateSpent SafeUtxoState = "spent"
)
type (
SafeUtxoState string
SafeUtxo struct {
OutputID string `json:"output_id,omitempty"`
RequestID string `json:"request_id,omitempty"`
TransactionHash mixinnet.Hash `json:"transaction_hash,omitempty"`
OutputIndex uint8 `json:"output_index,omitempty"`
KernelAssetID mixinnet.Hash `json:"kernel_asset_id,omitempty"`
AssetID string `json:"asset_id,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
Mask mixinnet.Key `json:"mask,omitempty"`
Keys []mixinnet.Key `json:"keys,omitempty"`
SendersHash string `json:"senders_hash,omitempty"`
SendersThreshold uint8 `json:"senders_threshold,omitempty"`
Senders []string `json:"senders,omitempty"`
ReceiversHash mixinnet.Hash `json:"receivers_hash,omitempty"`
ReceiversThreshold uint8 `json:"receivers_threshold,omitempty"`
Receivers []string `json:"receivers,omitempty"`
Extra string `json:"extra,omitempty"`
State SafeUtxoState `json:"state,omitempty"`
Sequence uint64 `json:"sequence,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Signers []string `json:"signers,omitempty"`
SignedBy string `json:"signed_by,omitempty"`
SignedAt *time.Time `json:"signed_at,omitempty"`
SpentAt *time.Time `json:"spent_at,omitempty"`
}
)
type SafeListUtxoOption struct {
Members []string
Threshold uint8
Offset uint64
Asset string
Limit int
Order string
State SafeUtxoState
}
func (c *Client) SafeListUtxos(ctx context.Context, opt SafeListUtxoOption) ([]*SafeUtxo, error) {
params := make(map[string]string)
if opt.Offset > 0 {
params["offset"] = fmt.Sprint(opt.Offset)
}
if opt.Limit > 0 {
params["limit"] = strconv.Itoa(opt.Limit)
}
if len(opt.Members) == 0 {
opt.Members = []string{c.ClientID}
}
if opt.Threshold < 1 {
opt.Threshold = 1
}
if int(opt.Threshold) > len(opt.Members) {
return nil, errors.New("invalid members")
}
params["members"] = mixinnet.HashMembers(opt.Members)
params["threshold"] = fmt.Sprint(opt.Threshold)
switch opt.Order {
case "ASC", "DESC":
default:
opt.Order = "DESC"
}
params["order"] = opt.Order
if opt.State != "" {
params["state"] = string(opt.State)
}
if opt.Asset != "" {
params["asset"] = opt.Asset
}
var utxos []*SafeUtxo
if err := c.Get(ctx, "/safe/outputs", params, &utxos); err != nil {
return nil, err
}
return utxos, nil
}
func (c *Client) SafeReadUtxo(ctx context.Context, id string) (*SafeUtxo, error) {
uri := fmt.Sprintf("/safe/outputs/%s", id)
var utxo SafeUtxo
if err := c.Get(ctx, uri, nil, &utxo); err != nil {
return nil, err
}
return &utxo, nil
}
func (c *Client) SafeReadUtxoByHash(ctx context.Context, hash mixinnet.Hash, index uint8) (*SafeUtxo, error) {
id := fmt.Sprintf("%s:%d", hash.String(), index)
return c.SafeReadUtxo(ctx, id)
}