2020-01-14 14:00:37 +00:00
|
|
|
package auction_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2020-05-11 19:45:00 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-05-11 19:45:00 +00:00
|
|
|
|
2020-01-14 14:00:37 +00:00
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/auction"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/auction/types"
|
2020-01-14 14:00:37 +00:00
|
|
|
)
|
|
|
|
|
2022-05-09 18:37:36 +00:00
|
|
|
var (
|
|
|
|
_, testAddrs = app.GeneratePrivKeyAddressPairs(2)
|
|
|
|
testTime = time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
testAuction = types.NewCollateralAuction(
|
|
|
|
"seller",
|
|
|
|
c("lotdenom", 10),
|
|
|
|
testTime,
|
|
|
|
c("biddenom", 1000),
|
2023-04-05 23:21:59 +00:00
|
|
|
types.WeightedAddresses{Addresses: testAddrs, Weights: []sdkmath.Int{sdk.OneInt(), sdk.OneInt()}},
|
2022-05-09 18:37:36 +00:00
|
|
|
c("debt", 1000),
|
|
|
|
).WithID(3).(types.GenesisAuction)
|
|
|
|
)
|
2020-01-14 14:00:37 +00:00
|
|
|
|
|
|
|
func TestInitGenesis(t *testing.T) {
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
// setup keepers
|
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
|
|
|
|
|
2020-04-13 16:01:54 +00:00
|
|
|
// setup module account
|
2022-01-08 00:39:27 +00:00
|
|
|
modBaseAcc := authtypes.NewBaseAccount(authtypes.NewModuleAddress(types.ModuleName), nil, 0, 0)
|
|
|
|
modAcc := authtypes.NewModuleAccount(modBaseAcc, types.ModuleName, []string{authtypes.Minter, authtypes.Burner}...)
|
|
|
|
tApp.GetAccountKeeper().SetModuleAccount(ctx, modAcc)
|
|
|
|
tApp.GetBankKeeper().MintCoins(ctx, types.ModuleName, testAuction.GetModuleAccountCoins())
|
2020-04-13 16:01:54 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// set up auction genesis state with module account
|
|
|
|
auctionGS, err := types.NewGenesisState(
|
2020-01-14 14:00:37 +00:00
|
|
|
10,
|
2022-01-08 00:39:27 +00:00
|
|
|
types.DefaultParams(),
|
|
|
|
[]types.GenesisAuction{testAuction},
|
2020-01-14 14:00:37 +00:00
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-14 14:00:37 +00:00
|
|
|
|
|
|
|
// run init
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper := tApp.GetAuctionKeeper()
|
2020-01-14 14:00:37 +00:00
|
|
|
require.NotPanics(t, func() {
|
2022-01-08 00:39:27 +00:00
|
|
|
auction.InitGenesis(ctx, keeper, tApp.GetBankKeeper(), tApp.GetAccountKeeper(), auctionGS)
|
2020-01-14 14:00:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// check state is as expected
|
|
|
|
actualID, err := keeper.GetNextAuctionID(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.Equal(t, auctionGS.NextAuctionId, actualID)
|
|
|
|
|
|
|
|
require.Equal(t, auctionGS.Params, keeper.GetParams(ctx))
|
2020-01-14 14:00:37 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
genesisAuctions, err := types.UnpackGenesisAuctions(auctionGS.Auctions)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-01-14 14:00:37 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
sort.Slice(genesisAuctions, func(i, j int) bool {
|
|
|
|
return genesisAuctions[i].GetID() > genesisAuctions[j].GetID()
|
2020-01-14 14:00:37 +00:00
|
|
|
})
|
|
|
|
i := 0
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper.IterateAuctions(ctx, func(a types.Auction) bool {
|
|
|
|
require.Equal(t, genesisAuctions[i], a)
|
2020-01-14 14:00:37 +00:00
|
|
|
i++
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
})
|
2020-04-13 16:01:54 +00:00
|
|
|
t.Run("invalid (invalid nextAuctionID)", func(t *testing.T) {
|
2020-01-14 14:00:37 +00:00
|
|
|
// setup keepers
|
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
|
|
|
|
|
|
|
|
// setup module account
|
|
|
|
modBaseAcc := authtypes.NewBaseAccount(authtypes.NewModuleAddress(types.ModuleName), nil, 0, 0)
|
|
|
|
modAcc := authtypes.NewModuleAccount(modBaseAcc, types.ModuleName, []string{authtypes.Minter, authtypes.Burner}...)
|
|
|
|
tApp.GetAccountKeeper().SetModuleAccount(ctx, modAcc)
|
|
|
|
tApp.GetBankKeeper().MintCoins(ctx, types.ModuleName, testAuction.GetModuleAccountCoins())
|
2020-01-14 14:00:37 +00:00
|
|
|
|
|
|
|
// create invalid genesis
|
2022-01-08 00:39:27 +00:00
|
|
|
auctionGS, err := types.NewGenesisState(
|
2020-01-14 14:00:37 +00:00
|
|
|
0, // next id < testAuction ID
|
2022-01-08 00:39:27 +00:00
|
|
|
types.DefaultParams(),
|
|
|
|
[]types.GenesisAuction{testAuction},
|
2020-01-14 14:00:37 +00:00
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-13 16:01:54 +00:00
|
|
|
|
|
|
|
// check init fails
|
|
|
|
require.Panics(t, func() {
|
2022-01-08 00:39:27 +00:00
|
|
|
auction.InitGenesis(ctx, tApp.GetAuctionKeeper(), tApp.GetBankKeeper(), tApp.GetAccountKeeper(), auctionGS)
|
2020-04-13 16:01:54 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("invalid (missing mod account coins)", func(t *testing.T) {
|
|
|
|
// setup keepers
|
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
|
|
|
|
|
|
|
|
// invalid as there is no module account setup
|
2020-04-13 16:01:54 +00:00
|
|
|
|
|
|
|
// create invalid genesis
|
2022-01-08 00:39:27 +00:00
|
|
|
auctionGS, err := types.NewGenesisState(
|
2020-04-13 16:01:54 +00:00
|
|
|
10,
|
2022-01-08 00:39:27 +00:00
|
|
|
types.DefaultParams(),
|
|
|
|
[]types.GenesisAuction{testAuction},
|
2020-04-13 16:01:54 +00:00
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-14 14:00:37 +00:00
|
|
|
|
|
|
|
// check init fails
|
|
|
|
require.Panics(t, func() {
|
2022-01-08 00:39:27 +00:00
|
|
|
auction.InitGenesis(ctx, tApp.GetAuctionKeeper(), tApp.GetBankKeeper(), tApp.GetAccountKeeper(), auctionGS)
|
2020-01-14 14:00:37 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportGenesis(t *testing.T) {
|
|
|
|
t.Run("default", func(t *testing.T) {
|
|
|
|
// setup state
|
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
|
2020-01-14 14:00:37 +00:00
|
|
|
tApp.InitializeFromGenesisStates()
|
|
|
|
|
|
|
|
// export
|
|
|
|
gs := auction.ExportGenesis(ctx, tApp.GetAuctionKeeper())
|
|
|
|
|
|
|
|
// check state matches
|
2022-01-08 00:39:27 +00:00
|
|
|
defaultGS := types.DefaultGenesisState()
|
|
|
|
require.Equal(t, defaultGS, gs)
|
2020-01-14 14:00:37 +00:00
|
|
|
})
|
|
|
|
t.Run("one auction", func(t *testing.T) {
|
|
|
|
// setup state
|
|
|
|
tApp := app.NewTestApp()
|
2022-01-08 00:39:27 +00:00
|
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
|
2020-01-14 14:00:37 +00:00
|
|
|
tApp.InitializeFromGenesisStates()
|
|
|
|
tApp.GetAuctionKeeper().SetAuction(ctx, testAuction)
|
|
|
|
|
|
|
|
// export
|
|
|
|
gs := auction.ExportGenesis(ctx, tApp.GetAuctionKeeper())
|
|
|
|
|
|
|
|
// check state matches
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedGenesisState := types.DefaultGenesisState()
|
|
|
|
packedGenesisAuctions, err := types.PackGenesisAuctions([]types.GenesisAuction{testAuction})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedGenesisState.Auctions = append(expectedGenesisState.Auctions, packedGenesisAuctions...)
|
2020-01-14 14:00:37 +00:00
|
|
|
require.Equal(t, expectedGenesisState, gs)
|
|
|
|
})
|
|
|
|
}
|