/
grpc_query.go
192 lines (162 loc) · 4.95 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
180
181
182
183
184
185
186
187
188
189
190
191
192
package keeper
import (
"context"
"strings"
"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/ethereum/go-ethereum/common"
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/evmos/v8/x/revenue/types"
)
var _ types.QueryServer = Keeper{}
// Revenues returns all Revenues that have been registered for fee distribution
func (k Keeper) Revenues(
c context.Context,
req *types.QueryRevenuesRequest,
) (*types.QueryRevenuesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var revenues []types.Revenue
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixRevenue)
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
var revenue types.Revenue
if err := k.cdc.Unmarshal(value, &revenue); err != nil {
return err
}
revenues = append(revenues, revenue)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryRevenuesResponse{
Revenues: revenues,
Pagination: pageRes,
}, nil
}
// Revenue returns the Revenue that has been registered for fee distribution for a given
// contract
func (k Keeper) Revenue(
c context.Context,
req *types.QueryRevenueRequest,
) (*types.QueryRevenueResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
if strings.TrimSpace(req.ContractAddress) == "" {
return nil, status.Error(
codes.InvalidArgument,
"contract address is empty",
)
}
// check if the contract is a non-zero hex address
if err := ethermint.ValidateNonZeroAddress(req.ContractAddress); err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"invalid format for contract %s, should be non-zero hex ('0x...')", req.ContractAddress,
)
}
revenue, found := k.GetRevenue(ctx, common.HexToAddress(req.ContractAddress))
if !found {
return nil, status.Errorf(
codes.NotFound,
"fees registered contract '%s'",
req.ContractAddress,
)
}
return &types.QueryRevenueResponse{Revenue: revenue}, nil
}
// Params returns the fees module params
func (k Keeper) Params(
c context.Context,
_ *types.QueryParamsRequest,
) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
return &types.QueryParamsResponse{Params: params}, nil
}
// DeployerRevenues returns all contracts that have been registered for fee
// distribution by a given deployer
func (k Keeper) DeployerRevenues( // nolint: dupl
c context.Context,
req *types.QueryDeployerRevenuesRequest,
) (*types.QueryDeployerRevenuesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
if strings.TrimSpace(req.DeployerAddress) == "" {
return nil, status.Error(
codes.InvalidArgument,
"deployer address is empty",
)
}
deployer, err := sdk.AccAddressFromBech32(req.DeployerAddress)
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"invalid format for deployer %s, should be bech32 ('evmos...')", req.DeployerAddress,
)
}
var contracts []string
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.GetKeyPrefixDeployer(deployer),
)
pageRes, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
contracts = append(contracts, common.BytesToAddress(key).Hex())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryDeployerRevenuesResponse{
ContractAddresses: contracts,
Pagination: pageRes,
}, nil
}
// WithdrawerRevenues returns all fees for a given withdraw address
func (k Keeper) WithdrawerRevenues( // nolint: dupl
c context.Context,
req *types.QueryWithdrawerRevenuesRequest,
) (*types.QueryWithdrawerRevenuesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
if strings.TrimSpace(req.WithdrawerAddress) == "" {
return nil, status.Error(
codes.InvalidArgument,
"withdraw address is empty",
)
}
deployer, err := sdk.AccAddressFromBech32(req.WithdrawerAddress)
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
"invalid format for withdraw addr %s, should be bech32 ('evmos...')", req.WithdrawerAddress,
)
}
var contracts []string
store := prefix.NewStore(
ctx.KVStore(k.storeKey),
types.GetKeyPrefixWithdrawer(deployer),
)
pageRes, err := query.Paginate(store, req.Pagination, func(key, _ []byte) error {
contracts = append(contracts, common.BytesToAddress(key).Hex())
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryWithdrawerRevenuesResponse{
ContractAddresses: contracts,
Pagination: pageRes,
}, nil
}