-
Notifications
You must be signed in to change notification settings - Fork 0
/
btc.go
43 lines (39 loc) · 1.09 KB
/
btc.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
package main
import (
"fmt"
"math"
)
const (
CurrencyUSD = "USD"
CurrencyEUR = "EUR"
CurrencyGBP = "GBP"
CurrencyXAU = "XAU"
)
// ConvertBalance takes a currency and one or more balances in satoshis and
// returns the equivalent balance(s) in the currency specified with
// two digits of precision.
func (w Watcher) ConvertBalance(currency string, balancesSat ...int) (bs []string, err error) {
price, err := w.BTCAPI.Price()
if err != nil {
return nil, fmt.Errorf("error calling btcapi: %v", err)
}
for _, b := range balancesSat {
balanceCurrency := 0.0
bitcoinBalance := float64(b) / float64(SatsPerBitcoin)
switch currency {
case CurrencyUSD:
balanceCurrency = price.USD * bitcoinBalance
case CurrencyEUR:
balanceCurrency = price.EUR * bitcoinBalance
case CurrencyGBP:
balanceCurrency = price.GBP * bitcoinBalance
case CurrencyXAU:
balanceCurrency = price.XAU * bitcoinBalance
default:
balanceCurrency = price.USD * bitcoinBalance
}
bs = append(bs, fmt.Sprint(math.Round(balanceCurrency*100)/100))
}
// Round with two digits of precision
return bs, nil
}