Merge pull request #1456 from c9s/edwin/bitget/get-account-assets

FEATURE: [bitget] get account assets
This commit is contained in:
bailantaotao 2024-01-03 13:01:35 +08:00 committed by GitHub
commit 769d3ce2d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 254 additions and 16 deletions

View File

@ -101,4 +101,10 @@ func TestClient(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
t.Logf("resp: %+v", resp) 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)
})
} }

View File

@ -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:"assetType,query"`
}
func (c *Client) NewGetAccountAssetsRequest() *GetAccountAssetsRequest {
return &GetAccountAssetsRequest{client: c.Client}
}

View File

@ -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
}

View File

@ -7,17 +7,16 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2" v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
func toGlobalBalance(asset bitgetapi.AccountAsset) types.Balance { func toGlobalBalance(asset v2.AccountAsset) types.Balance {
return types.Balance{ return types.Balance{
Currency: asset.CoinName, Currency: asset.Coin,
Available: asset.Available, Available: asset.Available,
Locked: asset.Lock.Add(asset.Frozen), Locked: asset.Locked.Add(asset.Frozen),
Borrowed: fixedpoint.Zero, Borrowed: fixedpoint.Zero,
Interest: fixedpoint.Zero, Interest: fixedpoint.Zero,
NetAsset: fixedpoint.Zero, NetAsset: fixedpoint.Zero,

View File

@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi"
v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2" v2 "github.com/c9s/bbgo/pkg/exchange/bitget/bitgetapi/v2"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
@ -23,13 +22,13 @@ func Test_toGlobalBalance(t *testing.T) {
// "lock":"0", // "lock":"0",
// "uTime":"1622697148" // "uTime":"1622697148"
// } // }
asset := bitgetapi.AccountAsset{ asset := v2.AccountAsset{
CoinId: 2, Coin: "USDT",
CoinName: "USDT", Available: fixedpoint.NewFromFloat(1.2),
Available: fixedpoint.NewFromFloat(1.2), Frozen: fixedpoint.NewFromFloat(0.5),
Frozen: fixedpoint.NewFromFloat(0.5), Locked: fixedpoint.NewFromFloat(0.5),
Lock: fixedpoint.NewFromFloat(0.5), LimitAvailable: fixedpoint.Zero,
UTime: types.NewMillisecondTimestampFromInt(1622697148), UpdatedTime: types.NewMillisecondTimestampFromInt(1622697148),
} }
assert.Equal(t, types.Balance{ assert.Equal(t, types.Balance{

View File

@ -126,10 +126,10 @@ func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticke
req.Symbol(symbol) req.Symbol(symbol)
resp, err := req.Do(ctx) resp, err := req.Do(ctx)
if err != nil { 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 { 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]) ticker := toGlobalTicker(resp[0])
@ -237,7 +237,7 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap,
return nil, fmt.Errorf("account rate limiter wait error: %w", err) 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) resp, err := req.Do(ctx)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to query account assets: %w", err) return nil, fmt.Errorf("failed to query account assets: %w", err)
@ -246,7 +246,7 @@ func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap,
bals := types.BalanceMap{} bals := types.BalanceMap{}
for _, asset := range resp { for _, asset := range resp {
b := toGlobalBalance(asset) b := toGlobalBalance(asset)
bals[asset.CoinName] = b bals[asset.Coin] = b
} }
return bals, nil return bals, nil