Skip to content

Commit

Permalink
fixed all position_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Nov 9, 2023
1 parent 28ce193 commit 56bc9c2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
60 changes: 41 additions & 19 deletions x/leverage/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,29 +267,17 @@ func (ap *AccountPosition) Limit() sdk.Dec {
unusedCollateralValue := ap.CollateralValue().Sub(usage) // can be negative

var avgWeight sdk.Dec
if unusedCollateralValue.IsPositive() {
// if user is below limit, unused collateral can be borrowed against at its average collateral weight at most
avgWeight = ap.averageWeight(ap.collateralValue)
if unusedCollateralValue.IsNegative() {
// if user if above limit, overused collateral is being borrowed against at
// the borrow factor of the surplus borrows (which are by definition not paired)
avgWeight = ap.averageBorrowFactor(ap.unpairedBorrows())
} else {
// if user if above limit, overused collateral is being borrowed against at borrow factor
avgWeight = borrowedValue.Quo(usage)
// if user is below limit, unused collateral can be borrowed against at the
// average collateral weight of its unpaired collateral at most
avgWeight = ap.averageWeight(ap.unpairedCollateral())
}
borrowFactorLimit := ap.BorrowedValue().Add(unusedCollateralValue.Mul(avgWeight))

// TODO: clean this up (used to trace through tests)
if len(ap.collateralValue) == 1 && ap.collateralValue[0].Denom == "FFFF" {
if len(ap.borrowedValue) == 1 && ap.borrowedValue[0].Denom == "HHHH" {
fmt.Printf("%s -> %s (%t)\n %s, %s\n >>> %s\n",
ap.collateralValue,
ap.borrowedValue,
ap.isForLiquidation,
limit,
borrowFactorLimit,
usage,
)
}
}

// return the minimum of the two limits
return sdk.MinDec(limit, borrowFactorLimit)
}
Expand Down Expand Up @@ -329,6 +317,26 @@ func (ap *AccountPosition) tokenWeight(denom string) sdk.Dec {
return sdk.ZeroDec()
}

// unpairedBorrows returns an account's borrowed value minus any borrows tied up in special asset pairs
func (ap *AccountPosition) unpairedBorrows() sdk.DecCoins {
total := sdk.NewDecCoins(ap.borrowedValue...)
special := sdk.NewDecCoins()
for _, wsp := range ap.specialPairs {
special = special.Add(wsp.Borrow)
}
return total.Sub(special)
}

// unpairedCollateral returns an account's collateral value minus any collateral tied up in special asset pairs
func (ap *AccountPosition) unpairedCollateral() sdk.DecCoins {
total := sdk.NewDecCoins(ap.collateralValue...)
special := sdk.NewDecCoins()
for _, wsp := range ap.specialPairs {
special = special.Add(wsp.Collateral)
}
return total.Sub(special)
}

// averageWeight gets the weighted average collateral weight (or liquidation threshold) of a set of tokens
func (ap *AccountPosition) averageWeight(coins sdk.DecCoins) sdk.Dec {
if coins.IsZero() {
Expand All @@ -343,6 +351,20 @@ func (ap *AccountPosition) averageWeight(coins sdk.DecCoins) sdk.Dec {
return weightSum.Quo(valueSum)
}

// averageBorrowFactor gets the weighted average borrow factor of a set of tokens
func (ap *AccountPosition) averageBorrowFactor(coins sdk.DecCoins) sdk.Dec {
if coins.IsZero() {
return sdk.OneDec()
}
valueSum := sdk.ZeroDec()
weightSum := sdk.ZeroDec()
for _, c := range coins {
weightSum = weightSum.Add(c.Amount.Mul(ap.borrowFactor(c.Denom)))
valueSum = valueSum.Add(c.Amount)
}
return weightSum.Quo(valueSum)
}

// borrowFactor gets a token's collateral weight or liquidation threshold (or minimumBorrowFactor if greater)
// if the token is registered, else zero.
func (ap *AccountPosition) borrowFactor(denom string) sdk.Dec {
Expand Down
3 changes: 1 addition & 2 deletions x/leverage/types/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ func TestBorrowLimit(t *testing.T) {
coin.Dec("AAAA", "80"),
),
// 80 A would consume 160 F collateral (weight 0.5 due to MinimumBorrowFactor),
// meanwhile 100F on its own would have 60, 65 borrow limit and liquidation threshold.
// The calculation works backwards from the 160/80 collateral usage to find the limit at 100
// The calculation works backwards from the 160/80 collateral usage to find the limit at 100 F
// the F <-> H special pair has no effect
"50.00",
"50.00",
Expand Down

0 comments on commit 56bc9c2

Please sign in to comment.