/
delegates.go
112 lines (98 loc) · 3.44 KB
/
delegates.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"context"
"fmt"
"strconv"
"blockwatch.cc/tzgo/tezos"
)
// Delegate holds information about an active delegate
type Delegate struct {
// extra info
Delegate tezos.Address `json:"-"`
Height int64 `json:"-"`
Block string `json:"-"`
// tezos data
Deactivated bool `json:"deactivated"`
Balance int64 `json:"balance,string"`
DelegatedContracts []tezos.Address `json:"delegated_contracts"`
FrozenBalance int64 `json:"frozen_balance,string"`
FrozenBalanceByCycle []CycleBalance `json:"frozen_balance_by_cycle"`
GracePeriod int64 `json:"grace_period"`
StakingBalance int64 `json:"staking_balance,string"`
DelegatedBalance int64 `json:"delegated_balance,string"`
VotingPower Int64orString `json:"voting_power"`
// v012+
FullBalance int64 `json:"full_balance,string"`
FrozenDeposits int64 `json:"frozen_deposits,string"`
CurrentFrozenDeposits int64 `json:"current_frozen_deposits,string"`
FrozenDepositsLimit int64 `json:"frozen_deposits_limit,string"`
// v015+
ActiveConsensusKey tezos.Address `json:"active_consensus_key"`
PendingConsensusKeys []CycleKey `json:"pending_consensus_keys"`
}
type CycleKey struct {
Cycle int64 `json:"cycle"`
Pkh tezos.Address `json:"pkh"`
}
type CycleBalance struct {
Cycle int64 `json:"cycle"`
Deposit int64 `json:"deposit,string"`
Fees int64 `json:"fees,string"`
Rewards int64 `json:"rewards,string"`
}
// DelegateList contains a list of delegates
type DelegateList []tezos.Address
// ListActiveDelegates returns information about all active delegates at a block.
func (c *Client) ListActiveDelegates(ctx context.Context, id BlockID) (DelegateList, error) {
p, err := c.GetParams(ctx, id)
if err != nil {
return nil, err
}
selector := "active=true"
if p.Version >= 13 {
selector = "with_minimal_stake=true"
}
delegates := make(DelegateList, 0)
u := fmt.Sprintf("chains/main/blocks/%s/context/delegates?%s", id, selector)
if err := c.Get(ctx, u, &delegates); err != nil {
return nil, err
}
return delegates, nil
}
// GetDelegate returns information about a delegate at a specific height.
func (c *Client) GetDelegate(ctx context.Context, addr tezos.Address, id BlockID) (*Delegate, error) {
delegate := &Delegate{
Delegate: addr,
Height: id.Int64(),
Block: id.String(),
}
u := fmt.Sprintf("chains/main/blocks/%s/context/delegates/%s", id, addr)
if err := c.Get(ctx, u, &delegate); err != nil {
return nil, err
}
return delegate, nil
}
// GetDelegateBalance returns a delegate's balance
func (c *Client) GetDelegateBalance(ctx context.Context, addr tezos.Address, id BlockID) (int64, error) {
u := fmt.Sprintf("chains/main/blocks/%s/context/delegates/%s/balance", id, addr)
var bal string
err := c.Get(ctx, u, &bal)
if err != nil {
return 0, err
}
return strconv.ParseInt(bal, 10, 64)
}
// GetDelegateKey returns a delegate's current consensus key
func (c *Client) GetDelegateKey(ctx context.Context, addr tezos.Address, id BlockID) (tezos.Key, error) {
u := fmt.Sprintf("chains/main/blocks/%s/context/delegates/%s/consensus_key", id, addr)
type ActiveConsensusKey struct {
Active struct {
Pk tezos.Key `json:"pk"`
} `json:"active"`
}
var key ActiveConsensusKey
err := c.Get(ctx, u, &key)
return key.Active.Pk, err
}