basic auction bidding system
parent
3eb5705b92
commit
d13f2956ac
|
@ -2,16 +2,51 @@ package keeper
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"cosmos-test/x/cosmostest/types"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
func (k msgServer) NewBid(goCtx context.Context, msg *types.MsgNewBid) (*types.MsgNewBidResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
// TODO: Handling the message
|
||||
_ = ctx
|
||||
auction, found := k.Keeper.GetAuction(ctx, msg.AuctionIndex)
|
||||
if !found {
|
||||
return nil, fmt.Errorf("didn't find auction of index %s", msg.AuctionIndex)
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
amtPrev := big.NewInt(0)
|
||||
if len(auction.Bids) > 0 {
|
||||
// shouldn't need to check validity, because we assume it's checked
|
||||
// before approving & inserting the bid
|
||||
latestBid := auction.Bids[len(auction.Bids)-1]
|
||||
amtPrev, _ = amtPrev.SetString(latestBid.Amount, 10)
|
||||
}
|
||||
|
||||
if amt.Cmp(amtPrev) < 1 {
|
||||
return nil, fmt.Errorf("bid amount must be greater than largest bid")
|
||||
}
|
||||
|
||||
bid := &types.Bid{
|
||||
Amount: msg.Amount,
|
||||
Owner: msg.Creator,
|
||||
}
|
||||
auction.Bids = append(auction.Bids, bid)
|
||||
|
||||
k.Keeper.SetAuction(ctx, auction)
|
||||
|
||||
return &types.MsgNewBidResponse{}, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue