forked from blockwatch-cc/tzstats-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaker.go
220 lines (197 loc) · 8.48 KB
/
baker.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package tzstats
import (
"context"
"fmt"
"strconv"
"time"
"blockwatch.cc/tzgo/tezos"
)
type Baker struct {
Address tezos.Address `json:"address"`
BakerSince time.Time `json:"baker_since_time"`
BakerUntil *time.Time `json:"baker_until,omitempty"`
GracePeriod int64 `json:"grace_period"`
BakerVersion string `json:"baker_version"`
TotalBalance float64 `json:"total_balance"`
SpendableBalance float64 `json:"spendable_balance"`
FrozenBalance float64 `json:"frozen_balance"`
DelegatedBalance float64 `json:"delegated_balance"`
StakingBalance float64 `json:"staking_balance"`
StakingCapacity float64 `json:"staking_capacity"`
DepositsLimit *float64 `json:"deposits_limit"`
StakingShare float64 `json:"staking_share"`
ActiveDelegations int64 `json:"active_delegations"`
IsFull bool `json:"is_full"`
IsActive bool `json:"is_active"`
Events *BakerEvents `json:"events,omitempty"`
Stats *BakerStatistics `json:"stats,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}
type BakerStatistics struct {
TotalRewardsEarned float64 `json:"total_rewards_earned"`
TotalFeesEarned float64 `json:"total_fees_earned"`
TotalLost float64 `json:"total_lost"`
BlocksBaked int64 `json:"blocks_baked"`
BlocksProposed int64 `json:"blocks_proposed"`
SlotsEndorsed int64 `json:"slots_endorsed"`
AvgLuck64 int64 `json:"avg_luck_64"`
AvgPerformance64 int64 `json:"avg_performance_64"`
AvgContribution64 int64 `json:"avg_contribution_64"`
NBakerOps int64 `json:"n_baker_ops"`
NProposal int64 `json:"n_proposals"`
NBallot int64 `json:"n_ballots"`
NEndorsement int64 `json:"n_endorsements"`
NPreendorsement int64 `json:"n_preendorsements"`
NSeedNonce int64 `json:"n_nonce_revelations"`
N2Baking int64 `json:"n_double_bakings"`
N2Endorsement int64 `json:"n_double_endorsements"`
NSetDepositsLimit int64 `json:"n_set_limits"`
}
type BakerEvents struct {
LastBakeHeight int64 `json:"last_bake_height"`
LastBakeBlock string `json:"last_bake_block"`
LastBakeTime time.Time `json:"last_bake_time"`
LastEndorseHeight int64 `json:"last_endorse_height"`
LastEndorseBlock string `json:"last_endorse_block"`
LastEndorseTime time.Time `json:"last_endorse_time"`
NextBakeHeight int64 `json:"next_bake_height"`
NextBakeTime time.Time `json:"next_bake_time"`
NextEndorseHeight int64 `json:"next_endorse_height"`
NextEndorseTime time.Time `json:"next_endorse_time"`
}
type CycleIncome struct {
Address tezos.Address `json:"address"`
Cycle int64 `json:"cycle"`
Rolls int64 `json:"snapshot_rolls"`
Balance float64 `json:"own_balance"`
Delegated float64 `json:"delegated_balance"`
Staking float64 `json:"staking_balance"`
NDelegations int64 `json:"n_delegations"`
NBakingRights int64 `json:"n_baking_rights"`
NEndorsingRights int64 `json:"n_endorsing_rights"`
Luck float64 `json:"luck"`
LuckPct int64 `json:"luck_percent"`
ContributionPct int64 `json:"contribution_percent"`
PerformancePct int64 `json:"performance_percent"`
NBlocksBaked int64 `json:"n_blocks_baked"`
NBlocksProposed int64 `json:"n_blocks_proposed"`
NSlotsEndorsed int64 `json:"n_slots_endorsed"`
NSeedsRevealed int64 `json:"n_seeds_revealed"`
ExpectedIncome float64 `json:"expected_income"`
TotalIncome float64 `json:"total_income"`
TotalBonds float64 `json:"total_bonds"`
BakingIncome float64 `json:"baking_income"`
EndorsingIncome float64 `json:"endorsing_income"`
AccusationIncome float64 `json:"accusation_income"`
SeedIncome float64 `json:"seed_income"`
FeesIncome float64 `json:"fees_income"`
TotalLoss float64 `json:"total_loss"`
AccusationLoss float64 `json:"accusation_loss"`
SeedLoss float64 `json:"seed_loss"`
EndorsingLoss float64 `json:"endorsing_loss"`
}
type Delegator struct {
Address tezos.Address `json:"address"`
Balance float64 `json:"balance"`
}
type CycleSnapshot struct {
BakeCycle int64 `json:"baking_cycle"`
Height int64 `json:"snapshot_height"`
Cycle int64 `json:"snapshot_cycle"`
Timestamp time.Time `json:"snapshot_time"`
Index int `json:"snapshot_index"`
Rolls int64 `json:"snapshot_rolls"`
StakingBalance float64 `json:"staking_balance"`
OwnBalance float64 `json:"own_balance"`
DelegatedBalance float64 `json:"delegated_balance"`
NDelegations int64 `json:"n_delegations"`
Delegators []Delegator `json:"delegators"`
}
type BakerParams struct {
Params
}
func NewBakerParams() BakerParams {
return BakerParams{NewParams()}
}
func (p BakerParams) WithLimit(v uint) BakerParams {
p.Query.Set("limit", strconv.Itoa(int(v)))
return p
}
func (p BakerParams) WithOffset(v uint) BakerParams {
p.Query.Set("offset", strconv.Itoa(int(v)))
return p
}
func (p BakerParams) WithCursor(v uint) BakerParams {
p.Query.Set("cursor", strconv.Itoa(int(v)))
return p
}
func (p BakerParams) WithMeta() BakerParams {
p.Query.Set("meta", "1")
return p
}
func (c *Client) GetBaker(ctx context.Context, addr tezos.Address, params BakerParams) (*Baker, error) {
b := &Baker{}
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s", addr))
if err := c.get(ctx, u, nil, b); err != nil {
return nil, err
}
return b, nil
}
func (c *Client) ListBakers(ctx context.Context, params BakerParams) ([]*Baker, error) {
b := make([]*Baker, 0)
u := params.AppendQuery("/explorer/bakers")
if err := c.get(ctx, u, nil, &b); err != nil {
return nil, err
}
return b, nil
}
func (c *Client) ListBakerVotes(ctx context.Context, addr tezos.Address, params OpParams) ([]*Ballot, error) {
cc := make([]*Ballot, 0)
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s/votes", addr))
if err := c.get(ctx, u, nil, &cc); err != nil {
return nil, err
}
return cc, nil
}
func (c *Client) ListBakerEndorsements(ctx context.Context, addr tezos.Address, params OpParams) ([]*Op, error) {
ops := make([]*Op, 0)
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s/endorsements", addr))
if err := c.get(ctx, u, nil, &ops); err != nil {
return nil, err
}
return ops, nil
}
func (c *Client) ListBakerDelegations(ctx context.Context, addr tezos.Address, params OpParams) ([]*Op, error) {
ops := make([]*Op, 0)
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s/delegations", addr))
if err := c.get(ctx, u, nil, &ops); err != nil {
return nil, err
}
return ops, nil
}
func (c *Client) ListBakerRights(ctx context.Context, addr tezos.Address, cycle int64, params BakerParams) (*CycleRights, error) {
var r CycleRights
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s/rights/%d", addr, cycle))
if err := c.get(ctx, u, nil, &r); err != nil {
return nil, err
}
return &r, nil
}
func (c *Client) GetBakerIncome(ctx context.Context, addr tezos.Address, cycle int64, params BakerParams) (*CycleIncome, error) {
var r CycleIncome
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s/income/%d", addr, cycle))
if err := c.get(ctx, u, nil, &r); err != nil {
return nil, err
}
return &r, nil
}
func (c *Client) GetBakerSnapshot(ctx context.Context, addr tezos.Address, cycle int64, params BakerParams) (*CycleSnapshot, error) {
var r CycleSnapshot
u := params.AppendQuery(fmt.Sprintf("/explorer/bakers/%s/snapshot/%d", addr, cycle))
if err := c.get(ctx, u, nil, &r); err != nil {
return nil, err
}
return &r, nil
}