-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaemon_rpc.go
126 lines (97 loc) · 2.68 KB
/
daemon_rpc.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
package main
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"github.com/dReam-dApps/dReams/rpc"
dero "github.com/deroproject/derohe/rpc"
)
const (
DAEMON_MAINNET_DEFAULT = "127.0.0.1:10102"
DAEMON_TESTNET_DEFAULT = "127.0.0.1:40402"
DAEMON_SIMULATOR_DEFAULT = "127.0.0.1:20000"
)
// Gets current height and displays
func GetHeight() {
rpcClientD, ctx, cancel := rpc.SetDaemonClient(rpc.Daemon.Rpc)
defer cancel()
var result *dero.Daemon_GetHeight_Result
if err := rpcClientD.CallFor(ctx, &result, "DERO.GetHeight"); err != nil {
return
}
h := result.Height
str := strconv.FormatUint(h, 10)
currentHeight.SetText("Height: " + str)
}
// Search SC using getsc method
func getSC(p *dero.GetSC_Params) {
rpcClientD, ctx, cancel := rpc.SetDaemonClient(rpc.Daemon.Rpc)
defer cancel()
var result *dero.GetSC_Result
if err := rpcClientD.CallFor(ctx, &result, "DERO.GetSC", p); err != nil {
logger.Errorln("[getSC]", err)
return
}
bal := result.Balances /// retrieve all sc balances
balM, _ := json.Marshal(bal)
balances := string(balM)
string_keys := SortStringMap(result.VariableStringKeys) /// retrieve all sc string keys, use result.VariableStringKeys["KEY"] for single value
uint_keys := SortUintMap(result.VariableUint64Keys) /// retrieve all sc uint keys use result.VariableUint64Keys[0] for single value
go searchPopUp(balances, string_keys, uint_keys, result.Code) /// displays results
}
// Get SC code and print result in terminal
func getSCcode(scid string) {
rpcClientD, ctx, cancel := rpc.SetDaemonClient(rpc.Daemon.Rpc)
defer cancel()
var result *dero.GetSC_Result
params := dero.GetSC_Params{
SCID: scid,
Code: true,
Variables: false,
}
if err := rpcClientD.CallFor(ctx, &result, "DERO.GetSC", params); err != nil {
logger.Errorln("[getSCcode]", err)
return
}
fmt.Println(result.Code)
}
// Switch for interface type to string
func findKey(i interface{}) (text string) {
switch v := i.(type) {
case uint64:
text = strconv.Itoa(int(v))
case string:
text = v
case float64:
text = strconv.Itoa(int(v))
default:
}
return
}
func SortStringMap(m map[string]interface{}) (str string) {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if m[k] == m["C"] {
/// skipping C
} else {
str = str + k + " " + findKey(m[k]) + " \n"
}
}
return
}
func SortUintMap(m map[uint64]interface{}) (str string) {
keys := make([]uint64, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
for _, k := range keys {
str = str + strconv.Itoa(int(k)) + " " + findKey(m[k]) + " \n"
}
return
}