Add commands to get auctions by id and owner

This commit is contained in:
Prathamesh Musale 2024-02-12 11:09:29 +05:30
parent d445811e80
commit 218d8a7df1
3 changed files with 69 additions and 4 deletions

View File

@ -1,6 +1,8 @@
package keeper package keeper
import ( import (
"errors"
"cosmossdk.io/collections" "cosmossdk.io/collections"
"cosmossdk.io/collections/indexes" "cosmossdk.io/collections/indexes"
storetypes "cosmossdk.io/core/store" storetypes "cosmossdk.io/core/store"
@ -117,6 +119,28 @@ func (k Keeper) ListAuctions(ctx sdk.Context) ([]auctiontypes.Auction, error) {
return auctions, nil 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. // CreateAuction creates a new auction.
func (k Keeper) CreateAuction(ctx sdk.Context, msg auctiontypes.MsgCreateAuction) (*auctiontypes.Auction, error) { func (k Keeper) CreateAuction(ctx sdk.Context, msg auctiontypes.MsgCreateAuction) (*auctiontypes.Auction, error) {
// TODO: Setup checks // TODO: Setup checks

View File

@ -3,8 +3,11 @@ package keeper
import ( import (
"context" "context"
auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction" errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types" 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 // 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 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) { 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 // GetBid queries and auction bid
@ -66,7 +80,18 @@ func (qs queryServer) AuctionsByBidder(c context.Context, req *auctiontypes.Quer
// AuctionsByOwner queries auctions by owner // AuctionsByOwner queries auctions by owner
func (qs queryServer) AuctionsByOwner(c context.Context, req *auctiontypes.QueryAuctionsByOwnerRequest) (*auctiontypes.QueryAuctionsByOwnerResponse, error) { 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 // GetAuctionModuleBalance queries the auction module account balance

View File

@ -27,6 +27,22 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Short: "List auctions", Short: "List auctions",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{}, 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{ Tx: &autocliv1.ServiceCommandDescriptor{