Skip to content

Commit

Permalink
feat: get okx jumpstart stake amount
Browse files Browse the repository at this point in the history
Calculate by the net flow between wallet and Jumpstart.
  • Loading branch information
dasbd72 committed Aug 11, 2024
1 parent 86691a3 commit 3cfe7e5
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions manager/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"math"

binanceModels "github.com/dasbd72/go-exchange-sdk/binance/pkg/models"
"github.com/dasbd72/go-exchange-sdk/max"
Expand All @@ -30,6 +31,44 @@ 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) {
balance := 0.0
// Get balance from Jumpstart, from wallet to Jumpstart, hence minus
outBills, err := c.okxClient.GetFundingBills(ctx, okxModels.NewGetFundingBillsRequest().Type("78"))
if err != nil {
return 0, err
}
for _, b := range outBills.Bills {
price := 1.0
if b.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, b.Ccy+"-USDT")
if err != nil {
return 0, err
}
}
balance -= b.BalChg.Float64() * price
}
// Get balance from Jumpstart, from Jumpstart to wallet, hence minus
inBills, err := c.okxClient.GetFundingBills(ctx, okxModels.NewGetFundingBillsRequest().Type("124"))
if err != nil {
return 0, err
}
for _, b := range inBills.Bills {
price := 1.0
if b.Ccy != "USDT" {
price, err = c.GetOkxSymbolPrice(ctx, b.Ccy+"-USDT")
if err != nil {
return 0, err
}
}
balance -= b.BalChg.Float64() * price
}
balance = math.Max(balance, 0)
slog.Info(fmt.Sprintf("OKX Jumpstart balance: %f", balance))
return balance, nil
}

func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
var (
totalBalanceUsdt float64
Expand Down Expand Up @@ -115,6 +154,12 @@ func (c *Client) GetBalance(ctx context.Context) (*Balance, error) {
}
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
}
Expand Down

0 comments on commit 3cfe7e5

Please sign in to comment.