-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwallet_balance.go
62 lines (52 loc) · 1.52 KB
/
wallet_balance.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
"text/tabwriter"
)
// get wallet balance
func getWalletBalances(apiKey string) error {
fmt.Println("Wallet Balance:\n")
url := fmt.Sprintf("https://hashes.com/en/api/balance?key=%s", apiKey)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
var response struct {
Success bool `json:"success"`
WalletBalances
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return fmt.Errorf("failed to decode response: %v", err)
}
if !response.Success {
return fmt.Errorf("request was not successful")
}
walletBalances := response.WalletBalances
writer := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.AlignRight|tabwriter.Debug)
fmt.Fprintln(writer, "Crypto \t Coins \t USD")
// Convert and print balances for BTC, XMR, LTC
for _, crypto := range []struct{ name, value string }{
{"BTC", walletBalances.BTC},
{"XMR", walletBalances.XMR},
{"LTC", walletBalances.LTC},
} {
amount, err := strconv.ParseFloat(crypto.value, 64)
if err != nil {
return fmt.Errorf("Error parsing %s amount: %v\n", crypto.name, err)
}
cryptoUSD, err := toUSD(amount, crypto.name)
if err != nil {
return fmt.Errorf("Error converting %s: %v\n", crypto.name, err)
}
fmt.Fprintf(writer, "%s \t %s \t %s\n", crypto.name, crypto.value, cryptoUSD["converted"])
}
fmt.Fprintf(writer, "Credits \t %s\n", walletBalances.Credits)
writer.Flush()
return nil
}