From 58deb49e5553f99d059dc90fe2cdc2c6271b724d Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Wed, 22 Jan 2020 16:56:30 +0000 Subject: [PATCH] Fix withdraw panic (#315) * fix: remove redundant debt limit param * wip: test pricefeed genesis * fix: pricefeed querier * fix: comments, naming * fix: query path * fix: store methods * fix: query methods * feat: Liquidation Penalty * feat: enforce debt floor on repayment * fix: don't panic if withdrawing full amount * fix: remove debt from liquidation penalty --- x/cdp/keeper/cdp.go | 3 +++ x/cdp/keeper/deposit_test.go | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/x/cdp/keeper/cdp.go b/x/cdp/keeper/cdp.go index 0b41943f..0675154d 100644 --- a/x/cdp/keeper/cdp.go +++ b/x/cdp/keeper/cdp.go @@ -412,6 +412,9 @@ func (k Keeper) CalculateCollateralToDebtRatio(ctx sdk.Context, collateral sdk.C // CalculateCollateralizationRatio returns the collateralization ratio of the input collateral to the input debt plus fees func (k Keeper) CalculateCollateralizationRatio(ctx sdk.Context, collateral sdk.Coins, principal sdk.Coins, fees sdk.Coins) (sdk.Dec, sdk.Error) { + if collateral.IsZero() { + return sdk.ZeroDec(), nil + } marketID := k.getMarketID(ctx, collateral[0].Denom) price, err := k.pricefeedKeeper.GetCurrentPrice(ctx, marketID) if err != nil { diff --git a/x/cdp/keeper/deposit_test.go b/x/cdp/keeper/deposit_test.go index fab85c8a..d9a5e6b0 100644 --- a/x/cdp/keeper/deposit_test.go +++ b/x/cdp/keeper/deposit_test.go @@ -93,7 +93,9 @@ func (suite *DepositTestSuite) TestDepositCollateral() { } func (suite *DepositTestSuite) TestWithdrawCollateral() { - err := suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], cs(c("xrp", 321000000))) + err := suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], cs(c("xrp", 400000000))) + suite.Equal(types.CodeInvalidCollateralRatio, err.Result().Code) + err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[0], suite.addrs[0], cs(c("xrp", 321000000))) suite.Equal(types.CodeInvalidCollateralRatio, err.Result().Code) err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[1], suite.addrs[0], cs(c("xrp", 10000000))) suite.Equal(types.CodeCdpNotFound, err.Result().Code)