You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.7 KiB
63 lines
1.7 KiB
package cmd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/woop-chain/go-sdk/pkg/common"
|
|
"github.com/woop-chain/go-sdk/pkg/rpc"
|
|
"github.com/woop-chain/go-sdk/pkg/sharding"
|
|
)
|
|
|
|
func init() {
|
|
cmdQuery := &cobra.Command{
|
|
Use: "balances",
|
|
Short: "Check account balance on all shards",
|
|
Long: "Query for the latest account balance given a Woop Address",
|
|
Args: cobra.ExactArgs(1),
|
|
PreRunE: validateAddress,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if checkNodeInput(node) {
|
|
balanceRPCReply, err := rpc.Request(rpc.Method.GetBalance, node, []interface{}{addr.address, "latest"})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
nodeRPCReply, err := rpc.Request(rpc.Method.GetShardID, node, []interface{}{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
balance, _ := balanceRPCReply["result"].(string)
|
|
bln := common.NewDecFromHex(balance)
|
|
bln = bln.Quo(oneAsDec)
|
|
var out bytes.Buffer
|
|
out.WriteString("[")
|
|
out.WriteString(fmt.Sprintf(`{"shard":%d, "amount":%s}`,
|
|
uint32(nodeRPCReply["result"].(float64)),
|
|
bln.String(),
|
|
))
|
|
out.WriteString("]")
|
|
fmt.Println(common.JSONPrettyFormat(out.String()))
|
|
return nil
|
|
}
|
|
r, err := sharding.CheckAllShards(node, addr.String(), noPrettyOutput)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Println(r)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
RootCmd.AddCommand(cmdQuery)
|
|
}
|
|
|
|
// Check if input for --node is an IP address
|
|
func checkNodeInput(node string) bool {
|
|
removePrefix := strings.TrimPrefix(node, "http://")
|
|
removePrefix = strings.TrimPrefix(removePrefix, "https://")
|
|
possibleIP := strings.Split(removePrefix, ":")[0]
|
|
return net.ParseIP(string(possibleIP)) != nil
|
|
}
|
|
|