2021-07-15 14:05:54 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2021-07-15 14:05:54 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
hardtypes "github.com/kava-labs/kava/x/hard/types"
|
|
|
|
"github.com/kava-labs/kava/x/incentive/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
oneYear time.Duration = time.Hour * 24 * 365
|
|
|
|
)
|
|
|
|
|
|
|
|
// IncentiveGenesisBuilder is a tool for creating an incentive genesis state.
|
|
|
|
// Helper methods add values onto a default genesis state.
|
|
|
|
// All methods are immutable and return updated copies of the builder.
|
|
|
|
type IncentiveGenesisBuilder struct {
|
|
|
|
types.GenesisState
|
|
|
|
genesisTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIncentiveGenesisBuilder() IncentiveGenesisBuilder {
|
|
|
|
return IncentiveGenesisBuilder{
|
|
|
|
GenesisState: types.DefaultGenesisState(),
|
|
|
|
genesisTime: time.Time{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) Build() types.GenesisState {
|
|
|
|
return builder.GenesisState
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) BuildMarshalled(cdc codec.JSONCodec) app.GenesisState {
|
|
|
|
built := builder.Build()
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return app.GenesisState{
|
2022-01-08 00:39:27 +00:00
|
|
|
types.ModuleName: cdc.MustMarshalJSON(&built),
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithGenesisTime(time time.Time) IncentiveGenesisBuilder {
|
|
|
|
builder.genesisTime = time
|
|
|
|
builder.Params.ClaimEnd = time.Add(5 * oneYear)
|
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// WithInitializedBorrowRewardPeriod sets the genesis time as the previous accumulation time for the specified period.
|
|
|
|
// This can be helpful in tests. With no prev time set, the first block accrues no rewards as it just sets the prev time to the current.
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) WithInitializedBorrowRewardPeriod(period types.MultiRewardPeriod) IncentiveGenesisBuilder {
|
|
|
|
builder.Params.HardBorrowRewardPeriods = append(builder.Params.HardBorrowRewardPeriods, period)
|
|
|
|
|
2021-07-15 14:41:55 +00:00
|
|
|
accumulationTimeForPeriod := types.NewAccumulationTime(period.CollateralType, builder.genesisTime)
|
|
|
|
builder.HardBorrowRewardState.AccumulationTimes = append(
|
|
|
|
builder.HardBorrowRewardState.AccumulationTimes,
|
|
|
|
accumulationTimeForPeriod,
|
|
|
|
)
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// TODO remove to better reflect real states
|
2021-07-15 14:41:55 +00:00
|
|
|
builder.HardBorrowRewardState.MultiRewardIndexes = builder.HardBorrowRewardState.MultiRewardIndexes.With(
|
|
|
|
period.CollateralType,
|
|
|
|
newZeroRewardIndexesFromCoins(period.RewardsPerSecond...),
|
|
|
|
)
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithSimpleBorrowRewardPeriod(ctype string, rewardsPerSecond sdk.Coins) IncentiveGenesisBuilder {
|
2021-07-15 14:41:55 +00:00
|
|
|
return builder.WithInitializedBorrowRewardPeriod(builder.simpleRewardPeriod(ctype, rewardsPerSecond))
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// WithInitializedSupplyRewardPeriod sets the genesis time as the previous accumulation time for the specified period.
|
|
|
|
// This can be helpful in tests. With no prev time set, the first block accrues no rewards as it just sets the prev time to the current.
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) WithInitializedSupplyRewardPeriod(period types.MultiRewardPeriod) IncentiveGenesisBuilder {
|
|
|
|
builder.Params.HardSupplyRewardPeriods = append(builder.Params.HardSupplyRewardPeriods, period)
|
|
|
|
|
2021-07-15 14:41:55 +00:00
|
|
|
accumulationTimeForPeriod := types.NewAccumulationTime(period.CollateralType, builder.genesisTime)
|
|
|
|
builder.HardSupplyRewardState.AccumulationTimes = append(
|
|
|
|
builder.HardSupplyRewardState.AccumulationTimes,
|
|
|
|
accumulationTimeForPeriod,
|
|
|
|
)
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// TODO remove to better reflect real states
|
2021-07-15 14:41:55 +00:00
|
|
|
builder.HardSupplyRewardState.MultiRewardIndexes = builder.HardSupplyRewardState.MultiRewardIndexes.With(
|
|
|
|
period.CollateralType,
|
|
|
|
newZeroRewardIndexesFromCoins(period.RewardsPerSecond...),
|
|
|
|
)
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithSimpleSupplyRewardPeriod(ctype string, rewardsPerSecond sdk.Coins) IncentiveGenesisBuilder {
|
2021-07-15 14:41:55 +00:00
|
|
|
return builder.WithInitializedSupplyRewardPeriod(builder.simpleRewardPeriod(ctype, rewardsPerSecond))
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// WithInitializedDelegatorRewardPeriod sets the genesis time as the previous accumulation time for the specified period.
|
|
|
|
// This can be helpful in tests. With no prev time set, the first block accrues no rewards as it just sets the prev time to the current.
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) WithInitializedDelegatorRewardPeriod(period types.MultiRewardPeriod) IncentiveGenesisBuilder {
|
|
|
|
builder.Params.DelegatorRewardPeriods = append(builder.Params.DelegatorRewardPeriods, period)
|
|
|
|
|
2021-07-15 14:41:55 +00:00
|
|
|
accumulationTimeForPeriod := types.NewAccumulationTime(period.CollateralType, builder.genesisTime)
|
|
|
|
builder.DelegatorRewardState.AccumulationTimes = append(
|
|
|
|
builder.DelegatorRewardState.AccumulationTimes,
|
|
|
|
accumulationTimeForPeriod,
|
|
|
|
)
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// TODO remove to better reflect real states
|
2021-07-15 14:41:55 +00:00
|
|
|
builder.DelegatorRewardState.MultiRewardIndexes = builder.DelegatorRewardState.MultiRewardIndexes.With(
|
|
|
|
period.CollateralType,
|
|
|
|
newZeroRewardIndexesFromCoins(period.RewardsPerSecond...),
|
|
|
|
)
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithSimpleDelegatorRewardPeriod(ctype string, rewardsPerSecond sdk.Coins) IncentiveGenesisBuilder {
|
2021-07-15 14:41:55 +00:00
|
|
|
return builder.WithInitializedDelegatorRewardPeriod(builder.simpleRewardPeriod(ctype, rewardsPerSecond))
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// WithInitializedSwapRewardPeriod sets the genesis time as the previous accumulation time for the specified period.
|
|
|
|
// This can be helpful in tests. With no prev time set, the first block accrues no rewards as it just sets the prev time to the current.
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) WithInitializedSwapRewardPeriod(period types.MultiRewardPeriod) IncentiveGenesisBuilder {
|
|
|
|
builder.Params.SwapRewardPeriods = append(builder.Params.SwapRewardPeriods, period)
|
|
|
|
|
2021-07-15 14:41:55 +00:00
|
|
|
accumulationTimeForPeriod := types.NewAccumulationTime(period.CollateralType, builder.genesisTime)
|
|
|
|
builder.SwapRewardState.AccumulationTimes = append(
|
|
|
|
builder.SwapRewardState.AccumulationTimes,
|
|
|
|
accumulationTimeForPeriod,
|
|
|
|
)
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithSimpleSwapRewardPeriod(poolID string, rewardsPerSecond sdk.Coins) IncentiveGenesisBuilder {
|
2021-07-15 14:41:55 +00:00
|
|
|
return builder.WithInitializedSwapRewardPeriod(builder.simpleRewardPeriod(poolID, rewardsPerSecond))
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// WithInitializedUSDXRewardPeriod sets the genesis time as the previous accumulation time for the specified period.
|
|
|
|
// This can be helpful in tests. With no prev time set, the first block accrues no rewards as it just sets the prev time to the current.
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) WithInitializedUSDXRewardPeriod(period types.RewardPeriod) IncentiveGenesisBuilder {
|
|
|
|
builder.Params.USDXMintingRewardPeriods = append(builder.Params.USDXMintingRewardPeriods, period)
|
|
|
|
|
2021-07-15 14:41:55 +00:00
|
|
|
accumulationTimeForPeriod := types.NewAccumulationTime(period.CollateralType, builder.genesisTime)
|
|
|
|
builder.USDXRewardState.AccumulationTimes = append(
|
|
|
|
builder.USDXRewardState.AccumulationTimes,
|
|
|
|
accumulationTimeForPeriod,
|
|
|
|
)
|
|
|
|
|
2021-07-22 12:53:18 +00:00
|
|
|
// TODO remove to better reflect real states
|
2021-07-15 14:41:55 +00:00
|
|
|
builder.USDXRewardState.MultiRewardIndexes = builder.USDXRewardState.MultiRewardIndexes.With(
|
|
|
|
period.CollateralType,
|
|
|
|
newZeroRewardIndexesFromCoins(period.RewardsPerSecond),
|
|
|
|
)
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithSimpleUSDXRewardPeriod(ctype string, rewardsPerSecond sdk.Coin) IncentiveGenesisBuilder {
|
|
|
|
return builder.WithInitializedUSDXRewardPeriod(types.NewRewardPeriod(
|
|
|
|
true,
|
|
|
|
ctype,
|
|
|
|
builder.genesisTime,
|
|
|
|
builder.genesisTime.Add(4*oneYear),
|
|
|
|
rewardsPerSecond,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) WithMultipliers(multipliers types.MultipliersPerDenoms) IncentiveGenesisBuilder {
|
2021-07-15 14:05:54 +00:00
|
|
|
builder.Params.ClaimMultipliers = multipliers
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:41:55 +00:00
|
|
|
func (builder IncentiveGenesisBuilder) simpleRewardPeriod(ctype string, rewardsPerSecond sdk.Coins) types.MultiRewardPeriod {
|
|
|
|
return types.NewMultiRewardPeriod(
|
|
|
|
true,
|
|
|
|
ctype,
|
|
|
|
builder.genesisTime,
|
|
|
|
builder.genesisTime.Add(4*oneYear),
|
|
|
|
rewardsPerSecond,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newZeroRewardIndexesFromCoins(coins ...sdk.Coin) types.RewardIndexes {
|
|
|
|
var ri types.RewardIndexes
|
|
|
|
for _, coin := range coins {
|
|
|
|
ri = ri.With(coin.Denom, sdk.ZeroDec())
|
|
|
|
}
|
|
|
|
return ri
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
// HardGenesisBuilder is a tool for creating a hard genesis state.
|
|
|
|
// Helper methods add values onto a default genesis state.
|
|
|
|
// All methods are immutable and return updated copies of the builder.
|
|
|
|
type HardGenesisBuilder struct {
|
|
|
|
hardtypes.GenesisState
|
|
|
|
genesisTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHardGenesisBuilder() HardGenesisBuilder {
|
|
|
|
return HardGenesisBuilder{
|
|
|
|
GenesisState: hardtypes.DefaultGenesisState(),
|
|
|
|
}
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder HardGenesisBuilder) Build() hardtypes.GenesisState {
|
|
|
|
return builder.GenesisState
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (builder HardGenesisBuilder) BuildMarshalled(cdc codec.JSONCodec) app.GenesisState {
|
|
|
|
built := builder.Build()
|
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
return app.GenesisState{
|
2022-01-08 00:39:27 +00:00
|
|
|
hardtypes.ModuleName: cdc.MustMarshalJSON(&built),
|
2021-07-15 14:05:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder HardGenesisBuilder) WithGenesisTime(genTime time.Time) HardGenesisBuilder {
|
|
|
|
builder.genesisTime = genTime
|
|
|
|
return builder
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (builder HardGenesisBuilder) WithInitializedMoneyMarket(market hardtypes.MoneyMarket) HardGenesisBuilder {
|
2021-07-15 14:05:54 +00:00
|
|
|
builder.Params.MoneyMarkets = append(builder.Params.MoneyMarkets, market)
|
|
|
|
|
|
|
|
builder.PreviousAccumulationTimes = append(
|
|
|
|
builder.PreviousAccumulationTimes,
|
|
|
|
hardtypes.NewGenesisAccumulationTime(market.Denom, builder.genesisTime, sdk.OneDec(), sdk.OneDec()),
|
|
|
|
)
|
|
|
|
return builder
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
func (builder HardGenesisBuilder) WithMinBorrow(minUSDValue sdk.Dec) HardGenesisBuilder {
|
|
|
|
builder.Params.MinimumBorrowUSDValue = minUSDValue
|
|
|
|
return builder
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2021-07-15 14:05:54 +00:00
|
|
|
func NewStandardMoneyMarket(denom string) hardtypes.MoneyMarket {
|
|
|
|
return hardtypes.NewMoneyMarket(
|
|
|
|
denom,
|
2022-01-08 00:39:27 +00:00
|
|
|
hardtypes.NewBorrowLimit(
|
2021-07-15 14:05:54 +00:00
|
|
|
false,
|
|
|
|
sdk.NewDec(1e15),
|
|
|
|
sdk.MustNewDecFromStr("0.6"),
|
|
|
|
),
|
|
|
|
denom+":usd",
|
|
|
|
sdk.NewInt(1e6),
|
2022-01-08 00:39:27 +00:00
|
|
|
hardtypes.NewInterestRateModel(
|
2021-07-15 14:05:54 +00:00
|
|
|
sdk.MustNewDecFromStr("0.05"),
|
|
|
|
sdk.MustNewDecFromStr("2"),
|
|
|
|
sdk.MustNewDecFromStr("0.8"),
|
|
|
|
sdk.MustNewDecFromStr("10"),
|
|
|
|
),
|
|
|
|
sdk.MustNewDecFromStr("0.05"),
|
|
|
|
sdk.ZeroDec(),
|
|
|
|
)
|
|
|
|
}
|
2022-04-12 14:14:14 +00:00
|
|
|
|
|
|
|
// WithInitializedSavingsRewardPeriod sets the genesis time as the previous accumulation time for the specified period.
|
|
|
|
// This can be helpful in tests. With no prev time set, the first block accrues no rewards as it just sets the prev time to the current.
|
|
|
|
func (builder IncentiveGenesisBuilder) WithInitializedSavingsRewardPeriod(period types.MultiRewardPeriod) IncentiveGenesisBuilder {
|
|
|
|
builder.Params.SavingsRewardPeriods = append(builder.Params.SavingsRewardPeriods, period)
|
|
|
|
|
|
|
|
accumulationTimeForPeriod := types.NewAccumulationTime(period.CollateralType, builder.genesisTime)
|
|
|
|
builder.SavingsRewardState.AccumulationTimes = append(
|
|
|
|
builder.SavingsRewardState.AccumulationTimes,
|
|
|
|
accumulationTimeForPeriod,
|
|
|
|
)
|
|
|
|
|
|
|
|
builder.SavingsRewardState.MultiRewardIndexes = builder.SavingsRewardState.MultiRewardIndexes.With(
|
|
|
|
period.CollateralType,
|
|
|
|
newZeroRewardIndexesFromCoins(period.RewardsPerSecond...),
|
|
|
|
)
|
|
|
|
|
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func (builder IncentiveGenesisBuilder) WithSimpleSavingsRewardPeriod(ctype string, rewardsPerSecond sdk.Coins) IncentiveGenesisBuilder {
|
|
|
|
return builder.WithInitializedSavingsRewardPeriod(builder.simpleRewardPeriod(ctype, rewardsPerSecond))
|
|
|
|
}
|