-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstake_account.go
409 lines (368 loc) · 10.3 KB
/
stake_account.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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 (
// AccountInfo data returned by `/account_info`.
AccountInfo struct {
Status string `json:"status"`
DelegatedPool *PoolID `json:"delegated_pool"`
StakeAddress Address `json:"stake_address"`
TotalBalance decimal.Decimal `json:"total_balance"`
UTxO decimal.Decimal `json:"utxo"`
Rewards decimal.Decimal `json:"rewards"`
Withdrawals decimal.Decimal `json:"withdrawals"`
RewardsAvailable decimal.Decimal `json:"rewards_available"`
Reserves decimal.Decimal `json:"reserves"`
Treasury decimal.Decimal `json:"treasury"`
}
// AccountRewardsInfo data returned by `/account_rewards`.
AccountRewardsInfo struct {
StakeAddress Address `json:"stake_address"`
Rewards []AccountRewards `json:"rewards"`
}
AccountRewards struct {
PoolID PoolID `json:"pool_id"`
EarnedEpoch EpochNo `json:"earned_epoch"`
SpendableEpoch EpochNo `json:"spendable_epoch"`
Amount decimal.Decimal `json:"amount"`
Type string `json:"type"`
}
// AccountHistoryEntry history entry list item.
AccountHistory struct {
StakeAddress Address `json:"stake_address"`
History []AccountHistoryEntry `json:"history"`
}
AccountHistoryEntry struct {
PoolID PoolID `json:"pool_id"`
EpochNo EpochNo `json:"epoch_no"`
ActiveStake decimal.Decimal `json:"active_stake"`
}
// AccountListResponse represents response from `/account_list` endpoint.
AccountListResponse struct {
Response
Data []Address `json:"data"`
}
// AccountInfoResponse represents response from `/account_info` endpoint.
AccountInfoResponse struct {
Response
Data *AccountInfo `json:"data"`
}
AccountsInfoResponse struct {
Response
Data []AccountInfo `json:"data"`
}
// AccountRewardsResponse represents response from `/account_rewards` endpoint.
AccountRewardsResponse struct {
Response
Data *AccountRewardsInfo `json:"data"`
}
AccountsRewardsResponse struct {
Response
Data []AccountRewardsInfo `json:"data"`
}
// AccountAction data entry for `/account_updates`.
AccountUpdate struct {
ActionType string `json:"action_type"`
TxHash TxHash `json:"tx_hash"`
EpochNo EpochNo `json:"epoch_no"`
EpochSlot Slot `json:"epoch_slot"`
AbsoluteSlot Slot `json:"absolute_slot"`
BlockTime Timestamp `json:"block_time"`
}
AccountUpdates struct {
StakeAddress Address `json:"stake_address"`
Updates []AccountUpdate `json:"updates"`
}
// AccountUpdatesResponse represents response from `/account_rewards` endpoint.
AccountUpdatesResponse struct {
Response
Data *AccountUpdates `json:"data"`
}
AccountsUpdatesResponse struct {
Response
Data []AccountUpdates `json:"data"`
}
AccountAddresses struct {
StakeAddress Address `json:"stake_address"`
Addresses []Address `json:"addresses"`
}
// AccountAddressesResponse represents response from `/account_addresses` endpoint.
AccountAddressesResponse struct {
Response
Data *AccountAddresses `json:"data"`
}
AccountsAddressesResponse struct {
Response
Data []AccountAddresses `json:"data"`
}
// AccountAssetsResponse represents response from `/account_assets` endpoint.
AccountAssetsResponse struct {
Response
Data *AccountAssets `json:"data"`
}
AccountsAssetsResponse struct {
Response
Data []AccountAssets `json:"data"`
}
AccountAssets struct {
StakeAddress Address `json:"stake_address"`
Asset
}
// AccountHistoryResponse represents response from `/account_history` endpoint.
AccountHistoryResponse struct {
Response
Data *AccountHistory `json:"data"`
}
AccountsHistoryResponse struct {
Response
Data []AccountHistory `json:"data"`
}
AccountTXsResponse struct {
Response
Data []TxListItem `json:"data"`
}
)
// GetAccountList returns a list of all accounts.
func (c *Client) GetAccountList(
ctx context.Context,
opts *RequestOptions,
) (res *AccountListResponse, err error) {
res = &AccountListResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", "/account_list", nil, opts)
if err != nil {
return
}
accs := []struct {
ID Address `json:"id"`
}{}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &accs)
if len(accs) > 0 {
for _, a := range accs {
res.Data = append(res.Data, a.ID)
}
}
return
}
// GetAccountInfo returns the account info of any (payment or staking) address.
func (c *Client) GetAccountInfo(
ctx context.Context,
accs []Address,
opts *RequestOptions,
) (res *AccountsInfoResponse, err error) {
res = &AccountsInfoResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
endpoint := "/account_info"
rsp, err := c.request(ctx, &res.Response, "POST", endpoint, stakeAddressesPL(accs, nil, nil), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func (c *Client) GetAccountInfoCached(
ctx context.Context,
accs []Address,
opts *RequestOptions,
) (res *AccountsInfoResponse, err error) {
res = &AccountsInfoResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
endpoint := "/account_info_cached"
rsp, err := c.request(ctx, &res.Response, "POST", endpoint, stakeAddressesPL(accs, nil, nil), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetAccountRewards retruns the full rewards history (including MIR)
// for a stake address, or certain epoch if specified.
func (c *Client) GetAccountRewards(
ctx context.Context,
accs []Address,
epoch EpochNo,
opts *RequestOptions,
) (res *AccountsRewardsResponse, err error) {
res = &AccountsRewardsResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
var epochNo *EpochNo
if epoch > 0 {
epochNo = &epoch
}
rsp, err := c.request(ctx, &res.Response, "POST", "/account_rewards", stakeAddressesPL(accs, epochNo, nil), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetAccountUpdates (History) retruns the account updates
// (registration, deregistration, delegation and withdrawals).
func (c *Client) GetAccountUpdates(
ctx context.Context,
accs []Address,
opts *RequestOptions,
) (res *AccountsUpdatesResponse, err error) {
res = &AccountsUpdatesResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/account_updates", stakeAddressesPL(accs, nil, nil), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetAccountAddresses retruns all addresses associated with an account.
func (c *Client) GetAccountAddresses(
ctx context.Context,
accs []Address,
firstOnly, empty bool,
opts *RequestOptions,
) (res *AccountsAddressesResponse, err error) {
res = &AccountsAddressesResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/account_addresses", stakeAddresses2PL(accs, firstOnly, empty), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetAccountAssets retruns all the native asset balance of an account.
func (c *Client) GetAccountAssets(
ctx context.Context,
accs []Address,
opts *RequestOptions,
) (res *AccountsAssetsResponse, err error) {
res = &AccountsAssetsResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/account_assets", stakeAddressesPL(accs, nil, nil), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetAccountHistory retruns the staking history of an account.
func (c *Client) GetAccountHistory(
ctx context.Context,
accs []Address,
epoch *EpochNo,
opts *RequestOptions,
) (res *AccountsHistoryResponse, err error) {
res = &AccountsHistoryResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/account_history", stakeAddressesPL(accs, epoch, nil), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func (c *Client) GetAccountUtxos(
ctx context.Context,
accs []Address,
extended bool,
opts *RequestOptions,
) (res *UTxOsResponse, err error) {
res = &UTxOsResponse{}
if len(accs) == 0 {
err = ErrNoAddress
res.applyError(nil, err)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/account_utxos", stakeAddressesPL(accs, nil, &extended), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func (c *Client) GetAccountTxs(
ctx context.Context,
acc Address,
afterBlockHeight uint64,
opts *RequestOptions,
) (res *AccountTXsResponse, err error) {
res = &AccountTXsResponse{}
opts.QueryAdd("_stake_address", acc.String())
if afterBlockHeight > 0 {
opts.QueryAdd("after_block_height", fmt.Sprint(afterBlockHeight))
}
rsp, err := c.request(ctx, &res.Response, "GET", "/account_txs", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func stakeAddressesPL(addrs []Address, epoch *EpochNo, extended *bool) io.Reader {
var payload = struct {
Adresses []Address `json:"_stake_addresses"`
Epoch *EpochNo `json:"_epoch_no,omitempty"`
Extended *bool `json:"_extended,omitempty"`
}{addrs, epoch, extended}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}
func stakeAddresses2PL(addrs []Address, firstOnly, empty bool) io.Reader {
var (
firstOnlyVal, emptyVal *bool
)
if firstOnly {
firstOnlyVal = &firstOnly
}
if empty {
emptyVal = &empty
}
var payload = struct {
Adresses []Address `json:"_stake_addresses"`
FirstOnly *bool `json:"_first_only,omitempty"`
Empty *bool `json:"_empty,omitempty"`
}{addrs, firstOnlyVal, emptyVal}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}