2020-01-12 15:35:34 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2020-04-23 16:35:58 +00:00
|
|
|
"errors"
|
2020-01-12 15:35:34 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"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 DrawTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
keeper keeper.Keeper
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
addrs []sdk.AccAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DrawTestSuite) SetupTest() {
|
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(3)
|
|
|
|
authGS := app.NewAuthGenState(
|
|
|
|
addrs,
|
|
|
|
[]sdk.Coins{
|
2020-01-22 16:50:27 +00:00
|
|
|
cs(c("xrp", 500000000), c("btc", 500000000), c("usdx", 10000000000)),
|
2020-01-12 15:35:34 +00:00
|
|
|
cs(c("xrp", 200000000)),
|
|
|
|
cs(c("xrp", 10000000000000), c("usdx", 100000000000))})
|
|
|
|
tApp.InitializeFromGenesisStates(
|
|
|
|
authGS,
|
|
|
|
NewPricefeedGenStateMulti(),
|
|
|
|
NewCDPGenStateMulti(),
|
|
|
|
)
|
|
|
|
keeper := tApp.GetCDPKeeper()
|
|
|
|
suite.app = tApp
|
|
|
|
suite.keeper = keeper
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.addrs = addrs
|
2020-08-21 19:42:46 +00:00
|
|
|
err := suite.keeper.AddCdp(suite.ctx, addrs[0], c("xrp", 400000000), c("usdx", 10000000), "xrp-a")
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DrawTestSuite) TestAddRepayPrincipal() {
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err := suite.keeper.AddPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
t, found := suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(1))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.True(found)
|
|
|
|
suite.Equal(c("usdx", 20000000), t.Principal)
|
2020-08-21 19:42:46 +00:00
|
|
|
ctd := suite.keeper.CalculateCollateralToDebtRatio(suite.ctx, t.Collateral, "xrp-a", t.Principal.Add(t.AccumulatedFees))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(d("20.0"), ctd)
|
2020-08-21 19:42:46 +00:00
|
|
|
ts := suite.keeper.GetAllCdpsByCollateralTypeAndRatio(suite.ctx, "xrp-a", d("20.0"))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(0, len(ts))
|
2020-08-21 19:42:46 +00:00
|
|
|
ts = suite.keeper.GetAllCdpsByCollateralTypeAndRatio(suite.ctx, "xrp-a", d("20.0").Add(sdk.SmallestDec()))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(ts[0], t)
|
2020-08-21 19:42:46 +00:00
|
|
|
tp := suite.keeper.GetTotalPrincipal(suite.ctx, "xrp-a", "usdx")
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(i(20000000), tp)
|
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
acc := sk.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
suite.Equal(cs(c("xrp", 400000000), c("debt", 20000000)), acc.GetCoins())
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("susd", 10000000))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrInvalidDebtRequest))
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddPrincipal(suite.ctx, suite.addrs[1], "xrp-a", c("usdx", 10000000))
|
2020-04-23 16:35:58 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrCdpNotFound))
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("xusd", 10000000))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrInvalidDebtRequest))
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("usdx", 311000000))
|
2020-04-23 16:35:58 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrInvalidCollateralRatio))
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
t, found = suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(1))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.True(found)
|
|
|
|
suite.Equal(c("usdx", 10000000), t.Principal)
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
ctd = suite.keeper.CalculateCollateralToDebtRatio(suite.ctx, t.Collateral, "xrp-a", t.Principal.Add(t.AccumulatedFees))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(d("40.0"), ctd)
|
2020-08-21 19:42:46 +00:00
|
|
|
ts = suite.keeper.GetAllCdpsByCollateralTypeAndRatio(suite.ctx, "xrp-a", d("40.0"))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(0, len(ts))
|
2020-08-21 19:42:46 +00:00
|
|
|
ts = suite.keeper.GetAllCdpsByCollateralTypeAndRatio(suite.ctx, "xrp-a", d("40.0").Add(sdk.SmallestDec()))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(ts[0], t)
|
|
|
|
sk = suite.app.GetSupplyKeeper()
|
|
|
|
acc = sk.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
suite.Equal(cs(c("xrp", 400000000), c("debt", 10000000)), acc.GetCoins())
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("xusd", 10000000))
|
2020-04-23 16:35:58 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrInvalidPayment))
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[1], "xrp-a", c("xusd", 10000000))
|
2020-04-23 16:35:58 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrCdpNotFound))
|
2020-01-12 15:35:34 +00:00
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("usdx", 9000000))
|
2020-04-23 16:35:58 +00:00
|
|
|
suite.Require().True(errors.Is(err, types.ErrBelowDebtFloor))
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
_, found = suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(1))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.False(found)
|
2020-08-21 19:42:46 +00:00
|
|
|
ts = suite.keeper.GetAllCdpsByCollateralTypeAndRatio(suite.ctx, "xrp-a", types.MaxSortableDec)
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(0, len(ts))
|
2020-08-21 19:42:46 +00:00
|
|
|
ts = suite.keeper.GetAllCdpsByCollateralType(suite.ctx, "xrp-a")
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Equal(0, len(ts))
|
|
|
|
sk = suite.app.GetSupplyKeeper()
|
|
|
|
acc = sk.GetModuleAccount(suite.ctx, types.ModuleName)
|
|
|
|
suite.Equal(sdk.Coins(nil), acc.GetCoins())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-22 16:50:27 +00:00
|
|
|
func (suite *DrawTestSuite) TestRepayPrincipalOverpay() {
|
2020-08-21 19:42:46 +00:00
|
|
|
err := suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[0], "xrp-a", c("usdx", 20000000))
|
2020-01-22 16:50:27 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
acc := ak.GetAccount(suite.ctx, suite.addrs[0])
|
|
|
|
suite.Equal(i(10000000000), (acc.GetCoins().AmountOf("usdx")))
|
2020-08-21 19:42:46 +00:00
|
|
|
_, found := suite.keeper.GetCDP(suite.ctx, "xrp-a", 1)
|
2020-01-22 16:50:27 +00:00
|
|
|
suite.False(found)
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
func (suite *DrawTestSuite) TestAddRepayPrincipalFees() {
|
2020-08-21 19:42:46 +00:00
|
|
|
err := suite.keeper.AddCdp(suite.ctx, suite.addrs[2], c("xrp", 1000000000000), c("usdx", 100000000000), "xrp-a")
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(time.Minute * 10))
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.UpdateFeesForAllCdps(suite.ctx, "xrp-a")
|
2020-04-23 23:25:44 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddPrincipal(suite.ctx, suite.addrs[2], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
t, _ := suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(2))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.Equal(c("usdx", 92827), t.AccumulatedFees)
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[2], "xrp-a", c("usdx", 100))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
t, _ = suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(2))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.Equal(c("usdx", 92727), t.AccumulatedFees)
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(suite.ctx, suite.addrs[2], "xrp-a", c("usdx", 100010092727))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
_, f := suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(2))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.False(f)
|
|
|
|
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddCdp(suite.ctx, suite.addrs[2], c("xrp", 1000000000000), c("usdx", 100000000), "xrp-a")
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
|
2020-04-23 23:25:44 +00:00
|
|
|
suite.ctx = suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(time.Second * 31536000)) // move forward one year in time
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.UpdateFeesForAllCdps(suite.ctx, "xrp-a")
|
2020-04-23 23:25:44 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.AddPrincipal(suite.ctx, suite.addrs[2], "xrp-a", c("usdx", 100000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
t, _ = suite.keeper.GetCDP(suite.ctx, "xrp-a", uint64(3))
|
2020-04-27 14:40:34 +00:00
|
|
|
suite.Equal(c("usdx", 5000000), t.AccumulatedFees)
|
2020-01-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DrawTestSuite) TestPricefeedFailure() {
|
|
|
|
ctx := suite.ctx.WithBlockTime(suite.ctx.BlockTime().Add(time.Hour * 2))
|
|
|
|
pfk := suite.app.GetPriceFeedKeeper()
|
|
|
|
pfk.SetCurrentPrices(ctx, "xrp:usd")
|
2020-08-21 19:42:46 +00:00
|
|
|
err := suite.keeper.AddPrincipal(ctx, suite.addrs[0], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.Error(err)
|
2020-08-21 19:42:46 +00:00
|
|
|
err = suite.keeper.RepayPrincipal(ctx, suite.addrs[0], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
suite.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *DrawTestSuite) TestModuleAccountFailure() {
|
|
|
|
suite.Panics(func() {
|
|
|
|
ctx := suite.ctx.WithBlockHeader(suite.ctx.BlockHeader())
|
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
acc := sk.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
ak.RemoveAccount(ctx, acc)
|
2020-08-21 19:42:46 +00:00
|
|
|
suite.keeper.RepayPrincipal(ctx, suite.addrs[0], "xrp-a", c("usdx", 10000000))
|
2020-01-12 15:35:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDrawTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(DrawTestSuite))
|
|
|
|
}
|