-
Notifications
You must be signed in to change notification settings - Fork 3
/
apiGetStatus.go
65 lines (55 loc) · 1.79 KB
/
apiGetStatus.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
package mintersdk
import (
//"encoding/json" -- переход на easyjson
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
//easyjson:json
type node_status struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Result ResultNetwork `json:"result"`
Error ErrorStruct `json:"error"`
}
type ResultNetwork struct {
Version string `json:"version" bson:"version" gorm:"version" db:"version"`
LatestBlockHash string `json:"latest_block_hash" bson:"-" gorm:"-" db:"-"`
LatestAppHash string `json:"latest_app_hash" bson:"-" gorm:"-" db:"-"`
LatestBlockHeightTx string `json:"latest_block_height" bson:"-" gorm:"-" db:"-"`
LatestBlockHeight int `json:"latest_block_height_i32" bson:"latest_block_height_i32" gorm:"latest_block_height_i32" db:"latest_block_height_i32"`
LatestBlockTime time.Time `json:"latest_block_time" bson:"-" gorm:"-" db:"-"`
// tm_status {...}
}
// получение сколько всего блоков в сети
func (c *SDK) GetStatus() (ResultNetwork, error) {
url := fmt.Sprintf("%s/status", c.MnAddress)
res, err := http.Get(url)
if err != nil {
return ResultNetwork{}, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return ResultNetwork{}, err
}
var data node_status
//json.Unmarshal(body, &data) -- переход на easyjson
err = data.UnmarshalJSON(body)
if err != nil {
return ResultNetwork{}, err
}
if data.Error.Code != 0 {
err = errors.New(fmt.Sprint(data.Error.Code, " - ", data.Error.Message))
return ResultNetwork{}, err
}
data.Result.LatestBlockHeight, err = strconv.Atoi(data.Result.LatestBlockHeightTx)
if err != nil {
// пусть и не полностью
return data.Result, err
}
return data.Result, nil
}