Skip to content

Commit

Permalink
Add master package for balance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
dasbd72 committed Feb 19, 2024
1 parent 322ce6b commit 9e5162e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 80 deletions.
96 changes: 17 additions & 79 deletions cmd/ccy-cli/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"context"
"fmt"
"log"
"os"

"github.com/dasbd72/asset-management/binance"
"github.com/dasbd72/asset-management/master"
"github.com/dasbd72/asset-management/max"
"github.com/dasbd72/asset-management/okx"
"github.com/spf13/cobra"
Expand All @@ -16,96 +16,34 @@ func Balance(cmd *cobra.Command, args []string) error {
// Load environment variables
ctx := context.Background()

totalBalance := 0.0
err := func() error {
apiKey := os.Getenv("BINANCE_API_KEY")
apiSecret := os.Getenv("BINANCE_API_SECRET")
if apiKey == "" || apiSecret == "" {
return nil
}
// Create a new Binance client
c := binance.NewClient(apiKey, apiSecret)

sum := 0.0
wallet, err := c.GetUserWalletBalance(ctx)
if err != nil {
return err
}
for _, w := range *wallet {
sum += w.Balance.Float64()
}

averagePrice, err := c.GetAveragePrice(ctx, binance.NewGetAveragePriceRequest("BTCUSDT"))
if err != nil {
return err
}
btcPrice := averagePrice.Price.Float64()
binanceApiKey := os.Getenv("BINANCE_API_KEY")
binanceApiSecret := os.Getenv("BINANCE_API_SECRET")
if binanceApiKey == "" || binanceApiSecret == "" {
return nil
}

totalBalance += sum * btcPrice
if useLog {
log.Printf("Binance total balance: %.2f USDT\n", sum*btcPrice)
}
okxApiKey := os.Getenv("OKX_API_KEY")
okxApiSecret := os.Getenv("OKX_API_SECRET")
okxPassphrase := os.Getenv("OKX_PASSPHRASE")
if okxApiKey == "" || okxApiSecret == "" || okxPassphrase == "" {
return nil
}()
if err != nil {
return err
}
err = func() error {
apiKey := os.Getenv("OKX_API_KEY")
apiSecret := os.Getenv("OKX_API_SECRET")
passphrase := os.Getenv("OKX_PASSPHRASE")
if apiKey == "" || apiSecret == "" || passphrase == "" {
return nil
}
// Create a new OKX client
c := okx.NewClient(apiKey, apiSecret, passphrase)

sum := 0.0
wallet, err := c.GetBalance(ctx, okx.NewGetBalanceRequest())
if err != nil {
return err
}
for _, w := range wallet.Balances {
sum += w.TotalEq.Float64()
}
funding, err := c.GetFundingBalances(ctx, okx.NewGetFundingBalancesRequest())
if err != nil {
return err
}
for _, f := range funding.Balances {
sum += f.Bal.Float64()
}
savings, err := c.GetSavingBalance(ctx, okx.NewGetSavingBalanceRequest())
if err != nil {
return err
}
for _, s := range savings.Balances {
price := 1.0
if s.Ccy != "USDT" {
ticker, err := c.GetTicker(ctx, okx.NewGetTickerRequest(s.Ccy+"-USDT"))
if err != nil {
return err
}
price = ticker.Tickers[0].Last.Float64()
}
sum += s.Amt.Float64() * price
}
c := master.NewClient(
binance.NewClient(binanceApiKey, binanceApiSecret),
okx.NewClient(okxApiKey, okxApiSecret, okxPassphrase),
)

totalBalance += sum
if useLog {
log.Printf("OKX total balance: %.2f USDT\n", sum)
}
return nil
}()
balance, err := c.GetBalance(ctx)
if err != nil {
return err
}
usdtToTWD, err := max.GetUsdtToTWD()
if err != nil {
return err
}
fmt.Printf("Total balance: %10.2f USDT\n", totalBalance)
fmt.Printf("Total balance: %10.2f TWD\n", totalBalance*usdtToTWD)
fmt.Printf("Total balance: %10.2f USDT\n", balance.Usdt)
fmt.Printf("Total balance: %10.2f TWD\n", balance.Twd)
fmt.Printf("USDT to TWD: %.2f\n", usdtToTWD)

return nil
Expand Down
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
go 1.22
go 1.22.0

use (
./binance
./cmd/ccy-cli
./master
./max
./okx
)
96 changes: 96 additions & 0 deletions master/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package master

import (
"context"

"github.com/dasbd72/asset-management/binance"
"github.com/dasbd72/asset-management/max"
"github.com/dasbd72/asset-management/okx"
)

type (
Balance struct {
Usdt float64
Twd float64
}
)

func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
var (
totalBalanceUsdt float64
totalBalanceTwd float64
)
funcs := []func() error{
func() error {
sum := 0.0
wallet, err := c.binanceClient.GetUserWalletBalance(ctx)
if err != nil {
return err
}
for _, w := range *wallet {
sum += w.Balance.Float64()
}

averagePrice, err := c.binanceClient.GetAveragePrice(ctx, binance.NewGetAveragePriceRequest("BTCUSDT"))
if err != nil {
return err
}
btcPrice := averagePrice.Price.Float64()

totalBalanceUsdt += sum * btcPrice
return nil
},
func() error {
sum := 0.0
wallet, err := c.okxClient.GetBalance(ctx, okx.NewGetBalanceRequest())
if err != nil {
return err
}
for _, w := range wallet.Balances {
sum += w.TotalEq.Float64()
}
funding, err := c.okxClient.GetFundingBalances(ctx, okx.NewGetFundingBalancesRequest())
if err != nil {
return err
}
for _, f := range funding.Balances {
sum += f.Bal.Float64()
}
savings, err := c.okxClient.GetSavingBalance(ctx, okx.NewGetSavingBalanceRequest())
if err != nil {
return err
}
for _, s := range savings.Balances {
price := 1.0
if s.Ccy != "USDT" {
ticker, err := c.okxClient.GetTicker(ctx, okx.NewGetTickerRequest(s.Ccy+"-USDT"))
if err != nil {
return err
}
price = ticker.Tickers[0].Last.Float64()
}
sum += s.Amt.Float64() * price
}

totalBalanceUsdt += sum
return nil
},
func() error {
usdtToTWD, err := max.GetUsdtToTWD()
if err != nil {
return err
}
totalBalanceTwd = totalBalanceUsdt * usdtToTWD
return nil
},
}
for _, f := range funcs {
if err := f(); err != nil {
return nil, err
}
}
return &Balance{
Usdt: totalBalanceUsdt,
Twd: totalBalanceTwd,
}, nil
}
18 changes: 18 additions & 0 deletions master/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package master

import (
"github.com/dasbd72/asset-management/binance"
"github.com/dasbd72/asset-management/okx"
)

type Client struct {
binanceClient *binance.Client
okxClient *okx.Client
}

func NewClient(binanceClient *binance.Client, okxClient *okx.Client) *Client {
return &Client{
binanceClient: binanceClient,
okxClient: okxClient,
}
}
9 changes: 9 additions & 0 deletions master/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/dasbd72/asset-management/master

go 1.22.0

require (
github.com/dasbd72/asset-management/binance v0.0.0-20240219071459-322ce6b28840
github.com/dasbd72/asset-management/max v0.0.0-20240219071459-322ce6b28840
github.com/dasbd72/asset-management/okx v0.0.0-20240219071459-322ce6b28840
)
8 changes: 8 additions & 0 deletions master/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/dasbd72/asset-management/binance v0.0.0-20240219071459-322ce6b28840 h1:LErZfCFuHhDtbF3Jw0D6DdqwCDuO83/avVp5AqVA4S8=
github.com/dasbd72/asset-management/binance v0.0.0-20240219071459-322ce6b28840/go.mod h1:7MWjdfOOk6OwTja1SeVFRkyhHB4OkJqoloL9eTDwDUU=
github.com/dasbd72/asset-management/max v0.0.0-20240219071459-322ce6b28840 h1:I3b7yD8vASGD6tvm+6II1SlV81VYWRsHa/EDEjqcJQI=
github.com/dasbd72/asset-management/max v0.0.0-20240219071459-322ce6b28840/go.mod h1:nQOAzru+GswuHXPTz2Y4KNxMtryD2FxppkqkHkRQopo=
github.com/dasbd72/asset-management/okx v0.0.0-20240219071459-322ce6b28840 h1:Gi7JQtwjfO7cAcFn8r6i4ipAqfYK000yWiwFSueTwgY=
github.com/dasbd72/asset-management/okx v0.0.0-20240219071459-322ce6b28840/go.mod h1:pqzDe3U2v2iB61vcAPfQL6KQ0BHBwwWYydZGGVOLbgw=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=

0 comments on commit 9e5162e

Please sign in to comment.