/
pool.go
534 lines (432 loc) · 13.6 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
// Copyright 2022 The Cardano Community Authors
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
// or LICENSE file in repository root.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package koios
import (
"context"
"encoding/json"
"io"
)
// introduces breaking change since v1.3.0
type (
// PoolListItem defines model for pool list item.
PoolListItem struct {
// PoolID Bech32 representation of pool ID.
PoolID PoolID `json:"pool_id_bech32"`
// Ticker of Pool.
Ticker *string `json:"ticker,omitempty"`
}
// 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"`
}
// 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 {
// 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 Lovelace `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"`
// LivePledge Pool live pledge
LivePledge Lovelace `json:"live_pledge"`
// Margin (decimal format)
Margin float32 `json:"margin"`
// MetaHash Pool metadata hash
MetaHash string `json:"meta_hash"`
// MetaJson pool meta json
MetaJSON PoolMetaJSON `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 []StakeAddress `json:"owners"`
// Pledge pledge in lovelace
Pledge Lovelace `json:"pledge"`
// ID (bech32 format)
ID PoolID `json:"pool_id_bech32"`
// IDHex Pool ID (Hex format)
IDHex string `json:"pool_id_hex"`
// Pool status (registered | retiring | retired)
Status string `json:"pool_status"`
// Announced retiring epoch (nullable)
RetiringEpoch *EpochNo `json:"retiring_epoch"`
// Pool reward address
RewardAddr StakeAddress `json:"reward_addr"`
// Pool VRF key hash
VrfKeyHash string `json:"vrf_key_hash"`
// Relays of the pool
Relays []Relay `json:"relays"`
}
// PoolUpdateInfo response item from `/pool_updates`.
PoolUpdateInfo struct {
// TxHash update transaction
TxHash TxHash `json:"tx_hash"`
// Time time of the block.
BlockTime Time `json:"block_time"`
// ID (bech32 format)
ID PoolID `json:"pool_id_bech32"`
// IDHex Pool ID (Hex format)
IDHex 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 Lovelace `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 []StakeAddress `json:"owners"`
// Pledge pledge in lovelace.
Pledge Lovelace `json:"pledge"`
// Pool status (registered | retiring | retired).
Status string `json:"pool_status"`
// Announced retiring epoch (nullable).
RetiringEpoch *EpochNo `json:"retiring_epoch"`
// Pool reward address.
RewardAddr StakeAddress `json:"reward_addr"`
// Pool VRF key hash.
VrfKeyHash string `json:"vrf_key_hash"`
// Relays of the pool.
Relays []PoolRelays `json:"relays"`
}
// PoolDelegator info.
PoolDelegator struct {
StakeAddress StakeAddress `json:"stake_address"`
Amount Lovelace `json:"amount"`
Epoch EpochNo `json:"active_epoch_no"`
}
// PoolRelays list item.
PoolRelays struct {
PoolID PoolID `json:"pool_id_bech32"`
// Relays of the pool.
Relays []Relay `json:"relays"`
}
// PoolBlockInfo block info.
PoolBlockInfo struct {
// Slot is overall slot number (slots from genesis block of chain).
AbsSlot uint64 `json:"abs_slot"`
// Hash block hash
Hash BlockHash `json:"block_hash"`
// BlockHeight ogf the block
Height uint64 `json:"block_height"`
// Time time of the block.
Time Time `json:"block_time"`
// Epoch number.
Epoch EpochNo `json:"epoch_no"`
// EpochSlot slot number within epoch.
EpochSlot uint64 `json:"epoch_slot"`
}
// PoolHistory entry.
PoolHistory struct {
// Epoch number.
Epoch EpochNo `json:"epoch_no"`
// ActiveStake Pool active stake.
ActiveStake Lovelace `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 Lovelace `json:"fixed_cost"`
PoolFees Lovelace `json:"pool_fees"`
DelegRewards Lovelace `json:"deleg_rewards"`
EpochROS float64 `json:"epoch_ros"`
}
// PoolListResponse represents response from `/pool_list` endpoint.
PoolListResponse struct {
Response
Data []PoolListItem `json:"response"`
}
// PoolInfosResponse represents response from `/pool_info` endpoint.
PoolInfosResponse struct {
Response
Data []PoolInfo `json:"response"`
}
// PoolInfoResponse represents response from `/pool_info` endpoint.
// when requesting info about single pool.
PoolInfoResponse struct {
Response
Data *PoolInfo `json:"response"`
}
// PoolDelegatorsResponse represents response from `/pool_delegators` endpoint.
PoolDelegatorsResponse struct {
Response
Data []PoolDelegator `json:"response"`
}
// PoolBlocksResponse represents response from `/pool_blocks` endpoint.
PoolBlocksResponse struct {
Response
Data []PoolBlockInfo `json:"response"`
}
// PoolUpdatesResponse represents response from `/pool_updates` endpoint.
PoolUpdatesResponse struct {
Response
Data []PoolUpdateInfo `json:"response"`
}
// PoolRelaysResponse represents response from `/pool_relays` endpoint.
PoolRelaysResponse struct {
Response
Data []PoolRelays `json:"response"`
}
// PoolMetadataResponse represents response from `/pool_metadata` endpoint.
PoolMetadataResponse struct {
Response
Data []PoolMetadata `json:"response"`
}
// PoolHistoryResponse represents response from `/pool_history` endpoint.
PoolHistoryResponse struct {
Response
Data []PoolHistory `json:"response"`
}
)
// 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
}
// GetPoolDelegators returns information about delegators
// by a given pool and optional epoch (current if omitted).
func (c *Client) GetPoolDelegators(
ctx context.Context,
pid PoolID,
epoch *EpochNo,
opts *RequestOptions,
) (res *PoolDelegatorsResponse, err error) {
res = &PoolDelegatorsResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_pool_bech32", pid.String())
if epoch != nil {
opts.QuerySet("_epoch_no", epoch.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
}
// 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 != nil {
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 != nil {
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 != nil {
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
}