implement auction finalization system

This commit is contained in:
2022-08-31 22:40:28 +00:00
parent 5aea9c162e
commit fdbed8aa41
11 changed files with 129 additions and 18 deletions

View File

@@ -0,0 +1,42 @@
package keeper
import (
"cosmos-test/x/cosmostest/auctionconfig"
"cosmos-test/x/cosmostest/memdb"
"github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
)
func (k *Keeper) AuctionIsExpired(ctx types.Context, auctionId string) (bool, error) {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
if !found {
return true, errors.Errorf("auction %s not found", auctionId)
}
return uint64(ctx.BlockHeight()) >= auction.Deadline, nil
}
func (k *Keeper) EndExpiredAuctions(ctx types.Context) {
memdb.BidDB.ForEachAuction(func(auctionId string) error {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
if !found {
return errors.Errorf("auction %s not found", auctionId)
}
if uint64(ctx.BlockHeight()) >= auction.Deadline-auctionconfig.AuctionTime {
var err error
auction.TopBid, err = memdb.BidDB.GetHighestBid(auctionId)
if err != nil {
return errors.Errorf("could not get highest bid for auction %s: %s", auctionId, err)
}
k.SetAuction(ctx, auction)
memdb.BidDB.ClearAuction(auctionId)
}
return nil
})
}

View File

@@ -24,6 +24,10 @@ func (k Keeper) AuctionBids(goCtx context.Context, req *types.QueryAuctionBidsRe
return nil, fmt.Errorf("auction %s not found", req.Index)
}
if uint64(ctx.BlockHeight()) >= auction.Deadline {
return nil, fmt.Errorf("auction %s is already finalized", req.Index)
}
bids, err := memdb.BidDB.GetBids(auction.Index)
if err != nil {
return nil, fmt.Errorf("failed to get bids for auction %s: %s", auction.Index, err)

View File

@@ -5,6 +5,7 @@ import (
"errors"
"strconv"
"cosmos-test/x/cosmostest/auctionconfig"
"cosmos-test/x/cosmostest/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -24,6 +25,7 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Name: msg.Name,
Description: msg.Description,
TopBid: new(types.Bid),
Deadline: uint64(ctx.BlockHeight()) + auctionconfig.AuctionTime,
}
k.Keeper.SetAuction(ctx, auction)

View File

@@ -20,6 +20,14 @@ func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.M
return nil, fmt.Errorf("didn't find auction of index %s", msg.AuctionIndex)
}
auctionExpired, err := k.Keeper.AuctionIsExpired(ctx, msg.AuctionIndex)
if err != nil {
return nil, fmt.Errorf("error while checking auction %s expiry status: %s", msg.AuctionIndex, err)
}
if auctionExpired {
return nil, fmt.Errorf("auction %s is expired", msg.AuctionIndex)
}
ok := false
amt := new(big.Int)
amt, ok = amt.SetString(msg.Amount, 10)