/
transaction_query_base.go
81 lines (63 loc) · 1.46 KB
/
transaction_query_base.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
package zcncore
import (
"encoding/json"
stderrors "errors"
thrown "github.com/0chain/errors"
)
// GetUserLockedTotal get total token user locked
// # Inputs
// - clientID wallet id
func GetUserLockedTotal(clientID string) (int64, error) {
err := checkSdkInit()
if err != nil {
return 0, err
}
var url = withParams(STORAGESC_GET_USER_LOCKED_TOTAL, Params{
"client_id": clientID,
})
cb := createGetInfoCallback()
go GetInfoFromSharders(url, OpStorageSCGetStakePoolInfo, cb)
info, err := cb.Wait()
if err != nil {
return 0, err
}
result := make(map[string]int64)
err = json.Unmarshal([]byte(info), &result)
if err != nil {
return 0, thrown.Throw(err, "invalid json format")
}
total, ok := result["total"]
if ok {
return total, nil
}
return 0, stderrors.New("invalid result")
}
func createGetInfoCallback() *getInfoCallback {
return &getInfoCallback{
callback: make(chan bool),
}
}
type getInfoCallback struct {
callback chan bool
status int
info string
err string
}
func (cb *getInfoCallback) OnInfoAvailable(op int, status int, info string, err string) {
// if status == StatusSuccess then info is valid
// is status != StatusSuccess then err will give the reason
cb.status = status
if status == StatusSuccess {
cb.info = info
} else {
cb.err = err
}
cb.callback <- true
}
func (cb *getInfoCallback) Wait() (string, error) {
<-cb.callback
if cb.err == "" {
return cb.info, nil
}
return "", stderrors.New(cb.err)
}