From 3f043b90ac83ab02e76f352cf83849f01b8db20b Mon Sep 17 00:00:00 2001 From: Nick DeLuca Date: Wed, 10 Jun 2020 09:18:13 -0500 Subject: [PATCH] Update GetTotalSurplus and GetTotalDebt to be consistent (#567) * update GetTotalSurplus and GetTotalDebt code blocks to be consistent and resolve feedback in exhibit 13 * spelling fix * remove uneeded temp variables * address PR feedback - use suite.Require() instead of default assertions --- x/cdp/keeper/auctions.go | 8 ++--- x/cdp/keeper/auctions_test.go | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/x/cdp/keeper/auctions.go b/x/cdp/keeper/auctions.go index b6906865..c3ba3b35 100644 --- a/x/cdp/keeper/auctions.go +++ b/x/cdp/keeper/auctions.go @@ -88,18 +88,14 @@ func (k Keeper) NetSurplusAndDebt(ctx sdk.Context) error { // GetTotalSurplus returns the total amount of surplus tokens held by the liquidator module account func (k Keeper) GetTotalSurplus(ctx sdk.Context, accountName string) sdk.Int { acc := k.supplyKeeper.GetModuleAccount(ctx, accountName) - totalSurplus := sdk.ZeroInt() dp := k.GetParams(ctx).DebtParam - surplus := acc.GetCoins().AmountOf(dp.Denom) - totalSurplus = totalSurplus.Add(surplus) - return totalSurplus + return acc.GetCoins().AmountOf(dp.Denom) } // GetTotalDebt returns the total amount of debt tokens held by the liquidator module account func (k Keeper) GetTotalDebt(ctx sdk.Context, accountName string) sdk.Int { acc := k.supplyKeeper.GetModuleAccount(ctx, accountName) - debt := acc.GetCoins().AmountOf(k.GetDebtDenom(ctx)) - return debt + return acc.GetCoins().AmountOf(k.GetDebtDenom(ctx)) } // RunSurplusAndDebtAuctions nets the surplus and debt balances and then creates surplus or debt auctions if the remaining balance is above the auction threshold parameter diff --git a/x/cdp/keeper/auctions_test.go b/x/cdp/keeper/auctions_test.go index 57273c7b..7f5edf14 100644 --- a/x/cdp/keeper/auctions_test.go +++ b/x/cdp/keeper/auctions_test.go @@ -92,6 +92,66 @@ func (suite *AuctionTestSuite) TestDebtAuction() { suite.Equal(cs(c("debt", 90000000000)), acc.GetCoins()) } +func (suite *AuctionTestSuite) TestGetTotalSurplus() { + sk := suite.app.GetSupplyKeeper() + + // liquidator account has zero coins + suite.Require().Equal(sdk.NewInt(0), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc)) + + // mint some coins + err := sk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 100e6))) + suite.Require().NoError(err) + err = sk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 200e6))) + suite.Require().NoError(err) + + // liquidator account has 300e6 total usdx + suite.Require().Equal(sdk.NewInt(300e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc)) + + // mint some debt + err = sk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 500e6))) + suite.Require().NoError(err) + + // liquidator account still has 300e6 total usdx -- debt balance is ignored + suite.Require().Equal(sdk.NewInt(300e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc)) + + // burn some usdx + err = sk.BurnCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 50e6))) + suite.Require().NoError(err) + + // liquidator usdx decreases + suite.Require().Equal(sdk.NewInt(250e6), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc)) +} + +func (suite *AuctionTestSuite) TestGetTotalDebt() { + sk := suite.app.GetSupplyKeeper() + + // liquidator account has zero debt + suite.Require().Equal(sdk.NewInt(0), suite.keeper.GetTotalSurplus(suite.ctx, types.LiquidatorMacc)) + + // mint some debt + err := sk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 100e6))) + suite.Require().NoError(err) + err = sk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 200e6))) + suite.Require().NoError(err) + + // liquidator account has 300e6 total debt + suite.Require().Equal(sdk.NewInt(300e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc)) + + // mint some usdx + err = sk.MintCoins(suite.ctx, types.LiquidatorMacc, cs(c("usdx", 500e6))) + suite.Require().NoError(err) + + // liquidator account still has 300e6 total debt -- usdx balance is ignored + suite.Require().Equal(sdk.NewInt(300e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc)) + + // burn some debt + err = sk.BurnCoins(suite.ctx, types.LiquidatorMacc, cs(c("debt", 50e6))) + suite.Require().NoError(err) + + // liquidator debt decreases + suite.Require().Equal(sdk.NewInt(250e6), suite.keeper.GetTotalDebt(suite.ctx, types.LiquidatorMacc)) +} + func TestAuctionTestSuite(t *testing.T) { suite.Run(t, new(AuctionTestSuite)) }