Skip to content

Commit

Permalink
++
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Nov 6, 2023
1 parent 3a7d58b commit f82df13
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions x/leverage/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (ap *AccountPosition) MaxBorrow(denom string) sdk.Dec {
if ap.isForLiquidation {
return sdk.ZeroDec()
}

// TODO:
// - improve for cases with special assets
// - check restrictive borrow factor cases
return sdk.ZeroDec()
}

Expand All @@ -197,16 +201,10 @@ func (ap *AccountPosition) HasCollateral(denom string) bool {
// (or liquidation threshold if ap.isForLiquidation is true).
func (ap *AccountPosition) Limit() sdk.Dec {
// compute limit due to collateral weights
limit := ap.normalBorrowLimit()
for _, wsp := range ap.specialPairs {
limit = limit.Add(ap.borrowLimitIncrease(wsp))
}
limit := ap.totalBorrowLimit()

// compute collateral usage due to borrow factors
usage := ap.normalCollateralUsage()
for _, wsp := range ap.specialPairs {
usage = usage.Sub(ap.collateralUsageDecrease(wsp))
}
usage := ap.totalCollateralUsage()

// average collateral weight before special pairs
avgWeight := ap.normalBorrowLimit().Quo(ap.CollateralValue())
Expand Down Expand Up @@ -267,6 +265,15 @@ func (ap *AccountPosition) borrowFactor(denom string) sdk.Dec {
return sdk.ZeroDec()
}

// totalCollateralUsage computes normalCollateralUsage and then applies the effects of special asset pairs.
func (ap *AccountPosition) totalCollateralUsage() sdk.Dec {
usage := ap.normalCollateralUsage()
for _, wsp := range ap.specialPairs {
usage = usage.Sub(ap.collateralUsageDecrease(wsp))
}
return usage
}

// normalCollateralUsage sums the total borrowed value in a position,
// increased according to each token's borrow factor (collateral weight or liquidation threshold),
// or ap.minimumBorrowFactor if greater. Does not use special asset weights for paired assets.
Expand All @@ -285,6 +292,15 @@ func (ap *AccountPosition) normalCollateralUsage() sdk.Dec {
return sum
}

// totalBorrowLimit computes normalBorrowLimit and then applies the effects of special asset pairs.
func (ap *AccountPosition) totalBorrowLimit() sdk.Dec {
limit := ap.normalBorrowLimit()
for _, wsp := range ap.specialPairs {
limit = limit.Add(ap.borrowLimitIncrease(wsp))
}
return limit
}

// normalBorrowLimit sums the total collateral value in a position,
// reduced according to each token's collateral weight or liquidation threshold.
// Does not use special asset weights for paired assets.
Expand Down

0 comments on commit f82df13

Please sign in to comment.