2020-09-21 21:08:43 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
2020-12-03 23:05:56 +00:00
|
|
|
"fmt"
|
2020-11-13 15:51:52 +00:00
|
|
|
"strconv"
|
2020-09-21 21:08:43 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2020-09-21 21:08:43 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
tmtime "github.com/tendermint/tendermint/types/time"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
2022-01-08 00:39:27 +00:00
|
|
|
auctionkeeper "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"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test suite used for all keeper tests
|
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper keeper.Keeper
|
|
|
|
auctionKeeper auctionkeeper.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()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
2020-09-21 21:08:43 +00:00
|
|
|
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) TestGetSetDeleteDeposit() {
|
2023-04-04 00:08:45 +00:00
|
|
|
addr := suite.addrs[0]
|
|
|
|
dep := types.NewDeposit(
|
|
|
|
addr,
|
2023-04-05 23:21:59 +00:00
|
|
|
sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))),
|
2023-04-04 00:08:45 +00:00
|
|
|
types.SupplyInterestFactors{types.NewSupplyInterestFactor("bnb", sdk.MustNewDecFromStr("1.12"))},
|
|
|
|
)
|
|
|
|
|
|
|
|
_, f := suite.keeper.GetDeposit(suite.ctx, addr)
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().False(f)
|
|
|
|
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, dep)
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
storedDeposit, f := suite.keeper.GetDeposit(suite.ctx, addr)
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().True(f)
|
2023-04-04 00:08:45 +00:00
|
|
|
suite.Require().Equal(dep, storedDeposit)
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.DeleteDeposit(suite.ctx, dep) })
|
|
|
|
|
2023-04-04 00:08:45 +00:00
|
|
|
_, f = suite.keeper.GetDeposit(suite.ctx, addr)
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.Require().False(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestIterateDeposits() {
|
2023-04-04 00:08:45 +00:00
|
|
|
var deposits types.Deposits
|
2020-09-21 21:08:43 +00:00
|
|
|
for i := 0; i < 5; i++ {
|
2023-04-04 00:08:45 +00:00
|
|
|
dep := types.NewDeposit(
|
|
|
|
sdk.AccAddress("test"+fmt.Sprint(i)),
|
2023-04-05 23:21:59 +00:00
|
|
|
sdk.NewCoins(sdk.NewCoin("bnb", sdkmath.NewInt(100))),
|
2023-04-04 00:08:45 +00:00
|
|
|
types.SupplyInterestFactors{types.NewSupplyInterestFactor("bnb", sdk.MustNewDecFromStr("1.12"))},
|
|
|
|
)
|
|
|
|
deposits = append(deposits, dep)
|
|
|
|
suite.keeper.SetDeposit(suite.ctx, dep)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2023-04-04 00:08:45 +00:00
|
|
|
var storedDeposits types.Deposits
|
2020-09-21 21:08:43 +00:00
|
|
|
suite.keeper.IterateDeposits(suite.ctx, func(d types.Deposit) bool {
|
2023-04-04 00:08:45 +00:00
|
|
|
storedDeposits = append(storedDeposits, d)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
suite.Require().Equal(deposits, storedDeposits)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestGetSetDeleteBorrow() {
|
|
|
|
addr := suite.addrs[0]
|
|
|
|
|
|
|
|
borrow := types.NewBorrow(
|
|
|
|
addr,
|
|
|
|
sdk.NewCoins(sdk.NewInt64Coin("bnb", 1e9)),
|
|
|
|
types.BorrowInterestFactors{types.NewBorrowInterestFactor("bnb", sdk.MustNewDecFromStr("1.12"))},
|
|
|
|
)
|
|
|
|
|
|
|
|
_, f := suite.keeper.GetBorrow(suite.ctx, addr)
|
|
|
|
suite.Require().False(f)
|
|
|
|
|
|
|
|
suite.keeper.SetBorrow(suite.ctx, borrow)
|
|
|
|
|
|
|
|
storedBorrow, f := suite.keeper.GetBorrow(suite.ctx, addr)
|
|
|
|
suite.Require().True(f)
|
|
|
|
suite.Require().Equal(borrow, storedBorrow)
|
|
|
|
|
|
|
|
suite.Require().NotPanics(func() { suite.keeper.DeleteBorrow(suite.ctx, borrow) })
|
|
|
|
|
|
|
|
_, f = suite.keeper.GetBorrow(suite.ctx, addr)
|
|
|
|
suite.Require().False(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestIterateBorrows() {
|
|
|
|
var borrows types.Borrows
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
borrow := types.NewBorrow(
|
|
|
|
sdk.AccAddress("test"+fmt.Sprint(i)),
|
|
|
|
sdk.NewCoins(sdk.NewInt64Coin("bnb", 1e9)),
|
|
|
|
types.BorrowInterestFactors{types.NewBorrowInterestFactor("bnb", sdk.MustNewDecFromStr("1.12"))},
|
|
|
|
)
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
suite.keeper.SetBorrow(suite.ctx, borrow)
|
|
|
|
}
|
|
|
|
var storedBorrows types.Borrows
|
|
|
|
suite.keeper.IterateBorrows(suite.ctx, func(b types.Borrow) bool {
|
|
|
|
storedBorrows = append(storedBorrows, b)
|
2020-09-21 21:08:43 +00:00
|
|
|
return false
|
|
|
|
})
|
2023-04-04 00:08:45 +00:00
|
|
|
suite.Require().Equal(borrows, storedBorrows)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
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"))
|
2023-04-05 23:21:59 +00:00
|
|
|
moneyMarket := types.NewMoneyMarket(denom, borrowLimit, denom+":usd", sdkmath.NewInt(1000000), 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"))
|
2023-04-05 23:21:59 +00:00
|
|
|
moneyMarket := types.NewMoneyMarket(denom, borrowLimit, denom+":usd", sdkmath.NewInt(1000000), 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)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetBorrowedCoins() {
|
|
|
|
suite.keeper.SetBorrowedCoins(suite.ctx, sdk.Coins{c("ukava", 123)})
|
|
|
|
|
|
|
|
coins, found := suite.keeper.GetBorrowedCoins(suite.ctx)
|
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Len(coins, 1)
|
|
|
|
suite.Require().Equal(coins, cs(c("ukava", 123)))
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetBorrowedCoins_Empty() {
|
|
|
|
coins, found := suite.keeper.GetBorrowedCoins(suite.ctx)
|
|
|
|
suite.Require().False(found)
|
|
|
|
suite.Require().Empty(coins)
|
|
|
|
|
|
|
|
// None set and setting empty coins should both be the same
|
|
|
|
suite.keeper.SetBorrowedCoins(suite.ctx, sdk.Coins{})
|
|
|
|
|
|
|
|
coins, found = suite.keeper.GetBorrowedCoins(suite.ctx)
|
|
|
|
suite.Require().False(found)
|
|
|
|
suite.Require().Empty(coins)
|
2020-12-03 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) getAccountCoins(acc authtypes.AccountI) sdk.Coins {
|
|
|
|
bk := suite.app.GetBankKeeper()
|
|
|
|
return bk.GetAllBalances(suite.ctx, acc.GetAddress())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) getAccount(addr sdk.AccAddress) authtypes.AccountI {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetAccount(suite.ctx, addr)
|
2020-12-03 21:50:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) getAccountAtCtx(addr sdk.AccAddress, ctx sdk.Context) authtypes.AccountI {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetAccount(ctx, addr)
|
|
|
|
}
|
2020-12-18 01:12:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) getModuleAccount(name string) authtypes.ModuleAccountI {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetModuleAccount(suite.ctx, name)
|
|
|
|
}
|
2020-12-18 01:12:48 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *KeeperTestSuite) getModuleAccountAtCtx(name string, ctx sdk.Context) authtypes.ModuleAccountI {
|
|
|
|
ak := suite.app.GetAccountKeeper()
|
|
|
|
return ak.GetModuleAccount(ctx, name)
|
2020-12-18 01:12:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|