From c5decf9bf804905a7176e9b458a803fcd672e17f Mon Sep 17 00:00:00 2001 From: Edwin Date: Mon, 18 Dec 2023 12:17:49 +0800 Subject: [PATCH 1/3] pkg/exchange: support v2 get asset api --- .../bitget/bitgetapi/v2/client_test.go | 6 + .../v2/get_account_assets_request.go | 39 ++++ .../get_account_assets_request_requestgen.go | 195 ++++++++++++++++++ 3 files changed, 240 insertions(+) create mode 100644 pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go create mode 100644 pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go diff --git a/pkg/exchange/bitget/bitgetapi/v2/client_test.go b/pkg/exchange/bitget/bitgetapi/v2/client_test.go index f8eef7862..3873b1479 100644 --- a/pkg/exchange/bitget/bitgetapi/v2/client_test.go +++ b/pkg/exchange/bitget/bitgetapi/v2/client_test.go @@ -101,4 +101,10 @@ func TestClient(t *testing.T) { assert.NoError(t, err) t.Logf("resp: %+v", resp) }) + + t.Run("GetAccountAssetsRequest", func(t *testing.T) { + resp, err := client.NewGetAccountAssetsRequest().AssetType(AssetTypeHoldOnly).Do(ctx) + assert.NoError(t, err) + t.Logf("resp: %+v", resp) + }) } diff --git a/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go new file mode 100644 index 000000000..d87468143 --- /dev/null +++ b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go @@ -0,0 +1,39 @@ +package bitgetapi + +//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data +//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data + +import ( + "github.com/c9s/requestgen" + + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/types" +) + +type AccountAsset struct { + Coin string `json:"coin"` + Available fixedpoint.Value `json:"available"` + Frozen fixedpoint.Value `json:"frozen"` + Locked fixedpoint.Value `json:"locked"` + LimitAvailable fixedpoint.Value `json:"limitAvailable"` + UpdatedTime types.MillisecondTimestamp `json:"uTime"` +} + +type AssetType string + +const ( + AssetTypeHoldOnly AssetType = "hold_only" + AssetTypeHAll AssetType = "all" +) + +//go:generate GetRequest -url "/api/v2/spot/account/assets" -type GetAccountAssetsRequest -responseDataType []AccountAsset +type GetAccountAssetsRequest struct { + client requestgen.AuthenticatedAPIClient + + coin *string `param:"symbol,query"` + assetType AssetType `param:"limit,query"` +} + +func (c *Client) NewGetAccountAssetsRequest() *GetAccountAssetsRequest { + return &GetAccountAssetsRequest{client: c.Client} +} diff --git a/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go new file mode 100644 index 000000000..945b25675 --- /dev/null +++ b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request_requestgen.go @@ -0,0 +1,195 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Data -url /api/v2/spot/account/assets -type GetAccountAssetsRequest -responseDataType []AccountAsset"; DO NOT EDIT. + +package bitgetapi + +import ( + "context" + "encoding/json" + "fmt" + "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi" + "net/url" + "reflect" + "regexp" +) + +func (c *GetAccountAssetsRequest) Coin(coin string) *GetAccountAssetsRequest { + c.coin = &coin + return c +} + +func (c *GetAccountAssetsRequest) AssetType(assetType AssetType) *GetAccountAssetsRequest { + c.assetType = assetType + return c +} + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (c *GetAccountAssetsRequest) GetQueryParameters() (url.Values, error) { + var params = map[string]interface{}{} + // check coin field -> json key symbol + if c.coin != nil { + coin := *c.coin + + // assign parameter of coin + params["symbol"] = coin + } else { + } + // check assetType field -> json key limit + assetType := c.assetType + + // TEMPLATE check-valid-values + switch assetType { + case AssetTypeHoldOnly, AssetTypeHAll: + params["limit"] = assetType + + default: + return nil, fmt.Errorf("limit value %v is invalid", assetType) + + } + // END TEMPLATE check-valid-values + + // assign parameter of assetType + params["limit"] = assetType + + query := url.Values{} + for _k, _v := range params { + query.Add(_k, fmt.Sprintf("%v", _v)) + } + + return query, nil +} + +// GetParameters builds and checks the parameters and return the result in a map object +func (c *GetAccountAssetsRequest) GetParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +// GetParametersQuery converts the parameters from GetParameters into the url.Values format +func (c *GetAccountAssetsRequest) GetParametersQuery() (url.Values, error) { + query := url.Values{} + + params, err := c.GetParameters() + if err != nil { + return query, err + } + + for _k, _v := range params { + if c.isVarSlice(_v) { + c.iterateSlice(_v, func(it interface{}) { + query.Add(_k+"[]", fmt.Sprintf("%v", it)) + }) + } else { + query.Add(_k, fmt.Sprintf("%v", _v)) + } + } + + return query, nil +} + +// GetParametersJSON converts the parameters from GetParameters into the JSON format +func (c *GetAccountAssetsRequest) GetParametersJSON() ([]byte, error) { + params, err := c.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} + +// GetSlugParameters builds and checks the slug parameters and return the result in a map object +func (c *GetAccountAssetsRequest) GetSlugParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +func (c *GetAccountAssetsRequest) applySlugsToUrl(url string, slugs map[string]string) string { + for _k, _v := range slugs { + needleRE := regexp.MustCompile(":" + _k + "\\b") + url = needleRE.ReplaceAllString(url, _v) + } + + return url +} + +func (c *GetAccountAssetsRequest) iterateSlice(slice interface{}, _f func(it interface{})) { + sliceValue := reflect.ValueOf(slice) + for _i := 0; _i < sliceValue.Len(); _i++ { + it := sliceValue.Index(_i).Interface() + _f(it) + } +} + +func (c *GetAccountAssetsRequest) isVarSlice(_v interface{}) bool { + rt := reflect.TypeOf(_v) + switch rt.Kind() { + case reflect.Slice: + return true + } + return false +} + +func (c *GetAccountAssetsRequest) GetSlugsMap() (map[string]string, error) { + slugs := map[string]string{} + params, err := c.GetSlugParameters() + if err != nil { + return slugs, nil + } + + for _k, _v := range params { + slugs[_k] = fmt.Sprintf("%v", _v) + } + + return slugs, nil +} + +// GetPath returns the request path of the API +func (c *GetAccountAssetsRequest) GetPath() string { + return "/api/v2/spot/account/assets" +} + +// Do generates the request object and send the request object to the API endpoint +func (c *GetAccountAssetsRequest) Do(ctx context.Context) ([]AccountAsset, error) { + + // no body params + var params interface{} + query, err := c.GetQueryParameters() + if err != nil { + return nil, err + } + + var apiURL string + + apiURL = c.GetPath() + + req, err := c.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := c.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse bitgetapi.APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + + type responseValidator interface { + Validate() error + } + validator, ok := interface{}(apiResponse).(responseValidator) + if ok { + if err := validator.Validate(); err != nil { + return nil, err + } + } + var data []AccountAsset + if err := json.Unmarshal(apiResponse.Data, &data); err != nil { + return nil, err + } + return data, nil +} From 30164acdcff551e74dc9a510a9cac313c8d002cc Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 3 Jan 2024 11:25:46 +0800 Subject: [PATCH 2/3] pkg/exchange: use v2 get account asset api --- .../bitgetapi/v2/get_account_assets_request.go | 2 +- pkg/exchange/bitget/convert.go | 7 +++---- pkg/exchange/bitget/convert_test.go | 15 +++++++-------- pkg/exchange/bitget/exchange.go | 4 ++-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go index d87468143..51ab05806 100644 --- a/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go +++ b/pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go @@ -31,7 +31,7 @@ type GetAccountAssetsRequest struct { client requestgen.AuthenticatedAPIClient coin *string `param:"symbol,query"` - assetType AssetType `param:"limit,query"` + assetType AssetType `param:"assetType,query"` } func (c *Client) NewGetAccountAssetsRequest() *GetAccountAssetsRequest { diff --git a/pkg/exchange/bitget/convert.go b/pkg/exchange/bitget/convert.go index c0167d16f..9e6a65504 100644 --- a/pkg/exchange/bitget/convert.go +++ b/pkg/exchange/bitget/convert.go @@ -7,17 +7,16 @@ import ( "strconv" "time" - "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi" v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" ) -func toGlobalBalance(asset bitgetapi.AccountAsset) types.Balance { +func toGlobalBalance(asset v2.AccountAsset) types.Balance { return types.Balance{ - Currency: asset.CoinName, + Currency: asset.Coin, Available: asset.Available, - Locked: asset.Lock.Add(asset.Frozen), + Locked: asset.Locked.Add(asset.Frozen), Borrowed: fixedpoint.Zero, Interest: fixedpoint.Zero, NetAsset: fixedpoint.Zero, diff --git a/pkg/exchange/bitget/convert_test.go b/pkg/exchange/bitget/convert_test.go index a0898e21d..cf3f0ab94 100644 --- a/pkg/exchange/bitget/convert_test.go +++ b/pkg/exchange/bitget/convert_test.go @@ -7,7 +7,6 @@ import ( "github.com/stretchr/testify/assert" - "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi" v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" @@ -23,13 +22,13 @@ func Test_toGlobalBalance(t *testing.T) { // "lock":"0", // "uTime":"1622697148" // } - asset := bitgetapi.AccountAsset{ - CoinId: 2, - CoinName: "USDT", - Available: fixedpoint.NewFromFloat(1.2), - Frozen: fixedpoint.NewFromFloat(0.5), - Lock: fixedpoint.NewFromFloat(0.5), - UTime: types.NewMillisecondTimestampFromInt(1622697148), + asset := v2.AccountAsset{ + Coin: "USDT", + Available: fixedpoint.NewFromFloat(1.2), + Frozen: fixedpoint.NewFromFloat(0.5), + Locked: fixedpoint.NewFromFloat(0.5), + LimitAvailable: fixedpoint.Zero, + UpdatedTime: types.NewMillisecondTimestampFromInt(1622697148), } assert.Equal(t, types.Balance{ diff --git a/pkg/exchange/bitget/exchange.go b/pkg/exchange/bitget/exchange.go index dc0ca8e7a..4cacd200d 100644 --- a/pkg/exchange/bitget/exchange.go +++ b/pkg/exchange/bitget/exchange.go @@ -254,7 +254,7 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, return nil, fmt.Errorf("account rate limiter wait error: %w", err) } - req := e.client.NewGetAccountAssetsRequest() + req := e.v2client.NewGetAccountAssetsRequest().AssetType(v2.AssetTypeHoldOnly) resp, err := req.Do(ctx) if err != nil { return nil, fmt.Errorf("failed to query account assets: %w", err) @@ -263,7 +263,7 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, bals := types.BalanceMap{} for _, asset := range resp { b := toGlobalBalance(asset) - bals[asset.CoinName] = b + bals[asset.Coin] = b } return bals, nil From b5ff066aa23697b8b335ff6c00fcf3134db04a97 Mon Sep 17 00:00:00 2001 From: Edwin Date: Wed, 3 Jan 2024 11:30:50 +0800 Subject: [PATCH 3/3] pkg/exchange: print symbol --- pkg/exchange/bitget/exchange.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/exchange/bitget/exchange.go b/pkg/exchange/bitget/exchange.go index 4cacd200d..38b96bf42 100644 --- a/pkg/exchange/bitget/exchange.go +++ b/pkg/exchange/bitget/exchange.go @@ -143,10 +143,10 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke req.Symbol(symbol) resp, err := req.Do(ctx) if err != nil { - return nil, fmt.Errorf("failed to query ticker: %w", err) + return nil, fmt.Errorf("failed to query ticker, symbol: %s, err: %w", symbol, err) } if len(resp) != 1 { - return nil, fmt.Errorf("unexpected length of query single symbol: %+v", resp) + return nil, fmt.Errorf("unexpected length of query single symbol: %s, resp: %+v", symbol, resp) } ticker := toGlobalTicker(resp[0])