2020-09-21 21:08:43 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2020-12-03 23:05:56 +00:00
|
|
|
"fmt"
|
2020-12-18 01:12:48 +00:00
|
|
|
"sort"
|
2020-11-13 15:51:52 +00:00
|
|
|
"strconv"
|
2020-09-21 21:08:43 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
|
|
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
2020-12-15 17:38:14 +00:00
|
|
|
aucKeeper "github.com/kava-labs/kava/x/auction/keeper"
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2021-02-01 21:13:17 +00:00
|
|
|
pfKeeper "github.com/kava-labs/kava/x/pricefeed/keeper"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test suite used for all keeper tests
|
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
2021-02-01 21:13:17 +00:00
|
|
|
keeper keeper.Keeper
|
|
|
|
auctionKeeper aucKeeper.Keeper
|
|
|
|
pricefeedKeeper pfKeeper.Keeper
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
addrs []sdk.AccAddress
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The default state used by each test
|
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
2020-10-05 21:44:17 +00:00
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
tApp := app.NewTestApp()
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{Height: 1, Time: tmtime.Now()})
|
|
|
|
tApp.InitializeFromGenesisStates()
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(1)
|
2020-12-21 17:18:55 +00:00
|
|
|
keeper := tApp.GetHardKeeper()
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.app = tApp
|
|
|
|
suite.ctx = ctx
|
|
|
|
suite.keeper = keeper
|
|
|
|
suite.addrs = addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestGetSetPreviousBlockTime() {
|
|
|
|
now := tmtime.Now()
|
|
|
|
|
|
|
|
_, f := suite.keeper.GetPreviousBlockTime(suite.ctx)
|
|
|
|
suite.Require().False(f)
|
|
|
|
|
|
|
|
suite.NotPanics(func() { suite.keeper.SetPreviousBlockTime(suite.ctx, now) })
|
|
|
|
|
|
|
|
pbt, f := suite.keeper.GetPreviousBlockTime(suite.ctx)
|
|
|
|
suite.True(f)
|
|
|
|
suite.Equal(now, pbt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestGetSetDeleteDeposit() {
|
2021-01-07 10:23:05 +00:00
|
|
|
dep := types.NewDeposit(sdk.AccAddress("test"), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))),
|
|
|
|
types.SupplyInterestFactors{types.NewSupplyInterestFactor("", sdk.MustNewDecFromStr("0"))})
|
2020-09-21 21:08:43 +00:00
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
_, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().False(f)
|
|
|
|
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, dep)
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
testDeposit, f := suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().True(f)
|
|
|
|
suite.Require().Equal(dep, testDeposit)
|
|
|
|
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) })
|
|
|
|
|
2020-12-18 16:05:21 +00:00
|
|
|
_, f = suite.keeper.GetDeposit(suite.ctx, sdk.AccAddress("test"))
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().False(f)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestIterateDeposits() {
|
|
|
|
for i := 0; i < 5; i++ {
|
2021-01-07 10:23:05 +00:00
|
|
|
dep := types.NewDeposit(sdk.AccAddress("test"+fmt.Sprint(i)), sdk.NewCoins(sdk.NewCoin("bnb", sdk.NewInt(100))), types.SupplyInterestFactors{})
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().NotPanics(func() { suite.keeper.SetDeposit(suite.ctx, dep) })
|
|
|
|
}
|
|
|
|
var deposits []types.Deposit
|
|
|
|
suite.keeper.IterateDeposits(suite.ctx, func(d types.Deposit) bool {
|
|
|
|
deposits = append(deposits, d)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
suite.Require().Equal(5, len(deposits))
|
|
|
|
}
|
|
|
|
|
2020-11-13 15:51:52 +00:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetDeleteInterestRateModel() {
|
|
|
|
denom := "test"
|
|
|
|
model := types.NewInterestRateModel(sdk.MustNewDecFromStr("0.05"), sdk.MustNewDecFromStr("2"), sdk.MustNewDecFromStr("0.8"), sdk.MustNewDecFromStr("10"))
|
2020-12-03 21:50:35 +00:00
|
|
|
borrowLimit := types.NewBorrowLimit(false, sdk.MustNewDecFromStr("0.2"), sdk.MustNewDecFromStr("0.5"))
|
2020-12-18 01:12:48 +00:00
|
|
|
moneyMarket := types.NewMoneyMarket(denom, borrowLimit, denom+":usd", sdk.NewInt(1000000), sdk.NewInt(1000000000), model, sdk.MustNewDecFromStr("0.05"), sdk.ZeroDec())
|
2020-11-13 15:51:52 +00:00
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
_, f := suite.keeper.GetMoneyMarket(suite.ctx, denom)
|
2020-11-13 15:51:52 +00:00
|
|
|
suite.Require().False(f)
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
suite.keeper.SetMoneyMarket(suite.ctx, denom, moneyMarket)
|
2020-11-13 15:51:52 +00:00
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
testMoneyMarket, f := suite.keeper.GetMoneyMarket(suite.ctx, denom)
|
2020-11-13 15:51:52 +00:00
|
|
|
suite.Require().True(f)
|
2020-12-03 21:50:35 +00:00
|
|
|
suite.Require().Equal(moneyMarket, testMoneyMarket)
|
2020-11-13 15:51:52 +00:00
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
suite.Require().NotPanics(func() { suite.keeper.DeleteMoneyMarket(suite.ctx, denom) })
|
2020-11-13 15:51:52 +00:00
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
_, f = suite.keeper.GetMoneyMarket(suite.ctx, denom)
|
2020-11-13 15:51:52 +00:00
|
|
|
suite.Require().False(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestIterateInterestRateModels() {
|
|
|
|
testDenom := "test"
|
2020-12-03 21:50:35 +00:00
|
|
|
var setMMs types.MoneyMarkets
|
2020-11-13 15:51:52 +00:00
|
|
|
var setDenoms []string
|
|
|
|
for i := 0; i < 5; i++ {
|
2020-12-03 21:50:35 +00:00
|
|
|
// Initialize a new money market
|
2020-11-13 15:51:52 +00:00
|
|
|
denom := testDenom + strconv.Itoa(i)
|
|
|
|
model := types.NewInterestRateModel(sdk.MustNewDecFromStr("0.05"), sdk.MustNewDecFromStr("2"), sdk.MustNewDecFromStr("0.8"), sdk.MustNewDecFromStr("10"))
|
2020-12-03 21:50:35 +00:00
|
|
|
borrowLimit := types.NewBorrowLimit(false, sdk.MustNewDecFromStr("0.2"), sdk.MustNewDecFromStr("0.5"))
|
2020-12-18 01:12:48 +00:00
|
|
|
moneyMarket := types.NewMoneyMarket(denom, borrowLimit, denom+":usd", sdk.NewInt(1000000), sdk.NewInt(1000000000), model, sdk.MustNewDecFromStr("0.05"), sdk.ZeroDec())
|
2020-12-03 21:50:35 +00:00
|
|
|
|
|
|
|
// Store money market in the module's store
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.SetMoneyMarket(suite.ctx, denom, moneyMarket) })
|
|
|
|
|
2020-11-13 15:51:52 +00:00
|
|
|
// Save the denom and model
|
|
|
|
setDenoms = append(setDenoms, denom)
|
2020-12-03 21:50:35 +00:00
|
|
|
setMMs = append(setMMs, moneyMarket)
|
2020-11-13 15:51:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
var seenMMs types.MoneyMarkets
|
2020-11-13 15:51:52 +00:00
|
|
|
var seenDenoms []string
|
2020-12-03 21:50:35 +00:00
|
|
|
suite.keeper.IterateMoneyMarkets(suite.ctx, func(denom string, i types.MoneyMarket) bool {
|
2020-11-13 15:51:52 +00:00
|
|
|
seenDenoms = append(seenDenoms, denom)
|
2020-12-03 21:50:35 +00:00
|
|
|
seenMMs = append(seenMMs, i)
|
2020-11-13 15:51:52 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
suite.Require().Equal(setMMs, seenMMs)
|
2020-11-13 15:51:52 +00:00
|
|
|
suite.Require().Equal(setDenoms, seenDenoms)
|
|
|
|
}
|
|
|
|
|
2020-12-16 21:08:29 +00:00
|
|
|
func (suite *KeeperTestSuite) TestSetDeleteLtvIndex() {
|
|
|
|
// LTV index should have 0 items
|
2020-12-18 01:12:48 +00:00
|
|
|
firstAddrs := suite.keeper.GetLtvIndexSlice(suite.ctx, 10)
|
2020-12-16 21:08:29 +00:00
|
|
|
suite.Require().Equal(0, len(firstAddrs))
|
|
|
|
|
|
|
|
// Add an item to the LTV index
|
|
|
|
addr := sdk.AccAddress("test")
|
|
|
|
ltv := sdk.MustNewDecFromStr("1.1")
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.InsertIntoLtvIndex(suite.ctx, ltv, addr) })
|
|
|
|
|
|
|
|
// LTV index should have 1 item
|
2020-12-18 01:12:48 +00:00
|
|
|
secondAddrs := suite.keeper.GetLtvIndexSlice(suite.ctx, 10)
|
2020-12-16 21:08:29 +00:00
|
|
|
suite.Require().Equal(1, len(secondAddrs))
|
|
|
|
|
|
|
|
// Attempt to remove invalid item from LTV index
|
|
|
|
fakeLtv := sdk.MustNewDecFromStr("1.2")
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.RemoveFromLtvIndex(suite.ctx, fakeLtv, addr) })
|
|
|
|
|
|
|
|
// LTV index should still have 1 item
|
2020-12-18 01:12:48 +00:00
|
|
|
thirdAddrs := suite.keeper.GetLtvIndexSlice(suite.ctx, 10)
|
2020-12-16 21:08:29 +00:00
|
|
|
suite.Require().Equal(1, len(thirdAddrs))
|
|
|
|
|
|
|
|
// Attempt to remove valid item from LTV index
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.RemoveFromLtvIndex(suite.ctx, ltv, addr) })
|
|
|
|
|
|
|
|
// LTV index should still have 0 items
|
2020-12-18 01:12:48 +00:00
|
|
|
fourthAddrs := suite.keeper.GetLtvIndexSlice(suite.ctx, 10)
|
2020-12-16 21:08:29 +00:00
|
|
|
suite.Require().Equal(0, len(fourthAddrs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestIterateLtvIndex() {
|
|
|
|
var setAddrs []sdk.AccAddress
|
|
|
|
for i := 1; i <= 20; i++ {
|
|
|
|
addr := sdk.AccAddress("test" + fmt.Sprint(i))
|
|
|
|
incrementalDec := sdk.NewDec(int64(i)).Quo(sdk.NewDec(10))
|
|
|
|
ltv := sdk.OneDec().Add(incrementalDec)
|
|
|
|
|
|
|
|
// Set the ltv-address pair in the store
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.InsertIntoLtvIndex(suite.ctx, ltv, addr) })
|
|
|
|
|
|
|
|
setAddrs = append(setAddrs, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only the first 10 addresses should be returned
|
2020-12-18 01:12:48 +00:00
|
|
|
sliceAddrs := suite.keeper.GetLtvIndexSlice(suite.ctx, 10)
|
|
|
|
suite.Require().Equal(addressSort(setAddrs[10:20]), addressSort(sliceAddrs))
|
|
|
|
|
|
|
|
// Insert an additional item into the LTV index that should be returned in the first 10 elements
|
|
|
|
addr := sdk.AccAddress("test" + fmt.Sprint(21))
|
|
|
|
ltv := sdk.OneDec().Add(sdk.MustNewDecFromStr("15").Quo(sdk.NewDec(10)))
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.InsertIntoLtvIndex(suite.ctx, ltv, addr) })
|
|
|
|
|
|
|
|
// Fetch the updated LTV index
|
|
|
|
updatedSliceAddrs := suite.keeper.GetLtvIndexSlice(suite.ctx, 10)
|
|
|
|
sawAddr := false
|
|
|
|
for _, updatedSliceAddr := range updatedSliceAddrs {
|
|
|
|
if updatedSliceAddr.Equals(addr) {
|
|
|
|
sawAddr = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
suite.Require().Equal(true, sawAddr)
|
2020-12-16 21:08:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
func (suite *KeeperTestSuite) getAccount(addr sdk.AccAddress) authexported.Account {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetAccount(suite.ctx, addr)
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
func (suite *KeeperTestSuite) getAccountAtCtx(addr sdk.AccAddress, ctx sdk.Context) authexported.Account {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetAccount(ctx, addr)
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
func (suite *KeeperTestSuite) getModuleAccount(name string) supplyexported.ModuleAccountI {
|
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
return sk.GetModuleAccount(suite.ctx, name)
|
|
|
|
}
|
|
|
|
|
2020-12-03 21:50:35 +00:00
|
|
|
func (suite *KeeperTestSuite) getModuleAccountAtCtx(name string, ctx sdk.Context) supplyexported.ModuleAccountI {
|
|
|
|
sk := suite.app.GetSupplyKeeper()
|
|
|
|
return sk.GetModuleAccount(ctx, name)
|
|
|
|
}
|
|
|
|
|
2020-12-18 01:12:48 +00:00
|
|
|
func addressSort(addrs []sdk.AccAddress) (sortedAddrs []sdk.AccAddress) {
|
|
|
|
addrStrs := []string{}
|
|
|
|
for _, addr := range addrs {
|
|
|
|
addrStrs = append(addrStrs, addr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(addrStrs)
|
|
|
|
|
|
|
|
for _, addrStr := range addrStrs {
|
|
|
|
addr, _ := sdk.AccAddressFromBech32(addrStr)
|
|
|
|
sortedAddrs = append(sortedAddrs, addr)
|
|
|
|
}
|
|
|
|
return sortedAddrs
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|