fix: enforce max number of liquidations in cdp begin blocker (#849)

* fix: enforce max number of liquidations in cdp begin blocker

* address review comments
This commit is contained in:
Kevin Davis 2021-02-23 12:39:39 -07:00 committed by GitHub
parent 3a3dd7970d
commit e4daffcaf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 8 deletions

View File

@ -35,7 +35,7 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {
panic(err) panic(err)
} }
err = k.LiquidateCdps(ctx, cp.LiquidationMarketID, cp.Type, cp.LiquidationRatio) err = k.LiquidateCdps(ctx, cp.LiquidationMarketID, cp.Type, cp.LiquidationRatio, cp.CheckCollateralizationIndexCount)
if err != nil && !errors.Is(err, pricefeedtypes.ErrNoValidPrice) { if err != nil && !errors.Is(err, pricefeedtypes.ErrNoValidPrice) {
panic(err) panic(err)
} }

View File

@ -135,7 +135,7 @@ func (suite *ModuleTestSuite) TestBeginBlock() {
finalXrpCollateral := acc.GetCoins().AmountOf("xrp") finalXrpCollateral := acc.GetCoins().AmountOf("xrp")
seizedXrpCollateral := originalXrpCollateral.Sub(finalXrpCollateral) seizedXrpCollateral := originalXrpCollateral.Sub(finalXrpCollateral)
xrpLiquidations := int(seizedXrpCollateral.Quo(i(10000000000)).Int64()) xrpLiquidations := int(seizedXrpCollateral.Quo(i(10000000000)).Int64())
suite.Equal(len(suite.liquidations.xrp), xrpLiquidations) suite.Equal(10, xrpLiquidations)
acc = sk.GetModuleAccount(suite.ctx, cdp.ModuleName) acc = sk.GetModuleAccount(suite.ctx, cdp.ModuleName)
originalBtcCollateral := acc.GetCoins().AmountOf("btc") originalBtcCollateral := acc.GetCoins().AmountOf("btc")
@ -145,10 +145,10 @@ func (suite *ModuleTestSuite) TestBeginBlock() {
finalBtcCollateral := acc.GetCoins().AmountOf("btc") finalBtcCollateral := acc.GetCoins().AmountOf("btc")
seizedBtcCollateral := originalBtcCollateral.Sub(finalBtcCollateral) seizedBtcCollateral := originalBtcCollateral.Sub(finalBtcCollateral)
btcLiquidations := int(seizedBtcCollateral.Quo(i(100000000)).Int64()) btcLiquidations := int(seizedBtcCollateral.Quo(i(100000000)).Int64())
suite.Equal(len(suite.liquidations.btc), btcLiquidations) suite.Equal(10, btcLiquidations)
acc = sk.GetModuleAccount(suite.ctx, auction.ModuleName) acc = sk.GetModuleAccount(suite.ctx, auction.ModuleName)
suite.Equal(suite.liquidations.debt, acc.GetCoins().AmountOf("debt").Int64()) suite.Equal(int64(71955653865), acc.GetCoins().AmountOf("debt").Int64())
} }

View File

@ -87,7 +87,7 @@ func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) error {
} }
// LiquidateCdps seizes collateral from all CDPs below the input liquidation ratio // LiquidateCdps seizes collateral from all CDPs below the input liquidation ratio
func (k Keeper) LiquidateCdps(ctx sdk.Context, marketID string, collateralType string, liquidationRatio sdk.Dec) error { func (k Keeper) LiquidateCdps(ctx sdk.Context, marketID string, collateralType string, liquidationRatio sdk.Dec, count sdk.Int) error {
price, err := k.pricefeedKeeper.GetCurrentPrice(ctx, marketID) price, err := k.pricefeedKeeper.GetCurrentPrice(ctx, marketID)
if err != nil { if err != nil {
return err return err
@ -100,7 +100,7 @@ func (k Keeper) LiquidateCdps(ctx sdk.Context, marketID string, collateralType s
// liquidation ratio = 1.5 // liquidation ratio = 1.5
// normalizedRatio = (1/(0.5/1.5)) = 3 // normalizedRatio = (1/(0.5/1.5)) = 3
normalizedRatio := sdk.OneDec().Quo(priceDivLiqRatio) normalizedRatio := sdk.OneDec().Quo(priceDivLiqRatio)
cdpsToLiquidate := k.GetAllCdpsByCollateralTypeAndRatio(ctx, collateralType, normalizedRatio) cdpsToLiquidate := k.GetSliceOfCDPsByRatioAndType(ctx, count, normalizedRatio, collateralType)
for _, c := range cdpsToLiquidate { for _, c := range cdpsToLiquidate {
k.hooks.BeforeCDPModified(ctx, c) k.hooks.BeforeCDPModified(ctx, c)
err := k.SeizeCollateral(ctx, c) err := k.SeizeCollateral(ctx, c)

View File

@ -188,12 +188,12 @@ func (suite *SeizeTestSuite) TestLiquidateCdps() {
suite.setPrice(d("0.2"), "xrp:usd") suite.setPrice(d("0.2"), "xrp:usd")
p, found := suite.keeper.GetCollateral(suite.ctx, "xrp-a") p, found := suite.keeper.GetCollateral(suite.ctx, "xrp-a")
suite.True(found) suite.True(found)
suite.keeper.LiquidateCdps(suite.ctx, "xrp:usd", "xrp-a", p.LiquidationRatio) suite.keeper.LiquidateCdps(suite.ctx, "xrp:usd", "xrp-a", p.LiquidationRatio, p.CheckCollateralizationIndexCount)
acc = sk.GetModuleAccount(suite.ctx, types.ModuleName) acc = sk.GetModuleAccount(suite.ctx, types.ModuleName)
finalXrpCollateral := acc.GetCoins().AmountOf("xrp") finalXrpCollateral := acc.GetCoins().AmountOf("xrp")
seizedXrpCollateral := originalXrpCollateral.Sub(finalXrpCollateral) seizedXrpCollateral := originalXrpCollateral.Sub(finalXrpCollateral)
xrpLiquidations := int(seizedXrpCollateral.Quo(i(10000000000)).Int64()) xrpLiquidations := int(seizedXrpCollateral.Quo(i(10000000000)).Int64())
suite.Equal(len(suite.liquidations.xrp), xrpLiquidations) suite.Equal(10, xrpLiquidations)
} }
func (suite *SeizeTestSuite) TestApplyLiquidationPenalty() { func (suite *SeizeTestSuite) TestApplyLiquidationPenalty() {