-
Notifications
You must be signed in to change notification settings - Fork 170
/
grpc_query.go
179 lines (150 loc) · 4.63 KB
/
grpc_query.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package keeper
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/CosmosContracts/juno/v19/x/feeshare/types"
)
var _ types.QueryServer = Querier{}
// Querier defines a wrapper around the x/FeeShare keeper providing gRPC method
// handlers.
type Querier struct {
Keeper
}
func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}
// FeeShares returns all FeeShares that have been registered for fee distribution
func (q Querier) FeeShares(
c context.Context,
req *types.QueryFeeSharesRequest,
) (*types.QueryFeeSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var feeshares []types.FeeShare
store := prefix.NewStore(ctx.KVStore(q.storeKey), types.KeyPrefixFeeShare)
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
var feeshare types.FeeShare
if err := q.cdc.Unmarshal(value, &feeshare); err != nil {
return err
}
feeshares = append(feeshares, feeshare)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryFeeSharesResponse{
Feeshare: feeshares,
Pagination: pageRes,
}, nil
}
// FeeShare returns the FeeShare that has been registered for fee distribution for a given
// contract
func (q Querier) FeeShare(
c context.Context,
req *types.QueryFeeShareRequest,
) (*types.QueryFeeShareResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
// check if the contract is a non-zero hex address
contract, err := sdk.AccAddressFromBech32(req.ContractAddress)
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"invalid format for contract %s, should be bech32 ('juno...')", req.ContractAddress,
)
}
feeshare, found := q.GetFeeShare(ctx, contract)
if !found {
return nil, status.Errorf(
codes.NotFound,
"fees registered contract '%s'",
req.ContractAddress,
)
}
return &types.QueryFeeShareResponse{Feeshare: feeshare}, nil
}
// Params returns the fees module params
func (q Querier) Params(
c context.Context,
_ *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := q.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// DeployerFeeShares returns all contracts that have been registered for fee
// distribution by a given deployer
func (q Querier) DeployerFeeShares( // nolint: dupl
c context.Context,
req *types.QueryDeployerFeeSharesRequest,
) (*types.QueryDeployerFeeSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
deployer, err := sdk.AccAddressFromBech32(req.DeployerAddress)
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"invalid format for deployer %s, should be bech32 ('juno...')", req.DeployerAddress,
)
}
var contracts []string
store := prefix.NewStore(
ctx.KVStore(q.storeKey),
types.GetKeyPrefixDeployer(deployer),
)
pageRes, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
contracts = append(contracts, sdk.AccAddress(key).String())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryDeployerFeeSharesResponse{
ContractAddresses: contracts,
Pagination: pageRes,
}, nil
}
// WithdrawerFeeShares returns all fees for a given withdraw address
func (q Querier) WithdrawerFeeShares( // nolint: dupl
c context.Context,
req *types.QueryWithdrawerFeeSharesRequest,
) (*types.QueryWithdrawerFeeSharesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
deployer, err := sdk.AccAddressFromBech32(req.WithdrawerAddress)
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"invalid format for withdraw addr %s, should be bech32 ('juno...')", req.WithdrawerAddress,
)
}
var contracts []string
store := prefix.NewStore(
ctx.KVStore(q.storeKey),
types.GetKeyPrefixWithdrawer(deployer),
)
pageRes, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
contracts = append(contracts, sdk.AccAddress(key).String())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryWithdrawerFeeSharesResponse{
ContractAddresses: contracts,
Pagination: pageRes,
}, nil
}