Skip to content

Commit

Permalink
guard against zero div
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Nov 7, 2023
1 parent de59001 commit 7f0659f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions x/leverage/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func (ap *AccountPosition) Validate() error {
// at or over their borrow limit, returns zero.
// Returns zero if a position was computed with liquidation in mind.
func (ap *AccountPosition) MaxBorrow(denom string) sdk.Dec {
if ap.isForLiquidation {
borrowFactor := ap.borrowFactor(denom)
if ap.isForLiquidation || !borrowFactor.IsPositive() {
return sdk.ZeroDec()
}

Expand All @@ -182,7 +183,7 @@ func (ap *AccountPosition) MaxBorrow(denom string) sdk.Dec {
limit.Sub(ap.BorrowedValue()),
// limited by borrow factor: borrow up to unused collateral / borrow factor
ap.CollateralValue().Sub(usage).Quo(
ap.borrowFactor(denom),
borrowFactor,
),
)

Expand All @@ -206,14 +207,19 @@ func (ap *AccountPosition) MaxWithdraw(denom string) sdk.Dec {
usage := ap.totalCollateralUsage() // collateral usage after special pairs
owned := ap.collateralValue.AmountOf(denom)

collateralWeight := ap.tokenWeight(denom)
if !collateralWeight.IsPositive() {
return owned
}

//
// TODO: withdraw first from normal, then from special pairs, one at a time.
//

// - for borrow limit, subtracting [collat * weight] from borrow limit
// - TODO: for special pairs, subtracting additional [collateral * delta weight]
unusedLimit := limit.Sub(ap.BorrowedValue())
max1 := unusedLimit.Quo(ap.tokenWeight(denom))
max1 := unusedLimit.Quo(collateralWeight)

// - for borrow factor, subtracting [collat] from TC
// - TODO: for special pairs, adding additional [borrow * delta factor] to collateral usage
Expand All @@ -235,12 +241,17 @@ func (ap *AccountPosition) HasCollateral(denom string) bool {
// Limit calculates the borrow limit of an account position
// (or liquidation threshold if ap.isForLiquidation is true).
func (ap *AccountPosition) Limit() sdk.Dec {
collateralValue := ap.CollateralValue()
if !collateralValue.IsPositive() {
return sdk.ZeroDec()
}

// compute limit due to collateral weights
limit := ap.totalBorrowLimit()

// compute limit due to borrow factors
usage := ap.totalCollateralUsage()
avgWeight := ap.normalBorrowLimit().Quo(ap.CollateralValue())
avgWeight := ap.normalBorrowLimit().Quo(collateralValue)
unusedCollateralValue := ap.CollateralValue().Sub(usage) // can be negative
borrowFactorLimit := ap.BorrowedValue().Add(unusedCollateralValue.Mul(avgWeight))

Expand Down Expand Up @@ -367,6 +378,6 @@ func (ap *AccountPosition) collateralUsageDecrease(wsp WeightedSpecialPair) sdk.
return sdk.ZeroDec()
}
// decreases effective collateral usage due to the difference in parameters
return wsp.Borrow.Amount.Quo(ap.borrowFactor(wsp.Borrow.Denom)).Sub( // original usage
return wsp.Borrow.Amount.Quo(borrowFactor).Sub( // original usage
wsp.Borrow.Amount.Quo(wsp.SpecialWeight)) // special usage
}

0 comments on commit 7f0659f

Please sign in to comment.