-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
217 lines (180 loc) · 5.33 KB
/
query.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
package commands
import (
"fmt"
"os"
"strings"
"github.com/assetcloud/chain/rpc/jsonclient"
rpctypes "github.com/assetcloud/chain/rpc/types"
"github.com/assetcloud/chain/types"
"github.com/assetcloud/plugin/plugin/dapp/x2ethereum/ebrelayer/utils"
types2 "github.com/assetcloud/plugin/plugin/dapp/x2ethereum/types"
"github.com/spf13/cobra"
)
func queryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "query x2ethereum",
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(
queryEthProphecyCmd(),
queryValidatorsCmd(),
queryConsensusCmd(),
queryTotalPowerCmd(),
querySymbolTotalAmountByTxTypeCmd(),
)
return cmd
}
func queryEthProphecyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "prophecy",
Short: "query prophecy",
Run: queryEthProphecy,
}
cmd.Flags().StringP("id", "i", "", "prophecy id")
_ = cmd.MarkFlagRequired("id")
return cmd
}
func queryEthProphecy(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
id, _ := cmd.Flags().GetString("id")
get := &types2.QueryEthProphecyParams{
ID: id,
}
payLoad, err := types.PBToJSON(get)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "ErrPbToJson:"+err.Error())
return
}
query := rpctypes.Query4Jrpc{
Execer: types2.X2ethereumX,
FuncName: types2.FuncQueryEthProphecy,
Payload: payLoad,
}
channel := &types2.ReceiptEthProphecy{}
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain.Query", query, channel)
ctx.Run()
}
func queryValidatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validators",
Short: "query current validators",
Run: queryValidators,
}
cmd.Flags().StringP("validator", "v", "", "write if you want to check specific validator")
//_ = cmd.MarkFlagRequired("validator")
return cmd
}
func queryValidators(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
validator, _ := cmd.Flags().GetString("validator")
get := &types2.QueryValidatorsParams{
Validator: validator,
}
payLoad, err := types.PBToJSON(get)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "ErrPbToJson:"+err.Error())
return
}
query := rpctypes.Query4Jrpc{
Execer: types2.X2ethereumX,
FuncName: types2.FuncQueryValidators,
Payload: payLoad,
}
channel := &types2.ReceiptQueryValidator{}
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain.Query", query, channel)
ctx.Run()
}
func queryConsensusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "consensus",
Short: "query current consensus need",
Run: queryConsensus,
}
return cmd
}
func queryConsensus(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := rpctypes.Query4Jrpc{
Execer: types2.X2ethereumX,
FuncName: types2.FuncQueryConsensusThreshold,
}
channel := &types2.ReceiptQueryConsensusThreshold{}
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain.Query", query, channel)
ctx.Run()
}
func queryTotalPowerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "totalpower",
Short: "query current total power",
Run: queryTotalPower,
}
return cmd
}
func queryTotalPower(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
query := rpctypes.Query4Jrpc{
Execer: types2.X2ethereumX,
FuncName: types2.FuncQueryTotalPower,
}
channel := &types2.ReceiptQueryTotalPower{}
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain.Query", query, channel)
ctx.Run()
}
func querySymbolTotalAmountByTxTypeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "lockburnasset",
Short: "query current symbol total amount by tx type lock or withdraw",
Run: querySymbolTotalAmountByTxType,
}
cmd.Flags().StringP("symbol", "s", "", "token symbol")
_ = cmd.MarkFlagRequired("symbol")
cmd.Flags().Int64P("direction", "d", 0, "eth2chain = 1,chaintoeth = 2")
_ = cmd.MarkFlagRequired("direction")
cmd.Flags().Int64P("txtype", "t", 0, "lock = 1,burn = 2")
_ = cmd.MarkFlagRequired("txtype")
cmd.Flags().StringP("tokenaddress", "a", "", "token address,nil for all this token symbol,and if you want to query eth,you can also ignore it")
return cmd
}
func querySymbolTotalAmountByTxType(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
symbol, _ := cmd.Flags().GetString("symbol")
direction, _ := cmd.Flags().GetInt64("direction")
txType, _ := cmd.Flags().GetInt64("txtype")
contract, _ := cmd.Flags().GetString("tokenaddress")
nodeAddr, _ := cmd.Flags().GetString("node_addr")
decimal, err := utils.GetDecimalsFromNode(contract, nodeAddr)
if err != nil {
fmt.Println("get decimal error")
return
}
if strings.ToLower(symbol) == "eth" && contract == "" {
contract = "0x0000000000000000000000000000000000000000"
}
var txTypeStr string
if txType == 1 {
txTypeStr = "lock"
} else if txType == 2 {
txTypeStr = "withdraw"
}
get := &types2.QuerySymbolAssetsByTxTypeParams{
TokenSymbol: symbol,
Direction: direction,
TxType: txTypeStr,
TokenAddr: contract,
Decimal: decimal,
}
payLoad, err := types.PBToJSON(get)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, "ErrPbToJson:"+err.Error())
return
}
query := rpctypes.Query4Jrpc{
Execer: types2.X2ethereumX,
FuncName: types2.FuncQuerySymbolTotalAmountByTxType,
Payload: payLoad,
}
channel := &types2.ReceiptQuerySymbolAssets{}
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain.Query", query, channel)
ctx.Run()
}