Skip to content

Commit

Permalink
pkg/exchange: generate account by requestgen
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Jan 9, 2024
1 parent 188b781 commit 5ce2d39
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 47 deletions.
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 string `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.

0 comments on commit 5ce2d39

Please sign in to comment.