fix: enforce cdp denom matches collateral type (#638)

This commit is contained in:
Kevin Davis 2020-08-27 07:21:35 -04:00 committed by GitHub
parent 5fc85f10a6
commit a53fbc354a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 1 deletions

View File

@ -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)

View File

@ -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

View File

@ -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")
)