diff --git a/x/leverage/types/position.go b/x/leverage/types/position.go index 416e821a32..7b0f3d2183 100644 --- a/x/leverage/types/position.go +++ b/x/leverage/types/position.go @@ -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 diff --git a/x/leverage/types/position_test.go b/x/leverage/types/position_test.go index b1ddf382a2..d17048a035 100644 --- a/x/leverage/types/position_test.go +++ b/x/leverage/types/position_test.go @@ -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), @@ -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 {