Skip to content

Commit

Permalink
autoborrow: add minDebtAmount condition
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 27, 2024
1 parent 4945391 commit 0055712
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
36 changes: 22 additions & 14 deletions pkg/strategy/autoborrow/alert_interest_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,34 @@ func (w *MarginHighInterestRateWorker) Run(ctx context.Context) {

log.Infof("found high interest rate assets: %+v", highRateAssets)

shouldAlert := func() bool { return len(highRateAssets) > 0 }
totalDebtValue := fixedpoint.Zero
nextTotalInterestValue := fixedpoint.Zero
debts := w.session.Account.Balances().Debts()
for cur, bal := range debts {
price, ok := w.session.GetPriceSolver().ResolvePrice(cur, currency.USDT)
if !ok {
log.Warnf("unable to resolve price for %s", cur)
continue
}

rate := rateMap[cur]
nextTotalInterestValue = nextTotalInterestValue.Add(
bal.Debt().Mul(rate.HourlyRate).Mul(price))

totalDebtValue = totalDebtValue.Add(bal.Debt().Mul(price))
}

shouldAlert := func() bool {
return len(highRateAssets) > 0 &&
w.config.MinDebtAmount.Sign() > 0 &&
totalDebtValue.Compare(w.config.MinDebtAmount) > 0
}

// either danger or margin level is less than the minimal margin level
// if the previous danger is set to true, we should send the alert again to
// update the previous danger margin alert message
if danger || shouldAlert() {
// calculate the debt value by the price solver
nextTotalInterestValue := fixedpoint.Zero
debts := w.session.Account.Balances().Debts()
for cur, bal := range debts {
price, ok := w.session.GetPriceSolver().ResolvePrice(cur, currency.USDT)
if !ok {
log.Warnf("unable to resolve price for %s", cur)
continue
}

rate := rateMap[cur]
nextTotalInterestValue = nextTotalInterestValue.Add(
bal.Debt().Mul(rate.HourlyRate).Mul(price))
}

alert := &MarginHighInterestRateAlert{
AlertID: alertId,
Expand Down
2 changes: 2 additions & 0 deletions pkg/strategy/autoborrow/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type MarginHighInterestRateAlertConfig struct {

Interval types.Duration `json:"interval"`

MinDebtAmount fixedpoint.Value `json:"minDebtAmount"`

MinAnnualInterestRate fixedpoint.Value `json:"minAnnualInterestRate"`
}

Expand Down

0 comments on commit 0055712

Please sign in to comment.