Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 39e73fa

Browse files
committed
added balance command.
1 parent 5f8eafd commit 39e73fa

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

internal/getbalance/getbalance.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// Copyright 2015-2019 Pedro Salgado
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package getbalance
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/steenzout/go-env"
23+
"github.com/toorop/go-bittrex"
24+
25+
cli "gopkg.in/urfave/cli.v1"
26+
)
27+
28+
const (
29+
// resolution number of digits after the decimal point.
30+
resolution = 8
31+
32+
// flags
33+
flagKey = "key"
34+
flagSecret = "secret"
35+
36+
// EnvBittrexKey environment variable containing the Bittrex key.
37+
EnvBittrexKey = "BITTREX_KEY"
38+
// EnvBittrexSecret environment variable containing the Bittrex secret.
39+
EnvBittrexSecret = "BITTREX_SECRET"
40+
)
41+
42+
var (
43+
// arguments
44+
currency string
45+
key string
46+
secret string
47+
)
48+
49+
// Key returns the Bittrex jey.
50+
func Key(c *cli.Context) string {
51+
v := c.String(flagKey)
52+
if v == "" {
53+
return env.GetString(EnvBittrexKey)
54+
}
55+
56+
return v
57+
}
58+
59+
// Secret returns the Bittrex secret.
60+
func Secret(c *cli.Context) string {
61+
v := c.String(flagSecret)
62+
if v == "" {
63+
return env.GetString(EnvBittrexSecret)
64+
}
65+
66+
return v
67+
}
68+
69+
// ValidateArg validate input arguments
70+
func ValidateArg(c *cli.Context) error {
71+
if c.NArg() != 1 {
72+
return fmt.Errorf("invalid number of arguments")
73+
}
74+
75+
currency = c.Args().First()
76+
77+
secret = Secret(c)
78+
79+
key = Key(c)
80+
81+
return nil
82+
}
83+
84+
// Run request a BUY order.
85+
func Run(cctx *cli.Context) error {
86+
api := bittrex.New(key, secret)
87+
88+
balance, err := api.GetBalance(currency)
89+
if err != nil {
90+
return err
91+
}
92+
93+
fmt.Printf(
94+
"Available: %s\nBalance: %s\nCryptoAddress: %s\nCurrency: %s\nPending: %s\nRequested: %s\nUUID: %s\n",
95+
balance.Available.StringFixed(resolution),
96+
balance.Balance.StringFixed(resolution),
97+
balance.CryptoAddress,
98+
balance.Currency,
99+
balance.Pending.StringFixed(resolution),
100+
balance.Requested,
101+
balance.Uuid,
102+
)
103+
104+
return nil
105+
}

main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
cli "gopkg.in/urfave/cli.v1"
2424

2525
"github.com/CoinMintTech/go-bittrex-cli/internal/buylimit"
26+
"github.com/CoinMintTech/go-bittrex-cli/internal/getbalance"
2627
"github.com/CoinMintTech/go-bittrex-cli/internal/getmarkets"
2728
"github.com/CoinMintTech/go-bittrex-cli/internal/getmarketsummary"
2829
"github.com/CoinMintTech/go-bittrex-cli/internal/getticker"
@@ -38,6 +39,23 @@ func main() {
3839
app.Usage = "bittrex-cli is a command-line interface to the Bittrex API."
3940
app.Version = version.Version()
4041
app.Commands = []cli.Command{
42+
{
43+
Name: "balance",
44+
Usage: "balance <currency>",
45+
ArgsUsage: "<currency>",
46+
Before: getbalance.ValidateArg,
47+
Action: getbalance.Run,
48+
Flags: []cli.Flag{
49+
cli.StringFlag{
50+
Name: "bittrex-key",
51+
Usage: "Bittrex key (optional, BITTREX_KEY environment variable).",
52+
},
53+
cli.StringFlag{
54+
Name: "bittrex-secret",
55+
Usage: "Bittrex secret (optional, BITTREX_SECRET environment variable).",
56+
},
57+
},
58+
},
4159
{
4260
Name: "buy",
4361
Usage: "buy <market> <quantity> <rate>",

0 commit comments

Comments
 (0)