1// SPDX-License-Identifier: ISC
2// Copyright (c) 2014-2020 Bitmark Inc.
3// Use of this source code is governed by an ISC
4// license that can be found in the LICENSE file.
5
6package main
7
8import (
9	"fmt"
10
11	"github.com/urfave/cli"
12
13	"github.com/bitmark-inc/bitmarkd/command/bitmark-cli/rpccalls"
14)
15
16func runBalance(c *cli.Context) error {
17
18	m := c.App.Metadata["config"].(*metadata)
19
20	shareId := c.String("share-id")
21
22	count := c.Int("count")
23	if count <= 0 {
24		return fmt.Errorf("invalid count: %d", count)
25	}
26
27	name, owner, err := checkRecipient(c, "owner", m.config)
28	if nil != err {
29		name = c.GlobalString("identity")
30		if "" == name {
31			name = m.config.DefaultIdentity
32		}
33		owner, err = m.config.Account(name)
34		if nil != err {
35			return err
36		}
37	}
38
39	if m.verbose {
40		fmt.Fprintf(m.e, "owner: %s\n", name)
41		fmt.Fprintf(m.e, "shareId: %s\n", shareId)
42		fmt.Fprintf(m.e, "count: %d\n", count)
43	}
44
45	client, err := rpccalls.NewClient(m.testnet, m.config.Connections[m.connectionOffset], m.verbose, m.e)
46	if nil != err {
47		return err
48	}
49	defer client.Close()
50
51	balanceConfig := &rpccalls.BalanceData{
52		Owner:   owner,
53		ShareId: shareId,
54		Count:   count,
55	}
56
57	response, err := client.GetBalance(balanceConfig)
58	if nil != err {
59		return err
60	}
61
62	printJson(m.w, response)
63
64	return nil
65}
66