Skip to content

Commit

Permalink
chore: adding query for all auctions with rewards (#2564)
Browse files Browse the repository at this point in the history
* adding query for all auctions with rewards

* fix: fix the results on query
  • Loading branch information
gsk967 authored Jul 1, 2024
1 parent d4d2adc commit e8cb7b0
Show file tree
Hide file tree
Showing 5 changed files with 691 additions and 44 deletions.
18 changes: 18 additions & 0 deletions proto/umee/auction/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "umee/auction/v1/auction.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";

option go_package = "github.com/umee-network/umee/v6/x/auction";

Expand All @@ -21,6 +22,10 @@ service Query {
// RewardAuction queries the information of the auction by ID. If ID is ommitted, returns
// current reward auction.
rpc RewardsAuction(QueryRewardsAuction) returns (QueryRewardsAuctionResponse) {
option (google.api.http).get = "/umee/auction/v1/rewards/{id}";
}
// RewardsAuctions returns all reward auctions.
rpc RewardsAuctions(QueryRewardsAuctions) returns (QueryRewardsAuctionsResponse) {
option (google.api.http).get = "/umee/auction/v1/rewards";
}
}
Expand Down Expand Up @@ -48,3 +53,16 @@ message QueryRewardsAuctionResponse {
repeated cosmos.base.v1beta1.Coin rewards = 4 [(gogoproto.nullable) = false];
google.protobuf.Timestamp ends_at = 5 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}

// Query type for QueryRewardsAuctions
message QueryRewardsAuctions {
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

// QueryRewardsAuctionsResponse is response type for Query/QueryRewardsAuctions
message QueryRewardsAuctionsResponse {
repeated QueryRewardsAuctionResponse auctions = 1;
// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
29 changes: 29 additions & 0 deletions x/auction/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func GetQueryCmd() *cobra.Command {
cmd.AddCommand(
RewardsParams(),
RewardsAuction(),
RewardsAuctions(),
)

return cmd
Expand Down Expand Up @@ -81,6 +82,34 @@ func RewardsAuction() *cobra.Command {
return cmd
}

func RewardsAuctions() *cobra.Command {
cmd := &cobra.Command{
Use: "rewards-auctions",
Args: cobra.NoArgs,
Short: "Query all rewards auctions",
RunE: func(cmd *cobra.Command, args []string) error {
cctx, q, err := prepareQueryCtx(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

req := auction.QueryRewardsAuctions{}
req.Pagination = pageReq
resp, err := q.RewardsAuctions(cmd.Context(), &req)
return cli.PrintOrErr(resp, err, cctx)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "rewards-auctions")
return cmd
}

func prepareQueryCtx(cmd *cobra.Command) (client.Context, auction.QueryClient, error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions x/auction/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/umee-network/umee/v6/util/coin"
"github.com/umee-network/umee/v6/util/store"
"github.com/umee-network/umee/v6/x/auction"
)

Expand Down Expand Up @@ -53,3 +56,50 @@ func (q Querier) RewardsAuction(goCtx context.Context, msg *auction.QueryRewards

return r, nil
}

// RewardsAuctions returns all the auctions with bids and rewards
func (q Querier) RewardsAuctions(goCtx context.Context, req *auction.QueryRewardsAuctions) (
*auction.QueryRewardsAuctionsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
k := q.Keeper(&ctx)
rewardsStore := prefix.NewStore(k.store, keyPrefixRewardsCoins)
auctions := make([]*auction.QueryRewardsAuctionResponse, 0)

pageRes, err := query.Paginate(rewardsStore, req.Pagination, func(key, value []byte) error {
var rewards auction.Rewards
err := rewards.Unmarshal(value)
if err != nil {
return err
}
var auctionID store.Uint32
err = auctionID.Unmarshal(key)
if err != nil {
return err
}
auction := &auction.QueryRewardsAuctionResponse{
Id: uint32(auctionID),
}
auction.Rewards = rewards.Rewards
auction.EndsAt = rewards.EndsAt
auctions = append(auctions, auction)
return nil
})

if err != nil {
return nil, err
}

for index, auction := range auctions {
// get the bids info
bid := k.getRewardsBid(auction.Id)
if bid != nil {
auctions[index].Bidder = bid.Bidder
auctions[index].Bid = coin.UmeeInt(bid.Amount)
}
}

return &auction.QueryRewardsAuctionsResponse{
Auctions: auctions,
Pagination: pageRes,
}, nil
}
Loading

0 comments on commit e8cb7b0

Please sign in to comment.