From d13f2956ac4022f6c0c0ef17d833cf749b4395eb Mon Sep 17 00:00:00 2001 From: turtlebasket Date: Sun, 28 Aug 2022 20:28:25 +0000 Subject: [PATCH] basic auction bidding system --- x/cosmostest/keeper/msg_server_new_bid.go | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/x/cosmostest/keeper/msg_server_new_bid.go b/x/cosmostest/keeper/msg_server_new_bid.go index 80b4311..c5300df 100644 --- a/x/cosmostest/keeper/msg_server_new_bid.go +++ b/x/cosmostest/keeper/msg_server_new_bid.go @@ -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 }