Skip to content

Commit

Permalink
Update OKX structure and fix balance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
dasbd72 committed Mar 25, 2024
1 parent 06cca5e commit de9cbc4
Show file tree
Hide file tree
Showing 30 changed files with 776 additions and 638 deletions.
4 changes: 2 additions & 2 deletions cmd/ccy-cli/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/dasbd72/go-exchange-sdk/config"
"github.com/dasbd72/go-exchange-sdk/manager"
"github.com/dasbd72/go-exchange-sdk/max"
"github.com/dasbd72/go-exchange-sdk/okx"
okxRest "github.com/dasbd72/go-exchange-sdk/okx/pkg/rest"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +33,7 @@ func Balance(cmd *cobra.Command, args []string) error {

c := manager.Client_builder{
BinanceClient: binanceSpot.NewClient(cfg.BinanceApiKey, cfg.BinanceApiSecret),
OkxClient: okx.NewClient(cfg.OKXApiKey, cfg.OKXApiSecret, cfg.OKXPassphrase),
OkxClient: okxRest.NewClient(cfg.OKXApiKey, cfg.OKXApiSecret, cfg.OKXPassphrase),
BitfinexRestClient: bitfinexRest.NewClient(cfg.BitfinexApiKey, cfg.BitfinexApiSecret),
}.Build()

Expand Down
6 changes: 0 additions & 6 deletions cmd/ccy-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ func main() {
Use: "experimental",
Short: "Experimental commands",
}
okxCmd := &cobra.Command{
Use: "okx",
Short: "OKX commands",
Run: OKX,
}
maxCmd := &cobra.Command{
Use: "max",
Short: "Max commands",
Expand All @@ -37,7 +32,6 @@ func main() {
RunE: Balance,
}
root.PersistentFlags().BoolVarP(&useLog, "log", "l", false, "Use log")
root.AddCommand(okxCmd)
root.AddCommand(maxCmd)
root.AddCommand(balanceCmd)
root.Execute()
Expand Down
47 changes: 0 additions & 47 deletions cmd/ccy-cli/okx.go

This file was deleted.

28 changes: 21 additions & 7 deletions manager/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package manager

import (
"context"
"fmt"

binanceModels "github.com/dasbd72/go-exchange-sdk/binance/pkg/models"
"github.com/dasbd72/go-exchange-sdk/max"
"github.com/dasbd72/go-exchange-sdk/okx"
okxModels "github.com/dasbd72/go-exchange-sdk/okx/pkg/models"
)

type (
Expand Down Expand Up @@ -43,6 +44,7 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
btcPrice := averagePrice.Price.Float64()

totalBalanceUsdt += sum * btcPrice
fmt.Println("Binance balance: ", sum*btcPrice)
return nil
},
func() error {
Expand All @@ -52,22 +54,32 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
}
sum := 0.0
// Get balance from wallet
wallet, err := c.okxClient.GetBalance(ctx, okx.NewGetBalanceRequest())
wallet, err := c.okxClient.GetBalance(ctx, okxModels.NewGetBalanceRequest())
if err != nil {
return err
}
for _, w := range wallet.Balances {
sum += w.TotalEq.Float64()
for _, detail := range w.Details {
price := 1.0
if detail.Ccy != "USDT" {
ticker, err := c.okxClient.GetTicker(ctx, okxModels.NewGetTickerRequest(detail.Ccy+"-USDT"))
if err != nil {
return err
}
price = ticker.Tickers[0].Last.Float64()
}
sum += detail.Eq.Float64() * price
}
}
// Get balance from funding
funding, err := c.okxClient.GetFundingBalances(ctx, okx.NewGetFundingBalancesRequest())
funding, err := c.okxClient.GetFundingBalances(ctx, okxModels.NewGetFundingBalancesRequest())
if err != nil {
return err
}
for _, f := range funding.Balances {
price := 1.0
if f.Ccy != "USDT" {
ticker, err := c.okxClient.GetTicker(ctx, okx.NewGetTickerRequest(f.Ccy+"-USDT"))
ticker, err := c.okxClient.GetTicker(ctx, okxModels.NewGetTickerRequest(f.Ccy+"-USDT"))
if err != nil {
return err
}
Expand All @@ -76,14 +88,14 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
sum += f.Bal.Float64() * price
}
// Get balance from saving
savings, err := c.okxClient.GetSavingBalance(ctx, okx.NewGetSavingBalanceRequest())
savings, err := c.okxClient.GetSavingBalance(ctx, okxModels.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"))
ticker, err := c.okxClient.GetTicker(ctx, okxModels.NewGetTickerRequest(s.Ccy+"-USDT"))
if err != nil {
return err
}
Expand All @@ -92,6 +104,7 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
sum += s.Amt.Float64() * price
}
totalBalanceUsdt += sum
fmt.Println("OKX balance: ", sum)
return nil
},
func() error {
Expand All @@ -112,6 +125,7 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
}

totalBalanceUsdt += sum
fmt.Println("Bitfinex balance: ", sum)
return nil
},
func() error {
Expand Down
6 changes: 3 additions & 3 deletions manager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package manager
import (
binanceRest "github.com/dasbd72/go-exchange-sdk/binance/pkg/spot"
bitfinexRest "github.com/dasbd72/go-exchange-sdk/bitfinex/pkg/rest"
"github.com/dasbd72/go-exchange-sdk/okx"
okxRest "github.com/dasbd72/go-exchange-sdk/okx/pkg/rest"
)

type Client struct {
binanceClient *binanceRest.Client
okxClient *okx.Client
okxClient *okxRest.Client
bitfinexRestClient *bitfinexRest.Client
}

type Client_builder struct {
BinanceClient *binanceRest.Client
OkxClient *okx.Client
OkxClient *okxRest.Client
BitfinexRestClient *bitfinexRest.Client
}

Expand Down
32 changes: 32 additions & 0 deletions okx/example/rest/financial_service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"encoding/json"

"github.com/dasbd72/go-exchange-sdk/config"
"github.com/dasbd72/go-exchange-sdk/okx/pkg/models"
"github.com/dasbd72/go-exchange-sdk/okx/pkg/rest"
)

func main() {
cfg, err := config.Load()
if err != nil {
panic(err)
}

ctx := context.Background()
c := rest.NewClient(cfg.OKXApiKey, cfg.OKXApiSecret, cfg.OKXPassphrase)

{
res, err := c.GetSavingBalance(ctx, models.NewGetSavingBalanceRequest())
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(res, "", " ")
if err != nil {
panic(err)
}
println(string(b))
}
}
32 changes: 32 additions & 0 deletions okx/example/rest/funding_service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"encoding/json"

"github.com/dasbd72/go-exchange-sdk/config"
"github.com/dasbd72/go-exchange-sdk/okx/pkg/models"
"github.com/dasbd72/go-exchange-sdk/okx/pkg/rest"
)

func main() {
cfg, err := config.Load()
if err != nil {
panic(err)
}

ctx := context.Background()
c := rest.NewClient(cfg.OKXApiKey, cfg.OKXApiSecret, cfg.OKXPassphrase)

{
res, err := c.GetFundingBalances(ctx, models.NewGetFundingBalancesRequest())
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(res, "", " ")
if err != nil {
panic(err)
}
println(string(b))
}
}
32 changes: 32 additions & 0 deletions okx/example/rest/trading_service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"encoding/json"

"github.com/dasbd72/go-exchange-sdk/config"
"github.com/dasbd72/go-exchange-sdk/okx/pkg/models"
"github.com/dasbd72/go-exchange-sdk/okx/pkg/rest"
)

func main() {
cfg, err := config.Load()
if err != nil {
panic(err)
}

ctx := context.Background()
c := rest.NewClient(cfg.OKXApiKey, cfg.OKXApiSecret, cfg.OKXPassphrase)

{
res, err := c.GetBalance(ctx, models.NewGetBalanceRequest())
if err != nil {
panic(err)
}
b, err := json.MarshalIndent(res, "", " ")
if err != nil {
panic(err)
}
println(string(b))
}
}
Loading

0 comments on commit de9cbc4

Please sign in to comment.