-
Notifications
You must be signed in to change notification settings - Fork 4
/
pool.go
658 lines (558 loc) · 17.4 KB
/
pool.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2022 The Cardano Community Authors
package koios
import (
"context"
"encoding/json"
"io"
"github.com/shopspring/decimal"
)
// introduces breaking change since v1.3.0
type (
// PoolListItem defines model for pool list item.
PoolListItem struct {
// PoolID Bech32 representation of pool ID.
PoolIDBech32 PoolID `json:"pool_id_bech32"`
PoolIDHEX PoolID `json:"pool_id_hex"`
// ActiveEpochNo Block number on chain where transaction was included.
ActiveEpochNo EpochNo `json:"active_epoch_no"`
// Margin (decimal format)
Margin float32 `json:"margin"`
// FixedCost Pool fixed cost per epoch
FixedCost decimal.Decimal `json:"fixed_cost"`
// Pledge pledge in lovelace.
Pledge decimal.Decimal `json:"pledge"`
// Pool reward address.
RewardAddr Address `json:"reward_addr"`
// Owners of the pool
Owners []Address `json:"owners"`
// Relays of the pool
Relays []Relay `json:"relays"`
// Ticker of Pool.
Ticker string `json:"ticker,omitempty"`
// MetaUrl Pool metadata URL
MetaURL string `json:"meta_url"`
// MetaHash Pool metadata hash
MetaHash string `json:"meta_hash"`
// Pool status (registered | retiring | retired).
PoolStatus string `json:"pool_status"`
// Announced retiring epoch (nullable).
RetiringEpoch *EpochNo `json:"retiring_epoch.omitempty"`
}
PoolRegistrationOrRetirement struct {
PoolIDBech32 PoolID `json:"pool_id_bech32"`
TxHash TxHash `json:"tx_hash"`
BlockHash BlockHash `json:"block_hash"`
BlockHeight uint64 `json:"block_height"`
EpochNo EpochNo `json:"epoch_no"`
EpochSlot Slot `json:"epoch_slot"`
ActiveEpochNo EpochNo `json:"active_epoch_no"`
}
// PoolMetaJSON pool meadata json.
PoolMetaJSON struct {
// Pool description
Description *string `json:"description"`
// Pool homepage URL
Homepage *string `json:"homepage"`
// Pool name
Name *string `json:"name"`
// Pool ticker
Ticker *string `json:"ticker"`
}
// PoolMetadata metadata list item.
PoolMetadata struct {
// ID (bech32 format)
PoolID PoolID `json:"pool_id_bech32"`
// MetaUrl Pool metadata URL
MetaURL string `json:"meta_url"`
// MetaHash Pool metadata hash
MetaHash string `json:"meta_hash"`
// MetaJson pool meta json
MetaJSON *PoolMetaJSON `json:"meta_json,omitempty"`
PoolStatus string `json:"pool_status"`
}
// Relay defines model for pool relay.
Relay struct {
// DNS name of the relay (nullable)
DNS string `json:"dns"`
// IPv4 address of the relay (nullable)
Ipv4 string `json:"ipv4,"`
// IPv6 address of the relay (nullable)
Ipv6 string `json:"ipv6,"`
// Port number of the relay (nullable)
Port uint16 `json:"port"`
// DNS service name of the relay (nullable)
Srv string `json:"srv"`
}
// PoolInfo defines model for pool_info.
PoolInfo struct {
// ID (bech32 format)
PoolIDBech32 PoolID `json:"pool_id_bech32"`
// IDHex Pool ID (Hex format)
PoolIDHex string `json:"pool_id_hex"`
// ActiveEpochNo Block number on chain where transaction was included.
ActiveEpoch EpochNo `json:"active_epoch_no"`
// Pool VRF key hash
VrfKeyHash string `json:"vrf_key_hash"`
// Margin (decimal format)
Margin float32 `json:"margin"`
// FixedCost Pool fixed cost per epoch
FixedCost decimal.Decimal `json:"fixed_cost"`
// Pledge pledge in lovelace
Pledge decimal.Decimal `json:"pledge"`
// Pool reward address
RewardAddr Address `json:"reward_addr"`
// Owners of the pool
Owners []Address `json:"owners"`
// Relays of the pool
Relays []Relay `json:"relays"`
// MetaUrl Pool metadata URL
MetaURL string `json:"meta_url"`
// MetaHash Pool metadata hash
MetaHash string `json:"meta_hash"`
// MetaJson pool meta json
MetaJSON PoolMetaJSON `json:"meta_json"`
// Pool status (registered | retiring | retired)
PoolStatus string `json:"pool_status"`
// Announced retiring epoch (nullable)
RetiringEpoch *EpochNo `json:"retiring_epoch"`
// OpCert Pool latest operational certificate hash
OpCert string `json:"op_cert"`
// OpCertCounter Pool latest operational certificate counter value
OpCertCounter int `json:"op_cert_counter"`
// ActiveStake Pool active stake.
ActiveStake decimal.Decimal `json:"active_stake"`
// Pool relative active stake share
Sigma decimal.Decimal `json:"sigma"`
// Total pool blocks on chain
BlockCount uint64 `json:"block_count"`
// LivePledge Pool live pledge
LivePledge decimal.Decimal `json:"live_pledge"`
// LiveStake Pool live stake
LiveStake decimal.Decimal `json:"live_stake"`
// LiveDelegators Pool live delegator count
LiveDelegators uint64 `json:"live_delegators"`
// LiveSaturation Pool live saturation (decimal format)
LiveSaturation float32 `json:"live_saturation"`
}
// PoolUpdateInfo response item from `/pool_updates`.
PoolUpdateInfo struct {
// TxHash update transaction
TxHash TxHash `json:"tx_hash"`
// Time time of the block.
BlockTime Timestamp `json:"block_time"`
// ID (bech32 format)
PoolID PoolID `json:"pool_id_bech32"`
// IDHex Pool ID (Hex format)
PoolIDHex string `json:"pool_id_hex"`
// ActiveEpochNo Block number on chain where transaction was included.
ActiveEpoch EpochNo `json:"active_epoch_no"`
// // ActiveStake Pool active stake.
// ActiveStake Lovelace `json:"active_stake"`
// // Total pool blocks on chain
// BlockCount uint64 `json:"block_count"`
// FixedCost Pool fixed cost per epoch
FixedCost decimal.Decimal `json:"fixed_cost"`
// // LiveDelegators Pool live delegator count
// LiveDelegators uint64 `json:"live_delegators"`
// // LiveSaturation Pool live saturation (decimal format)
// LiveSaturation float32 `json:"live_saturation"`
// // LiveStake Pool live stake
// LiveStake Lovelace `json:"live_stake"`
// Margin (decimal format)
Margin float32 `json:"margin"`
// MetaHash Pool metadata hash
MetaHash string `json:"meta_hash"`
// // MetaJson pool meta json
// MetaJSON PoolMeta `json:"meta_json"`
// MetaUrl Pool metadata URL
MetaURL string `json:"meta_url"`
// // OpCert Pool latest operational certificate hash
// OpCert string `json:"op_cert"`
// // OpCertCounter Pool latest operational certificate counter value
// OpCertCounter int `json:"op_cert_counter"`
// Owners of the pool.
Owners []Address `json:"owners"`
// Pledge pledge in lovelace.
Pledge decimal.Decimal `json:"pledge"`
// Pool status (registered | retiring | retired).
PoolStatus string `json:"pool_status"`
// Announced retiring epoch (nullable).
RetiringEpoch *EpochNo `json:"retiring_epoch.omitempty"`
// Pool reward address.
RewardAddr Address `json:"reward_addr"`
// Pool VRF key hash.
VrfKeyHash string `json:"vrf_key_hash"`
// Relays of the pool.
Relays []Relay `json:"relays"`
}
// PoolDelegator info.
PoolDelegator struct {
StakeAddress Address `json:"stake_address"`
Amount decimal.Decimal `json:"amount"`
ActiveEpochNo EpochNo `json:"active_epoch_no"`
LatestDelegationTxHash TxHash `json:"latest_delegation_tx_hash"`
}
PoolDelegatorHistory struct {
StakeAddress Address `json:"stake_address"`
Amount decimal.Decimal `json:"amount"`
EpochNo EpochNo `json:"epoch_no"`
}
// PoolRelays list item.
PoolRelays struct {
PoolIDBech32 PoolID `json:"pool_id_bech32"`
Relays []Relay `json:"relays"`
PoolStatus string `json:"pool_status"`
}
// PoolBlockInfo block info.
PoolBlockInfo struct {
// Slot is overall slot number (slots from genesis block of chain).
AbsSlot Slot `json:"abs_slot"`
// Hash block hash
BlockHash BlockHash `json:"block_hash"`
// BlockHeight ogf the block
BlockHeight uint64 `json:"block_height"`
// Time time of the block.
BlockTime Timestamp `json:"block_time"`
// Epoch number.
EpochNo EpochNo `json:"epoch_no"`
// EpochSlot slot number within epoch.
EpochSlot Slot `json:"epoch_slot"`
}
// PoolHistory entry.
PoolHistory struct {
// Epoch number.
EpochNo EpochNo `json:"epoch_no"`
// ActiveStake Pool active stake.
ActiveStake decimal.Decimal `json:"active_stake"`
ActiveStakePCT float64 `json:"active_stake_pct"`
SaturationPCT float64 `json:"saturation_pct"`
BlockCNT int `json:"block_cnt"`
DelegatorCNT int `json:"delegator_cnt"`
Margin float64 `json:"margin"`
FixedCost decimal.Decimal `json:"fixed_cost"`
PoolFees decimal.Decimal `json:"pool_fees"`
DelegRewards decimal.Decimal `json:"deleg_rewards"`
MemberRewards decimal.Decimal `json:"member_rewards"`
EpochROS decimal.Decimal `json:"epoch_ros"`
}
PoolSnapshot struct {
Snapshot string `json:"snapshot"`
EpochNo EpochNo `json:"epoch_no"`
Nonce string `json:"nonce"`
PoolStake decimal.Decimal `json:"pool_stake"`
ActiveStake decimal.Decimal `json:"active_stake"`
}
PoolSnapshotResponse struct {
Response
Data []PoolSnapshot `json:"data"`
}
// PoolListResponse represents response from `/pool_list` endpoint.
PoolListResponse struct {
Response
Data []PoolListItem `json:"data"`
}
// PoolInfosResponse represents response from `/pool_info` endpoint.
PoolInfosResponse struct {
Response
Data []PoolInfo `json:"data"`
}
// PoolInfoResponse represents response from `/pool_info` endpoint.
// when requesting info about single pool.
PoolInfoResponse struct {
Response
Data *PoolInfo `json:"data"`
}
// PoolDelegatorsResponse represents response from `/pool_delegators` endpoint.
PoolDelegatorsResponse struct {
Response
Data []PoolDelegator `json:"data"`
}
PoolDelegatorsHistoryResponse struct {
Response
Data []PoolDelegatorHistory `json:"data"`
}
// PoolBlocksResponse represents response from `/pool_blocks` endpoint.
PoolBlocksResponse struct {
Response
Data []PoolBlockInfo `json:"data"`
}
// PoolUpdatesResponse represents response from `/pool_updates` endpoint.
PoolUpdatesResponse struct {
Response
Data []PoolUpdateInfo `json:"data"`
}
// PoolRelaysResponse represents response from `/pool_relays` endpoint.
PoolRelaysResponse struct {
Response
Data []PoolRelays `json:"data"`
}
// PoolMetadataResponse represents response from `/pool_metadata` endpoint.
PoolMetadataResponse struct {
Response
Data []PoolMetadata `json:"data"`
}
// PoolHistoryResponse represents response from `/pool_history` endpoint.
PoolHistoryResponse struct {
Response
Data []PoolHistory `json:"data"`
}
PoolRegistrationsResponse struct {
Response
Data []PoolRegistrationOrRetirement `json:"data"`
}
PoolRetirementsResponse struct {
Response
Data []PoolRegistrationOrRetirement `json:"data"`
}
)
// GetPoolList returns the list of all currently registered/retiring (not retired) pools.
func (c *Client) GetPoolList(
ctx context.Context,
opts *RequestOptions,
) (res *PoolListResponse, err error) {
res = &PoolListResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_list", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolInfo returns current pool status and details for a specified pool.
func (c *Client) GetPoolInfo(
ctx context.Context,
pid PoolID,
opts *RequestOptions,
) (res *PoolInfoResponse, err error) {
res = &PoolInfoResponse{}
rsp, err := c.GetPoolInfos(ctx, []PoolID{pid}, opts)
res.Response = rsp.Response
if len(rsp.Data) == 1 {
res.Data = &rsp.Data[0]
}
return
}
// GetPoolInfos returns current pool statuses and details for a specified list of pool ids.
func (c *Client) GetPoolInfos(
ctx context.Context,
pids []PoolID,
opts *RequestOptions,
) (res *PoolInfosResponse, err error) {
res = &PoolInfosResponse{}
if len(pids) == 0 {
err = ErrNoPoolID
res.applyError(nil, err)
return
}
rsp, err := c.request(ctx, &res.Response, "POST", "/pool_info", poolIdsPL(pids), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func (c *Client) GetPoolStakeSnapshot(
ctx context.Context,
pid PoolID,
opts *RequestOptions,
) (res *PoolSnapshotResponse, err error) {
res = &PoolSnapshotResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_pool_bech32", pid.String())
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_stake_snapshot", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolDelegators returns information about delegators
// by a given pool and optional epoch (current if omitted).
func (c *Client) GetPoolDelegators(
ctx context.Context,
pid PoolID,
opts *RequestOptions,
) (res *PoolDelegatorsResponse, err error) {
res = &PoolDelegatorsResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_pool_bech32", pid.String())
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_delegators", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func (c *Client) GetPoolDelegatorsHistory(
ctx context.Context,
pid PoolID,
epoch EpochNo,
opts *RequestOptions,
) (res *PoolDelegatorsHistoryResponse, err error) {
res = &PoolDelegatorsHistoryResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_pool_bech32", pid.String())
if epoch > 0 {
opts.QuerySet("_epoch_no", epoch.String())
}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_delegators_history", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolBlocks returns information about blocks minted by a given pool
// in current epoch (or _epoch_no if provided).
func (c *Client) GetPoolBlocks(
ctx context.Context,
pid PoolID,
epoch EpochNo,
opts *RequestOptions,
) (res *PoolBlocksResponse, err error) {
res = &PoolBlocksResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_pool_bech32", pid.String())
if epoch > 0 {
opts.QuerySet("_epoch_no", epoch.String())
}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_blocks", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolUpdates returns all pool updates for all pools or
// only updates for specific pool if specified.
func (c *Client) GetPoolUpdates(
ctx context.Context,
pid PoolID,
opts *RequestOptions,
) (res *PoolUpdatesResponse, err error) {
res = &PoolUpdatesResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
if pid.Valid() {
opts.QuerySet("_pool_bech32", pid.String())
}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_updates", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolRelays returns a list of registered relays
// for all currently registered/retiring (not retired) pools.
func (c *Client) GetPoolRelays(
ctx context.Context,
opts *RequestOptions,
) (res *PoolRelaysResponse, err error) {
res = &PoolRelaysResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_relays", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolMetadata returns Metadata(on & off-chain)
// for all currently registered/retiring (not retired) pools.
func (c *Client) GetPoolMetadata(
ctx context.Context,
pids []PoolID,
opts *RequestOptions,
) (res *PoolMetadataResponse, err error) {
res = &PoolMetadataResponse{}
rsp, err := c.request(ctx, &res.Response, "POST", "/pool_metadata", poolIdsPL(pids), opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPoolHistory returns information about pool stake, block and reward history
// in a given epoch _epoch_no (or all epochs that pool existed for, in descending
// order if no _epoch_no was provided).
func (c *Client) GetPoolHistory(
ctx context.Context,
pid PoolID,
epoch EpochNo,
opts *RequestOptions,
) (res *PoolHistoryResponse, err error) {
res = &PoolHistoryResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_pool_bech32", pid.String())
if epoch > 0 {
opts.QuerySet("_epoch_no", epoch.String())
}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_history", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func poolIdsPL(pids []PoolID) io.Reader {
var payload = struct {
PIDS []PoolID `json:"_pool_bech32_ids"`
}{pids}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}
func (c *Client) GetPoolRegistrations(
ctx context.Context,
epoch EpochNo,
opts *RequestOptions,
) (res *PoolRegistrationsResponse, err error) {
res = &PoolRegistrationsResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
if epoch > 0 {
opts.QuerySet("_epoch_no", epoch.String())
}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_registrations", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
func (c *Client) GetPoolRetirements(
ctx context.Context,
epoch EpochNo,
opts *RequestOptions,
) (res *PoolRetirementsResponse, err error) {
res = &PoolRetirementsResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
if epoch > 0 {
opts.QuerySet("_epoch_no", epoch.String())
}
rsp, err := c.request(ctx, &res.Response, "GET", "/pool_retirements", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}