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 // 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? // TODO: handle missing trades here. If the process crashed during the transfer, how to recover?
if err := backoff.RetryGeneral(ctx, func() error { 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 { }); err != nil {
log.WithError(err).Errorf("spot-to-futures transfer in retry failed") log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
return return

View File

@ -77,27 +77,20 @@ func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, currency
return nil return nil
} }
func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, currency string, trade types.Trade) error { func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, asset string, quantity fixedpoint.Value) error {
// base asset needs BUY trades
if trade.Side == types.SideTypeSell {
return nil
}
balances, err := s.spotSession.Exchange.QueryAccountBalances(ctx) balances, err := s.spotSession.Exchange.QueryAccountBalances(ctx)
if err != nil { if err != nil {
return err return err
} }
b, ok := balances[currency] b, ok := balances[asset]
if !ok { 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 // 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 { 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) s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return nil return nil
} }
@ -113,8 +106,8 @@ func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, currency
amount = fixedpoint.Min(rest, amount) amount = fixedpoint.Min(rest, amount)
log.Infof("transfering in futures account asset %s %s", amount, currency) log.Infof("transfering in futures account asset %s %s", amount, asset)
if err := ex.TransferFuturesAccountAsset(ctx, currency, amount, types.TransferIn); err != nil { if err := ex.TransferFuturesAccountAsset(ctx, asset, amount, types.TransferIn); err != nil {
return err return err
} }