From 743ad0455fc02f689750e63b18776e62bfb11bf3 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 23 Apr 2022 15:18:25 +0800 Subject: [PATCH] add autoborrow strategy --- pkg/strategy/autoborrow/strategy.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/strategy/autoborrow/strategy.go b/pkg/strategy/autoborrow/strategy.go index 52f9a8e69..9161e8a8d 100644 --- a/pkg/strategy/autoborrow/strategy.go +++ b/pkg/strategy/autoborrow/strategy.go @@ -88,7 +88,7 @@ func (s *Strategy) checkAndBorrow(ctx context.Context) { b, ok := balances[marginAsset.Asset] if ok { - toBorrow := marginAsset.Low - b.Total() + toBorrow := marginAsset.Low.Sub(b.Total()) if toBorrow.Sign() < 0 { log.Debugf("no need to borrow asset %+v", marginAsset) continue @@ -99,6 +99,14 @@ func (s *Strategy) checkAndBorrow(ctx context.Context) { } if !marginAsset.MaxTotalBorrow.IsZero() { + // check if we over borrow + if toBorrow.Add(b.Borrowed).Compare(marginAsset.MaxTotalBorrow) > 0 { + toBorrow = toBorrow.Sub( toBorrow.Add(b.Borrowed).Sub(marginAsset.MaxTotalBorrow) ) + if toBorrow.Sign() < 0 { + log.Warnf("over borrowed, skip borrowing %+v", marginAsset) + continue + } + } toBorrow = fixedpoint.Min(toBorrow.Add(b.Borrowed), marginAsset.MaxTotalBorrow) }