Skip to content

Commit

Permalink
Merge pull request #1456 from c9s/edwin/bitget/get-account-assets
Browse files Browse the repository at this point in the history
FEATURE: [bitget] get account assets
  • Loading branch information
bailantaotao authored Jan 3, 2024
2 parents 22a9ab0 + b5ff066 commit 769d3ce
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 16 deletions.
6 changes: 6 additions & 0 deletions pkg/exchange/bitget/bitgetapi/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
39 changes: 39 additions & 0 deletions pkg/exchange/bitget/bitgetapi/v2/get_account_assets_request.go
Original file line number Diff line number Diff line change
@@ -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}
}

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

7 changes: 3 additions & 4 deletions pkg/exchange/bitget/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 7 additions & 8 deletions pkg/exchange/bitget/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions pkg/exchange/bitget/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,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])
Expand Down Expand Up @@ -237,7 +237,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)
Expand All @@ -246,7 +246,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
Expand Down

0 comments on commit 769d3ce

Please sign in to comment.