From a53fbc354a826d344ecd9c0ce24a8c9ac8760c66 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Thu, 27 Aug 2020 07:21:35 -0400 Subject: [PATCH] fix: enforce cdp denom matches collateral type (#638) --- x/cdp/keeper/cdp.go | 3 +++ x/cdp/keeper/cdp_test.go | 4 +++- x/cdp/types/errors.go | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/x/cdp/keeper/cdp.go b/x/cdp/keeper/cdp.go index ee364e82..72e99dba 100644 --- a/x/cdp/keeper/cdp.go +++ b/x/cdp/keeper/cdp.go @@ -350,6 +350,9 @@ func (k Keeper) ValidateCollateral(ctx sdk.Context, collateral sdk.Coin, collate if !found { return sdkerrors.Wrap(types.ErrCollateralNotSupported, collateral.Denom) } + if cp.Denom != collateral.Denom { + return sdkerrors.Wrapf(types.ErrInvalidCollateral, "collateral type: %s expected denom: %s got: %s", collateralType, cp.Denom, collateral.Denom) + } ok := k.GetMarketStatus(ctx, cp.SpotMarketID) if !ok { return sdkerrors.Wrap(types.ErrPricefeedDown, collateral.Denom) diff --git a/x/cdp/keeper/cdp_test.go b/x/cdp/keeper/cdp_test.go index 0bd0139e..e3c754e6 100644 --- a/x/cdp/keeper/cdp_test.go +++ b/x/cdp/keeper/cdp_test.go @@ -47,7 +47,9 @@ func (suite *CdpTestSuite) TestAddCdp() { acc := ak.NewAccountWithAddress(suite.ctx, addrs[0]) acc.SetCoins(cs(c("xrp", 200000000), c("btc", 500000000))) ak.SetAccount(suite.ctx, acc) - err := suite.keeper.AddCdp(suite.ctx, addrs[0], c("xrp", 200000000), c("usdx", 26000000), "xrp-a") + err := suite.keeper.AddCdp(suite.ctx, addrs[0], c("xrp", 200000000), c("usdx", 10000000), "btc-a") + suite.Require().True(errors.Is(err, types.ErrInvalidCollateral)) + err = suite.keeper.AddCdp(suite.ctx, addrs[0], c("xrp", 200000000), c("usdx", 26000000), "xrp-a") suite.Require().True(errors.Is(err, types.ErrInvalidCollateralRatio)) err = suite.keeper.AddCdp(suite.ctx, addrs[0], c("xrp", 500000000), c("usdx", 26000000), "xrp-a") suite.Error(err) // insufficient balance diff --git a/x/cdp/types/errors.go b/x/cdp/types/errors.go index d28416bf..dec900da 100644 --- a/x/cdp/types/errors.go +++ b/x/cdp/types/errors.go @@ -43,4 +43,6 @@ var ( ErrDenomPrefixNotFound = sdkerrors.Register(ModuleName, 18, "denom prefix not found") // ErrPricefeedDown error for when a price for the input denom is not found ErrPricefeedDown = sdkerrors.Register(ModuleName, 19, "no price found for collateral") + // ErrInvalidCollateral error for when the input collateral denom does not match the expected collateral denom + ErrInvalidCollateral = sdkerrors.Register(ModuleName, 20, "invalid collateral for input collateral type") )