-
Notifications
You must be signed in to change notification settings - Fork 4
/
address.go
348 lines (302 loc) · 7.89 KB
/
address.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2022 The Cardano Community Authors
package koios
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/shopspring/decimal"
)
type (
// Address defines type for _address.
Address string
// AddressInfo esponse for `/address_info`.
AddressInfo struct {
// Balance ADA Lovelace balance of address
Balance decimal.Decimal `json:"balance"`
Address Address `json:"address"`
// StakeAddress associated with address
StakeAddress Address `json:"stake_address,omitempty"`
ScriptAddress bool `json:"script_address"`
UTxOs []UTxO `json:"utxo_set"`
}
AddressTx struct {
TxHash TxHash `json:"tx_hash"`
EpochNo EpochNo `json:"epoch_no"`
BlockTime Timestamp `json:"block_time"`
BlockHeight uint64 `json:"block_height"`
}
AddressAssets struct {
Asset
Address Address `json:"address"`
}
// AddressInfoResponse represents response from `/address_info` endpoint.
AddressInfoResponse struct {
Response
Data *AddressInfo `json:"data"`
}
AddressesInfoResponse struct {
Response
Data []AddressInfo `json:"data"`
}
// AddressTxsResponse represents response from `/address_txs` endpoint.
AddressTxsResponse struct {
Response
Data []AddressTx `json:"data"`
}
// CredentialTxsResponse represents response from `/credential_txs` endpoint.
CredentialTxsResponse struct {
Response
Data []AddressTx `json:"data"`
}
// AddressAssetsResponse represents response from `/address_info` endpoint.
AddressAssetsResponse struct {
Response
Data *AddressAssets `json:"data"`
}
AddressesAssetsResponse struct {
Response
Data []AddressAssets `json:"data"`
}
AddressAsset struct {
AssetName AssetName `json:"asset_name"`
AssetNameASCII string `json:"asset_name_ascii"`
Balance decimal.Decimal `json:"balance"`
}
UTxOsResponse struct {
Response
Data []UTxO `json:"data"`
}
)
// Valid validates address and returns false and error
// if address is invalid otherwise it returns true, nil.
func (a Address) Valid() (bool, error) {
if len(a) == 0 {
return false, ErrNoAddress
}
return true, nil
}
// String returns StakeAddress as string.
func (a Address) String() string {
return string(a)
}
// Bytes returns address bytes.
func (a Address) Bytes() []byte {
return []byte(a)
}
// GetAddressInfo returns address info - balance,
// associated stake address (if any) and UTxO set.
func (c *Client) GetAddressInfo(
ctx context.Context,
addr Address,
opts *RequestOptions,
) (res *AddressInfoResponse, err error) {
res = &AddressInfoResponse{}
res2, err := c.GetAddressesInfo(ctx, []Address{addr}, opts)
if err != nil {
return
}
if len(res2.Data) == 1 {
res.Data = &res2.Data[0]
}
return
}
func (c *Client) GetAddressesInfo(
ctx context.Context,
addr []Address,
opts *RequestOptions,
) (res *AddressesInfoResponse, err error) {
res = &AddressesInfoResponse{}
if len(addr) == 0 {
res.applyError(nil, ErrNoAddressesProvided)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/address_info", addressesPL(addr), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetAddressTxs returns the transaction hash list of input address array,
// optionally filtering after specified block height (inclusive).
func (c *Client) GetAddressTxs(
ctx context.Context,
addrs []Address,
h uint64,
opts *RequestOptions,
) (*AddressTxsResponse, error) {
res := &AddressTxsResponse{}
if len(addrs) == 0 {
err := ErrNoAddress
res.applyError(nil, err)
return res, err
}
var payload = struct {
Adresses []Address `json:"_addresses"`
AfterBlockHeight uint64 `json:"_after_block_height,omitempty"`
}{
Adresses: addrs,
AfterBlockHeight: h,
}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
rsp, err := c.request(ctx, &res.Response, "POST", "/address_txs", rpipe, opts)
if err != nil {
return res, err
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return res, err
}
// GetAddressAssets returns the list of all the assets (policy, name and quantity)
// for a given address.
func (c *Client) GetAddressAssets(
ctx context.Context,
addr Address,
opts *RequestOptions,
) (res *AddressAssetsResponse, err error) {
res = &AddressAssetsResponse{}
if len(addr) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
rsp, err := c.GetAddressesAssets(ctx, []Address{addr}, opts)
if err != nil {
return
}
if len(rsp.Data) == 1 {
res.Data = &rsp.Data[0]
} else {
return nil, fmt.Errorf("%w: no assets on address %s", ErrNoData, addr)
}
return
}
func (c *Client) GetAddressesAssets(
ctx context.Context,
addrs []Address,
opts *RequestOptions,
) (*AddressesAssetsResponse, error) {
res := &AddressesAssetsResponse{}
if len(addrs) == 0 {
err := ErrNoAddress
res.applyError(nil, err)
return res, err
}
rsp, err := c.request(ctx, &res.Response, "POST", "/address_assets", addressesPL(addrs), opts)
if err != nil {
return res, err
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
if err != nil {
return res, err
}
if len(res.Data) == 0 {
return nil, fmt.Errorf("%w: no assets on %d addresses", ErrNoData, len(addrs))
}
return res, err
}
// GetCredentialTxs returns the transaction hash list of input
// payment credential array, optionally filtering after specified block height (inclusive).
func (c *Client) GetCredentialTxs(
ctx context.Context,
creds []PaymentCredential,
h uint64,
opts *RequestOptions,
) (res *CredentialTxsResponse, err error) {
res = &CredentialTxsResponse{}
if len(creds) == 0 || len(creds[0]) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
var payload = struct {
Credentials []PaymentCredential `json:"_payment_credentials"`
AfterBlockHeight uint64 `json:"_after_block_height,omitempty"`
}{
Credentials: creds,
AfterBlockHeight: h,
}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
rsp, err := c.request(ctx, &res.Response, "POST", "/credential_txs", rpipe, opts)
res.applyError(nil, err)
return res, ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
}
func (c *Client) GetAddressUTxOs(
ctx context.Context,
addrs []Address,
extended bool,
opts *RequestOptions,
) (*UTxOsResponse, error) {
res := &UTxOsResponse{}
if len(addrs) == 0 {
err := ErrNoAddress
res.applyError(nil, err)
return res, err
}
var payload = struct {
Adresses []Address `json:"_addresses"`
Extended bool `json:"_extended,omitempty"`
}{
Adresses: addrs,
Extended: extended,
}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
rsp, err := c.request(ctx, &res.Response, "POST", "/address_utxos", rpipe, opts)
res.applyError(nil, err)
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return res, err
}
func (c *Client) GetCredentialUTxOs(
ctx context.Context,
creds []PaymentCredential,
extended bool,
opts *RequestOptions,
) (*UTxOsResponse, error) {
res := &UTxOsResponse{}
if len(creds) == 0 || len(creds[0]) == 0 {
err := ErrNoCredentialsProvided
res.applyError(nil, err)
return res, err
}
var payload = struct {
Creds []PaymentCredential `json:"_payment_credentials"`
Extended bool `json:"_extended,omitempty"`
}{
Creds: creds,
Extended: extended,
}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
rsp, err := c.request(ctx, &res.Response, "POST", "/credential_utxos", rpipe, opts)
res.applyError(nil, err)
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return res, err
}
func addressesPL(addrs []Address) io.Reader {
var payload = struct {
Adresses []Address `json:"_addresses"`
}{addrs}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}