0g-chain/x/liquidator/keeper/keeper_test.go

119 lines
3.5 KiB
Go
Raw Normal View History

2019-12-06 23:49:47 +00:00
package keeper_test
2019-11-25 19:46:02 +00:00
import (
"testing"
"time"
2019-11-25 19:46:02 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
2019-12-06 23:49:47 +00:00
abci "github.com/tendermint/tendermint/abci/types"
tmtime "github.com/tendermint/tendermint/types/time"
2019-11-25 19:46:02 +00:00
2019-12-06 23:49:47 +00:00
"github.com/kava-labs/kava/app"
2019-11-25 19:46:02 +00:00
"github.com/kava-labs/kava/x/liquidator/types"
)
func TestKeeper_SeizeAndStartCollateralAuction(t *testing.T) {
// Setup
2019-12-06 23:49:47 +00:00
_, addrs := app.GeneratePrivKeyAddressPairs(1)
tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates(
2019-12-07 00:12:07 +00:00
app.NewAuthGenState(addrs, []sdk.Coins{cs(c("btc", 100))}),
2019-12-06 23:49:47 +00:00
NewPFGenState("btc", sdk.MustNewDecFromStr("8000.00")),
NewCDPGenState(),
NewLiquidatorGenState(),
)
ctx := tApp.NewContext(false, abci.Header{})
2019-11-25 19:46:02 +00:00
2019-12-06 23:49:47 +00:00
require.NoError(t, tApp.GetCDPKeeper().ModifyCDP(ctx, addrs[0], "btc", i(3), i(16000)))
2019-11-25 19:46:02 +00:00
2019-12-06 23:49:47 +00:00
_, err := tApp.GetPriceFeedKeeper().SetPrice(ctx, addrs[0], "btc", sdk.MustNewDecFromStr("7999.99"), time.Unix(9999999999, 0))
require.NoError(t, err)
require.NoError(t, tApp.GetPriceFeedKeeper().SetCurrentPrices(ctx, "btc"))
2019-11-25 19:46:02 +00:00
// Run test function
2019-12-06 23:49:47 +00:00
auctionID, err := tApp.GetLiquidatorKeeper().SeizeAndStartCollateralAuction(ctx, addrs[0], "btc")
2019-11-25 19:46:02 +00:00
// Check CDP
require.NoError(t, err)
2019-12-06 23:49:47 +00:00
cdp, found := tApp.GetCDPKeeper().GetCDP(ctx, addrs[0], "btc")
2019-11-25 19:46:02 +00:00
require.True(t, found)
require.Equal(t, cdp.CollateralAmount, i(2)) // original amount - params.CollateralAuctionSize
require.Equal(t, cdp.Debt, i(10667)) // original debt scaled by amount of collateral removed
// Check auction exists
2019-12-06 23:49:47 +00:00
_, found = tApp.GetAuctionKeeper().GetAuction(ctx, auctionID)
2019-11-25 19:46:02 +00:00
require.True(t, found)
}
func TestKeeper_StartDebtAuction(t *testing.T) {
// Setup
2019-12-06 23:49:47 +00:00
tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates(
NewLiquidatorGenState(),
)
keeper := tApp.GetLiquidatorKeeper()
ctx := tApp.NewContext(false, abci.Header{})
2019-11-25 19:46:02 +00:00
initSDebt := types.SeizedDebt{i(2000), i(0)}
2019-12-06 23:49:47 +00:00
keeper.SetSeizedDebt(ctx, initSDebt)
2019-11-25 19:46:02 +00:00
// Execute
2019-12-06 23:49:47 +00:00
auctionID, err := keeper.StartDebtAuction(ctx)
2019-11-25 19:46:02 +00:00
// Check
require.NoError(t, err)
require.Equal(t,
types.SeizedDebt{
initSDebt.Total,
2019-12-06 23:49:47 +00:00
initSDebt.SentToAuction.Add(keeper.GetParams(ctx).DebtAuctionSize),
2019-11-25 19:46:02 +00:00
},
2019-12-06 23:49:47 +00:00
keeper.GetSeizedDebt(ctx),
2019-11-25 19:46:02 +00:00
)
2019-12-06 23:49:47 +00:00
_, found := tApp.GetAuctionKeeper().GetAuction(ctx, auctionID)
2019-11-25 19:46:02 +00:00
require.True(t, found)
}
func TestKeeper_partialSeizeCDP(t *testing.T) {
// Setup
2019-12-06 23:49:47 +00:00
_, addrs := app.GeneratePrivKeyAddressPairs(1)
tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates(
2019-12-07 00:12:07 +00:00
app.NewAuthGenState(addrs, []sdk.Coins{cs(c("btc", 100))}),
2019-12-06 23:49:47 +00:00
NewPFGenState("btc", sdk.MustNewDecFromStr("8000.00")),
NewCDPGenState(),
NewLiquidatorGenState(),
)
ctx := tApp.NewContext(false, abci.Header{})
2019-11-25 19:46:02 +00:00
2019-12-06 23:49:47 +00:00
tApp.GetCDPKeeper().ModifyCDP(ctx, addrs[0], "btc", i(3), i(16000))
2019-11-25 19:46:02 +00:00
2019-12-06 23:49:47 +00:00
tApp.GetPriceFeedKeeper().SetPrice(ctx, addrs[0], "btc", sdk.MustNewDecFromStr("7999.99"), tmtime.Now().Add(time.Hour*1))
tApp.GetPriceFeedKeeper().SetCurrentPrices(ctx, "btc")
2019-11-25 19:46:02 +00:00
// Run test function
2019-12-06 23:49:47 +00:00
err := tApp.GetLiquidatorKeeper().PartialSeizeCDP(ctx, addrs[0], "btc", i(2), i(10000))
2019-11-25 19:46:02 +00:00
// Check
require.NoError(t, err)
2019-12-06 23:49:47 +00:00
cdp, found := tApp.GetCDPKeeper().GetCDP(ctx, addrs[0], "btc")
2019-11-25 19:46:02 +00:00
require.True(t, found)
require.Equal(t, i(1), cdp.CollateralAmount)
require.Equal(t, i(6000), cdp.Debt)
}
func TestKeeper_GetSetSeizedDebt(t *testing.T) {
// Setup
2019-12-06 23:49:47 +00:00
tApp := app.NewTestApp()
ctx := tApp.NewContext(true, abci.Header{})
2019-11-25 19:46:02 +00:00
debt := types.SeizedDebt{i(234247645), i(2343)}
// Run test function
2019-12-06 23:49:47 +00:00
tApp.GetLiquidatorKeeper().SetSeizedDebt(ctx, debt)
readDebt := tApp.GetLiquidatorKeeper().GetSeizedDebt(ctx)
2019-11-25 19:46:02 +00:00
// Check
require.Equal(t, debt, readDebt)
}