xfunding: refactor transferIn

This commit is contained in:
c9s 2023-03-26 17:49:33 +08:00
parent ce0b73b6e4
commit 088a36a169
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 7 additions and 14 deletions

View File

@ -376,7 +376,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
// if we have trade, try to query the balance and transfer the balance to the futures wallet account
// TODO: handle missing trades here. If the process crashed during the transfer, how to recover?
if err := backoff.RetryGeneral(ctx, func() error {
return s.transferIn(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, trade)
return s.transferIn(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, trade.Quantity)
}); err != nil {
log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
return

View File

@ -77,27 +77,20 @@ func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, currency
return nil
}
func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, currency string, trade types.Trade) error {
// base asset needs BUY trades
if trade.Side == types.SideTypeSell {
return nil
}
func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, asset string, quantity fixedpoint.Value) error {
balances, err := s.spotSession.Exchange.QueryAccountBalances(ctx)
if err != nil {
return err
}
b, ok := balances[currency]
b, ok := balances[asset]
if !ok {
return fmt.Errorf("%s balance not found", currency)
return fmt.Errorf("%s balance not found", asset)
}
// TODO: according to the fee, we might not be able to get enough balance greater than the trade quantity, we can adjust the quantity here
quantity := trade.Quantity
if b.Available.Compare(quantity) < 0 {
log.Infof("adding to pending base transfer: %s %s", quantity, currency)
log.Infof("adding to pending base transfer: %s %s", quantity, asset)
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return nil
}
@ -113,8 +106,8 @@ func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, currency
amount = fixedpoint.Min(rest, amount)
log.Infof("transfering in futures account asset %s %s", amount, currency)
if err := ex.TransferFuturesAccountAsset(ctx, currency, amount, types.TransferIn); err != nil {
log.Infof("transfering in futures account asset %s %s", amount, asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, amount, types.TransferIn); err != nil {
return err
}