2020-04-24 15:44:44 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2021-06-10 13:35:44 +00:00
|
|
|
"time"
|
2020-04-24 15:44:44 +00:00
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2021-06-10 13:35:44 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-04-24 15:44:44 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-30 14:13:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
2020-04-24 15:44:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test suite used for all keeper tests
|
|
|
|
type KeeperTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
keeper keeper.Keeper
|
|
|
|
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
|
|
|
|
genesisTime time.Time
|
|
|
|
addrs []sdk.AccAddress
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
// SetupTest is run automatically before each suite test
|
2020-04-24 15:44:44 +00:00
|
|
|
func (suite *KeeperTestSuite) SetupTest() {
|
2021-03-25 06:10:13 +00:00
|
|
|
config := sdk.GetConfig()
|
|
|
|
app.SetBech32AddressPrefixes(config)
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
_, suite.addrs = app.GeneratePrivKeyAddressPairs(5)
|
2021-01-25 12:58:12 +00:00
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.genesisTime = time.Date(2020, 12, 15, 14, 0, 0, 0, time.UTC)
|
2021-03-25 06:10:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
func (suite *KeeperTestSuite) SetupApp() {
|
|
|
|
suite.app = app.NewTestApp()
|
2020-04-24 15:44:44 +00:00
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.keeper = suite.app.GetIncentiveKeeper()
|
2020-04-24 15:44:44 +00:00
|
|
|
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.ctx = suite.app.NewContext(true, abci.Header{Height: 1, Time: suite.genesisTime})
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetDeleteUSDXMintingClaim() {
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.SetupApp()
|
2021-01-18 19:12:37 +00:00
|
|
|
c := types.NewUSDXMintingClaim(suite.addrs[0], c("ukava", 1000000), types.RewardIndexes{types.NewRewardIndex("bnb-a", sdk.ZeroDec())})
|
2021-01-21 13:52:09 +00:00
|
|
|
_, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().False(found)
|
|
|
|
suite.Require().NotPanics(func() {
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.SetUSDXMintingClaim(suite.ctx, c)
|
2020-04-24 15:44:44 +00:00
|
|
|
})
|
2021-01-21 13:52:09 +00:00
|
|
|
testC, found := suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().True(found)
|
|
|
|
suite.Require().Equal(c, testC)
|
|
|
|
suite.Require().NotPanics(func() {
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.DeleteUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2020-04-24 15:44:44 +00:00
|
|
|
})
|
2021-01-21 13:52:09 +00:00
|
|
|
_, found = suite.keeper.GetUSDXMintingClaim(suite.ctx, suite.addrs[0])
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().False(found)
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
func (suite *KeeperTestSuite) TestIterateUSDXMintingClaims() {
|
2021-06-10 13:35:44 +00:00
|
|
|
suite.SetupApp()
|
2021-01-18 19:12:37 +00:00
|
|
|
for i := 0; i < len(suite.addrs); i++ {
|
|
|
|
c := types.NewUSDXMintingClaim(suite.addrs[i], c("ukava", 100000), types.RewardIndexes{types.NewRewardIndex("bnb-a", sdk.ZeroDec())})
|
|
|
|
suite.Require().NotPanics(func() {
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.SetUSDXMintingClaim(suite.ctx, c)
|
2021-01-18 19:12:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
claims := types.USDXMintingClaims{}
|
2021-01-21 13:52:09 +00:00
|
|
|
suite.keeper.IterateUSDXMintingClaims(suite.ctx, func(c types.USDXMintingClaim) bool {
|
2020-04-24 15:44:44 +00:00
|
|
|
claims = append(claims, c)
|
|
|
|
return false
|
|
|
|
})
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().Equal(len(suite.addrs), len(claims))
|
2020-04-24 15:44:44 +00:00
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
claims = suite.keeper.GetAllUSDXMintingClaims(suite.ctx)
|
2021-01-18 19:12:37 +00:00
|
|
|
suite.Require().Equal(len(suite.addrs), len(claims))
|
2020-04-24 15:44:44 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 13:23:06 +00:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetSwapRewardIndexes() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
poolName string
|
|
|
|
indexes types.RewardIndexes
|
|
|
|
panics bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "two factors can be written and read",
|
|
|
|
poolName: "btc/usdx",
|
|
|
|
indexes: types.RewardIndexes{
|
|
|
|
{
|
|
|
|
CollateralType: "hard",
|
|
|
|
RewardFactor: d("0.02"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CollateralType: "ukava",
|
|
|
|
RewardFactor: d("0.04"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "indexes with empty pool name can be written and read",
|
|
|
|
poolName: "",
|
|
|
|
indexes: types.RewardIndexes{
|
|
|
|
{
|
|
|
|
CollateralType: "hard",
|
|
|
|
RewardFactor: d("0.02"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CollateralType: "ukava",
|
|
|
|
RewardFactor: d("0.04"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// this test is to detect any changes in behavior, it would be nice if Set didn't panic
|
|
|
|
name: "setting empty indexes panics",
|
|
|
|
poolName: "btc/usdx",
|
|
|
|
indexes: types.RewardIndexes{},
|
|
|
|
panics: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// this test is to detect any changes in behavior, it would be nice if Set didn't panic
|
|
|
|
name: "setting nil indexes panics",
|
|
|
|
poolName: "btc/usdx",
|
|
|
|
indexes: nil,
|
|
|
|
panics: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupApp()
|
|
|
|
|
|
|
|
_, found := suite.keeper.GetSwapRewardIndexes(suite.ctx, tc.poolName)
|
|
|
|
suite.False(found)
|
|
|
|
|
|
|
|
setFunc := func() { suite.keeper.SetSwapRewardIndexes(suite.ctx, tc.poolName, tc.indexes) }
|
|
|
|
if tc.panics {
|
|
|
|
suite.Panics(setFunc)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
suite.NotPanics(setFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
storedIndexes, found := suite.keeper.GetSwapRewardIndexes(suite.ctx, tc.poolName)
|
|
|
|
suite.True(found)
|
|
|
|
suite.Equal(tc.indexes, storedIndexes)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *KeeperTestSuite) TestGetSetSwapRewardAccrualTimes() {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
poolName string
|
|
|
|
accrualTime time.Time
|
|
|
|
panics bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "normal time can be written and read",
|
|
|
|
poolName: "btc/usdx",
|
|
|
|
accrualTime: time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "zero time can be written and read",
|
|
|
|
poolName: "btc/usdx",
|
|
|
|
accrualTime: time.Time{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(tc.name, func() {
|
|
|
|
suite.SetupApp()
|
|
|
|
|
|
|
|
_, found := suite.keeper.GetSwapRewardAccrualTime(suite.ctx, tc.poolName)
|
|
|
|
suite.False(found)
|
|
|
|
|
|
|
|
setFunc := func() { suite.keeper.SetSwapRewardAccrualTime(suite.ctx, tc.poolName, tc.accrualTime) }
|
|
|
|
if tc.panics {
|
|
|
|
suite.Panics(setFunc)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
suite.NotPanics(setFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
storedTime, found := suite.keeper.GetSwapRewardAccrualTime(suite.ctx, tc.poolName)
|
|
|
|
suite.True(found)
|
|
|
|
suite.Equal(tc.accrualTime, storedTime)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 13:52:09 +00:00
|
|
|
func TestKeeperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(KeeperTestSuite))
|
|
|
|
}
|