2019-12-03 14:35:40 +00:00
|
|
|
package cdp_test
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-03 14:35:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2019-11-25 19:46:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2019-12-03 14:35:40 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/cdp"
|
2019-11-25 19:46:02 +00:00
|
|
|
"github.com/kava-labs/kava/x/pricefeed"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestApp_CreateModifyDeleteCDP(t *testing.T) {
|
|
|
|
// Setup
|
2019-12-03 14:35:40 +00:00
|
|
|
tApp := app.NewTestApp()
|
|
|
|
privKeys, addrs := app.GeneratePrivKeyAddressPairs(1)
|
2019-11-25 19:46:02 +00:00
|
|
|
testAddr := addrs[0]
|
|
|
|
testPrivKey := privKeys[0]
|
2019-12-03 14:35:40 +00:00
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
tApp.NewAuthGenStateFromAccounts(addrs, []sdk.Coins{cs(c("xrp", 100))}),
|
|
|
|
)
|
|
|
|
// check balance
|
|
|
|
ctx := tApp.NewContext(false, abci.Header{})
|
|
|
|
require.Equal(t, cs(c("xrp", 100)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
|
|
|
|
|
|
|
|
// setup cdp keeper
|
|
|
|
keeper := tApp.GetCDPKeeper()
|
|
|
|
params := cdp.CdpParams{
|
2019-11-25 19:46:02 +00:00
|
|
|
GlobalDebtLimit: sdk.NewInt(100000),
|
2019-12-03 14:35:40 +00:00
|
|
|
CollateralParams: []cdp.CollateralParams{
|
2019-11-25 19:46:02 +00:00
|
|
|
{
|
|
|
|
Denom: "xrp",
|
|
|
|
LiquidationRatio: sdk.MustNewDecFromStr("1.5"),
|
|
|
|
DebtLimit: sdk.NewInt(10000),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StableDenoms: []string{"usdx"},
|
|
|
|
}
|
|
|
|
keeper.SetParams(ctx, params)
|
|
|
|
keeper.SetGlobalDebt(ctx, sdk.NewInt(0))
|
2019-12-03 14:35:40 +00:00
|
|
|
// setup pricefeed
|
|
|
|
pfKeeper := tApp.GetPriceFeedKeeper()
|
2019-11-25 19:46:02 +00:00
|
|
|
ap := pricefeed.AssetParams{
|
|
|
|
Assets: []pricefeed.Asset{pricefeed.Asset{AssetCode: "xrp", Description: ""}},
|
|
|
|
}
|
|
|
|
pfKeeper.SetAssetParams(ctx, ap)
|
|
|
|
pfKeeper.SetPrice(
|
|
|
|
ctx, sdk.AccAddress{}, "xrp",
|
|
|
|
sdk.MustNewDecFromStr("1.00"),
|
|
|
|
sdk.NewInt(10))
|
|
|
|
pfKeeper.SetCurrentPrices(ctx)
|
2019-12-03 14:35:40 +00:00
|
|
|
tApp.EndBlock(abci.RequestEndBlock{})
|
|
|
|
tApp.Commit()
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
// Create CDP
|
2019-12-03 14:35:40 +00:00
|
|
|
msgs := []sdk.Msg{cdp.NewMsgCreateOrModifyCDP(testAddr, "xrp", i(10), i(5))}
|
|
|
|
simapp.SignCheckDeliver(t, tApp.Codec(), tApp.BaseApp, abci.Header{Height: tApp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{0}, true, true, testPrivKey)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2019-12-03 14:35:40 +00:00
|
|
|
// check balance
|
|
|
|
ctx = tApp.NewContext(true, abci.Header{})
|
|
|
|
require.Equal(t, cs(c("usdx", 5), c("xrp", 90)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
// Modify CDP
|
2019-12-03 14:35:40 +00:00
|
|
|
msgs = []sdk.Msg{cdp.NewMsgCreateOrModifyCDP(testAddr, "xrp", i(40), i(5))}
|
|
|
|
simapp.SignCheckDeliver(t, tApp.Codec(), tApp.BaseApp, abci.Header{Height: tApp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{1}, true, true, testPrivKey)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2019-12-03 14:35:40 +00:00
|
|
|
// check balance
|
|
|
|
ctx = tApp.NewContext(true, abci.Header{})
|
|
|
|
require.Equal(t, cs(c("usdx", 10), c("xrp", 50)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
// Delete CDP
|
2019-12-03 14:35:40 +00:00
|
|
|
msgs = []sdk.Msg{cdp.NewMsgCreateOrModifyCDP(testAddr, "xrp", i(-50), i(-10))}
|
|
|
|
simapp.SignCheckDeliver(t, tApp.Codec(), tApp.BaseApp, abci.Header{Height: tApp.LastBlockHeight() + 1}, msgs, []uint64{0}, []uint64{2}, true, true, testPrivKey)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2019-12-03 14:35:40 +00:00
|
|
|
// check balance
|
|
|
|
ctx = tApp.NewContext(true, abci.Header{})
|
|
|
|
require.Equal(t, cs(c("xrp", 100)), tApp.GetAccountKeeper().GetAccount(ctx, testAddr).GetCoins())
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|