This repository was archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
422 lines (370 loc) · 11.3 KB
/
client.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
package metahash
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
const torUrl = "http://tor.net-main.metahashnetwork.com:5795"
const totalSupplyUrl = "https://app.metahash.io/api/stat/?method=supply"
var metahashClient RPCClient
func init() {
metahashClient = NewClient(torUrl)
}
// FetchBalance gets the balance information of a given address
func FetchBalance(address string) (*Balance, error) {
responseBalance, err := metahashClient.Call("fetch-balance", &BalanceArgs{Address: address})
if err == nil {
var resultBalance *Balance
err = responseBalance.GetObject(&resultBalance)
if err == nil {
return resultBalance, nil
}
return nil, err
}
return nil, err
}
// FetchBalances gets the balance information of list of addresses
func FetchBalances(addresses ...string) ([]*Balance, error) {
responseBalance, err := metahashClient.Call("fetch-balances", &BalancesArgs{Addresses: addresses})
if err == nil {
var resultBalances []*Balance
err = responseBalance.GetObject(&resultBalances)
if err == nil {
return resultBalances, nil
}
return nil, err
}
return nil, err
}
// FetchHistory returns all transactions history of a given address
func FetchHistory(address string) ([]*TransactionInfo, error) {
responseHistory, err := metahashClient.Call("fetch-history", &HistoryArgs{Address: address})
if err == nil {
var resultHistory []*TransactionInfo
err = responseHistory.GetObject(&resultHistory)
if err == nil {
return resultHistory, nil
}
return nil, err
}
return nil, err
}
//FetchHistoryRange returns list of transaction history from a given index
func FetchHistoryFiter(address string, startIndex, numTrx int64) ([]*TransactionInfo, error) {
responseHistory, err := metahashClient.Call("fetch-history", &HistoryArgs{Address: address, BeginTx: startIndex, CountTxs: numTrx})
if err == nil {
var resultHistory []*TransactionInfo
err = responseHistory.GetObject(&resultHistory)
if err == nil {
return resultHistory, nil
}
return nil, err
}
return nil, err
}
/*
//This function is not working because of the metahash api error
//FetchHistoryFilter returns list of transaction history based on the provide filter
func FetchHistoryFilter(address string, filter *HistoryFilter) ([]*TransactionInfo, error) {
responseHistory, err := metahashClient.Call("fetch-history-filter", &HistoryArgs{Address: address, Filters: *filter})
if err == nil {
pp.Println("response", responseHistory) //TODO: check ifthe history is not nil
var resultHistory []*TransactionInfo
err = responseHistory.GetObject(&resultHistory)
if err == nil {
return resultHistory, nil
}
return nil, err
}
return nil, err
}*/
// GetTransaction returns the transaction details given the transaction hash
func GetTransaction(txHash string) (*Transaction, error) {
responseTransaction, err := metahashClient.Call("get-tx", &TransactionArgs{Hash: txHash})
if err == nil {
var resultTransaction *Transaction
err = responseTransaction.GetObject(&resultTransaction)
if err == nil {
return resultTransaction, nil
}
return nil, err
}
return nil, err
}
// GetLastTransactions returns the list of last transactions made
func GetLastTransactions() ([]*TransactionInfo, error) {
responseLastTxs, err := metahashClient.Call("get-last-txs", &LastTxsArgs{})
if err == nil {
var resultLastTxs []*TransactionInfo
err = responseLastTxs.GetObject(&resultLastTxs)
if err == nil {
return resultLastTxs, nil
}
return nil, err
}
return nil, err
}
//GetBlocks
func GetBlocks(startBlock, numBlocks int64) ([]*Block, error) {
responseBlocks, err := metahashClient.Call("get-blocks", &BlocksArgs{CountBlocks: numBlocks, BeginBlock: startBlock})
if err == nil {
var resultBlocks []*Block
err = responseBlocks.GetObject(&resultBlocks)
if err == nil {
return resultBlocks, nil
}
return nil, err
}
return nil, err
}
// GetBlockByNumber returns block info. Pass nil for startTx, numTx
// blockType should between 0 and 2: 0 = 0 or there isn’t - only block name, 1 = only hashes, 2 = full block dump,
//issue: block type 1 is not working at the moment
func GetBlockByNumber(blockNumber, blockType int64) (*Block, error) {
if blockType > 2 || blockType == 1 { //skip block type 1 as it not working properly
blockType = 0
}
blkArg := &BlockByNumberArgs{Number: blockNumber, Type: int8(blockType)}
responseBlockByNumber, err := metahashClient.Call("get-block-by-number", blkArg)
if err == nil {
var resultBlockByNumber *Block
err = responseBlockByNumber.GetObject(&resultBlockByNumber)
if err == nil {
return resultBlockByNumber, nil
}
return nil, err
}
return nil, err
}
func GetBlockByNumberFilter(blockNumber, startTx, numTx, blockType int64) (*Block, error) {
if blockType > 2 || blockType == 1 { //skip block type 1 as it not working properly
blockType = 0
}
blkArg := &BlockByNumberArgs{Number: blockNumber, BeginTx: startTx, CountTxs: numTx, Type: int8(blockType)}
responseBlockByNumber, err := metahashClient.Call("get-block-by-number", blkArg)
if err == nil {
var resultBlockByNumber *Block
err = responseBlockByNumber.GetObject(&resultBlockByNumber)
if err == nil {
return resultBlockByNumber, nil
}
return nil, err
}
return nil, err
}
func GetTotalBlocks() (int64, error) {
responseCountBlocks, err := metahashClient.Call("get-count-blocks", &CountBlocksArgs{})
if err == nil {
var resultCountBlocks *CountBlocks
err = responseCountBlocks.GetObject(&resultCountBlocks)
if err == nil {
return resultCountBlocks.CountBlocks, nil
}
return 0, err
}
return 0, err
}
// GetBlockByNumber returns block info. Pass nil for startTx, numTx
// blockType should between 0 and 2: 0 = 0 or there isn’t - only block name, 1 = only hashes, 2 = full block dump,
//issue: block type 1 is not working at the moment
func GetBlockByHash(blockHash string, startIndex, numTx, blockType int) (*Block, error) {
if blockType > 2 || blockType == 1 { //skip block type 1 as it not working properly
blockType = 0
}
blkArg := &BlockByHashArgs{Hash: blockHash, BeginTx: int64(startIndex), CountTxs: int64(numTx), Type: int8(blockType)}
responseBlockByNumber, err := metahashClient.Call("get-block-by-hash", blkArg)
if err == nil {
var resultBlockByNumber *Block
err = responseBlockByNumber.GetObject(&resultBlockByNumber)
if err == nil {
return resultBlockByNumber, nil
}
return nil, err
}
return nil, err
}
func GetDumpBlockByHash(blockHash string, isHex bool) (*DumpBlock, error) {
arg := &DumpBlockByHashArgs{Hash: blockHash, IsHex: isHex}
responseDumpBlockByHash, err := metahashClient.Call("get-dump-block-by-hash", arg)
if err == nil {
var dumpBlock *DumpBlock
err = responseDumpBlockByHash.GetObject(&dumpBlock)
if err == nil {
return dumpBlock, nil
}
return nil, err
}
return nil, err
}
func GetDumpBlockByNumber(blockNumber int64, isHex bool) (*DumpBlock, error) {
blockArg := &DumpBlockByNumberArgs{Number: blockNumber, IsHex: isHex}
responseDumpBlockByNumber, err := metahashClient.Call("get-dump-block-by-number", blockArg)
if err == nil {
var dumbBlock *DumpBlock
err = responseDumpBlockByNumber.GetObject(&dumbBlock)
if err == nil {
return dumbBlock, nil
}
return nil, err
}
return nil, err
}
func GetNodeStats(address string) (*NodeStats, error) {
args := &NodeArgs{
Address: address,
}
responsNodeStats, err := metahashClient.Call("get-last-node-stat-result", args)
if err == nil {
var nodeStats *NodeStats
err = responsNodeStats.GetObject(&nodeStats)
if err == nil {
return nodeStats, nil
}
return nil, err
}
return nil, err
}
func (ns *NodeStats) GetTest() (*NodeStatsState, error) {
var nodeTest *NodeStatsState
str := fmt.Sprint(ns.State)
err := json.Unmarshal([]byte(str), &nodeTest)
if err == nil {
return nodeTest, nil
}
return nil, errors.New("cannot parse state information")
}
func GetLastNodeStatTrust(address string) (*NodeTrust, error) {
args := &NodeArgs{
Address: address,
}
nodeTrustResp, err := metahashClient.Call("get-last-node-stat-trust", args)
if err == nil {
var nodeTrust *NodeTrust
err = nodeTrustResp.GetObject(&nodeTrust)
if err == nil {
return nodeTrust, nil
}
return nil, err
}
return nil, err
}
func GetLastNodeCount(counts int) (*LastNodeCount, error) {
arg := NodeArgs{CountTests: counts}
nodeLastNodeCountResp, err := metahashClient.Call("get-all-last-nodes-count", arg)
if err == nil {
var nodeNodeCount *LastNodeCount
err = nodeLastNodeCountResp.GetObject(&nodeNodeCount)
if err == nil {
return nodeNodeCount, nil
}
return nil, err
}
return nil, err
}
// GetTrustData returns trust and list of delete to and delegate from
func (nt *NodeTrust) GetTrustData() (*NodeTrustData, error) {
var nodeTrustData *NodeTrustData
str := fmt.Sprint(nt.Data)
fmt.Print("node trust data: ", str)
err := json.Unmarshal([]byte(str), &nodeTrustData)
if err == nil {
return nodeTrustData, nil
}
return nil, errors.New("cannot parse node trust data")
}
func GetNodeRaiting(address string, countTests int) (*NodeRaiting, error) {
args := &NodeArgs{
Address: address,
CountTests: countTests,
}
nodeRaitingResp, err := metahashClient.Call("get-nodes-raiting", args)
if err == nil {
var nodeRaiting *NodeRaiting
err = nodeRaitingResp.GetObject(&nodeRaiting)
if err == nil {
return nodeRaiting, nil
}
return nil, err
}
return nil, err
}
func GetAddressDelegations(address string, startTx, countTx int64) (*AddressDelegations, error) {
args := &NodeArgs{
Address: address,
BeginTx: startTx,
CountTxs: countTx,
}
responsNodeStats, err := metahashClient.Call("get-address-delegations", args)
if err == nil {
var nodeStats *AddressDelegations
err = responsNodeStats.GetObject(&nodeStats)
if err == nil {
return nodeStats, nil
}
return nil, err
}
return nil, err
}
// GetForgingSumAll returns sum all types of forging
//100; // forging transaction
//101; // wallet reward forging transaction
//102; // node reward forging transaction
//103; // coin reward forging transaction
//104; // random reward forging transaction
func GetForgingSumAll() (*ForgingSum, error) {
responseLastTxs, err := metahashClient.Call("get-forging-sum-all", nil)
if err == nil {
var forgingSum *ForgingSum
err = responseLastTxs.GetObject(&forgingSum)
if err == nil {
return forgingSum, nil
}
return nil, err
}
return nil, err
}
func GetForgingSum(blockIndent int) (*ForgingSum, error) {
arg := &ForginSumArgs{BlockIndent: blockIndent}
responseLastTxs, err := metahashClient.Call("get-forging-sum", arg)
if err == nil {
var forgingSum *ForgingSum
err = responseLastTxs.GetObject(&forgingSum)
if err == nil {
return forgingSum, nil
}
return nil, err
}
return nil, err
}
func GetCommonBalance() (int64, error) {
balanceRes, err := metahashClient.Call("get-common-balance", nil)
if err == nil {
var balance *CommonBalance
err = balanceRes.GetObject(&balance)
if err == nil {
return balance.Balance, nil
}
return 0, err
}
return 0, err
}
func MetahashSupply() (*TotalSupply, error) {
req, _ := http.NewRequest("GET", totalSupplyUrl, nil)
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
supply := &TotalSupply{}
err = json.Unmarshal(body, supply)
if err == nil {
return supply, nil
}
return nil, err
}