Skip to content

Commit

Permalink
zero weight case
Browse files Browse the repository at this point in the history
  • Loading branch information
toteki committed Nov 13, 2023
1 parent 7577d31 commit 497e5be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion x/leverage/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ func (ap *AccountPosition) MaxWithdraw(denom string) (sdk.Dec, bool) {
unusedLimit := ap.totalBorrowLimit().Sub(ap.BorrowedValue()) // unused borrow limit by collateral weight
unusedCollateral := ap.CollateralValue().Sub(ap.totalCollateralUsage()) // unused collateral by borrow factor
// - for borrow limit, withdraw subtracts [collat * weight] from borrow limit
max1 := unusedLimit.Quo(ap.tokenWeight(denom))
max1 := sdk.ZeroDec()
if ap.tokenWeight(denom).IsPositive() {
max1 = unusedLimit.Quo(ap.tokenWeight(denom))
}
// - for borrow factor, withdraw subtracts [collat] from TC
max2 := unusedCollateral
// replace maxWithdraw with the lower of borrow limit and borrow factor results
Expand Down
12 changes: 12 additions & 0 deletions x/leverage/types/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,11 @@ func TestArbitraryCases(t *testing.T) {
coin.Dec("BBBB", collateralB),
coin.Dec("CCCC", collateralC),
)
collat2 := append(collat,
coin.Dec("GGGG", collateralA),
coin.Dec("HHHH", collateralB),
coin.Dec("IIII", collateralC),
)
borrow := sdk.NewDecCoins(
coin.Dec("AAAA", borrowA),
coin.Dec("BBBB", borrowB),
Expand All @@ -883,6 +888,13 @@ func TestArbitraryCases(t *testing.T) {
denom,
fmt.Sprintf("\narbitrary position\n [%s]\n-> \n[%s]\n at %s, w: %s\n",
collat, borrow, min, denom),
}, testCase{
collat2,
borrow,
min,
denom,
fmt.Sprintf("\narbitrary position\n [%s]\n-> \n[%s]\n at %s, w: %s\n",
collat2, borrow, min, denom),
})
// Ensure we aren't making an excessive number of cases
if len(testCases) > 100000 {
Expand Down

0 comments on commit 497e5be

Please sign in to comment.