Skip to content

Commit

Permalink
feat: update conversion rate when getting info
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzbit committed May 2, 2022
1 parent 67d8e33 commit 82faf52
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 45 deletions.
37 changes: 25 additions & 12 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"fmt"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
)

// AddressInfo represents information about a given address.
// AddressInfo implements the Info interface
type AddressInfo struct {
Address string `gorm:"primaryKey"`
Nickname string
Expand Down Expand Up @@ -82,16 +81,17 @@ main:
log.Errorf("error calling btcapi: %v", err)
}

currencyBalance, err := w.ConvertBalance(addressSummary.TXHistory.BalanceSat)
if err != nil {
currencyBalance, err := w.ConvertBalance(oldAddressInfo.Currency, addressSummary.TXHistory.BalanceSat)
if err != nil || currencyBalance == nil {
log.Errorf("unable to convert balance of %d to %s, err: %v", addressSummary.TXHistory.BalanceSat, w.Currency, err)
currencyBalance[0] = "0.00"
}
addressInfo := AddressInfo{
Address: address,
Nickname: nickname,
BalanceSat: addressSummary.TXHistory.BalanceSat,
BalanceCurrency: strconv.FormatFloat(currencyBalance, 'f', 2, 64),
Currency: strings.ToUpper(w.Currency),
BalanceCurrency: currencyBalance[0],
Currency: oldAddressInfo.Currency,
PreviousBalanceSat: oldAddressInfo.BalanceSat,
PreviousBalanceCurrency: oldAddressInfo.BalanceCurrency,
TXCount: addressSummary.TXHistory.TXCount,
Expand Down Expand Up @@ -123,10 +123,10 @@ func (w Watcher) CreateNewAddressInfo(address string, nickname string) (AddressI
Address: address,
Nickname: nickname,
BalanceSat: 0,
BalanceCurrency: "0.0",
BalanceCurrency: "0.00",
Currency: w.Currency,
PreviousBalanceSat: 0,
PreviousBalanceCurrency: "0.0",
PreviousBalanceCurrency: "0.00",
TXCount: 0,
}

Expand All @@ -137,12 +137,25 @@ func (w Watcher) CreateNewAddressInfo(address string, nickname string) (AddressI
return addressInfo, nil
}

// Gets an AddressInfo object from the database with an address
func (w Watcher) GetAddressInfo(address string) (addressInfo AddressInfo) {
// Gets an AddressInfo object from the database identified by an address
func (w Watcher) GetAddressInfo(address string) (a AddressInfo) {
w.DB.Model(&AddressInfo{}).
Where(&AddressInfo{Address: address}).
Scan(&addressInfo)
return addressInfo
Scan(&a)
// Update the object with current exchange rates
if (a != AddressInfo{}) {
var err error
bs, err := w.ConvertBalance(a.Currency, a.PreviousBalanceSat, a.BalanceSat)
if err != nil || bs == nil {
log.Errorf("error converting balance, err: %v", err)
return a
}
a.PreviousBalanceCurrency = bs[0]
a.BalanceCurrency = bs[1]
// Update the currency data in the database
w.UpdateInfo(a)
}
return a
}

// Deletes an AddressInfo object from the database with an address
Expand Down
51 changes: 31 additions & 20 deletions btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@ package main

import (
"fmt"
"strings"
"math"
)

// ConvertBalance takes a balance in satoshis and returns the equivalent
// balance in the currency specified by Watcher.Currency
func (w Watcher) ConvertBalance(balanceSat int) (float64, error) {
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 0.0, fmt.Errorf("error calling btcapi: %v", err)
return nil, fmt.Errorf("error calling btcapi: %v", err)
}

balanceFiat := 0.0
currency := strings.ToUpper(w.Currency)
bitcoinBalance := float64(balanceSat) / float64(SatsPerBitcoin)
switch currency {
case "USD":
balanceFiat = price.USD * bitcoinBalance
case "EUR":
balanceFiat = price.EUR * bitcoinBalance
case "GBP":
balanceFiat = price.GBP * bitcoinBalance
case "XAU":
balanceFiat = price.XAU * bitcoinBalance
default:
balanceFiat = price.USD * bitcoinBalance
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))
}
return balanceFiat, nil
// Round with two digits of precision
return bs, nil
}
36 changes: 24 additions & 12 deletions pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package main

import (
"fmt"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/tyzbit/btcapi"
)

// PubkeyInfo represents information about a given pubkey.
// Pubkey implements the Info interface
type PubkeyInfo struct {
Pubkey string `gorm:"primaryKey"`
Nickname string
Expand Down Expand Up @@ -154,17 +153,18 @@ main:
totalTxCount = totalTxCount + totalPubkeyTxCount
}

currencyBalance, err := w.ConvertBalance(totalBalance)
if err != nil {
currencyBalance, err := w.ConvertBalance(oldPubkeyInfo.Currency, totalBalance)
if err != nil || currencyBalance == nil {
log.Errorf("unable to convert balance of %d to %s, err: %v", totalBalance, w.Currency, err)
currencyBalance[0] = "0.00"
}
pubkeyInfo := PubkeyInfo{
Pubkey: pubKeys[0],
Nickname: nickname,
BalanceSat: totalBalance,
BalanceCurrency: currencyBalance[0],
Currency: oldPubkeyInfo.Currency,
PreviousBalanceSat: oldPubkeyInfo.BalanceSat,
Currency: strings.ToUpper(w.Currency),
BalanceCurrency: strconv.FormatFloat(currencyBalance, 'f', 2, 64),
PreviousBalanceCurrency: oldPubkeyInfo.BalanceCurrency,
TXCount: totalTxCount,
}
Expand Down Expand Up @@ -194,10 +194,10 @@ func (w Watcher) CreateNewPubkeyInfo(pubkey string, nickname string) (PubkeyInfo
Pubkey: pubkey,
Nickname: nickname,
BalanceSat: 0,
BalanceCurrency: "0.0",
BalanceCurrency: "0.00",
Currency: w.Currency,
PreviousBalanceSat: 0,
PreviousBalanceCurrency: "0.0",
PreviousBalanceCurrency: "0.00",
TXCount: 0,
}
tx := w.DB.Model(&PubkeyInfo{}).Create(&pubkeyInfo)
Expand All @@ -219,12 +219,24 @@ func (w Watcher) UpdatePubkeysTotal(address string, totalPubkeyBalance *int, tot
return addressSummary, nil
}

// GetPubkeyInfo gets a PubkeyInfo object from the database with a pubkey
func (w Watcher) GetPubkeyInfo(pubkey string) (pubkeyInfo PubkeyInfo) {
// GetPubkeyInfo gets a PubkeyInfo object from the database identified by a pubkey
func (w Watcher) GetPubkeyInfo(pubkey string) (p PubkeyInfo) {
w.DB.Model(&PubkeyInfo{}).
Where(&PubkeyInfo{Pubkey: pubkey}).
Scan(&pubkeyInfo)
return pubkeyInfo
Scan(&p)
// Update the object with current exchange rates
if (p != PubkeyInfo{}) {
bs, err := w.ConvertBalance(p.Currency, p.PreviousBalanceSat, p.BalanceSat)
if err != nil || bs == nil {
log.Errorf("error converting balance, err: %v", err)
return p
}
p.PreviousBalanceCurrency = bs[0]
p.BalanceCurrency = bs[1]
// Update the currency data in the database
w.UpdateInfo(p)
}
return p
}

// DeletePubkeyInfo deletes a PubkeyInfo object from the database with a pubkey
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (w Watcher) FillDefaults() {
watcher.Port = "80"
}
if w.Currency == "" {
watcher.Currency = "USD"
watcher.Currency = CurrencyUSD
}

// Set up DB path
Expand Down

0 comments on commit 82faf52

Please sign in to comment.