From 218d8a7df1fcc41fdd77ad71b5263438c6308d45 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Mon, 12 Feb 2024 11:09:29 +0530 Subject: [PATCH] Add commands to get auctions by id and owner --- x/auction/keeper/keeper.go | 24 +++++++++++++++++++++++ x/auction/keeper/query_server.go | 33 ++++++++++++++++++++++++++++---- x/auction/module/autocli.go | 16 ++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index dd9cd183..8bb95a21 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -1,6 +1,8 @@ package keeper import ( + "errors" + "cosmossdk.io/collections" "cosmossdk.io/collections/indexes" storetypes "cosmossdk.io/core/store" @@ -117,6 +119,28 @@ func (k Keeper) ListAuctions(ctx sdk.Context) ([]auctiontypes.Auction, error) { return auctions, nil } +// GetAuction - gets a record from the store. +func (k Keeper) GetAuctionById(ctx sdk.Context, id string) (auctiontypes.Auction, error) { + auction, err := k.Auctions.Get(ctx, id) + if err != nil { + if errors.Is(err, collections.ErrNotFound) { + return auctiontypes.Auction{}, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Auction not found.") + } + return auctiontypes.Auction{}, err + } + + return auction, nil +} + +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 indexes.CollectValues(ctx, k.Auctions, iter) +} + // 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 65a8ac55..5233a684 100644 --- a/x/auction/keeper/query_server.go +++ b/x/auction/keeper/query_server.go @@ -3,8 +3,11 @@ package keeper import ( "context" - auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + + auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction" ) // TODO: Add required read methods @@ -44,9 +47,20 @@ func (qs queryServer) Auctions(c context.Context, req *auctiontypes.QueryAuction return &auctiontypes.QueryAuctionsResponse{Auctions: &auctiontypes.Auctions{Auctions: resp}}, nil } -// GetAuction queries an auction +// GetAuction queries an auction by id func (qs queryServer) GetAuction(c context.Context, req *auctiontypes.QueryAuctionRequest) (*auctiontypes.QueryAuctionResponse, error) { - panic("unimplemented") + ctx := sdk.UnwrapSDKContext(c) + + if req.Id == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "auction id is required") + } + + auction, err := qs.k.GetAuctionById(ctx, req.Id) + if err != nil { + return nil, err + } + + return &auctiontypes.QueryAuctionResponse{Auction: &auction}, nil } // GetBid queries and auction bid @@ -66,7 +80,18 @@ func (qs queryServer) AuctionsByBidder(c context.Context, req *auctiontypes.Quer // AuctionsByOwner queries auctions by owner func (qs queryServer) AuctionsByOwner(c context.Context, req *auctiontypes.QueryAuctionsByOwnerRequest) (*auctiontypes.QueryAuctionsByOwnerResponse, error) { - panic("unimplemented") + ctx := sdk.UnwrapSDKContext(c) + + if req.OwnerAddress == "" { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "owner address is required") + } + + auctions, err := qs.k.GetAuctionsByOwner(ctx, req.OwnerAddress) + if err != nil { + return nil, err + } + + return &auctiontypes.QueryAuctionsByOwnerResponse{Auctions: &auctiontypes.Auctions{Auctions: auctions}}, nil } // GetAuctionModuleBalance queries the auction module account balance diff --git a/x/auction/module/autocli.go b/x/auction/module/autocli.go index e81d26ee..0fbc9f9e 100644 --- a/x/auction/module/autocli.go +++ b/x/auction/module/autocli.go @@ -27,6 +27,22 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions { Short: "List auctions", PositionalArgs: []*autocliv1.PositionalArgDescriptor{}, }, + { + RpcMethod: "GetAuction", + Use: "get [auction-id]", + Short: "Get auction info by auction id", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "id"}, + }, + }, + { + RpcMethod: "AuctionsByOwner", + Use: "by-owner [owner-address]", + Short: "Get auctions list by owner / creator address", + PositionalArgs: []*autocliv1.PositionalArgDescriptor{ + {ProtoField: "owner_address"}, + }, + }, }, }, Tx: &autocliv1.ServiceCommandDescriptor{