Skip to content

Commit

Permalink
refactor: extract all sub-functions into separate callable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dasbd72 committed Aug 11, 2024
1 parent 3cfe7e5 commit b4ba84d
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 157 deletions.
28 changes: 23 additions & 5 deletions cmd/ccy-cli/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
bitfinexRest "github.com/dasbd72/go-exchange-sdk/bitfinex/pkg/rest"
"github.com/dasbd72/go-exchange-sdk/config"
"github.com/dasbd72/go-exchange-sdk/manager"
"github.com/dasbd72/go-exchange-sdk/max"
okxRest "github.com/dasbd72/go-exchange-sdk/okx/pkg/rest"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -37,16 +36,35 @@ func Balance(cmd *cobra.Command, args []string) error {
BitfinexRestClient: bitfinexRest.NewClient(cfg.BitfinexApiKey, cfg.BitfinexApiSecret),
}.Build()

balance, err := c.GetBalance(ctx)
// Get balance
balanceUsdt := 0.0
balanceTwd := 0.0
// Get Binance balance
binanceBalance, err := c.GetBinanceBalance(ctx)
if err != nil {
return err
}
usdtToTWD, err := max.GetUsdtToTWD()
balanceUsdt += binanceBalance
// Get OKX balance
okxBalance, err := c.GetOkxBalance(ctx)
if err != nil {
return err
}
fmt.Printf("Total balance: %10.2f USDT\n", balance.Usdt)
fmt.Printf("Total balance: %10.2f TWD\n", balance.Twd)
balanceUsdt += okxBalance
// Get Bitfinex balance
bitfinexBalance, err := c.GetBitfinexBalance(ctx)
if err != nil {
return err
}
balanceUsdt += bitfinexBalance
// Get USDT to TWD
usdtToTWD, err := c.GetUsdtToTWD()
if err != nil {
return err
}
balanceTwd = balanceUsdt * usdtToTWD
fmt.Printf("Total balance: %10.2f USDT\n", balanceUsdt)
fmt.Printf("Total balance: %10.2f TWD\n", balanceTwd)
fmt.Printf("USDT to TWD: %.2f\n", usdtToTWD)

return nil
Expand Down
279 changes: 127 additions & 152 deletions manager/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,35 @@ import (
okxModels "github.com/dasbd72/go-exchange-sdk/okx/pkg/models"
)

type (
Balance struct {
Usdt float64
Twd float64
// GetBinanceBalance returns the balance of Binance in USDT
func (c *Client) GetBinanceBalance(ctx context.Context) (float64, error) {
if c.binanceClient == nil {
return 0, nil
}
)
sum := 0.0
// Get balance from wallet
wallet, err := c.binanceClient.GetUserWalletBalance(ctx)
if err != nil {
return 0, err
}
for _, w := range wallet {
sum += w.Balance.Float64()
}

// Get average price of BTC
averagePrice, err := c.binanceClient.GetAveragePrice(ctx, binanceModels.NewGetAveragePriceRequest("BTCUSDT"))
if err != nil {
return 0, err
}
btcPrice := averagePrice.Price.Float64()

balance := sum * btcPrice
slog.Info(fmt.Sprintf("Binance balance: %f", balance))
return balance, nil
}

// GetOkxSymbolPrice returns the price of the symbol in USDT
func (c *Client) GetOkxSymbolPrice(ctx context.Context, symbol string) (float64, error) {
// getOkxSymbolPrice returns the price of the symbol in USDT
func (c *Client) getOkxSymbolPrice(ctx context.Context, symbol string) (float64, error) {
ticker, err := c.okxClient.GetTicker(ctx, okxModels.NewGetTickerRequest(symbol))
if err != nil {
return 0, err
Expand All @@ -31,8 +51,8 @@ func (c *Client) GetOkxSymbolPrice(ctx context.Context, symbol string) (float64,
return ticker.Tickers[0].Last.Float64(), nil
}

// GetOkxJumpstartBalance returns the balance of Jumpstart by the net flow from wallet to Jumpstart
func (c *Client) GetOkxJumpstartBalance(ctx context.Context) (float64, error) {
// getOkxJumpstartBalance returns the balance of Jumpstart by the net flow from wallet to Jumpstart
func (c *Client) getOkxJumpstartBalance(ctx context.Context) (float64, error) {
balance := 0.0
// Get balance from Jumpstart, from wallet to Jumpstart, hence minus
outBills, err := c.okxClient.GetFundingBills(ctx, okxModels.NewGetFundingBillsRequest().Type("78"))
Expand All @@ -42,7 +62,7 @@ func (c *Client) GetOkxJumpstartBalance(ctx context.Context) (float64, error) {
for _, b := range outBills.Bills {
price := 1.0
if b.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, b.Ccy+"-USDT")
price, err = c.getOkxSymbolPrice(ctx, b.Ccy+"-USDT")
if err != nil {
return 0, err
}
Expand All @@ -57,7 +77,7 @@ func (c *Client) GetOkxJumpstartBalance(ctx context.Context) (float64, error) {
for _, b := range inBills.Bills {
price := 1.0
if b.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, b.Ccy+"-USDT")
price, err = c.getOkxSymbolPrice(ctx, b.Ccy+"-USDT")
if err != nil {
return 0, err
}
Expand All @@ -69,162 +89,117 @@ func (c *Client) GetOkxJumpstartBalance(ctx context.Context) (float64, error) {
return balance, nil
}

func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
var (
totalBalanceUsdt float64
totalBalanceTwd float64
)

getBinanceBalance := func() (float64, error) {
if c.binanceClient == nil {
// Skip if binance client is not set
return 0, nil
}
sum := 0.0
// Get balance from wallet
wallet, err := c.binanceClient.GetUserWalletBalance(ctx)
if err != nil {
return 0, err
}
for _, w := range wallet {
sum += w.Balance.Float64()
}

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

balance := sum * btcPrice
slog.Info(fmt.Sprintf("Binance balance: %f", balance))
return balance, nil
// GetOkxBalance returns the balance of OKX in USDT
func (c *Client) GetOkxBalance(ctx context.Context) (float64, error) {
if c.okxClient == nil {
// Skip if okx client is not set
return 0, nil
}

getOkxBalance := func() (float64, error) {
if c.okxClient == nil {
// Skip if okx client is not set
return 0, nil
}
balance := 0.0
// Get balance from wallet
wallet, err := c.okxClient.GetBalance(ctx, okxModels.NewGetBalanceRequest())
if err != nil {
return 0, err
}
for _, w := range wallet.Balances {
for _, detail := range w.Details {
price := 1.0
if detail.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, detail.Ccy+"-USDT")
if err != nil {
return 0, err
}
}
balance += detail.Eq.Float64() * price
}
}
// Get balance from funding
funding, err := c.okxClient.GetFundingBalances(ctx, okxModels.NewGetFundingBalancesRequest())
if err != nil {
return 0, err
}
for _, f := range funding.Balances {
balance := 0.0
// Get balance from wallet
wallet, err := c.okxClient.GetBalance(ctx, okxModels.NewGetBalanceRequest())
if err != nil {
return 0, err
}
for _, w := range wallet.Balances {
for _, detail := range w.Details {
price := 1.0
if f.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, f.Ccy+"-USDT")
if detail.Ccy != "USDT" {
price, err = c.getOkxSymbolPrice(ctx, detail.Ccy+"-USDT")
if err != nil {
return 0, err
}
}
balance += f.Bal.Float64() * price
}
// Get balance from saving
savings, err := c.okxClient.GetSavingBalance(ctx, okxModels.NewGetSavingBalanceRequest())
if err != nil {
return 0, err
balance += detail.Eq.Float64() * price
}
for _, s := range savings.Balances {
price := 1.0
if s.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, s.Ccy+"-USDT")
if err != nil {
return 0, err
}
}
// Get balance from funding
funding, err := c.okxClient.GetFundingBalances(ctx, okxModels.NewGetFundingBalancesRequest())
if err != nil {
return 0, err
}
for _, f := range funding.Balances {
price := 1.0
if f.Ccy != "USDT" {
price, err = c.getOkxSymbolPrice(ctx, f.Ccy+"-USDT")
if err != nil {
return 0, err
}
balance += s.Amt.Float64() * price
}
// Get balance from Jumpstart
jumpstartBalance, err := c.GetOkxJumpstartBalance(ctx)
if err != nil {
return 0, err
}
balance += jumpstartBalance
slog.Info(fmt.Sprintf("OKX balance: %f", balance))
return balance, nil
balance += f.Bal.Float64() * price
}

getBitfinexBalance := func() (float64, error) {
if c.bitfinexRestClient == nil {
// Skip if bitfinex client is not set
return 0, nil
}
balance := 0.0
// Get balance from wallet
res, err := c.bitfinexRestClient.GetWallets(ctx)
if err != nil {
return 0, err
}
for _, w := range res.Wallets {
if w.Currency.String() == "USD" || w.Currency.String() == "UST" {
balance += w.Balance.Float64()
// Get balance from saving
savings, err := c.okxClient.GetSavingBalance(ctx, okxModels.NewGetSavingBalanceRequest())
if err != nil {
return 0, err
}
for _, s := range savings.Balances {
price := 1.0
if s.Ccy != "USDT" {
price, err = c.getOkxSymbolPrice(ctx, s.Ccy+"-USDT")
if err != nil {
return 0, err
}
}
slog.Info(fmt.Sprintf("Bitfinex balance: %f", balance))
return balance, nil
balance += s.Amt.Float64() * price
}
// Get balance from Jumpstart
jumpstartBalance, err := c.getOkxJumpstartBalance(ctx)
if err != nil {
return 0, err
}
balance += jumpstartBalance
slog.Info(fmt.Sprintf("OKX balance: %f", balance))
return balance, nil
}

funcs := []func() error{
func() error {
balance, err := getBinanceBalance()
if err != nil {
return err
}
totalBalanceUsdt += balance
return nil
},
func() error {
balance, err := getOkxBalance()
if err != nil {
return err
}
totalBalanceUsdt += balance
return nil
},
func() error {
balance, err := getBitfinexBalance()
if err != nil {
return err
}
totalBalanceUsdt += balance
return nil
},
func() error {
usdtToTWD, err := max.GetUsdtToTWD()
if err != nil {
return err
}
totalBalanceTwd = totalBalanceUsdt * usdtToTWD
return nil
},
// GetBitfinexBalance returns the balance of Bitfinex in USDT
func (c *Client) GetBitfinexBalance(ctx context.Context) (float64, error) {
if c.bitfinexRestClient == nil {
// Skip if bitfinex client is not set
return 0, nil
}
for _, f := range funcs {
if err := f(); err != nil {
return nil, err
balance := 0.0
// Get balance from wallet
res, err := c.bitfinexRestClient.GetWallets(ctx)
if err != nil {
return 0, err
}
for _, w := range res.Wallets {
if w.Currency.String() == "USD" || w.Currency.String() == "UST" {
balance += w.Balance.Float64()
}
}
return &Balance{
Usdt: totalBalanceUsdt,
Twd: totalBalanceTwd,
}, nil
slog.Info(fmt.Sprintf("Bitfinex balance: %f", balance))
return balance, nil
}

// GetUsdtToTWD returns the exchange rate of USDT to TWD
func (c *Client) GetUsdtToTWD() (float64, error) {
return max.GetUsdtToTWD()
}

// GetBalance returns the balance of all exchanges in USDT
func (c *Client) GetBalance(ctx context.Context) (float64, error) {
balanceUsdt := 0.0
// Get Binance balance
binanceBalance, err := c.GetBinanceBalance(ctx)
if err != nil {
return 0, err
}
balanceUsdt += binanceBalance
// Get OKX balance
okxBalance, err := c.GetOkxBalance(ctx)
if err != nil {
return 0, err
}
balanceUsdt += okxBalance
// Get Bitfinex balance
bitfinexBalance, err := c.GetBitfinexBalance(ctx)
if err != nil {
return 0, err
}
balanceUsdt += bitfinexBalance
slog.Info(fmt.Sprintf("Total balance: %f USDT", balanceUsdt))
return balanceUsdt, nil
}

0 comments on commit b4ba84d

Please sign in to comment.