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: [bitget] get account assets #1456

Merged
merged 3 commits into from
Jan 3, 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
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 @@ -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])
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
Loading