From 513ad9975bd6a8949d941a5ace422e70766585a1 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 13 Feb 2024 19:13:00 +0530 Subject: [PATCH] Add a command to get auctions by bidder address --- x/auction/keeper/keeper.go | 28 +++++++++++++++++++++++++++- x/auction/keeper/query_server.go | 13 ++++++++++++- x/auction/module/autocli.go | 8 ++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index 5ac0c61d..f11e3259 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -204,12 +204,38 @@ func (k Keeper) GetAuctionById(ctx sdk.Context, id string) (auctiontypes.Auction func (k Keeper) GetAuctionsByOwner(ctx sdk.Context, owner string) ([]auctiontypes.Auction, error) { iter, err := k.Auctions.Indexes.Owner.MatchExact(ctx, owner) if err != nil { - return []auctiontypes.Auction{}, err + return nil, err } return indexes.CollectValues(ctx, k.Auctions, iter) } +// QueryAuctionsByBidder - query auctions by bidder +func (k Keeper) QueryAuctionsByBidder(ctx sdk.Context, bidderAddress string) ([]auctiontypes.Auction, error) { + auctions := []auctiontypes.Auction{} + + iter, err := k.Bids.Indexes.Bidder.MatchExact(ctx, bidderAddress) + if err != nil { + return nil, err + } + + for ; iter.Valid(); iter.Next() { + keyPair, err := iter.PrimaryKey() + if err != nil { + return nil, err + } + + auction, err := k.GetAuctionById(ctx, keyPair.K1()) + if err != nil { + return nil, err + } + + auctions = append(auctions, auction) + } + + return auctions, nil +} + // CreateAuction creates a new auction. func (k Keeper) CreateAuction(ctx sdk.Context, msg auctiontypes.MsgCreateAuction) (*auctiontypes.Auction, error) { // TODO: Setup checks diff --git a/x/auction/keeper/query_server.go b/x/auction/keeper/query_server.go index 8ea8f89e..77cb32a6 100644 --- a/x/auction/keeper/query_server.go +++ b/x/auction/keeper/query_server.go @@ -101,7 +101,18 @@ func (qs queryServer) GetBids(c context.Context, req *auctiontypes.QueryBidsRequ // AuctionsByBidder queries auctions by bidder func (qs queryServer) AuctionsByBidder(c context.Context, req *auctiontypes.QueryAuctionsByBidderRequest) (*auctiontypes.QueryAuctionsByBidderResponse, error) { - panic("unimplemented") + ctx := sdk.UnwrapSDKContext(c) + + if req.BidderAddress == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "bidder address is required") + } + + auctions, err := qs.k.QueryAuctionsByBidder(ctx, req.BidderAddress) + if err != nil { + return nil, err + } + + return &auctiontypes.QueryAuctionsByBidderResponse{Auctions: &auctiontypes.Auctions{Auctions: auctions}}, nil } // AuctionsByOwner queries auctions by owner diff --git a/x/auction/module/autocli.go b/x/auction/module/autocli.go index ffaed1af..2861e133 100644 --- a/x/auction/module/autocli.go +++ b/x/auction/module/autocli.go @@ -60,6 +60,14 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { {ProtoField: "auction_id"}, }, }, + { + RpcMethod: "AuctionsByBidder", + Use: "by-bidder [bidder]", + Short: "Get auctions list by bidder", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "bidder_address"}, + }, + }, }, }, Tx: &autocliv1.ServiceCommandDescriptor{