-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
329 lines (292 loc) · 9.77 KB
/
querier.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package keeper
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/codec"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/store/prefix"
sdk "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types"
sdkerrors "github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types/errors"
"github.com/furyaxyz/fuxchain/libs/cosmos-sdk/types/query"
abci "github.com/furyaxyz/fuxchain/libs/tendermint/abci/types"
"github.com/furyaxyz/fuxchain/x/feesplit/types"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
if len(path) < 1 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 1 parameter is required")
}
switch path[0] {
case types.QueryParameters:
return queryParams(ctx, keeper)
case types.QueryFeeSplits:
return queryFeeSplits(ctx, req, keeper)
case types.QueryFeeSplit:
return queryFeeSplit(ctx, req, keeper)
case types.QueryDeployerFeeSplits:
return queryDeployerFeeSplits(ctx, req, keeper)
case types.QueryDeployerFeeSplitsDetail:
return queryDeployerFeeSplitsDetail(ctx, req, keeper)
case types.QueryWithdrawerFeeSplits:
return queryWithdrawerFeeSplits(ctx, req, keeper)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query endpoint")
}
}
}
// queryFeeSplits returns all FeeSplits that have been registered for fee distribution
func queryFeeSplits(
ctx sdk.Context,
req abci.RequestQuery,
k Keeper,
) ([]byte, sdk.Error) {
var params types.QueryWithdrawerFeeSplitsRequest
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
var feeSplits []types.FeeSplitWithShare
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeSplit)
pageRes, err := query.Paginate(store, params.Pagination, func(_, value []byte) error {
var fee types.FeeSplit
if err := k.cdc.UnmarshalBinaryBare(value, &fee); err != nil {
return err
}
share, found := k.GetContractShare(ctx, fee.ContractAddress)
if !found {
share = k.GetParams(ctx).DeveloperShares
}
feeSplits = append(feeSplits, types.FeeSplitWithShare{
ContractAddress: fee.ContractAddress.String(),
DeployerAddress: fee.DeployerAddress.String(),
WithdrawerAddress: fee.WithdrawerAddress.String(),
Share: share,
})
return nil
})
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInternal, err.Error())
}
resp := &types.QueryFeeSplitsResponse{
FeeSplits: feeSplits,
Pagination: pageRes,
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, resp)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryFeeSplit returns the FeeSplit that has been registered for fee distribution for a given
// contract
func queryFeeSplit(
ctx sdk.Context,
req abci.RequestQuery,
k Keeper,
) ([]byte, sdk.Error) {
var params types.QueryFeeSplitRequest
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
if strings.TrimSpace(params.ContractAddress) == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "contract address is empty")
}
// check if the contract is a non-zero hex address
if err := types.ValidateNonZeroAddress(params.ContractAddress); err != nil {
return nil, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("invalid format for contract %s, should be non-zero hex ('0x...')", params.ContractAddress),
)
}
feeSplit, found := k.GetFeeSplit(ctx, common.HexToAddress(params.ContractAddress))
if !found {
return nil, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("not found fees registered contract '%s'", params.ContractAddress),
)
}
share, found := k.GetContractShare(ctx, feeSplit.ContractAddress)
if !found {
share = k.GetParams(ctx).DeveloperShares
}
resp := &types.QueryFeeSplitResponse{FeeSplit: types.FeeSplitWithShare{
ContractAddress: feeSplit.ContractAddress.String(),
DeployerAddress: feeSplit.DeployerAddress.String(),
WithdrawerAddress: feeSplit.WithdrawerAddress.String(),
Share: share,
}}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, resp)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryParams returns the fees module params
func queryParams(
ctx sdk.Context,
k Keeper,
) ([]byte, sdk.Error) {
params := k.GetParams(ctx)
res, err := codec.MarshalJSONIndent(k.cdc, types.QueryParamsResponse{Params: params})
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryDeployerFeeSplits returns all contracts that have been registered for fee
// distribution by a given deployer
func queryDeployerFeeSplits(
ctx sdk.Context,
req abci.RequestQuery,
k Keeper,
) ([]byte, sdk.Error) {
var params types.QueryDeployerFeeSplitsRequest
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
if strings.TrimSpace(params.DeployerAddress) == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deployer address is empty")
}
deployer, err := sdk.AccAddressFromBech32(params.DeployerAddress)
if err != nil {
return nil, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("invalid format for deployer %s, should be bech32", params.DeployerAddress),
)
}
var contracts []string
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.GetKeyPrefixDeployer(deployer),
)
pageRes, err := query.Paginate(store, params.Pagination, func(key, _ []byte) error {
contracts = append(contracts, common.BytesToAddress(key).Hex())
return nil
})
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInternal, err.Error())
}
resp := &types.QueryDeployerFeeSplitsResponse{
ContractAddresses: contracts,
Pagination: pageRes,
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, resp)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryDeployerFeeSplitsDetail returns all contracts with feesplit info that have been registered for fee
// distribution by a given deployer
func queryDeployerFeeSplitsDetail(
ctx sdk.Context,
req abci.RequestQuery,
k Keeper,
) ([]byte, sdk.Error) {
var params types.QueryDeployerFeeSplitsRequest
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
if strings.TrimSpace(params.DeployerAddress) == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "deployer address is empty")
}
deployer, err := sdk.AccAddressFromBech32(params.DeployerAddress)
if err != nil {
return nil, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("invalid format for deployer %s, should be bech32", params.DeployerAddress),
)
}
var contracts []common.Address
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.GetKeyPrefixDeployer(deployer),
)
pageRes, err := query.Paginate(store, params.Pagination, func(key, _ []byte) error {
contracts = append(contracts, common.BytesToAddress(key))
return nil
})
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInternal, err.Error())
}
var feeSplits []types.FeeSplitWithShare
for _, contract := range contracts {
feeSplit, found := k.GetFeeSplit(ctx, contract)
if !found {
continue
}
share, found := k.GetContractShare(ctx, feeSplit.ContractAddress)
if !found {
share = k.GetParams(ctx).DeveloperShares
}
feeSplits = append(feeSplits, types.FeeSplitWithShare{
ContractAddress: feeSplit.ContractAddress.String(),
DeployerAddress: feeSplit.DeployerAddress.String(),
WithdrawerAddress: feeSplit.WithdrawerAddress.String(),
Share: share,
})
}
if len(feeSplits) == 0 {
return nil, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("not found fees registered contract for deployer '%s'", params.DeployerAddress),
)
}
resp := &types.QueryDeployerFeeSplitsResponseV2{
FeeSplits: feeSplits,
Pagination: pageRes,
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, resp)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// queryWithdrawerFeeSplits returns all fees for a given withdraw address
func queryWithdrawerFeeSplits(
ctx sdk.Context,
req abci.RequestQuery,
k Keeper,
) ([]byte, sdk.Error) {
var params types.QueryWithdrawerFeeSplitsRequest
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
if strings.TrimSpace(params.WithdrawerAddress) == "" {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "withdraw address is empty")
}
withdrawer, err := sdk.AccAddressFromBech32(params.WithdrawerAddress)
if err != nil {
return nil, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("invalid format for withdraw addr %s, should be bech32", params.WithdrawerAddress),
)
}
var contracts []string
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.GetKeyPrefixWithdrawer(withdrawer),
)
pageRes, err := query.Paginate(store, params.Pagination, func(key, _ []byte) error {
contracts = append(contracts, common.BytesToAddress(key).Hex())
return nil
})
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInternal, err.Error())
}
resp := &types.QueryWithdrawerFeeSplitsResponse{
ContractAddresses: contracts,
Pagination: pageRes,
}
res, err := codec.MarshalJSONIndent(types.ModuleCdc, resp)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}