gpu-compute-chain/x/cosmostest/keeper/msg_server_new_bid.go

53 lines
1.2 KiB
Go
Raw Normal View History

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-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-28 13:28:25 -07:00
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)
2022-08-28 10:57:06 -07:00
return &types.MsgNewBidResponse{}, nil
}