-
Notifications
You must be signed in to change notification settings - Fork 37
/
query_server.go
156 lines (118 loc) · 4.77 KB
/
query_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/peggyjv/sommelier/v7/x/auction/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var _ types.QueryServer = Keeper{}
// QueryParams implements QueryServer
func (k Keeper) QueryParams(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
return &types.QueryParamsResponse{
Params: k.GetParamSet(sdk.UnwrapSDKContext(c)),
}, nil
}
// QueryActiveAuction implements QueryServer
func (k Keeper) QueryActiveAuction(c context.Context, request *types.QueryActiveAuctionRequest) (*types.QueryActiveAuctionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Check active auctions
activeAuction, found := k.GetActiveAuctionByID(ctx, request.GetAuctionId())
if found {
return &types.QueryActiveAuctionResponse{Auction: &activeAuction}, nil
}
return &types.QueryActiveAuctionResponse{}, status.Errorf(codes.NotFound, "No active auction found for id: %d", request.GetAuctionId())
}
// QueryEndedAuction implements QueryServer
func (k Keeper) QueryEndedAuction(c context.Context, request *types.QueryEndedAuctionRequest) (*types.QueryEndedAuctionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
// Check ended auctions
endedAuction, found := k.GetEndedAuctionByID(ctx, request.GetAuctionId())
if found {
return &types.QueryEndedAuctionResponse{Auction: &endedAuction}, nil
}
return &types.QueryEndedAuctionResponse{}, status.Errorf(codes.NotFound, "No ended auction found for id: %d", request.GetAuctionId())
}
// QueryActiveAuctions implements QueryServer
func (k Keeper) QueryActiveAuctions(c context.Context, _ *types.QueryActiveAuctionsRequest) (*types.QueryActiveAuctionsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
auctions := k.GetActiveAuctions(ctx)
return &types.QueryActiveAuctionsResponse{Auctions: auctions}, nil
}
// QueryEndedAuctions implements QueryServer
func (k Keeper) QueryEndedAuctions(c context.Context, request *types.QueryEndedAuctionsRequest) (*types.QueryEndedAuctionsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var auctions []*types.Auction
store := k.getEndedAuctionsPrefixStore(ctx)
var err error
pageRes, err := query.FilteredPaginate(
store,
&request.Pagination,
func(key []byte, value []byte, accumulate bool) (bool, error) {
var auction types.Auction
err := auction.Unmarshal(value)
if err != nil {
return false, err
}
if accumulate {
auctions = append(auctions, &auction)
}
return true, nil
},
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryEndedAuctionsResponse{Auctions: auctions, Pagination: *pageRes}, nil
}
// QueryBid implements QueryServer
func (k Keeper) QueryBid(c context.Context, request *types.QueryBidRequest) (*types.QueryBidResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
bid, found := k.GetBid(ctx, request.GetAuctionId(), request.GetBidId())
if !found {
return &types.QueryBidResponse{}, status.Errorf(codes.NotFound, "No bid found for specified bid id: %d, and auction id: %d", request.GetBidId(), request.GetAuctionId())
}
return &types.QueryBidResponse{Bid: &bid}, nil
}
// QueryBidsByAuction implements QueryServer
func (k Keeper) QueryBidsByAuction(c context.Context, request *types.QueryBidsByAuctionRequest) (*types.QueryBidsByAuctionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var bids []*types.Bid
store := k.getBidsByAuctionPrefixStore(ctx, request.GetAuctionId())
var err error
pageRes, err := query.FilteredPaginate(
store,
&request.Pagination,
func(key []byte, value []byte, accumulate bool) (bool, error) {
var bid types.Bid
err := bid.Unmarshal(value)
if err != nil {
return false, err
}
if accumulate {
bids = append(bids, &bid)
}
return true, nil
},
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryBidsByAuctionResponse{Bids: bids, Pagination: *pageRes}, nil
}
// QueryTokenPrice implements QueryServer
func (k Keeper) QueryTokenPrice(c context.Context, request *types.QueryTokenPriceRequest) (*types.QueryTokenPriceResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
tokenPrice, found := k.GetTokenPrice(ctx, request.GetDenom())
if found {
return &types.QueryTokenPriceResponse{TokenPrice: &tokenPrice}, nil
}
return &types.QueryTokenPriceResponse{}, status.Errorf(codes.NotFound, "No token price found for denom: %s", request.GetDenom())
}
// QueryTokenPrices implements QueryServer
func (k Keeper) QueryTokenPrices(c context.Context, _ *types.QueryTokenPricesRequest) (*types.QueryTokenPricesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
tokenPrices := k.GetTokenPrices(ctx)
return &types.QueryTokenPricesResponse{TokenPrices: tokenPrices}, nil
}