2022-08-28 10:57:06 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-28 13:28:25 -07:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2022-08-30 16:29:04 -07:00
|
|
|
"reflect"
|
2022-08-28 10:57:06 -07:00
|
|
|
|
2022-08-30 12:30:17 -07:00
|
|
|
"cosmos-test/x/cosmostest/memdb"
|
2022-08-28 10:57:06 -07:00
|
|
|
"cosmos-test/x/cosmostest/types"
|
2022-08-28 13:28:25 -07:00
|
|
|
|
2022-08-28 10:57:06 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.MsgNewBidResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
2022-08-31 23:57:44 -07:00
|
|
|
auction, found := k.Keeper.GetAuction(ctx, msg.AuctionIndex)
|
2022-08-28 13:28:25 -07:00
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("didn't find auction of index %s", msg.AuctionIndex)
|
|
|
|
}
|
|
|
|
|
2022-08-31 15:40:28 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-28 13:28:25 -07:00
|
|
|
ok := false
|
|
|
|
amt := new(big.Int)
|
|
|
|
amt, ok = amt.SetString(msg.Amount, 10)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed to convert `%s` to a large integer", msg.Amount)
|
|
|
|
}
|
|
|
|
if amt.Sign() != 1 {
|
|
|
|
return nil, fmt.Errorf("bid amount must be greater than 0")
|
|
|
|
}
|
|
|
|
|
2022-08-31 23:57:44 -07:00
|
|
|
ceiling := new(big.Int)
|
|
|
|
ceiling.SetString(auction.Ceiling, 10)
|
|
|
|
if amt.Cmp(ceiling) == 1 {
|
|
|
|
return nil, fmt.Errorf("bid amount cannot be greater than auction price ceiling (%s)", auction.Ceiling)
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:48:54 -07:00
|
|
|
lowestBid, err := memdb.BidDB.GetLowestBid(msg.AuctionIndex)
|
2022-08-30 16:29:04 -07:00
|
|
|
// we manually handle KeyNotFound in GetHighestBid, so should return (nil, nil) if not found
|
2022-08-30 12:30:17 -07:00
|
|
|
if err != nil {
|
2022-08-31 21:48:54 -07:00
|
|
|
return nil, fmt.Errorf("failed to get lowest bid: %s", reflect.TypeOf(err))
|
2022-08-28 13:28:25 -07:00
|
|
|
}
|
|
|
|
|
2022-08-31 21:48:54 -07:00
|
|
|
if lowestBid != nil {
|
2022-08-30 16:29:04 -07:00
|
|
|
amtPrev := new(big.Int)
|
2022-08-31 21:48:54 -07:00
|
|
|
amtPrev, ok = amtPrev.SetString(lowestBid.Amount, 10)
|
2022-08-30 16:29:04 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-31 19:02:23 -07:00
|
|
|
if amt.Cmp(amtPrev) != -1 {
|
|
|
|
return nil, fmt.Errorf("bid amount must be less than current lowest bid (%s)", amtPrev)
|
2022-08-30 16:29:04 -07:00
|
|
|
}
|
2022-08-28 13:28:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bid := &types.Bid{
|
|
|
|
Amount: msg.Amount,
|
|
|
|
Owner: msg.Creator,
|
|
|
|
}
|
|
|
|
|
2022-08-31 21:48:54 -07:00
|
|
|
if err := memdb.BidDB.AddBid(msg.AuctionIndex, bid); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to add bid: %s", err)
|
|
|
|
}
|
2022-08-30 12:30:17 -07:00
|
|
|
|
|
|
|
// auction.Bids = append(auction.Bids, bid)
|
|
|
|
// k.Keeper.SetAuction(ctx, auction)
|
2022-08-28 10:57:06 -07:00
|
|
|
|
|
|
|
return &types.MsgNewBidResponse{}, nil
|
|
|
|
}
|