xfunding: rewrite transferIn method

This commit is contained in:
c9s 2024-03-06 21:01:44 +08:00
parent 8c517179dd
commit b2c6dce350
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -101,7 +101,12 @@ func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, asset st
return nil
}
// transferIn transfers the asset from the spot account to the futures account
func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, asset string, quantity fixedpoint.Value) error {
s.mu.Lock()
defer s.mu.Unlock()
// query spot balances to validate the quantity
balances, err := s.spotSession.Exchange.QueryAccountBalances(ctx)
if err != nil {
return err
@ -112,35 +117,42 @@ func (s *Strategy) transferIn(ctx context.Context, ex FuturesTransfer, asset str
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
if !quantity.IsZero() && b.Available.Compare(quantity) < 0 {
log.Infof("adding to pending base transfer: %s %s", quantity, asset)
// if quantity = 0, we will transfer all available balance into the futures wallet
if quantity.IsZero() {
quantity = b.Available
}
// add the pending transfer and reset the pending transfer
quantity = s.State.PendingBaseTransfer.Add(quantity)
s.State.PendingBaseTransfer = fixedpoint.Zero
// the available might not be "available" at the time point,
// we add the quantity to the pending transfer amount for the next tick.
if b.Available.Compare(quantity) < 0 {
log.Infof("%s available balance is not enough for transfer (%f < %f)",
asset,
b.Available.Float64(),
quantity.Float64())
availableToTransfer := fixedpoint.Min(b.Available, quantity)
pendingTransfer := quantity.Sub(availableToTransfer)
log.Infof("adjusted transfer quantity from %f to %f", quantity.Float64(), availableToTransfer.Float64())
quantity = availableToTransfer
s.State.PendingBaseTransfer = pendingTransfer
}
if quantity.IsZero() {
return fmt.Errorf("unable to transfer zero %s from spot wallet to futures wallet", asset)
}
log.Infof("transfering %f %s from the spot wallet into futures wallet...", quantity.Float64(), asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, quantity, types.TransferIn); err != nil {
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return nil
}
amount := b.Available
if !quantity.IsZero() {
amount = s.State.PendingBaseTransfer.Add(quantity)
}
pos := s.SpotPosition.GetBase().Abs()
rest := pos.Sub(s.State.TotalBaseTransfer)
if rest.Sign() < 0 {
return nil
}
amount = fixedpoint.Min(rest, amount)
log.Infof("transfering in futures account asset %s %s", amount, asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, amount, types.TransferIn); err != nil {
return err
}
// reset pending transfer
s.State.PendingBaseTransfer = fixedpoint.Zero
// record the transfer in the total base transfer
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Add(amount)
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Add(quantity)
return nil
}