2020-01-12 15:35:34 +00:00
|
|
|
package cdp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/simulation"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-01-12 15:35:34 +00:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
2022-01-08 00:39:27 +00:00
|
|
|
auctiontypes "github.com/kava-labs/kava/x/auction/types"
|
2020-04-30 14:13:31 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ModuleTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper keeper.Keeper
|
2020-01-12 15:35:34 +00:00
|
|
|
addrs []sdk.AccAddress
|
|
|
|
app app.TestApp
|
2022-01-08 00:39:27 +00:00
|
|
|
cdps types.CDPs
|
2020-01-12 15:35:34 +00:00
|
|
|
ctx sdk.Context
|
|
|
|
liquidations liquidationTracker
|
|
|
|
}
|
|
|
|
|
|
|
|
type liquidationTracker struct {
|
|
|
|
xrp []uint64
|
|
|
|
btc []uint64
|
|
|
|
debt int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ModuleTestSuite) SetupTest() {
|
2020-01-21 20:28:25 +00:00
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
2020-01-21 20:28:25 +00:00
|
|
|
tracker := liquidationTracker{}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
coins := cs(c("btc", 100000000), c("xrp", 10000000000))
|
2020-01-21 20:28:25 +00:00
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(100)
|
2022-01-08 00:39:27 +00:00
|
|
|
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
|
2020-01-21 20:28:25 +00:00
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
2022-01-08 00:39:27 +00:00
|
|
|
NewPricefeedGenStateMulti(tApp.AppCodec()),
|
|
|
|
NewCDPGenStateMulti(tApp.AppCodec()),
|
2020-01-21 20:28:25 +00:00
|
|
|
)
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.app = tApp
|
|
|
|
suite.keeper = tApp.GetCDPKeeper()
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.cdps = types.CDPs{}
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.addrs = addrs
|
|
|
|
suite.liquidations = tracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ModuleTestSuite) createCdps() {
|
2020-01-12 15:35:34 +00:00
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
cdps := make(types.CDPs, 100)
|
2020-01-12 15:35:34 +00:00
|
|
|
tracker := liquidationTracker{}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
coins := cs(c("btc", 100000000), c("xrp", 10000000000))
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(100)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
|
2020-01-12 15:35:34 +00:00
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
2022-01-08 00:39:27 +00:00
|
|
|
NewPricefeedGenStateMulti(tApp.AppCodec()),
|
|
|
|
NewCDPGenStateMulti(tApp.AppCodec()),
|
2020-01-12 15:35:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.app = tApp
|
|
|
|
suite.keeper = tApp.GetCDPKeeper()
|
|
|
|
|
|
|
|
for j := 0; j < 100; j++ {
|
|
|
|
collateral := "xrp"
|
|
|
|
amount := 10000000000
|
|
|
|
debt := simulation.RandIntBetween(rand.New(rand.NewSource(int64(j))), 750000000, 1249000000)
|
|
|
|
if j%2 == 0 {
|
|
|
|
collateral = "btc"
|
|
|
|
amount = 100000000
|
|
|
|
debt = simulation.RandIntBetween(rand.New(rand.NewSource(int64(j))), 2700000000, 5332000000)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 19:42:46 +00:00
|
|
|
suite.Nil(suite.keeper.AddCdp(suite.ctx, addrs[j], c(collateral, int64(amount)), c("usdx", int64(debt)), collateral+"-a"))
|
|
|
|
c, f := suite.keeper.GetCDP(suite.ctx, collateral+"-a", uint64(j+1))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.True(f)
|
|
|
|
cdps[j] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.cdps = cdps
|
|
|
|
suite.addrs = addrs
|
|
|
|
suite.liquidations = tracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ModuleTestSuite) setPrice(price sdk.Dec, market string) {
|
|
|
|
pfKeeper := suite.app.GetPriceFeedKeeper()
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
_, err := pfKeeper.SetPrice(suite.ctx, sdk.AccAddress{}, market, price, suite.ctx.BlockTime().Add(time.Hour*3))
|
|
|
|
suite.NoError(err)
|
|
|
|
|
|
|
|
err = pfKeeper.SetCurrentPrices(suite.ctx, market)
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
pp, err := pfKeeper.GetCurrentPrice(suite.ctx, market)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.Equal(price, pp.Price)
|
|
|
|
}
|
|
|
|
func (suite *ModuleTestSuite) TestBeginBlock() {
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.createCdps()
|
2022-01-08 00:39:27 +00:00
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
bk := suite.app.GetBankKeeper()
|
|
|
|
|
|
|
|
acc := ak.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
originalXrpCollateral := bk.GetBalance(suite.ctx, acc.GetAddress(), "xrp").Amount
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.setPrice(d("0.2"), "xrp:usd")
|
|
|
|
cdp.BeginBlocker(suite.ctx, abci.RequestBeginBlock{Header: suite.ctx.BlockHeader()}, suite.keeper)
|
2022-01-08 00:39:27 +00:00
|
|
|
acc = ak.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
finalXrpCollateral := bk.GetBalance(suite.ctx, acc.GetAddress(), "xrp").Amount
|
2020-01-12 15:35:34 +00:00
|
|
|
seizedXrpCollateral := originalXrpCollateral.Sub(finalXrpCollateral)
|
|
|
|
xrpLiquidations := int(seizedXrpCollateral.Quo(i(10000000000)).Int64())
|
2021-02-23 19:39:39 +00:00
|
|
|
suite.Equal(10, xrpLiquidations)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
acc = ak.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
originalBtcCollateral := bk.GetBalance(suite.ctx, acc.GetAddress(), "btc").Amount
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.setPrice(d("6000"), "btc:usd")
|
|
|
|
cdp.BeginBlocker(suite.ctx, abci.RequestBeginBlock{Header: suite.ctx.BlockHeader()}, suite.keeper)
|
2022-01-08 00:39:27 +00:00
|
|
|
acc = ak.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
finalBtcCollateral := bk.GetBalance(suite.ctx, acc.GetAddress(), "btc").Amount
|
2020-01-12 15:35:34 +00:00
|
|
|
seizedBtcCollateral := originalBtcCollateral.Sub(finalBtcCollateral)
|
|
|
|
btcLiquidations := int(seizedBtcCollateral.Quo(i(100000000)).Int64())
|
2021-02-23 19:39:39 +00:00
|
|
|
suite.Equal(10, btcLiquidations)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
acc = ak.GetModuleAccount(suite.ctx, auctiontypes.ModuleName)
|
|
|
|
suite.Equal(int64(71955653865), bk.GetBalance(suite.ctx, acc.GetAddress(), "debt").Amount.Int64())
|
2020-01-12 15:35:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:28:25 +00:00
|
|
|
func (suite *ModuleTestSuite) TestSeizeSingleCdpWithFees() {
|
2020-08-21 19:42:46 +00:00
|
|
|
err := suite.keeper.AddCdp(suite.ctx, suite.addrs[0], c("xrp", 10000000000), c("usdx", 1000000000), "xrp-a")
|
2020-01-21 20:28:25 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
suite.Equal(i(1000000000), suite.keeper.GetTotalPrincipal(suite.ctx, "xrp-a", "usdx"))
|
2022-01-08 00:39:27 +00:00
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
bk := suite.app.GetBankKeeper()
|
|
|
|
|
|
|
|
cdpMacc := ak.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
suite.Equal(i(1000000000), bk.GetBalance(suite.ctx, cdpMacc.GetAddress(), "debt").Amount)
|
2020-01-21 20:28:25 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(time.Second * 6))
|
|
|
|
cdp.BeginBlocker(suite.ctx, abci.RequestBeginBlock{Header: suite.ctx.BlockHeader()}, suite.keeper)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
cdpMacc = ak.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
suite.Equal(i(1000000891), (bk.GetBalance(suite.ctx, cdpMacc.GetAddress(), "debt").Amount))
|
2020-08-21 19:42:46 +00:00
|
|
|
cdp, _ := suite.keeper.GetCDP(suite.ctx, "xrp-a", 1)
|
2020-01-21 20:28:25 +00:00
|
|
|
|
|
|
|
err = suite.keeper.SeizeCollateral(suite.ctx, cdp)
|
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
_, found := suite.keeper.GetCDP(suite.ctx, "xrp-a", 1)
|
2020-04-23 23:25:44 +00:00
|
|
|
suite.False(found)
|
2020-01-21 20:28:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
func TestModuleTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ModuleTestSuite))
|
|
|
|
}
|