fix auction logic, finalization, & memcaching

master
michael 2022-09-01 04:48:54 +00:00
parent a2bb428f2f
commit 9727e246ee
4 changed files with 23 additions and 20 deletions

View File

@ -1,7 +1,6 @@
package keeper
import (
"cosmos-test/x/cosmostest/auctionconfig"
"cosmos-test/x/cosmostest/memdb"
"github.com/cosmos/cosmos-sdk/types"
@ -24,10 +23,10 @@ func (k *Keeper) EndExpiredAuctions(ctx types.Context) {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
if !found {
return errors.Errorf("auction %s not found", auctionId)
return errors.Errorf("auction %s not found on-chain", auctionId)
}
if uint64(ctx.BlockHeight()) >= auction.Deadline-auctionconfig.AuctionTime {
if uint64(ctx.BlockHeight()) >= auction.Deadline {
var err error
auction.Best, err = memdb.BidDB.GetLowestBid(auctionId)
if err != nil {

View File

@ -38,15 +38,15 @@ func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.M
return nil, fmt.Errorf("bid amount must be greater than 0")
}
highestBid, err := memdb.BidDB.GetLowestBid(msg.AuctionIndex)
lowestBid, err := memdb.BidDB.GetLowestBid(msg.AuctionIndex)
// we manually handle KeyNotFound in GetHighestBid, so should return (nil, nil) if not found
if err != nil {
return nil, fmt.Errorf("failed to get highest bid: %s", reflect.TypeOf(err))
return nil, fmt.Errorf("failed to get lowest bid: %s", reflect.TypeOf(err))
}
if highestBid != nil {
if lowestBid != nil {
amtPrev := new(big.Int)
amtPrev, ok = amtPrev.SetString(highestBid.Amount, 10)
amtPrev, ok = amtPrev.SetString(lowestBid.Amount, 10)
if !ok { // this should have been checked before, but whatever
return nil, fmt.Errorf("failed to convert max bid (%s) to a large integer", msg.Amount)
}
@ -61,7 +61,9 @@ func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.M
Owner: msg.Creator,
}
memdb.BidDB.AddBid(msg.AuctionIndex, bid)
if err := memdb.BidDB.AddBid(msg.AuctionIndex, bid); err != nil {
return nil, fmt.Errorf("failed to add bid: %s", err)
}
// auction.Bids = append(auction.Bids, bid)
// k.Keeper.SetAuction(ctx, auction)

View File

@ -100,16 +100,18 @@ func (b *bidDB) GetLowestBid(auctionId string) (*types.Bid, error) {
return nil
}
amt := big.NewInt(0)
rAmt := big.NewInt(0)
for _, rBid := range bids {
minAmt := big.NewInt(0)
refAmt := big.NewInt(0)
for _, currBid := range bids {
if bid == nil {
bid = rBid
amt.SetString(rBid.Amount, 10)
bid = currBid
minAmt.SetString(currBid.Amount, 10)
} else {
rAmt.SetString(rBid.Amount, 10)
if rAmt.Cmp(amt) == -1 {
bid = rBid
// set ref amt.
refAmt.SetString(currBid.Amount, 10)
// if ref amt is less, then we have a new min
if refAmt.Cmp(minAmt) == -1 {
bid = currBid
}
}
}
@ -129,6 +131,7 @@ func (b *bidDB) GetBids(auctionId string) ([]*types.Bid, error) {
err := b.db.View(func(txn *badger.Txn) error {
res, err := txn.Get(k)
if err != nil {
return err
} else {
err := res.Value(func(val []byte) error {
dec := gob.NewDecoder(bytes.NewReader(val))
@ -137,7 +140,6 @@ func (b *bidDB) GetBids(auctionId string) ([]*types.Bid, error) {
})
return err
}
return err
})
return bids, err

View File

@ -34,12 +34,12 @@ func TestBasicFlow(t *testing.T) {
}
}
// check highest bid
// check lowest bid
if bid, err := BidDB.GetLowestBid("0"); err != nil {
panic(err)
} else {
if bid.Owner != "cosmos456" || bid.Amount != "200" {
panic("Highest auction item not selected")
if bid.Owner != "cosmos123" || bid.Amount != "100" {
panic(fmt.Sprintf("Lowest auction item not selected. Amount %s with owner %s was chosen.", bid.Amount, bid.Owner))
}
}