-
Notifications
You must be signed in to change notification settings - Fork 390
/
transactions.go
316 lines (266 loc) · 7.62 KB
/
transactions.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package coinpayments
import (
"context"
"encoding/json"
"math/big"
"net/url"
"strconv"
"strings"
"time"
"github.com/zeebo/errs"
)
const (
cmdCreateTransaction = "create_transaction"
cmdGetTransactionInfo = "get_tx_info"
cmdGetTransactionInfoList = "get_tx_info_multi"
)
// Status is a type wrapper for transaction statuses.
type Status int
const (
// StatusCancelled defines cancelled or timeout transaction.
StatusCancelled Status = -1
// StatusPending defines pending transaction which is waiting for buyer funds.
StatusPending Status = 0
// StatusReceived defines transaction which successfully received required amount of funds.
StatusReceived Status = 1
// StatusCompleted defines transaction which is fully completed.
StatusCompleted Status = 100
)
// Int returns int representation of status.
func (s Status) Int() int {
return int(s)
}
// String returns string representation of status.
func (s Status) String() string {
switch s {
case StatusCancelled:
return "cancelled/timeout"
case StatusPending:
return "pending"
case StatusReceived:
return "received"
case StatusCompleted:
return "completed"
default:
return "unknown"
}
}
// TransactionID is type wrapper for transaction id.
type TransactionID string
// String returns string representation of transaction id.
func (id TransactionID) String() string {
return string(id)
}
// TransactionIDList is a type wrapper for list of transactions.
type TransactionIDList []TransactionID
// Encode returns encoded string representation of transaction id list.
func (list TransactionIDList) Encode() string {
if len(list) == 0 {
return ""
}
if len(list) == 1 {
return string(list[0])
}
var builder strings.Builder
for _, id := range list[:len(list)-1] {
builder.WriteString(string(id))
builder.WriteString("|")
}
builder.WriteString(string(list[len(list)-1]))
return builder.String()
}
// Transaction contains data returned on transaction creation.
type Transaction struct {
ID TransactionID
Address string
Amount big.Float
DestTag string
ConfirmsNeeded int
Timeout time.Duration
CheckoutURL string
StatusURL string
QRCodeURL string
}
// UnmarshalJSON handles json unmarshaling for transaction.
func (tx *Transaction) UnmarshalJSON(b []byte) error {
var txRaw struct {
Amount string `json:"amount"`
Address string `json:"address"`
DestTag string `json:"dest_tag"`
TxID string `json:"txn_id"`
ConfirmsNeeded string `json:"confirms_needed"`
Timeout int `json:"timeout"`
CheckoutURL string `json:"checkout_url"`
StatusURL string `json:"status_url"`
QRCodeURL string `json:"qrcode_url"`
}
if err := json.Unmarshal(b, &txRaw); err != nil {
return err
}
amount, err := parseAmount(txRaw.Amount)
if err != nil {
return err
}
confirms, err := strconv.ParseInt(txRaw.ConfirmsNeeded, 10, 64)
if err != nil {
return err
}
*tx = Transaction{
ID: TransactionID(txRaw.TxID),
Address: txRaw.Address,
Amount: *amount,
DestTag: txRaw.DestTag,
ConfirmsNeeded: int(confirms),
Timeout: time.Second * time.Duration(txRaw.Timeout),
CheckoutURL: txRaw.CheckoutURL,
StatusURL: txRaw.StatusURL,
QRCodeURL: txRaw.QRCodeURL,
}
return nil
}
// TransactionInfo holds transaction information.
type TransactionInfo struct {
Address string
Coin Currency
Amount big.Float
Received big.Float
ConfirmsReceived int
Status Status
ExpiresAt time.Time
CreatedAt time.Time
}
// UnmarshalJSON handles json unmarshaling for transaction info.
func (info *TransactionInfo) UnmarshalJSON(b []byte) error {
var txInfoRaw struct {
Address string `json:"payment_address"`
Coin string `json:"coin"`
Status int `json:"status"`
AmountF string `json:"amountf"`
ReceivedF string `json:"receivedf"`
ConfirmsRecv int `json:"recv_confirms"`
ExpiresAt int64 `json:"time_expires"`
CreatedAt int64 `json:"time_created"`
}
if err := json.Unmarshal(b, &txInfoRaw); err != nil {
return err
}
amount, err := parseAmount(txInfoRaw.AmountF)
if err != nil {
return err
}
received, err := parseAmount(txInfoRaw.ReceivedF)
if err != nil {
return err
}
*info = TransactionInfo{
Address: txInfoRaw.Address,
Coin: Currency(txInfoRaw.Coin),
Amount: *amount,
Received: *received,
ConfirmsReceived: txInfoRaw.ConfirmsRecv,
Status: Status(txInfoRaw.Status),
ExpiresAt: time.Unix(txInfoRaw.ExpiresAt, 0),
CreatedAt: time.Unix(txInfoRaw.CreatedAt, 0),
}
return nil
}
// TransactionInfos is map of transaction infos by transaction id.
type TransactionInfos map[TransactionID]TransactionInfo
// UnmarshalJSON handles json unmarshaling for TransactionInfos.
func (infos *TransactionInfos) UnmarshalJSON(b []byte) error {
var _infos map[TransactionID]TransactionInfo
var errors map[TransactionID]struct {
Error string `json:"error"`
}
if err := json.Unmarshal(b, &errors); err != nil {
return err
}
var errg errs.Group
for _, info := range errors {
if info.Error != "ok" {
errg.Add(errs.New(info.Error))
}
}
if err := errg.Err(); err != nil {
return err
}
if err := json.Unmarshal(b, &_infos); err != nil {
return err
}
for id, info := range _infos {
(*infos)[id] = info
}
return nil
}
// CreateTX defines parameters for transaction creating.
type CreateTX struct {
Amount big.Float
CurrencyIn Currency
CurrencyOut Currency
BuyerEmail string
}
// Transactions defines transaction related API methods.
type Transactions struct {
client *Client
}
// Create creates new transaction.
func (t Transactions) Create(ctx context.Context, params *CreateTX) (*Transaction, error) {
values := make(url.Values)
values.Set("amount", params.Amount.Text('f', -1))
values.Set("currency1", params.CurrencyIn.String())
values.Set("currency2", params.CurrencyOut.String())
values.Set("buyer_email", params.BuyerEmail)
tx := new(Transaction)
res, err := t.client.do(ctx, cmdCreateTransaction, values)
if err != nil {
return nil, Error.Wrap(err)
}
if err = json.Unmarshal(res, tx); err != nil {
return nil, Error.Wrap(err)
}
return tx, nil
}
// Info receives transaction info by transaction id.
func (t Transactions) Info(ctx context.Context, id TransactionID) (*TransactionInfo, error) {
values := make(url.Values)
values.Set("txid", id.String())
txInfo := new(TransactionInfo)
res, err := t.client.do(ctx, cmdGetTransactionInfo, values)
if err != nil {
return nil, Error.Wrap(err)
}
if err = json.Unmarshal(res, txInfo); err != nil {
return nil, Error.Wrap(err)
}
return txInfo, nil
}
// ListInfos returns transaction infos.
func (t Transactions) ListInfos(ctx context.Context, ids TransactionIDList) (TransactionInfos, error) {
// The service supports a max batch size of 25 items
const batchSize = 25
var allErrors error
numIds := len(ids)
txInfos := make(TransactionInfos, numIds)
for i := 0; i < len(ids); i += batchSize {
j := i + batchSize
if j > numIds {
j = numIds
}
batchInfos := make(TransactionInfos, j-i)
values := make(url.Values, j-i)
values.Set("txid", ids[i:j].Encode())
res, err := t.client.do(ctx, cmdGetTransactionInfoList, values)
if err != nil {
allErrors = errs.Combine(allErrors, err)
}
if err = json.Unmarshal(res, &batchInfos); err != nil {
allErrors = errs.Combine(allErrors, err)
}
for k, v := range batchInfos {
txInfos[k] = v
}
}
return txInfos, allErrors
}