fix auction logic, finalization, & memcaching
parent
a2bb428f2f
commit
9727e246ee
|
@ -1,7 +1,6 @@
|
||||||
package keeper
|
package keeper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cosmos-test/x/cosmostest/auctionconfig"
|
|
||||||
"cosmos-test/x/cosmostest/memdb"
|
"cosmos-test/x/cosmostest/memdb"
|
||||||
|
|
||||||
"github.com/cosmos/cosmos-sdk/types"
|
"github.com/cosmos/cosmos-sdk/types"
|
||||||
|
@ -24,10 +23,10 @@ func (k *Keeper) EndExpiredAuctions(ctx types.Context) {
|
||||||
auction, found := k.GetAuction(ctx, auctionId)
|
auction, found := k.GetAuction(ctx, auctionId)
|
||||||
// make sure the auction exists on-chain
|
// make sure the auction exists on-chain
|
||||||
if !found {
|
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
|
var err error
|
||||||
auction.Best, err = memdb.BidDB.GetLowestBid(auctionId)
|
auction.Best, err = memdb.BidDB.GetLowestBid(auctionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -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")
|
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
|
// we manually handle KeyNotFound in GetHighestBid, so should return (nil, nil) if not found
|
||||||
if err != nil {
|
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 := 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
|
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)
|
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,
|
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)
|
// auction.Bids = append(auction.Bids, bid)
|
||||||
// k.Keeper.SetAuction(ctx, auction)
|
// k.Keeper.SetAuction(ctx, auction)
|
||||||
|
|
|
@ -100,16 +100,18 @@ func (b *bidDB) GetLowestBid(auctionId string) (*types.Bid, error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
amt := big.NewInt(0)
|
minAmt := big.NewInt(0)
|
||||||
rAmt := big.NewInt(0)
|
refAmt := big.NewInt(0)
|
||||||
for _, rBid := range bids {
|
for _, currBid := range bids {
|
||||||
if bid == nil {
|
if bid == nil {
|
||||||
bid = rBid
|
bid = currBid
|
||||||
amt.SetString(rBid.Amount, 10)
|
minAmt.SetString(currBid.Amount, 10)
|
||||||
} else {
|
} else {
|
||||||
rAmt.SetString(rBid.Amount, 10)
|
// set ref amt.
|
||||||
if rAmt.Cmp(amt) == -1 {
|
refAmt.SetString(currBid.Amount, 10)
|
||||||
bid = rBid
|
// 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 {
|
err := b.db.View(func(txn *badger.Txn) error {
|
||||||
res, err := txn.Get(k)
|
res, err := txn.Get(k)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return err
|
||||||
} else {
|
} else {
|
||||||
err := res.Value(func(val []byte) error {
|
err := res.Value(func(val []byte) error {
|
||||||
dec := gob.NewDecoder(bytes.NewReader(val))
|
dec := gob.NewDecoder(bytes.NewReader(val))
|
||||||
|
@ -137,7 +140,6 @@ func (b *bidDB) GetBids(auctionId string) ([]*types.Bid, error) {
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return bids, err
|
return bids, err
|
||||||
|
|
|
@ -34,12 +34,12 @@ func TestBasicFlow(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check highest bid
|
// check lowest bid
|
||||||
if bid, err := BidDB.GetLowestBid("0"); err != nil {
|
if bid, err := BidDB.GetLowestBid("0"); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else {
|
} else {
|
||||||
if bid.Owner != "cosmos456" || bid.Amount != "200" {
|
if bid.Owner != "cosmos123" || bid.Amount != "100" {
|
||||||
panic("Highest auction item not selected")
|
panic(fmt.Sprintf("Lowest auction item not selected. Amount %s with owner %s was chosen.", bid.Amount, bid.Owner))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue