/
msg_server_initiate_auction.go
57 lines (45 loc) · 1.71 KB
/
msg_server_initiate_auction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/uwezukwechibuzor/carDex/x/auction/types"
)
// InitiateAuction is used by the seller to initiate an auction with the some initial parameters on CarDex Chain
func (k msgServer) InitiateAuction(goCtx context.Context, msg *types.MsgInitiateAuction) (*types.MsgInitiateAuctionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Validate the message creator
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Creator)
}
// check that auctionID does not exist yet
auction, found := k.GetAuction(ctx, msg.AuctionID)
if found {
return nil, sdkerrors.Wrapf(types.ErrAuctionExists, "Auction: %s", &auction)
}
// check that this is not empty
if msg.MinimumBid == "" {
return nil, sdkerrors.Wrapf(err, "MinimumBid is empty: %s", msg.MinimumBid)
}
newAuction := types.Auction{
AuctionID: msg.AuctionID,
MinimumBid: msg.MinimumBid,
Bid: msg.Bid,
AuctionDuration: msg.AuctionDuration,
CarDescription: msg.CarDescription,
CarPictureUrl: msg.CarPictureUrl,
Status: msg.Status,
CreatedAt: msg.CreatedAt,
Creator: msg.Creator,
}
// set status to OPEN at the point of initiating the auction
newAuction.Status = "open"
// bidding should not be possible if it exceeds more than 100 blocks from the point the auction was initiated
newAuction.CreatedAt = ctx.BlockHeight()
// set to store
k.SetAuction(ctx, msg.AuctionID, newAuction)
// emit event
err = ctx.EventManager().EmitTypedEvent(msg)
return &types.MsgInitiateAuctionResponse{}, err
}