2020-01-12 15:35:34 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
|
|
|
"github.com/kava-labs/kava/app"
|
2020-01-15 14:19:33 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction"
|
2020-01-12 15:35:34 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SeizeTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
keeper keeper.Keeper
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
app app.TestApp
|
|
|
|
cdps types.CDPs
|
|
|
|
ctx sdk.Context
|
|
|
|
liquidations liquidationTracker
|
|
|
|
}
|
|
|
|
|
|
|
|
type liquidationTracker struct {
|
|
|
|
xrp []uint64
|
|
|
|
btc []uint64
|
|
|
|
debt int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) SetupTest() {
|
2020-01-21 20:28:25 +00:00
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
coins := []sdk.Coins{}
|
|
|
|
tracker := liquidationTracker{}
|
|
|
|
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
coins = append(coins, cs(c("btc", 100000000), c("xrp", 10000000000)))
|
|
|
|
}
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(100)
|
|
|
|
|
|
|
|
authGS := app.NewAuthGenState(
|
|
|
|
addrs, coins)
|
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
|
|
|
NewPricefeedGenStateMulti(),
|
|
|
|
NewCDPGenStateMulti(),
|
|
|
|
)
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.app = tApp
|
|
|
|
suite.keeper = tApp.GetCDPKeeper()
|
|
|
|
suite.cdps = types.CDPs{}
|
|
|
|
suite.addrs = addrs
|
|
|
|
suite.liquidations = tracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) createCdps() {
|
2020-01-12 15:35:34 +00:00
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
cdps := make(types.CDPs, 100)
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(100)
|
|
|
|
coins := []sdk.Coins{}
|
|
|
|
tracker := liquidationTracker{}
|
|
|
|
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
coins = append(coins, cs(c("btc", 100000000), c("xrp", 10000000000)))
|
|
|
|
}
|
|
|
|
|
|
|
|
authGS := app.NewAuthGenState(
|
|
|
|
addrs, coins)
|
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
|
|
|
NewPricefeedGenStateMulti(),
|
|
|
|
NewCDPGenStateMulti(),
|
|
|
|
)
|
|
|
|
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.app = tApp
|
|
|
|
suite.keeper = tApp.GetCDPKeeper()
|
2020-01-15 14:19:33 +00:00
|
|
|
randSource := rand.New(rand.NewSource(int64(777)))
|
2020-01-12 15:35:34 +00:00
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
collateral := "xrp"
|
|
|
|
amount := 10000000000
|
2020-01-15 14:19:33 +00:00
|
|
|
debt := simulation.RandIntBetween(randSource, 750000000, 1249000000)
|
2020-01-12 15:35:34 +00:00
|
|
|
if j%2 == 0 {
|
|
|
|
collateral = "btc"
|
|
|
|
amount = 100000000
|
2020-01-15 14:19:33 +00:00
|
|
|
debt = simulation.RandIntBetween(randSource, 2700000000, 5332000000)
|
2020-01-12 15:35:34 +00:00
|
|
|
if debt >= 4000000000 {
|
|
|
|
tracker.btc = append(tracker.btc, uint64(j+1))
|
|
|
|
tracker.debt += int64(debt)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if debt >= 1000000000 {
|
|
|
|
tracker.xrp = append(tracker.xrp, uint64(j+1))
|
|
|
|
tracker.debt += int64(debt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := suite.keeper.AddCdp(suite.ctx, addrs[j], cs(c(collateral, int64(amount))), cs(c("usdx", int64(debt))))
|
|
|
|
suite.NoError(err)
|
|
|
|
c, f := suite.keeper.GetCDP(suite.ctx, collateral, uint64(j+1))
|
|
|
|
suite.True(f)
|
|
|
|
cdps[j] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.cdps = cdps
|
|
|
|
suite.addrs = addrs
|
|
|
|
suite.liquidations = tracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) setPrice(price sdk.Dec, market string) {
|
|
|
|
pfKeeper := suite.app.GetPriceFeedKeeper()
|
|
|
|
|
|
|
|
pfKeeper.SetPrice(suite.ctx, sdk.AccAddress{}, market, price, suite.ctx.BlockTime().Add(time.Hour*3))
|
|
|
|
err := pfKeeper.SetCurrentPrices(suite.ctx, market)
|
|
|
|
suite.NoError(err)
|
|
|
|
pp, err := pfKeeper.GetCurrentPrice(suite.ctx, market)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(price, pp.Price)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) TestSeizeCollateral() {
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.createCdps()
|
2020-01-12 15:35:34 +00:00
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
cdp, _ := suite.keeper.GetCDP(suite.ctx, "xrp", uint64(2))
|
|
|
|
p := cdp.Principal[0].Amount
|
|
|
|
cl := cdp.Collateral[0].Amount
|
|
|
|
tpb := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")
|
2020-01-15 14:19:33 +00:00
|
|
|
err := suite.keeper.SeizeCollateral(suite.ctx, cdp)
|
|
|
|
suite.NoError(err)
|
|
|
|
tpa := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")
|
|
|
|
suite.Equal(tpb.Sub(tpa), p)
|
2020-01-21 17:52:07 +00:00
|
|
|
auctionKeeper := suite.app.GetAuctionKeeper()
|
|
|
|
_, found := auctionKeeper.GetAuction(suite.ctx, 0)
|
|
|
|
suite.True(found)
|
2020-01-15 14:19:33 +00:00
|
|
|
auctionMacc := sk.GetModuleAccount(suite.ctx, auction.ModuleName)
|
|
|
|
suite.Equal(cs(c("debt", p.Int64()), c("xrp", cl.Int64())), auctionMacc.GetCoins())
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
acc := ak.GetAccount(suite.ctx, suite.addrs[1])
|
|
|
|
suite.Equal(p.Int64(), acc.GetCoins().AmountOf("usdx").Int64())
|
|
|
|
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[1], suite.addrs[1], cs(c("xrp", 10)))
|
|
|
|
suite.Equal(types.CodeCdpNotFound, err.Result().Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) TestSeizeCollateralMultiDeposit() {
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.createCdps()
|
2020-01-15 14:19:33 +00:00
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
cdp, _ := suite.keeper.GetCDP(suite.ctx, "xrp", uint64(2))
|
|
|
|
err := suite.keeper.DepositCollateral(suite.ctx, suite.addrs[1], suite.addrs[0], cs(c("xrp", 6999000000)))
|
|
|
|
suite.NoError(err)
|
|
|
|
cdp, _ = suite.keeper.GetCDP(suite.ctx, "xrp", uint64(2))
|
|
|
|
deposits := suite.keeper.GetDeposits(suite.ctx, cdp.ID)
|
|
|
|
suite.Equal(2, len(deposits))
|
|
|
|
p := cdp.Principal[0].Amount
|
|
|
|
cl := cdp.Collateral[0].Amount
|
|
|
|
tpb := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")
|
|
|
|
err = suite.keeper.SeizeCollateral(suite.ctx, cdp)
|
|
|
|
suite.NoError(err)
|
2020-01-12 15:35:34 +00:00
|
|
|
tpa := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")
|
|
|
|
suite.Equal(tpb.Sub(tpa), p)
|
2020-01-15 14:19:33 +00:00
|
|
|
auctionMacc := sk.GetModuleAccount(suite.ctx, auction.ModuleName)
|
|
|
|
suite.Equal(cs(c("debt", p.Int64()), c("xrp", cl.Int64())), auctionMacc.GetCoins())
|
2020-01-12 15:35:34 +00:00
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
acc := ak.GetAccount(suite.ctx, suite.addrs[1])
|
|
|
|
suite.Equal(p.Int64(), acc.GetCoins().AmountOf("usdx").Int64())
|
2020-01-15 14:19:33 +00:00
|
|
|
err = suite.keeper.WithdrawCollateral(suite.ctx, suite.addrs[1], suite.addrs[1], cs(c("xrp", 10)))
|
|
|
|
suite.Equal(types.CodeCdpNotFound, err.Result().Code)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) TestLiquidateCdps() {
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.createCdps()
|
2020-01-12 15:35:34 +00:00
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
acc := sk.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
originalXrpCollateral := acc.GetCoins().AmountOf("xrp")
|
|
|
|
suite.setPrice(d("0.2"), "xrp:usd")
|
|
|
|
p, _ := suite.keeper.GetCollateral(suite.ctx, "xrp")
|
|
|
|
suite.keeper.LiquidateCdps(suite.ctx, "xrp:usd", "xrp", p.LiquidationRatio)
|
|
|
|
acc = sk.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
finalXrpCollateral := acc.GetCoins().AmountOf("xrp")
|
|
|
|
seizedXrpCollateral := originalXrpCollateral.Sub(finalXrpCollateral)
|
|
|
|
xrpLiquidations := int(seizedXrpCollateral.Quo(i(10000000000)).Int64())
|
|
|
|
suite.Equal(len(suite.liquidations.xrp), xrpLiquidations)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *SeizeTestSuite) TestHandleNewDebt() {
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.createCdps()
|
2020-01-12 15:35:34 +00:00
|
|
|
tpb := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")
|
|
|
|
suite.keeper.HandleNewDebt(suite.ctx, "xrp", "usdx", i(31536000))
|
|
|
|
tpa := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp", "usdx")
|
|
|
|
suite.Equal(sdk.NewDec(tpb.Int64()).Mul(d("1.05")).TruncateInt().Int64(), tpa.Int64())
|
|
|
|
}
|
|
|
|
|
2020-01-21 17:52:07 +00:00
|
|
|
func (suite *SeizeTestSuite) TestApplyLiquidationPenalty() {
|
|
|
|
penalty := suite.keeper.ApplyLiquidationPenalty(suite.ctx, "xrp", i(1000))
|
|
|
|
suite.Equal(i(50), penalty)
|
|
|
|
penalty = suite.keeper.ApplyLiquidationPenalty(suite.ctx, "btc", i(1000))
|
|
|
|
suite.Equal(i(25), penalty)
|
|
|
|
penalty = suite.keeper.ApplyLiquidationPenalty(suite.ctx, "xrp", i(675760172))
|
|
|
|
suite.Equal(i(33788009), penalty)
|
|
|
|
suite.Panics(func() { suite.keeper.ApplyLiquidationPenalty(suite.ctx, "lol", i(1000)) })
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
func TestSeizeTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(SeizeTestSuite))
|
|
|
|
}
|