/
utxo_list.go
67 lines (58 loc) · 1.46 KB
/
utxo_list.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
/*
* Copyright (c) 2021. Baidu Inc. All Rights Reserved.
*/
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/xuperchain/xuperos/common/xupospb/pb"
)
// ListUtxoCommand necessary parmeters for query utxo records
type ListUtxoCommand struct {
cli *Cli
cmd *cobra.Command
addr string
utxoItemNum int64
}
// NewListUtxoCommand an entry to query utxo records
func NewListUtxoCommand(cli *Cli) *cobra.Command {
c := new(ListUtxoCommand)
c.cli = cli
c.cmd = &cobra.Command{
Use: "list",
Short: "Get utxo records info of an user.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.TODO()
return c.queryUtxoRecords(ctx)
},
}
c.addFlags()
return c.cmd
}
func (c *ListUtxoCommand) addFlags() {
c.cmd.Flags().StringVarP(&c.addr, "address", "A", "", "address")
c.cmd.Flags().Int64VarP(&c.utxoItemNum, "num", "N", 1, "utxo items to be displayed")
}
func (c *ListUtxoCommand) queryUtxoRecords(ctx context.Context) error {
client := c.cli.XchainClient()
if c.addr == "" {
c.addr, _ = readAddress(c.cli.RootOptions.Keys)
}
request := &pb.UtxoRecordDetail{
Bcname: c.cli.RootOptions.Name,
AccountName: c.addr,
DisplayCount: c.utxoItemNum,
}
response, err := client.QueryUtxoRecord(ctx, request)
if err != nil {
return err
}
output, err := json.MarshalIndent(response, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
return nil
}