forked from goat-systems/go-tezos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goTezosBlock.go
302 lines (246 loc) · 7.09 KB
/
goTezosBlock.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
package goTezos
import (
"strconv"
"strings"
)
//Takes a cycle number and returns a helper structure describing a snap shot on the tezos network.
func (this *GoTezos) GetSnapShot(cycle int) (SnapShot, error) {
var snapShotQuery SnapShotQuery
var snap SnapShot
var get string
currentCycle, err := this.GetCurrentCycle()
if err != nil {
return snap, err
}
snap.Cycle = cycle
strCycle := strconv.Itoa(cycle)
if cycle < currentCycle {
block, err := this.GetBlockAtLevel(cycle * 4096)
if err != nil {
return snap, err
}
get = "/chains/main/blocks/" + block.Hash + "/context/raw/json/cycle/" + strCycle
} else {
get = "/chains/main/blocks/head/context/raw/json/cycle/" + strCycle
}
resp, err := this.GetResponse(get,"{}")
if err != nil {
this.logger.Println("Could not get snap shot: " + err.Error())
return snap, err
}
snapShotQuery, err = unMarshelSnapShotQuery(resp.Bytes)
if err != nil {
this.logger.Println("Could not get snap shot: " + err.Error())
return snap, err
}
snap.Number = snapShotQuery.RollSnapShot
snap.AssociatedBlock = ((cycle - 7) * 4096) + (snapShotQuery.RollSnapShot+1)*256
snap.AssociatedHash, _ = this.GetBlockHashAtLevel(snap.AssociatedBlock)
return snap, nil
}
//Gets a list of all known snapshots to the network
func (this *GoTezos) GetAllCurrentSnapShots() ([]SnapShot, error) {
var snapShotArray []SnapShot
currentCycle, err := this.GetCurrentCycle()
if err != nil {
return snapShotArray, err
}
for i := 7; i <= currentCycle; i++ {
snapShot, err := this.GetSnapShot(i)
if err != nil {
return snapShotArray, err
}
snapShotArray = append(snapShotArray, snapShot)
}
return snapShotArray, nil
}
//Returns the head block from the Tezos RPC.
func (this *GoTezos) GetChainHead() (Block, error) {
var block Block
resp, err := this.GetResponse("/chains/main/blocks/head","{}")
if err != nil {
this.logger.Println("Could not get /chains/main/blocks/head: " + err.Error())
return block, err
}
block, err = unMarshelBlock(resp.Bytes)
if err != nil {
this.logger.Println("Could not get block head: " + err.Error())
}
return block, nil
}
//Returns the level, and the hash, of the head block.
func (this *GoTezos) GetBlockLevelHead() (int, string, error) {
block, err := this.GetChainHead()
if err != nil {
return block.Header.Level, block.Hash, err
}
return block.Header.Level, block.Hash, err
}
//Returns the hash of a block at a specific level.
func (this *GoTezos) GetBlockHashAtLevel(level int) (string, error) {
head, headHash, err := this.GetBlockLevelHead()
if err != nil {
return "", err
}
diffStr := strconv.Itoa(head - level)
getBlockByLevel := "/chains/main/blocks/" + headHash + "~" + diffStr
resp, err := this.GetResponse(getBlockByLevel,"{}")
if err != nil {
return "", err
}
block, err := unMarshelBlock(resp.Bytes)
if err != nil {
return "", err
}
return block.Hash, nil
}
//Returns a Block at a specific level
func (this *GoTezos) GetBlockAtLevel(level int) (Block, error) {
var block Block
head, headHash, err := this.GetBlockLevelHead()
if err != nil {
return block, err
}
diffStr := strconv.Itoa(head - level)
getBlockByLevel := "/chains/main/blocks/" + headHash + "~" + diffStr
resp, err := this.GetResponse(getBlockByLevel, "{}")
if err != nil {
return block, err
}
block, err = unMarshelBlock(resp.Bytes)
if err != nil {
return block, err
}
return block, nil
}
//Returns a Block by the identifier hash.
func (this *GoTezos) GetBlockByHash(hash string) (Block, error) {
var block Block
getBlockByLevel := "/chains/main/blocks/" + hash
resp, err := this.GetResponse(getBlockByLevel,"{}")
if err != nil {
return block, err
}
block, err = unMarshelBlock(resp.Bytes)
if err != nil {
return block, err
}
return block, nil
}
//Gets the balance of a public key hash at a specific snapshot for a cycle.
func (this *GoTezos) GetAccountBalanceAtSnapshot(tezosAddr string, cycle int) (float64, error) {
snapShot, err := this.GetSnapShot(cycle)
if err != nil {
return 0, err
}
hash, err := this.GetBlockHashAtLevel(snapShot.AssociatedBlock)
if err != nil {
return 0, err
}
balanceCmdStr := "/chains/main/blocks/" + hash + "/context/contracts/" + tezosAddr + "/balance"
resp, err := this.GetResponse(balanceCmdStr,"{}")
if err != nil {
return 0, err
}
strBalance, err := unMarshelString(resp.Bytes)
if err != nil {
return 0, err
}
floatBalance, err := strconv.ParseFloat(strBalance, 64)
if err != nil {
return 0, err
}
return floatBalance / 1000000, nil
}
//Gets the balance of a public key hash at a specific snapshot for a cycle.
func (this *GoTezos) GetAccountBalance(tezosAddr string) (float64, error) {
balanceCmdStr := "/chains/main/blocks/head/context/contracts/" + tezosAddr + "/balance"
resp, err := this.GetResponse(balanceCmdStr,"{}")
if err != nil {
return 0, err
}
strBalance, err := unMarshelString(resp.Bytes)
if err != nil {
return 0, err
}
floatBalance, err := strconv.ParseFloat(strBalance, 64)
if err != nil {
return 0, err
}
return floatBalance / 1000000, nil
}
//Gets the staking balance for a delegate at a specific snapshot for a cycle.
func (this *GoTezos) GetDelegateStakingBalance(delegateAddr string, cycle int) (float64, error) {
var snapShot SnapShot
var err error
var hash string
snapShot, err = this.GetSnapShot(cycle)
if err != nil {
return 0, err
}
hash, err = this.GetBlockHashAtLevel(snapShot.AssociatedBlock)
if err != nil {
return 0, err
}
rpcCall := "/chains/main/blocks/" + hash + "/context/delegates/" + delegateAddr + "/staking_balance"
resp, err := this.GetResponse(rpcCall,"{}")
if err != nil {
return 0, err
}
strBalance, err := unMarshelString(resp.Bytes)
if err != nil {
return 0, err
}
floatBalance, err := strconv.ParseFloat(strBalance, 64) //TODO error checking
if err != nil {
return 0, err
}
return floatBalance / 1000000, nil
}
//Gets the current cycle of the chain
func (this *GoTezos) GetCurrentCycle() (int, error) {
block, err := this.GetChainHead()
if err != nil {
return 0, err
}
var cycle int
cycle = block.Header.Level / 4096
return cycle, nil
}
//Get the balance of an address at a specific hash
func (this *GoTezos) GetAccountBalanceAtBlock(tezosAddr string, hash string) (int, error) {
var balance string
balanceCmdStr := "/chains/main/blocks/" + hash + "/context/contracts/" + tezosAddr + "/balance"
resp, err := this.GetResponse(balanceCmdStr,"{}")
if err != nil {
return 0, err
}
balance, err = unMarshelString(resp.Bytes)
if err != nil {
return 0, err
}
var returnBalance int
if strings.Contains(balance, "No service found at this URL") {
returnBalance = 0
}
if len(balance) < 1 {
returnBalance = 0
} else {
floatBalance, _ := strconv.Atoi(balance) //TODO error checking
returnBalance = int(floatBalance)
}
return returnBalance, nil
}
//Gets the ID of the chain with the most fitness
func (this *GoTezos) GetChainId() (string, error) {
chainIdCmd := "/chains/main/chain_id"
resp, err := this.GetResponse(chainIdCmd,"{}")
if err != nil {
return "", err
}
chainId, err := unMarshelString(resp.Bytes)
if err != nil {
return "", err
}
return chainId, nil
}