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)