add support for bitcoin
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-11-12 16:50:46 -07:00
parent b690c20a90
commit bb7d0a29ea
6 changed files with 268 additions and 41 deletions

101
main.go
View File

@@ -7,16 +7,20 @@ import (
"strconv"
"time"
"deadbeef.codes/steven/ynab-portfolio-monitor/bitcoin"
"deadbeef.codes/steven/ynab-portfolio-monitor/questrade"
"deadbeef.codes/steven/ynab-portfolio-monitor/ynab"
)
var (
persistentData *PersistentData
questradeClient *questrade.Client
ynabClient *ynab.Client
questradeAccountIDs []int
ynabAccountIDs []string
persistentData *PersistentData
questradeClient *questrade.Client
ynabClient *ynab.Client
bitcoinClient *bitcoin.Client
questradeAccountIDs []int
questradeYnabAccountIDs []string
bitcoinAddresses []string
bitcoinYnabAccountID string
)
func init() {
@@ -27,6 +31,7 @@ func init() {
envVars["questrade_refresh_token"] = os.Getenv("questrade_refresh_token")
envVars["ynab_secret"] = os.Getenv("ynab_secret")
envVars["ynab_budget_id"] = os.Getenv("ynab_budget_id")
envVars["bitcoin_ynab_account"] = os.Getenv("bitcoin_ynab_account")
// Validate that all required environment variables are set
for key, value := range envVars {
@@ -35,6 +40,9 @@ func init() {
}
}
// questrade
questradeAccountIDs = make([]int, 0)
questradeYnabAccountIDs = make([]string, 0)
for i := 0; true; i++ {
questradeAccountIDString := os.Getenv(fmt.Sprintf("questrade_account_%d", i))
ynabAccountID := os.Getenv(fmt.Sprintf("questrade_ynab_account_%d", i))
@@ -47,9 +55,20 @@ func init() {
log.Fatalf("failed to convert environment variable questrade_account_%d with value of '%s' to integer: %v", i, questradeAccountIDString, err)
}
questradeAccountIDs = append(questradeAccountIDs, questradeAccountID)
ynabAccountIDs = append(ynabAccountIDs, ynabAccountID)
questradeYnabAccountIDs = append(questradeYnabAccountIDs, ynabAccountID)
}
// bitcoin
bitcoinAddresses = make([]string, 0)
for i := 0; true; i++ {
bitcoinAddress := os.Getenv(fmt.Sprintf("bitcoin_address_%d", i))
if bitcoinAddress == "" {
break
}
bitcoinAddresses = append(bitcoinAddresses, bitcoinAddress)
}
bitcoinYnabAccountID = envVars["bitcoin_ynab_account"]
// Load persistent data
var err error
persistentData, err = loadPersistentData()
@@ -62,6 +81,12 @@ func init() {
if err != nil {
log.Fatalf("failed to create ynab client: %v", err)
}
bitcoinClient, err = bitcoin.NewClient()
if err != nil {
log.Fatalf("failed to create bitcoin client: %v", err)
}
}
func main() {
@@ -90,8 +115,12 @@ func main() {
}
// Update Bitcoin account
err = syncBitoin()
if err != nil {
log.Fatalf("failed to sync bitcoin to ynab: %v", err)
}
// Update ComputerShare account
// TBD: Update ComputerShare account
log.Print("Sleeping for 6 hours...")
time.Sleep(time.Hour * 6)
@@ -108,40 +137,36 @@ func syncQuestrade() error {
return fmt.Errorf("failed to get questrade account balance for account ID '%d': %v", questradeAccountID, err)
}
ynabTransactionID, ynabTransactionAmount, err := ynabClient.GetTodayYnabCapitalGainsTransaction(ynabAccountIDs[i])
err = ynabClient.SetAccountBalance(questradeYnabAccountIDs[i], questradeBalance)
if err != nil {
return fmt.Errorf("failed to get ynab capital gains transaction ID: %v", err)
}
ynabAccount, err := ynabClient.GetAccount(ynabAccountIDs[i])
if err != nil {
return fmt.Errorf("failed to get ynab account with id '%s': %v", ynabAccountIDs[i], err)
}
balanceDelta := questradeBalance - ynabAccount.Data.Account.Balance
balanceDelta += ynabTransactionAmount // Take into account the existing transaction
if balanceDelta == 0 {
continue // If balanceDelta is 0 do not create a transaction i.e. market is closed today
}
if ynabTransactionID == "" {
// there is no transaction - so create a new one
err = ynabClient.CreateTodayYNABCapitalGainsTransaction(ynabAccountIDs[i], balanceDelta)
if err != nil {
return fmt.Errorf("failed to create YNAB capital gains transaction for account ID '%s': %v", ynabAccountIDs[i], err)
}
log.Printf("Creating new capital gains transaction for YNAB account '%s' for amount: %d", ynabAccountIDs[i], balanceDelta)
} else {
// there is an existing transaction - so update the existing one
err = ynabClient.UpdateTodayYNABCapitalGainsTransaction(ynabAccountIDs[i], ynabTransactionID, balanceDelta)
if err != nil {
return fmt.Errorf("failed to update YNAB capital gains transaction for account ID '%s': %v", ynabAccountIDs[i], err)
}
log.Printf("Updating existing capital gains transaction for YNAB account '%s' for amount: %d", ynabAccountIDs[i], balanceDelta)
return fmt.Errorf("failed to set YNAB account balance: %v", err)
}
}
return nil
}
func syncBitoin() error {
var satoshiBalance int
for _, bitcoinAddress := range bitcoinAddresses {
addressResponse, err := bitcoinClient.GetAddress(bitcoinAddress)
if err != nil {
return fmt.Errorf("failed to get bitcoin address '%s': %v", bitcoinAddress, err)
}
satoshiBalance += addressResponse.ChainStats.FundedTxoSum - addressResponse.ChainStats.SpentTxoSum
}
fiatBalance, err := bitcoinClient.ConvertBTCToCAD(satoshiBalance)
if err != nil {
return fmt.Errorf("failed to convert satoshi balance to fiat balance: %v", err)
}
err = ynabClient.SetAccountBalance(bitcoinYnabAccountID, fiatBalance)
if err != nil {
return fmt.Errorf("failed to set YNAB account balance: %v", err)
}
return nil
}