add cdp math fix and debugging

This commit is contained in:
rhuairahrighairigh 2020-05-11 20:48:42 +01:00
parent 5987d966ef
commit 7c477eb75d
3 changed files with 13 additions and 2 deletions

View File

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

View File

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

View File

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