From 7c477eb75db277a5f42371582031b4deac2f1e28 Mon Sep 17 00:00:00 2001 From: rhuairahrighairigh Date: Mon, 11 May 2020 20:48:42 +0100 Subject: [PATCH] add cdp math fix and debugging --- x/cdp/keeper/auctions.go | 8 ++++++-- x/cdp/keeper/seize.go | 2 ++ x/cdp/keeper/seize_test.go | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/x/cdp/keeper/auctions.go b/x/cdp/keeper/auctions.go index 5228ef08..5b739aee 100644 --- a/x/cdp/keeper/auctions.go +++ b/x/cdp/keeper/auctions.go @@ -1,6 +1,8 @@ package keeper import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/kava-labs/kava/x/cdp/types" @@ -60,6 +62,7 @@ func (k Keeper) AuctionCollateral(ctx sdk.Context, deposits types.Deposits, debt if err != nil { return err } + fmt.Printf("AuctionCollateral:createFromD:loop%d:liquidator:%s\n", i, k.supplyKeeper.GetModuleAccount(ctx, types.LiquidatorMacc)) debt = debt.Sub(debtChange) totalCollateral = totalCollateral.Sub(collateralChange) dep.Amount = sdk.NewCoin(collateralDenom, collateralAmount.Sub(collateralChange)) @@ -68,7 +71,7 @@ func (k Keeper) AuctionCollateral(ctx sdk.Context, deposits types.Deposits, debt if !dep.Amount.IsZero() { // figure out how much debt this deposit accounts for // (depositCollateral / totalCollateral) * totalDebtFromCDP - debtCoveredByDeposit := (collateralAmount.Quo(totalCollateral)).Mul(debt) + debtCoveredByDeposit := collateralAmount.Mul(debt).Quo(totalCollateral) // integer division (ie rounds down) // if adding this deposit to the other partial deposits is less than a lot if (partialAuctionDeposits.SumCollateral().Add(collateralAmount)).LT(auctionSize) { // append the deposit to the partial deposits and zero out the deposit @@ -79,7 +82,7 @@ func (k Keeper) AuctionCollateral(ctx sdk.Context, deposits types.Deposits, debt // if the sum of partial deposits now makes a lot partialCollateral := sdk.NewCoin(collateralDenom, auctionSize.Sub(partialAuctionDeposits.SumCollateral())) partialAmount := partialCollateral.Amount - partialDebt := (partialAmount.Quo(collateralAmount)).Mul(debtCoveredByDeposit) + partialDebt := partialAmount.Mul(debtCoveredByDeposit).Quo(collateralAmount) // integer division (ie rounds down) // create a partial deposit from the deposit partialDep := newPartialDeposit(dep.Depositor, partialCollateral, partialDebt) @@ -90,6 +93,7 @@ func (k Keeper) AuctionCollateral(ctx sdk.Context, deposits types.Deposits, debt if err != nil { return err } + fmt.Printf("AuctionCollateral:createFromPD:loop%d:liquidator:%s\n", i, k.supplyKeeper.GetModuleAccount(ctx, types.LiquidatorMacc)) debt = debt.Sub(debtChange) totalCollateral = totalCollateral.Sub(collateralChange) // reset partial deposits and update the deposit amount diff --git a/x/cdp/keeper/seize.go b/x/cdp/keeper/seize.go index b10c25ce..8d0112cc 100644 --- a/x/cdp/keeper/seize.go +++ b/x/cdp/keeper/seize.go @@ -32,6 +32,7 @@ func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) error { if err != nil { return err } + fmt.Printf("SeizeCollateral:liquidator: %s\n", k.supplyKeeper.GetModuleAccount(ctx, types.LiquidatorMacc)) // liquidate deposits and send collateral from cdp to liquidator for _, dep := range deposits { @@ -49,6 +50,7 @@ func (k Keeper) SeizeCollateral(ctx sdk.Context, cdp types.CDP) error { } k.DeleteDeposit(ctx, dep.CdpID, dep.Depositor) } + fmt.Printf("SeizeCollateral:liquidator: %s\n", k.supplyKeeper.GetModuleAccount(ctx, types.LiquidatorMacc)) err = k.AuctionCollateral(ctx, deposits, debt, cdp.Principal.Denom) if err != nil { return err diff --git a/x/cdp/keeper/seize_test.go b/x/cdp/keeper/seize_test.go index 45b707b9..b76f1e6c 100644 --- a/x/cdp/keeper/seize_test.go +++ b/x/cdp/keeper/seize_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "errors" + "fmt" "math/rand" "testing" "time" @@ -166,6 +167,10 @@ func (suite *SeizeTestSuite) TestSeizeCollateralMultiDeposit() { p := cdp.Principal.Amount cl := cdp.Collateral.Amount tpb := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx") + fmt.Printf("%s\n", cdp) + fmt.Printf("%s\n", deposits) + fmt.Printf("cdpaccount: %s\n", suite.app.GetSupplyKeeper().GetModuleAccount(suite.ctx, types.ModuleName)) + fmt.Printf("liquidator: %s\n", suite.app.GetSupplyKeeper().GetModuleAccount(suite.ctx, types.LiquidatorMacc)) err = suite.keeper.SeizeCollateral(suite.ctx, cdp) suite.NoError(err) tpa := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")