ceremonyclient/client/cmd/coins.go

63 lines
1.4 KiB
Go
Raw Normal View History

package cmd
import (
2024-10-14 01:37:19 +00:00
"context"
"encoding/hex"
"fmt"
2024-10-14 01:37:19 +00:00
"math/big"
2024-10-14 01:37:19 +00:00
"github.com/iden3/go-iden3-crypto/poseidon"
"github.com/spf13/cobra"
2024-10-14 01:37:19 +00:00
"source.quilibrium.com/quilibrium/monorepo/node/execution/intrinsics/token"
"source.quilibrium.com/quilibrium/monorepo/node/protobufs"
)
var coinsCmd = &cobra.Command{
Use: "coins",
Short: "Lists all coins under control of 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))
resp, err := client.GetTokensByAccount(
context.Background(),
&protobufs.GetTokensByAccountRequest{
Address: addrBytes,
},
)
if err != nil {
panic(err)
}
if len(resp.Coins) != len(resp.FrameNumbers) {
panic("invalid response from RPC")
}
for i, coin := range resp.Coins {
amount := new(big.Int).SetBytes(coin.Amount)
conversionFactor, _ := new(big.Int).SetString("1DCD65000", 16)
r := new(big.Rat).SetFrac(amount, conversionFactor)
addr, err := token.GetAddressOfCoin(coin, resp.FrameNumbers[i])
if err != nil {
panic(err)
}
fmt.Println(r.FloatString(12), "QUIL (Coin 0x", hex.EncodeToString(addr), ")")
}
},
}
func init() {
tokenCmd.AddCommand(coinsCmd)
}