From 00557125e0f5c7f3e0ab52b3ad4cacb0ab15e469 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 27 Dec 2024 17:21:37 +0800 Subject: [PATCH] autoborrow: add minDebtAmount condition --- .../autoborrow/alert_interest_rate.go | 36 +++++++++++-------- pkg/strategy/autoborrow/strategy.go | 2 ++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pkg/strategy/autoborrow/alert_interest_rate.go b/pkg/strategy/autoborrow/alert_interest_rate.go index 0e62480bb..7cff4912a 100644 --- a/pkg/strategy/autoborrow/alert_interest_rate.go +++ b/pkg/strategy/autoborrow/alert_interest_rate.go @@ -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, diff --git a/pkg/strategy/autoborrow/strategy.go b/pkg/strategy/autoborrow/strategy.go index 1576bd9eb..ac7f89c2b 100644 --- a/pkg/strategy/autoborrow/strategy.go +++ b/pkg/strategy/autoborrow/strategy.go @@ -64,6 +64,8 @@ type MarginHighInterestRateAlertConfig struct { Interval types.Duration `json:"interval"` + MinDebtAmount fixedpoint.Value `json:"minDebtAmount"` + MinAnnualInterestRate fixedpoint.Value `json:"minAnnualInterestRate"` }