2019-12-05 13:53:28 +00:00
|
|
|
package auction_test
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2023-04-05 23:21:59 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2019-11-25 19:46:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/auction"
|
|
|
|
"github.com/0glabs/0g-chain/x/auction/testutil"
|
|
|
|
types "github.com/0glabs/0g-chain/x/auction/types"
|
2019-11-25 19:46:02 +00:00
|
|
|
)
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
type abciTestSuite struct {
|
|
|
|
testutil.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *abciTestSuite) SetupTest() {
|
|
|
|
suite.Suite.SetupTest(4)
|
|
|
|
}
|
2019-12-05 13:53:28 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func TestABCITestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(abciTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *abciTestSuite) TestKeeper_BeginBlocker() {
|
|
|
|
buyer := suite.Addrs[0]
|
|
|
|
returnAddrs := []sdk.AccAddress{suite.Addrs[1]}
|
2023-04-05 23:21:59 +00:00
|
|
|
returnWeights := []sdkmath.Int{sdkmath.NewInt(1)}
|
2019-12-05 13:53:28 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.AddCoinsToNamedModule(suite.ModAcc.Name, cs(c("token1", 100), c("token2", 100), c("debt", 100)))
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-02-28 22:16:22 +00:00
|
|
|
// Start an auction and place a bid
|
2022-01-08 00:39:27 +00:00
|
|
|
auctionID, err := suite.Keeper.StartCollateralAuction(suite.Ctx, suite.ModAcc.Name, c("token1", 20), c("token2", 50), returnAddrs, returnWeights, c("debt", 40))
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NoError(suite.Keeper.PlaceBid(suite.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
|
2022-02-08 17:03:47 +00:00
|
|
|
preExpiryTime := suite.Ctx.BlockTime().Add(types.DefaultForwardBidDuration - 1)
|
2022-01-08 00:39:27 +00:00
|
|
|
auction.BeginBlocker(suite.Ctx.WithBlockTime(preExpiryTime), suite.Keeper)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2019-12-05 13:53:28 +00:00
|
|
|
// Check auction has not been closed yet
|
2022-01-08 00:39:27 +00:00
|
|
|
_, found := suite.Keeper.GetAuction(suite.Ctx, auctionID)
|
|
|
|
suite.True(found)
|
2019-12-05 13:53:28 +00:00
|
|
|
|
2020-01-12 15:12:22 +00:00
|
|
|
// Run the endblocker, simulating a block time equal to auction expiry
|
2022-02-08 17:03:47 +00:00
|
|
|
expiryTime := suite.Ctx.BlockTime().Add(types.DefaultForwardBidDuration)
|
2022-01-08 00:39:27 +00:00
|
|
|
auction.BeginBlocker(suite.Ctx.WithBlockTime(expiryTime), suite.Keeper)
|
2019-12-05 13:53:28 +00:00
|
|
|
|
|
|
|
// Check auction has been closed
|
2022-01-08 00:39:27 +00:00
|
|
|
_, found = suite.Keeper.GetAuction(suite.Ctx, auctionID)
|
|
|
|
suite.False(found)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
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...) }
|