Skip to content

Commit

Permalink
Merge pull request #1844 from c9s/c9s/d2t/repayment
Browse files Browse the repository at this point in the history
FEATURE: [deposit2transfer] support autoRepay
  • Loading branch information
c9s authored Nov 29, 2024
2 parents c0ee3a1 + c5b783e commit ce20d10
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pkg/strategy/deposit2transfer/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ type Strategy struct {
IgnoreDust bool `json:"ignoreDust"`
DustAmounts map[string]fixedpoint.Value `json:"dustAmounts"`

AutoRepay bool `json:"autoRepay"`

SlackAlert *SlackAlert `json:"slackAlert"`

marginTransferService marginTransferService
marginTransferService marginTransferService
marginBorrowRepayService types.MarginBorrowRepayService

depositHistoryService types.ExchangeTransferService

session *bbgo.ExchangeSession
Expand Down Expand Up @@ -129,6 +133,8 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return errMarginTransferNotSupport
}

s.marginBorrowRepayService, _ = session.Exchange.(types.MarginBorrowRepayService)

s.depositHistoryService, ok = session.Exchange.(types.ExchangeTransferService)
if !ok {
return errDepositHistoryNotSupport
Expand Down Expand Up @@ -263,6 +269,30 @@ func (s *Strategy) checkDeposits(ctx context.Context) {
livenote.Comment(fmt.Sprintf("✅ %s %s transferred successfully", amount.String(), d.Asset)),
)
}

if s.AutoRepay && s.marginBorrowRepayService != nil {
s.logger.Infof("autoRepay is enabled, repaying %s %s...", amount.String(), d.Asset)

if err2 := s.marginBorrowRepayService.RepayMarginAsset(ctx, d.Asset, amount); err2 != nil {
s.logger.WithError(err).Errorf("unable to repay the margin asset")

if s.SlackAlert != nil {
bbgo.PostLiveNote(&d,
livenote.Channel(s.SlackAlert.Channel),
livenote.Comment(fmt.Sprintf("❌ Unable to repay, error: %+v", err2)),
)
}
} else {
s.logger.Infof("%s %s repayed successfully", amount.String(), d.Asset)

if s.SlackAlert != nil {
bbgo.PostLiveNote(&d,
livenote.Channel(s.SlackAlert.Channel),
livenote.Comment(fmt.Sprintf("✅ %s %s repayed successfully", amount.String(), d.Asset)),
)
}
}
}
}
}
}
Expand Down

0 comments on commit ce20d10

Please sign in to comment.