|
| 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 | +} |
0 commit comments