2024-04-19 05:53:45 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-10-14 01:37:19 +00:00
|
|
|
"context"
|
2024-04-19 05:53:45 +00:00
|
|
|
"fmt"
|
2024-10-14 01:37:19 +00:00
|
|
|
"math/big"
|
2024-04-19 05:53:45 +00:00
|
|
|
|
2024-10-14 01:37:19 +00:00
|
|
|
"github.com/iden3/go-iden3-crypto/poseidon"
|
2024-04-19 05:53:45 +00:00
|
|
|
"github.com/spf13/cobra"
|
2024-10-14 01:37:19 +00:00
|
|
|
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
|
2024-04-19 05:53:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var balanceCmd = &cobra.Command{
|
|
|
|
Use: "balance",
|
|
|
|
Short: "Lists the total balance of tokens in the managing account",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-10-14 01:37:19 +00:00
|
|
|
conn, err := GetGRPCClient()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := protobufs.NewNodeServiceClient(conn)
|
|
|
|
peerId := GetPeerIDFromConfig(NodeConfig)
|
|
|
|
addr, err := poseidon.HashBytes([]byte(peerId))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrBytes := addr.FillBytes(make([]byte, 32))
|
|
|
|
info, err := client.GetTokenInfo(
|
|
|
|
context.Background(),
|
|
|
|
&protobufs.GetTokenInfoRequest{
|
|
|
|
Address: addrBytes,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.OwnedTokens == nil {
|
|
|
|
panic("invalid response from RPC")
|
|
|
|
}
|
|
|
|
tokens := new(big.Int).SetBytes(info.OwnedTokens)
|
|
|
|
conversionFactor, _ := new(big.Int).SetString("1DCD65000", 16)
|
|
|
|
r := new(big.Rat).SetFrac(tokens, conversionFactor)
|
|
|
|
fmt.Println("Total balance:", r.FloatString(12), "QUIL")
|
2024-04-19 05:53:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
tokenCmd.AddCommand(balanceCmd)
|
|
|
|
}
|