2019-12-05 13:53:28 +00:00
|
|
|
package auction_test
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-04-30 14:23:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-01-12 15:12:22 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
|
|
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2019-11-25 19:46:02 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-12-05 13:53:28 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/auction"
|
2020-01-12 15:35:34 +00:00
|
|
|
"github.com/kava-labs/kava/x/cdp"
|
2019-11-25 19:46:02 +00:00
|
|
|
)
|
|
|
|
|
2020-02-28 22:16:22 +00:00
|
|
|
func TestKeeper_BeginBlocker(t *testing.T) {
|
2019-12-05 13:53:28 +00:00
|
|
|
// Setup
|
2020-01-12 15:12:22 +00:00
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(2)
|
|
|
|
buyer := addrs[0]
|
|
|
|
returnAddrs := addrs[1:]
|
|
|
|
returnWeights := []sdk.Int{sdk.NewInt(1)}
|
2020-01-12 15:35:34 +00:00
|
|
|
sellerModName := cdp.LiquidatorMacc
|
2019-12-05 13:53:28 +00:00
|
|
|
|
|
|
|
tApp := app.NewTestApp()
|
2020-01-12 15:12:22 +00:00
|
|
|
sellerAcc := supply.NewEmptyModuleAccount(sellerModName)
|
2020-01-12 14:17:47 +00:00
|
|
|
require.NoError(t, sellerAcc.SetCoins(cs(c("token1", 100), c("token2", 100), c("debt", 100))))
|
2019-12-07 01:25:45 +00:00
|
|
|
tApp.InitializeFromGenesisStates(
|
2020-01-12 15:12:22 +00:00
|
|
|
NewAuthGenStateFromAccs(authexported.GenesisAccounts{
|
|
|
|
auth.NewBaseAccount(buyer, cs(c("token1", 100), c("token2", 100)), nil, 0, 0),
|
|
|
|
sellerAcc,
|
|
|
|
}),
|
2019-12-07 01:25:45 +00:00
|
|
|
)
|
2019-12-05 13:53:28 +00:00
|
|
|
|
|
|
|
ctx := tApp.NewContext(true, abci.Header{})
|
|
|
|
keeper := tApp.GetAuctionKeeper()
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-02-28 22:16:22 +00:00
|
|
|
// Start an auction and place a bid
|
2020-01-12 14:17:47 +00:00
|
|
|
auctionID, err := keeper.StartCollateralAuction(ctx, sellerModName, c("token1", 20), c("token2", 50), returnAddrs, returnWeights, c("debt", 40))
|
2019-12-05 13:53:28 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-12 15:12:22 +00:00
|
|
|
require.NoError(t, keeper.PlaceBid(ctx, auctionID, buyer, c("token2", 30)))
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-02-28 22:16:22 +00:00
|
|
|
// Run the beginblocker, simulating a block time 1ns before auction expiry
|
2020-01-12 15:12:22 +00:00
|
|
|
preExpiryTime := ctx.BlockTime().Add(auction.DefaultBidDuration - 1)
|
2020-02-28 22:16:22 +00:00
|
|
|
auction.BeginBlocker(ctx.WithBlockTime(preExpiryTime), keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2019-12-05 13:53:28 +00:00
|
|
|
// Check auction has not been closed yet
|
|
|
|
_, found := keeper.GetAuction(ctx, auctionID)
|
|
|
|
require.True(t, found)
|
|
|
|
|
2020-01-12 15:12:22 +00:00
|
|
|
// Run the endblocker, simulating a block time equal to auction expiry
|
|
|
|
expiryTime := ctx.BlockTime().Add(auction.DefaultBidDuration)
|
2020-02-28 22:16:22 +00:00
|
|
|
auction.BeginBlocker(ctx.WithBlockTime(expiryTime), keeper)
|
2019-12-05 13:53:28 +00:00
|
|
|
|
|
|
|
// Check auction has been closed
|
|
|
|
_, found = keeper.GetAuction(ctx, auctionID)
|
2019-11-25 19:46:02 +00:00
|
|
|
require.False(t, found)
|
|
|
|
}
|
2019-12-05 13:53:28 +00:00
|
|
|
|
|
|
|
func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
|
|
|
|
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }
|
2020-01-12 15:12:22 +00:00
|
|
|
|
|
|
|
func NewAuthGenStateFromAccs(accounts authexported.GenesisAccounts) app.GenesisState {
|
|
|
|
authGenesis := auth.NewGenesisState(auth.DefaultParams(), accounts)
|
|
|
|
return app.GenesisState{auth.ModuleName: auth.ModuleCdc.MustMarshalJSON(authGenesis)}
|
|
|
|
}
|