binance: use requestgen api to query futures balances

This commit is contained in:
c9s 2023-03-29 22:45:40 +08:00
parent 86c5ba603e
commit 321425709a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 31 additions and 18 deletions

View File

@ -8,15 +8,25 @@ import (
) )
type FuturesBalance struct { type FuturesBalance struct {
AccountAlias string `json:"accountAlias"` AccountAlias string `json:"accountAlias"`
Asset string `json:"asset"` Asset string `json:"asset"`
Balance fixedpoint.Value `json:"balance"`
CrossWalletBalance fixedpoint.Value `json:"crossWalletBalance"` // Balance - wallet balance
CrossUnPnl fixedpoint.Value `json:"crossUnPnl"` Balance fixedpoint.Value `json:"balance"`
AvailableBalance fixedpoint.Value `json:"availableBalance"`
MaxWithdrawAmount fixedpoint.Value `json:"maxWithdrawAmount"` CrossWalletBalance fixedpoint.Value `json:"crossWalletBalance"`
MarginAvailable bool `json:"marginAvailable"`
UpdateTime types.MillisecondTimestamp `json:"updateTime"` // CrossUnPnL unrealized profit of crossed positions
CrossUnPnl fixedpoint.Value `json:"crossUnPnl"`
AvailableBalance fixedpoint.Value `json:"availableBalance"`
// MaxWithdrawAmount - maximum amount for transfer out
MaxWithdrawAmount fixedpoint.Value `json:"maxWithdrawAmount"`
// MarginAvailable - whether the asset can be used as margin in Multi-Assets mode
MarginAvailable bool `json:"marginAvailable"`
UpdateTime types.MillisecondTimestamp `json:"updateTime"`
} }
//go:generate requestgen -method GET -url "/fapi/v2/balance" -type FuturesGetAccountBalanceRequest -responseType []FuturesBalance //go:generate requestgen -method GET -url "/fapi/v2/balance" -type FuturesGetAccountBalanceRequest -responseType []FuturesBalance

View File

@ -68,22 +68,23 @@ func (e *Exchange) QueryFuturesAccount(ctx context.Context) (*types.Account, err
return nil, err return nil, err
} }
// req := e.futuresClient2.NewFuturesGetAccountBalanceRequest() req := e.futuresClient2.NewFuturesGetAccountBalanceRequest()
accountBalances, err := req.Do(ctx)
accountBalances, err := e.futuresClient.NewGetBalanceService().Do(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var balances = map[string]types.Balance{} var balances = map[string]types.Balance{}
for _, b := range accountBalances { for _, b := range accountBalances {
balanceAvailable := fixedpoint.Must(fixedpoint.NewFromString(b.AvailableBalance)) // The futures account balance is much different from the spot balance:
balanceTotal := fixedpoint.Must(fixedpoint.NewFromString(b.Balance)) // - Balance is the actual balance of the asset
unrealizedPnl := fixedpoint.Must(fixedpoint.NewFromString(b.CrossUnPnl)) // - AvailableBalance is the available margin balance (can be used as notional)
// - CrossWalletBalance (this will be meaningful when using isolated margin)
balances[b.Asset] = types.Balance{ balances[b.Asset] = types.Balance{
Currency: b.Asset, Currency: b.Asset,
Available: balanceAvailable, Available: b.AvailableBalance, // AvailableBalance here is the available margin, like how much quantity/notional you can SHORT/LONG, not what you can withdraw
Locked: balanceTotal.Sub(balanceAvailable.Sub(unrealizedPnl)), Locked: b.Balance.Sub(b.AvailableBalance.Sub(b.CrossUnPnl)), // FIXME: AvailableBalance is the available margin balance, it could be re-calculated by the current formula.
MaxWithdrawAmount: b.MaxWithdrawAmount,
} }
} }

View File

@ -25,6 +25,8 @@ type Balance struct {
// NetAsset = (Available + Locked) - Borrowed - Interest // NetAsset = (Available + Locked) - Borrowed - Interest
NetAsset fixedpoint.Value `json:"net,omitempty"` NetAsset fixedpoint.Value `json:"net,omitempty"`
MaxWithdrawAmount fixedpoint.Value `json:"maxWithdrawAmount,omitempty"`
} }
func (b Balance) Add(b2 Balance) Balance { func (b Balance) Add(b2 Balance) Balance {