2022-07-28 17:01:30 +00:00
|
|
|
package earn_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/app"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn/testutil"
|
|
|
|
"github.com/0glabs/0g-chain/x/earn/types"
|
2022-07-28 17:01:30 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type genesisTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) Test_InitGenesis_ValidationPanic() {
|
|
|
|
invalidState := types.NewGenesisState(
|
|
|
|
types.Params{
|
|
|
|
AllowedVaults: types.AllowedVaults{
|
2022-09-12 17:29:35 +00:00
|
|
|
types.NewAllowedVault(
|
|
|
|
"usdx", types.StrategyTypes{types.STRATEGY_TYPE_HARD},
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.VaultRecords{
|
|
|
|
{
|
2022-09-12 16:23:26 +00:00
|
|
|
TotalShares: types.VaultShare{
|
|
|
|
Denom: "", Amount: sdk.NewDec(1),
|
|
|
|
},
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.VaultShareRecords{},
|
|
|
|
)
|
|
|
|
|
|
|
|
suite.Panics(func() {
|
|
|
|
earn.InitGenesis(suite.Ctx, suite.Keeper, suite.AccountKeeper, invalidState)
|
|
|
|
}, "expected init genesis to panic with invalid state")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) Test_InitAndExportGenesis() {
|
|
|
|
depositor_1, err := sdk.AccAddressFromBech32("kava1esagqd83rhqdtpy5sxhklaxgn58k2m3s3mnpea")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
depositor_2, err := sdk.AccAddressFromBech32("kava1mq9qxlhze029lm0frzw2xr6hem8c3k9ts54w0w")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// slices are sorted by key as stored in the data store, so init and export can be compared with equal
|
|
|
|
state := types.NewGenesisState(
|
|
|
|
types.Params{
|
|
|
|
AllowedVaults: types.AllowedVaults{
|
2022-09-12 17:29:35 +00:00
|
|
|
types.NewAllowedVault(
|
|
|
|
"usdx",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_HARD},
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
types.NewAllowedVault(
|
|
|
|
"ukava",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
true,
|
|
|
|
[]sdk.AccAddress{suite.AccountKeeper.GetModuleAddress("distribution")},
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.VaultRecords{
|
|
|
|
types.VaultRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
TotalShares: types.NewVaultShare("ukava", sdk.NewDec(3800000)),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
types.VaultRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
TotalShares: types.NewVaultShare("usdx", sdk.NewDec(1000000)),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.VaultShareRecords{
|
|
|
|
types.VaultShareRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
Depositor: depositor_1,
|
|
|
|
Shares: types.NewVaultShares(
|
|
|
|
types.NewVaultShare("usdx", sdk.NewDec(500000)),
|
|
|
|
types.NewVaultShare("ukava", sdk.NewDec(1900000)),
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
types.VaultShareRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
Depositor: depositor_2,
|
|
|
|
Shares: types.NewVaultShares(
|
|
|
|
types.NewVaultShare("usdx", sdk.NewDec(500000)),
|
|
|
|
types.NewVaultShare("ukava", sdk.NewDec(1900000)),
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
earn.InitGenesis(suite.Ctx, suite.Keeper, suite.AccountKeeper, state)
|
|
|
|
suite.Equal(state.Params, suite.Keeper.GetParams(suite.Ctx))
|
|
|
|
|
|
|
|
vaultRecord1, _ := suite.Keeper.GetVaultRecord(suite.Ctx, "ukava")
|
|
|
|
vaultRecord2, _ := suite.Keeper.GetVaultRecord(suite.Ctx, "usdx")
|
|
|
|
suite.Equal(state.VaultRecords[0], vaultRecord1)
|
|
|
|
suite.Equal(state.VaultRecords[1], vaultRecord2)
|
|
|
|
|
|
|
|
shareRecord1, _ := suite.Keeper.GetVaultShareRecord(suite.Ctx, depositor_1)
|
|
|
|
shareRecord2, _ := suite.Keeper.GetVaultShareRecord(suite.Ctx, depositor_2)
|
|
|
|
|
|
|
|
suite.Equal(state.VaultShareRecords[0], shareRecord1)
|
|
|
|
suite.Equal(state.VaultShareRecords[1], shareRecord2)
|
|
|
|
|
|
|
|
exportedState := earn.ExportGenesis(suite.Ctx, suite.Keeper)
|
|
|
|
suite.Equal(state, exportedState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *genesisTestSuite) Test_Marshall() {
|
|
|
|
depositor_1, err := sdk.AccAddressFromBech32("kava1esagqd83rhqdtpy5sxhklaxgn58k2m3s3mnpea")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
depositor_2, err := sdk.AccAddressFromBech32("kava1mq9qxlhze029lm0frzw2xr6hem8c3k9ts54w0w")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
|
|
|
|
// slices are sorted by key as stored in the data store, so init and export can be compared with equal
|
|
|
|
state := types.NewGenesisState(
|
|
|
|
types.Params{
|
|
|
|
AllowedVaults: types.AllowedVaults{
|
2022-09-12 17:29:35 +00:00
|
|
|
types.NewAllowedVault(
|
|
|
|
"usdx",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_HARD},
|
|
|
|
false,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
types.NewAllowedVault(
|
|
|
|
"ukava",
|
|
|
|
types.StrategyTypes{types.STRATEGY_TYPE_SAVINGS},
|
|
|
|
true,
|
|
|
|
[]sdk.AccAddress{suite.AccountKeeper.GetModuleAddress("distribution")},
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.VaultRecords{
|
|
|
|
types.VaultRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
TotalShares: types.NewVaultShare("ukava", sdk.NewDec(3800000)),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
types.VaultRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
TotalShares: types.NewVaultShare("usdx", sdk.NewDec(1000000)),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
types.VaultShareRecords{
|
|
|
|
types.VaultShareRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
Depositor: depositor_1,
|
|
|
|
Shares: types.NewVaultShares(
|
|
|
|
types.NewVaultShare("usdx", sdk.NewDec(500000)),
|
|
|
|
types.NewVaultShare("ukava", sdk.NewDec(1900000)),
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
types.VaultShareRecord{
|
2022-09-12 16:23:26 +00:00
|
|
|
Depositor: depositor_2,
|
|
|
|
Shares: types.NewVaultShares(
|
|
|
|
types.NewVaultShare("usdx", sdk.NewDec(500000)),
|
|
|
|
types.NewVaultShare("ukava", sdk.NewDec(1900000)),
|
|
|
|
),
|
2022-07-28 17:01:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
encodingCfg := app.MakeEncodingConfig()
|
|
|
|
cdc := encodingCfg.Marshaler
|
|
|
|
|
|
|
|
bz, err := cdc.Marshal(&state)
|
|
|
|
suite.Require().NoError(err, "expected genesis state to marshal without error")
|
|
|
|
|
|
|
|
var decodedState types.GenesisState
|
|
|
|
err = cdc.Unmarshal(bz, &decodedState)
|
|
|
|
suite.Require().NoError(err, "expected genesis state to unmarshal without error")
|
|
|
|
|
|
|
|
suite.Equal(state, decodedState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenesisTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(genesisTestSuite))
|
|
|
|
}
|