Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: [okx] generate account info request by requestgen #1493

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions pkg/exchange/okex/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
queryMarketLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryTickerLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryTickersLimiter = rate.NewLimiter(rate.Every(100*time.Millisecond), 10)
queryAccountLimiter = rate.NewLimiter(rate.Every(200*time.Millisecond), 5)
)

const ID = "okex"
Expand Down Expand Up @@ -167,28 +168,31 @@ func (e *Exchange) PlatformFeeCurrency() string {
}

func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
accountBalance, err := e.client.AccountBalances(ctx)
bals, err := e.QueryAccountBalances(ctx)
if err != nil {
return nil, err
}

var account = types.Account{
AccountType: types.AccountTypeSpot,
}

var balanceMap = toGlobalBalance(accountBalance)
account.UpdateBalances(balanceMap)
return &account, nil
account := types.NewAccount()
account.UpdateBalances(bals)
return account, nil
}

func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
accountBalances, err := e.client.AccountBalances(ctx)
if err := queryAccountLimiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("account rate limiter wait error: %w", err)
}

accountBalances, err := e.client.NewGetAccountInfoRequest().Do(ctx)
if err != nil {
return nil, err
}

var balanceMap = toGlobalBalance(accountBalances)
return balanceMap, nil
if len(accountBalances) != 1 {
return nil, fmt.Errorf("unexpected length of balances: %v", accountBalances)
}

return toGlobalBalance(&accountBalances[0]), nil
}

func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
Expand Down
47 changes: 0 additions & 47 deletions pkg/exchange/okex/okexapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/requestgen"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -158,52 +157,6 @@ func (c *RestClient) NewAuthenticatedRequest(ctx context.Context, method, refURL
return req, nil
}

type BalanceDetail struct {
Currency string `json:"ccy"`
Available fixedpoint.Value `json:"availEq"`
CashBalance fixedpoint.Value `json:"cashBal"`
OrderFrozen fixedpoint.Value `json:"ordFrozen"`
Frozen fixedpoint.Value `json:"frozenBal"`
Equity fixedpoint.Value `json:"eq"`
EquityInUSD fixedpoint.Value `json:"eqUsd"`
UpdateTime types.MillisecondTimestamp `json:"uTime"`
UnrealizedProfitAndLoss fixedpoint.Value `json:"upl"`
}

type Account struct {
TotalEquityInUSD fixedpoint.Value `json:"totalEq"`
UpdateTime string `json:"uTime"`
Details []BalanceDetail `json:"details"`
}

func (c *RestClient) AccountBalances(ctx context.Context) (*Account, error) {
req, err := c.NewAuthenticatedRequest(ctx, "GET", "/api/v5/account/balance", nil, nil)
if err != nil {
return nil, err
}

response, err := c.SendRequest(req)
if err != nil {
return nil, err
}

var balanceResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data []Account `json:"data"`
}

if err := response.DecodeJSON(&balanceResponse); err != nil {
return nil, err
}

if len(balanceResponse.Data) == 0 {
return nil, errors.New("empty account data")
}

return &balanceResponse.Data[0], nil
}

type AssetBalance struct {
Currency string `json:"ccy"`
Balance fixedpoint.Value `json:"bal"`
Expand Down
11 changes: 11 additions & 0 deletions pkg/exchange/okex/okexapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ func TestClient_GetMarketTicker(t *testing.T) {
t.Logf("tickers: %+v", tickers)
}

func TestClient_GetAcountInfo(t *testing.T) {
client := getTestClientOrSkip(t)
ctx := context.Background()
req := client.NewGetAccountInfoRequest()

acct, err := req.Do(ctx)
assert.NoError(t, err)
assert.NotEmpty(t, acct)
t.Logf("acct: %+v", acct)
}

func TestClient_GetFundingRateRequest(t *testing.T) {
client := NewClient()
ctx := context.Background()
Expand Down
40 changes: 40 additions & 0 deletions pkg/exchange/okex/okexapi/get_account_info_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package okexapi

import (
"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data

type BalanceDetail struct {
Currency string `json:"ccy"`
Available fixedpoint.Value `json:"availEq"`
CashBalance fixedpoint.Value `json:"cashBal"`
OrderFrozen fixedpoint.Value `json:"ordFrozen"`
Frozen fixedpoint.Value `json:"frozenBal"`
Equity fixedpoint.Value `json:"eq"`
EquityInUSD fixedpoint.Value `json:"eqUsd"`
UpdateTime types.MillisecondTimestamp `json:"uTime"`
UnrealizedProfitAndLoss fixedpoint.Value `json:"upl"`
}

type Account struct {
TotalEquityInUSD fixedpoint.Value `json:"totalEq"`
UpdateTime types.MillisecondTimestamp `json:"uTime"`
Details []BalanceDetail `json:"details"`
}

//go:generate GetRequest -url "/api/v5/account/balance" -type GetAccountInfoRequest -responseDataType []Account
type GetAccountInfoRequest struct {
client requestgen.AuthenticatedAPIClient
}

func (c *RestClient) NewGetAccountInfoRequest() *GetAccountInfoRequest {
return &GetAccountInfoRequest{
client: c,
}
}
157 changes: 157 additions & 0 deletions pkg/exchange/okex/okexapi/get_account_info_request_requestgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/exchange/okex/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Test_parseWebSocketEvent_accountEvent(t *testing.T) {

exp := &okexapi.Account{
TotalEquityInUSD: fixedpoint.NewFromFloat(91884),
UpdateTime: "1614846244194",
UpdateTime: types.NewMillisecondTimestampFromInt(1614846244194),
Details: []okexapi.BalanceDetail{
{
Currency: "BTC",
Expand Down
Loading