Skip to content

Commit

Permalink
xfunding: improve transfer logics
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 6, 2024
1 parent 4242f05 commit 4a4f91e
Showing 1 changed file with 49 additions and 67 deletions.
116 changes: 49 additions & 67 deletions pkg/strategy/xfunding/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,61 +43,66 @@ func (s *Strategy) resetTransfer(ctx context.Context, ex FuturesTransfer, asset

func (s *Strategy) transferOut(ctx context.Context, ex FuturesTransfer, asset string, quantity fixedpoint.Value) error {
// if transfer done
// TotalBaseTransfer here is the rest quantity we need to transfer
// (total spot -> futures transfer amount) is recorded in this variable.
//
// TotalBaseTransfer == 0 means we have nothing to transfer.
if s.State.TotalBaseTransfer.IsZero() {
return nil
}

balances, err := s.futuresSession.Exchange.QueryAccountBalances(ctx)
quantity = quantity.Add(s.State.PendingBaseTransfer)

// a simple protection here -- we can only transfer the rest quota (total base transfer) back to spot
quantity = fixedpoint.Min(s.State.TotalBaseTransfer, quantity)

available, pending, err := s.queryAvailableTransfer(ctx, s.futuresSession.Exchange, asset, quantity)
if err != nil {
log.Infof("balance query error, adding to pending base transfer: %s %s + %s", quantity.String(), asset, s.State.PendingBaseTransfer.String())
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
s.State.PendingBaseTransfer = quantity
return err
}

b, ok := balances[asset]
if !ok {
log.Infof("balance not found, adding to pending base transfer: %s %s + %s", quantity.String(), asset, s.State.PendingBaseTransfer.String())
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return fmt.Errorf("%s balance not found", asset)
}

log.Infof("found futures balance: %+v", b)
s.State.PendingBaseTransfer = pending

// add the previous pending base transfer and the current trade quantity
amount := b.MaxWithdrawAmount
if !quantity.IsZero() {
amount = s.State.PendingBaseTransfer.Add(quantity)
log.Infof("transfering out futures account asset %f %s", available.Float64(), asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, available, types.TransferOut); err != nil {
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(available)
return err
}

// try to transfer more if we enough balance
amount = fixedpoint.Min(amount, b.MaxWithdrawAmount)
// reduce the transfer in the total base transfer
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Sub(available)
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()

// we can only transfer the rest quota (total base transfer)
amount = fixedpoint.Min(s.State.TotalBaseTransfer, amount)
// add the pending transfer and reset the pending transfer
quantity = s.State.PendingBaseTransfer.Add(quantity)

// 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 amount.IsZero() {
log.Infof("zero amount, adding to pending base transfer: %s %s + %s ", quantity.String(), asset, s.State.PendingBaseTransfer.String())
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(quantity)
return nil
available, pending, err := s.queryAvailableTransfer(ctx, s.spotSession.Exchange, asset, quantity)
if err != nil {
s.State.PendingBaseTransfer = quantity
return err
}

// de-leverage and get the collateral base quantity
collateralBase := s.FuturesPosition.GetBase().Abs().Div(s.Leverage)
_ = collateralBase
s.State.PendingBaseTransfer = pending

// if s.State.TotalBaseTransfer.Compare(collateralBase)
if available.IsZero() {
return fmt.Errorf("unable to transfer zero %s from spot wallet to futures wallet", asset)
}

log.Infof("transfering out futures account asset %s %s", amount, asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, amount, types.TransferOut); err != nil {
log.Infof("transfering %f %s from the spot wallet into futures wallet...", available.Float64(), asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, available, types.TransferIn); err != nil {
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(available)
return err
}

// reset pending transfer
s.State.PendingBaseTransfer = fixedpoint.Zero

// reduce the transfer in the total base transfer
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Sub(amount)
// record the transfer in the total base transfer
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Add(available)
return nil
}

Expand All @@ -118,18 +123,25 @@ func (s *Strategy) queryAvailableTransfer(
return available, pending, fmt.Errorf("%s balance not found", asset)
}

log.Infof("loaded %s balance: %+v", asset, b)

// if quantity = 0, we will transfer all available balance into the futures wallet
if quantity.IsZero() {
quantity = b.Available
}

if b.Available.Compare(quantity) < 0 {
limit := b.Available
if b.MaxWithdrawAmount.Sign() > 0 {
limit = fixedpoint.Min(b.MaxWithdrawAmount, limit)
}

if limit.Compare(quantity) < 0 {
log.Infof("%s available balance is not enough for transfer (%f < %f)",
asset,
b.Available.Float64(),
quantity.Float64())

available = fixedpoint.Min(b.Available, quantity)
available = fixedpoint.Min(limit, quantity)
pending = quantity.Sub(available)
log.Infof("adjusted transfer quantity from %f to %f", quantity.Float64(), available.Float64())
return available, pending, nil
Expand All @@ -139,33 +151,3 @@ func (s *Strategy) queryAvailableTransfer(
pending = fixedpoint.Zero
return available, pending, 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()

// add the pending transfer and reset the pending transfer
quantity = s.State.PendingBaseTransfer.Add(quantity)

available, pending, err := s.queryAvailableTransfer(ctx, s.spotSession.Exchange, asset, quantity)
if err != nil {
return err
}

s.State.PendingBaseTransfer = pending

if available.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...", available.Float64(), asset)
if err := ex.TransferFuturesAccountAsset(ctx, asset, available, types.TransferIn); err != nil {
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(available)
return err
}

// record the transfer in the total base transfer
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Add(available)
return nil
}

0 comments on commit 4a4f91e

Please sign in to comment.