-
Notifications
You must be signed in to change notification settings - Fork 26
/
output.go
146 lines (137 loc) · 4.32 KB
/
output.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
139
140
141
142
143
144
145
146
package bot
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
)
type KernelDepositView struct {
Chain string `json:"chain"`
DepositHash string `json:"deposit_hash"`
DepositIndex int64 `json:"deposit_index"`
}
type Output struct {
Type string `json:"type"`
OutputID string `json:"output_id"`
TransactionHash string `json:"transaction_hash"`
OutputIndex uint `json:"output_index"`
AssetId string `json:"asset_id"`
KernelAssetId string `json:"kernel_asset_id"`
Amount string `json:"amount"`
Mask string `json:"mask"`
Keys []string `json:"keys"`
SendersHash string `json:"senders_hash"`
SendersThreshold int64 `json:"senders_threshold"`
Senders []string `json:"senders"`
ReceiversHash string `json:"receivers_hash"`
ReceiversThreshold int64 `json:"receivers_threshold"`
Receivers []string `json:"receivers"`
Extra string `json:"extra"`
State string `json:"state"`
Sequence int64 `json:"sequence"`
CreatedAt time.Time `json:"created_at"`
Signers []string `json:"signers"`
SignedBy string `json:"signed_by"`
InscriptionHash string `json:"inscription_hash,omitempty"`
Deposit *KernelDepositView `json:"deposit,omitempty"`
RequestId string `json:"request_id,omitempty"`
}
func ListUnspentOutputs(ctx context.Context, membersHash string, threshold byte, kernelAssetId string, u *SafeUser) ([]*Output, error) {
return ListOutputs(ctx, membersHash, threshold, kernelAssetId, "unspent", 0, 500, u)
}
func ListOutputs(ctx context.Context, membersHash string, threshold byte, assetId, state string, offset uint64, limit int, u *SafeUser) ([]*Output, error) {
v := url.Values{}
v.Set("members", membersHash)
v.Set("threshold", fmt.Sprint(threshold))
v.Set("limit", strconv.Itoa(limit))
if offset > 0 {
v.Set("offset", fmt.Sprint(offset))
}
if assetId != "" {
v.Set("asset", assetId)
}
if state != "" {
v.Set("state", state)
}
method, path := "GET", fmt.Sprintf("/safe/outputs?"+v.Encode())
token, err := SignAuthenticationToken(method, path, "", u)
if err != nil {
return nil, err
}
body, err := Request(ctx, method, path, []byte{}, token)
if err != nil {
return nil, err
}
var resp struct {
Data []*Output `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp.Data, nil
}
func ListUnspentOutputsByToken(ctx context.Context, membersHash string, threshold byte, kernelAssetId string, accessToken string) ([]*Output, error) {
return ListOutputsByToken(ctx, membersHash, threshold, kernelAssetId, "unspent", 0, 500, accessToken)
}
func ListOutputsByToken(ctx context.Context, membersHash string, threshold byte, assetId, state string, offset uint64, limit int, accessToken string) ([]*Output, error) {
v := url.Values{}
v.Set("members", membersHash)
v.Set("threshold", fmt.Sprint(threshold))
v.Set("limit", strconv.Itoa(limit))
if offset > 0 {
v.Set("offset", fmt.Sprint(offset))
}
if assetId != "" {
v.Set("asset", assetId)
}
if state != "" {
v.Set("state", state)
}
method, path := "GET", fmt.Sprintf("/safe/outputs?"+v.Encode())
body, err := Request(ctx, method, path, []byte{}, accessToken)
if err != nil {
return nil, err
}
var resp struct {
Data []*Output `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp.Data, nil
}
func GetOutput(ctx context.Context, id string, u *SafeUser) (*Output, error) {
method, path := "GET", fmt.Sprintf("/safe/outputs/%s", id)
token, err := SignAuthenticationToken(method, path, "", u)
if err != nil {
return nil, err
}
body, err := Request(ctx, method, path, []byte{}, token)
if err != nil {
return nil, ServerError(ctx, err)
}
var resp struct {
Data *Output `json:"data"`
Error Error `json:"error"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return nil, BadDataError(ctx)
}
if resp.Error.Code > 0 {
return nil, resp.Error
}
return resp.Data, nil
}