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 {
AccountAlias string `json:"accountAlias"`
Asset string `json:"asset"`
Balance fixedpoint.Value `json:"balance"`
CrossWalletBalance fixedpoint.Value `json:"crossWalletBalance"`
CrossUnPnl fixedpoint.Value `json:"crossUnPnl"`
AvailableBalance fixedpoint.Value `json:"availableBalance"`
MaxWithdrawAmount fixedpoint.Value `json:"maxWithdrawAmount"`
MarginAvailable bool `json:"marginAvailable"`
UpdateTime types.MillisecondTimestamp `json:"updateTime"`
AccountAlias string `json:"accountAlias"`
Asset string `json:"asset"`
// Balance - wallet balance
Balance fixedpoint.Value `json:"balance"`
CrossWalletBalance fixedpoint.Value `json:"crossWalletBalance"`
// 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

View File

@ -68,22 +68,23 @@ func (e *Exchange) QueryFuturesAccount(ctx context.Context) (*types.Account, err
return nil, err
}
// req := e.futuresClient2.NewFuturesGetAccountBalanceRequest()
accountBalances, err := e.futuresClient.NewGetBalanceService().Do(ctx)
req := e.futuresClient2.NewFuturesGetAccountBalanceRequest()
accountBalances, err := req.Do(ctx)
if err != nil {
return nil, err
}
var balances = map[string]types.Balance{}
for _, b := range accountBalances {
balanceAvailable := fixedpoint.Must(fixedpoint.NewFromString(b.AvailableBalance))
balanceTotal := fixedpoint.Must(fixedpoint.NewFromString(b.Balance))
unrealizedPnl := fixedpoint.Must(fixedpoint.NewFromString(b.CrossUnPnl))
// The futures account balance is much different from the spot balance:
// - Balance is the actual balance of the asset
// - 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{
Currency: b.Asset,
Available: balanceAvailable,
Locked: balanceTotal.Sub(balanceAvailable.Sub(unrealizedPnl)),
Currency: b.Asset,
Available: b.AvailableBalance, // AvailableBalance here is the available margin, like how much quantity/notional you can SHORT/LONG, not what you can withdraw
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 fixedpoint.Value `json:"net,omitempty"`
MaxWithdrawAmount fixedpoint.Value `json:"maxWithdrawAmount,omitempty"`
}
func (b Balance) Add(b2 Balance) Balance {