forked from goat-systems/go-tezos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goTezosDelegation.go
332 lines (285 loc) · 10.2 KB
/
goTezosDelegation.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
package goTezos
import "strconv"
//A function that retrieves a list of all currently delegated contracts for a delegate.
func (this *GoTezos) GetDelegationsForDelegate(delegatePhk string) ([]string, error) {
var rtnString []string
getDelegations := "/chains/main/blocks/head/context/delegates/" + delegatePhk + "/delegated_contracts"
resp, err := this.GetResponse(getDelegations,"{}")
if err != nil {
return rtnString, err
}
delegations, err := unMarshelStringArray(resp.Bytes)
if err != nil {
return rtnString, err
}
return delegations, nil
}
//A function that retrieves a list of all currently delegated contracts for a delegate at a specific cycle.
func (this *GoTezos) GetDelegationsForDelegateByCycle(delegatePhk string, cycle int) ([]string, error) {
var rtnString []string
snapShot, err := this.GetSnapShot(cycle)
if err != nil {
return rtnString, err
}
hash, err := this.GetBlockHashAtLevel(snapShot.AssociatedBlock)
if err != nil {
return rtnString, err
}
getDelegations := "/chains/main/blocks/" + hash + "/context/delegates/" + delegatePhk + "/delegated_contracts"
resp, err := this.GetResponse(getDelegations,"{}")
if err != nil {
return rtnString, err
}
delegations, err := unMarshelStringArray(resp.Bytes)
if err != nil {
return rtnString, err
}
return delegations, nil
}
//Gets the total rewards for a delegate earned and calculates the gross rewards earned by each delegation for multiple cycles. Also includes the share of each delegation.
func (this *GoTezos) GetRewardsForDelegateForCycles(delegatePhk string, cycleStart int, cycleEnd int) (DelegationServiceRewards, error) {
dgRewards := DelegationServiceRewards{}
dgRewards.DelegatePhk = delegatePhk
var cycleRewardsArray []CycleRewards
for cycleStart <= cycleEnd {
delegations, err := this.getCycleRewards(delegatePhk, cycleStart)
if err != nil {
return dgRewards, err
}
cycleRewardsArray = append(cycleRewardsArray, delegations)
cycleStart++
}
dgRewards.RewardsByCycle = cycleRewardsArray
return dgRewards, nil
}
//Gets the total rewards for a delegate earned and calculates the gross rewards earned by each delegation for a single cycle. Also includes the share of each delegation.
func (this *GoTezos) GetRewardsForDelegateCycle(delegatePhk string, cycle int) (DelegationServiceRewards, error) {
dgRewards := DelegationServiceRewards{}
dgRewards.DelegatePhk = delegatePhk
delegations, err := this.getCycleRewards(delegatePhk, cycle)
if err != nil {
return dgRewards, err
}
dgRewards.RewardsByCycle = append(dgRewards.RewardsByCycle, delegations)
return dgRewards, nil
}
//Get total rewards for a cycle for a delegate
func (this *GoTezos) getCycleRewards(delegatePhk string, cycle int) (CycleRewards, error) {
cycleRewards := CycleRewards{}
cycleRewards.Cycle = cycle
rewards, err := this.GetDelegateRewardsForCycle(delegatePhk, cycle)
if err != nil {
return cycleRewards, err
}
if rewards == "" {
rewards = "0"
}
cycleRewards.TotalRewards = rewards
contractRewards, err := this.getContractRewardsForDelegate(delegatePhk, cycleRewards.TotalRewards, cycle)
if err != nil {
return cycleRewards, err
}
cycleRewards.Delegations = contractRewards
return cycleRewards, nil
}
//A function that gets the rewards earned by a delegate for a specific cycle.
func (this *GoTezos) GetDelegateRewardsForCycle(delegatePhk string, cycle int) (string, error) {
get := "/chains/main/blocks/head/context/raw/json/contracts/index/" + delegatePhk + "/frozen_balance/" + strconv.Itoa(cycle) + "/"
resp, err := this.GetResponse(get,"{}")
if err != nil {
return "", err
}
rewards, err := unMarshelFrozenBalanceRewards(resp.Bytes)
if err != nil {
return rewards.Rewards, err
}
return rewards.Rewards, nil
}
//A private function to fill out delegation data like gross rewards and share.
func (this *GoTezos) getContractRewardsForDelegate(delegatePhk, totalRewards string, cycle int) ([]ContractRewards, error) {
var contractRewards []ContractRewards
delegations, err := this.GetDelegationsForDelegateByCycle(delegatePhk, cycle)
if err != nil {
return contractRewards, err
}
for _, contract := range delegations {
contractReward := ContractRewards{}
contractReward.DelegationPhk = contract
bigIntRewards, err := strconv.Atoi(totalRewards)
if err != nil {
return contractRewards, err
}
floatRewards := float64(bigIntRewards) / 1000000
share, err := this.GetShareOfContract(delegatePhk, contract, cycle)
if err != nil {
return contractRewards, err
}
contractReward.Share = share
bigIntGrossRewards := int((share * floatRewards) * 1000000)
strGrossRewards := strconv.Itoa(bigIntGrossRewards)
contractReward.GrossRewards = strGrossRewards
contractRewards = append(contractRewards, contractReward)
}
return contractRewards, nil
}
//Returns the share of a delegation for a specific cycle.
func (this *GoTezos) GetShareOfContract(delegatePhk, delegationPhk string, cycle int) (float64, error) {
stakingBalance, err := this.GetDelegateStakingBalance(delegatePhk, cycle)
if err != nil {
return 0, err
}
delegationBalance, err := this.GetAccountBalanceAtSnapshot(delegationPhk, cycle)
if err != nil {
return 0, err
}
return delegationBalance / stakingBalance, nil
}
//RPC command to retrieve information about a delegate at the head block
func (this *GoTezos) GetDelegate(delegatePhk string) (Delegate, error) {
var delegate Delegate
get := "/chains/main/blocks/head/context/delegates/" + delegatePhk
resp, err := this.GetResponse(get,"{}")
if err != nil {
return delegate, err
}
delegate, err = unMarshelDelegate(resp.Bytes)
if err != nil {
return delegate, err
}
return delegate, err
}
//RPC command to get the staking balance of a delegate at a specific cycle
func (this *GoTezos) GetStakingBalanceAtCycle(cycle int, delegateAddr string) (string, error) {
var balance string
snapShot, err := this.GetSnapShot(cycle)
if err != nil {
return balance, err
}
get := "/chains/main/blocks/" + snapShot.AssociatedHash + "/context/delegates/" + delegateAddr + "/staking_balance"
resp, err := this.GetResponse(get,"{}")
if err != nil {
return balance, err
}
balance, err = unMarshelString(resp.Bytes)
if err != nil {
return balance, err
}
return balance, nil
}
//Gets the baking rights for a specific cycle
func (this *GoTezos) GetBakingRights(cycle int) (Baking_Rights, error) {
var bakingRights Baking_Rights
get := "/chains/main/blocks/head/helpers/baking_rights?cycle=" + strconv.Itoa(cycle) + "?max_priority=4"
resp, err := this.GetResponse(get,"{}")
if err != nil {
return bakingRights, err
}
bakingRights, err = unMarshelBakingRights(resp.Bytes)
if err != nil {
return bakingRights, err
}
return bakingRights, nil
}
func (this *GoTezos) GetBakingRightsForDelegate(cycle int, delegatePhk string, priority int) (Baking_Rights, error) {
var bakingRights Baking_Rights
get := "/chains/main/blocks/head/helpers/baking_rights?cycle=" + strconv.Itoa(cycle) + "&max_priority=" + strconv.Itoa(priority) + "&delegate=" + delegatePhk
resp, err := this.GetResponse(get,"{}")
if err != nil {
return bakingRights, err
}
bakingRights, err = unMarshelBakingRights(resp.Bytes)
if err != nil {
return bakingRights, err
}
return bakingRights, nil
}
func (this *GoTezos) GetBakingRightsForDelegateForCycles(cycleStart int, cycleEnd int, delegatePhk string, priority int) ([]Baking_Rights, error) {
var bakingRights []Baking_Rights
for cycleStart <= cycleEnd {
get := "/chains/main/blocks/head/helpers/baking_rights?cycle=" + strconv.Itoa(cycleStart) + "&max_priority=" + strconv.Itoa(priority) + "&delegate=" + delegatePhk
resp, err := this.GetResponse(get,"{}")
if err != nil {
return bakingRights, err
}
bakingRight, err := unMarshelBakingRights(resp.Bytes)
if err != nil {
return bakingRights, err
}
bakingRights = append(bakingRights, bakingRight)
cycleStart++
}
return bakingRights, nil
}
//Gets the endorsing rights for a specific cycle
func (this *GoTezos) GetEndorsingRightsForDelegate(cycle int, delegatePhk string) (Endorsing_Rights, error) {
var endorsingRights Endorsing_Rights
get := "/chains/main/blocks/head/helpers/endorsing_rights?cycle=" + strconv.Itoa(cycle) + "&delegate=" + delegatePhk
resp, err := this.GetResponse(get,"{}")
if err != nil {
return endorsingRights, err
}
endorsingRights, err = unMarshelEndorsingRights(resp.Bytes)
if err != nil {
return endorsingRights, err
}
return endorsingRights, nil
}
func (this *GoTezos) GetEndorsingRightsForDelegateForCycles(cycleStart int, cycleEnd int, delegatePhk string) ([]Endorsing_Rights, error) {
var endorsingRights []Endorsing_Rights
for cycleStart <= cycleEnd {
get := "/chains/main/blocks/head/helpers/endorsing_rights?cycle=" + strconv.Itoa(cycleStart) + "&delegate=" + delegatePhk
resp, err := this.GetResponse(get,"{}")
if err != nil {
return endorsingRights, err
}
endorsingRight, err := unMarshelEndorsingRights(resp.Bytes)
if err != nil {
return endorsingRights, err
}
endorsingRights = append(endorsingRights, endorsingRight)
cycleStart++
}
return endorsingRights, nil
}
//Gets the endorsing rights for a specific cycle
func (this *GoTezos) GetEndorsingRights(cycle int) (Endorsing_Rights, error) {
var endorsingRights Endorsing_Rights
get := "/chains/main/blocks/head/helpers/endorsing_rights?cycle=" + strconv.Itoa(cycle)
resp, err := this.GetResponse(get,"{}")
if err != nil {
return endorsingRights, err
}
endorsingRights, err = unMarshelEndorsingRights(resp.Bytes)
if err != nil {
return endorsingRights, err
}
return endorsingRights, nil
}
//Retrieves a list of all tz1 addresses at a certain hash
func (this *GoTezos) GetAllDelegatesByHash(hash string) ([]string, error) {
var delList []string
get := "/chains/main/blocks" + hash + "/context/delegates?active"
resp, err := this.GetResponse(get,"{}")
if err != nil {
return delList, err
}
delList, err = unMarshelStringArray(resp.Bytes)
if err != nil {
return delList, err
}
return delList, nil
}
//Retrieves a list of all tz1 addresses
func (this *GoTezos) GetAllDelegates() ([]string, error) {
var delList []string
get := "/chains/main/blocks/head/context/delegates?active"
resp, err := this.GetResponse(get,"{}")
if err != nil {
return delList, err
}
delList, err = unMarshelStringArray(resp.Bytes)
if err != nil {
return delList, err
}
return delList, nil
}