From 23e6e2f16af0487e7b34be16baa9aed4febac1a5 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 13 Feb 2024 18:32:17 +0530 Subject: [PATCH] Add commands to get bids for an auction --- utils/json.go | 4 ---- x/auction/keeper/keeper.go | 29 +++++++++++++++++------------ x/auction/keeper/query_server.go | 32 +++++++++++++++++++++++++++++--- x/auction/module/autocli.go | 17 +++++++++++++++++ 4 files changed, 63 insertions(+), 19 deletions(-) diff --git a/utils/json.go b/utils/json.go index b10c501a..7ae9d280 100644 --- a/utils/json.go +++ b/utils/json.go @@ -1,7 +1,3 @@ -// -// Copyright 2020 Wireline, Inc. -// - package utils import ( diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 635cbb89..5ac0c61d 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -162,25 +162,30 @@ func (k Keeper) GetBid(ctx sdk.Context, id string, bidder string) (auctiontypes. return bid, nil } +// GetBids gets the auction bids. +func (k Keeper) GetBids(ctx sdk.Context, id string) ([]*auctiontypes.Bid, error) { + var bids []*auctiontypes.Bid + + // TODO: Optimize using return by value? + err := k.Bids.Walk(ctx, collections.NewPrefixedPairRange[string, string](id), func(key collections.Pair[string, string], value auctiontypes.Bid) (stop bool, err error) { + bids = append(bids, &value) + return false, nil + }) + if err != nil { + return nil, err + } + + return bids, nil +} + // ListAuctions - get all auctions. func (k Keeper) ListAuctions(ctx sdk.Context) ([]auctiontypes.Auction, error) { - var auctions []auctiontypes.Auction - iter, err := k.Auctions.Iterate(ctx, nil) if err != nil { return nil, err } - for ; iter.Valid(); iter.Next() { - auction, err := iter.Value() - if err != nil { - return nil, err - } - - auctions = append(auctions, auction) - } - - return auctions, nil + return iter.Values() } // GetAuction - gets a record from the store. diff --git a/x/auction/keeper/query_server.go b/x/auction/keeper/query_server.go index 5233a684..8ea8f89e 100644 --- a/x/auction/keeper/query_server.go +++ b/x/auction/keeper/query_server.go @@ -63,14 +63,40 @@ func (qs queryServer) GetAuction(c context.Context, req *auctiontypes.QueryAucti return &auctiontypes.QueryAuctionResponse{Auction: &auction}, nil } -// GetBid queries and auction bid +// GetBid queries an auction bid by auction-id and bidder func (qs queryServer) GetBid(c context.Context, req *auctiontypes.QueryBidRequest) (*auctiontypes.QueryBidResponse, error) { - panic("unimplemented") + ctx := sdk.UnwrapSDKContext(c) + + if req.AuctionId == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "auction id is required") + } + + if req.Bidder == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "bidder address is required") + } + + bid, err := qs.k.GetBid(ctx, req.AuctionId, req.Bidder) + if err != nil { + return nil, err + } + + return &auctiontypes.QueryBidResponse{Bid: &bid}, nil } // GetBids queries all auction bids func (qs queryServer) GetBids(c context.Context, req *auctiontypes.QueryBidsRequest) (*auctiontypes.QueryBidsResponse, error) { - panic("unimplemented") + ctx := sdk.UnwrapSDKContext(c) + + if req.AuctionId == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "auction id is required") + } + + resp, err := qs.k.GetBids(ctx, req.AuctionId) + if err != nil { + return nil, err + } + + return &auctiontypes.QueryBidsResponse{Bids: resp}, nil } // AuctionsByBidder queries auctions by bidder diff --git a/x/auction/module/autocli.go b/x/auction/module/autocli.go index 6f2b59b3..ffaed1af 100644 --- a/x/auction/module/autocli.go +++ b/x/auction/module/autocli.go @@ -43,6 +43,23 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { {ProtoField: "owner_address"}, }, }, + { + RpcMethod: "GetBid", + Use: "get-bid [auction-id] [bidder]", + Short: "Get auction bid by auction id and bidder", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "auction_id"}, + {ProtoField: "bidder"}, + }, + }, + { + RpcMethod: "GetBids", + Use: "get-bids [auction-id]", + Short: "Get all auction bids", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "auction_id"}, + }, + }, }, }, Tx: &autocliv1.ServiceCommandDescriptor{