/
grpc_query.go
297 lines (237 loc) · 9.67 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
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
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"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
sdkdistr "github.com/cosmos/cosmos-sdk/x/distribution/types"
sdkstaking "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/nephirim/fanfury-sdk/v2/x/lsnative/distribution/types"
)
var _ types.QueryServer = Keeper{}
// Params queries params of distribution module
func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
var params types.Params
k.paramSpace.GetParamSet(ctx, ¶ms)
return &types.QueryParamsResponse{Params: params}, nil
}
// ValidatorOutstandingRewards queries rewards of a validator address
func (k Keeper) ValidatorOutstandingRewards(c context.Context, req *types.QueryValidatorOutstandingRewardsRequest) (*types.QueryValidatorOutstandingRewardsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.ValidatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty validator address")
}
ctx := sdk.UnwrapSDKContext(c)
valAdr, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
rewards := k.GetValidatorOutstandingRewards(ctx, valAdr)
return &types.QueryValidatorOutstandingRewardsResponse{Rewards: rewards}, nil
}
// ValidatorCommission queries accumulated commission for a validator
func (k Keeper) ValidatorCommission(c context.Context, req *types.QueryValidatorCommissionRequest) (*types.QueryValidatorCommissionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.ValidatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty validator address")
}
ctx := sdk.UnwrapSDKContext(c)
valAdr, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
commission := k.GetValidatorAccumulatedCommission(ctx, valAdr)
return &types.QueryValidatorCommissionResponse{Commission: commission}, nil
}
// ValidatorSlashes queries slash events of a validator
func (k Keeper) ValidatorSlashes(c context.Context, req *types.QueryValidatorSlashesRequest) (*types.QueryValidatorSlashesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.ValidatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty validator address")
}
if req.EndingHeight < req.StartingHeight {
return nil, status.Errorf(codes.InvalidArgument, "starting height greater than ending height (%d > %d)", req.StartingHeight, req.EndingHeight)
}
ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(k.storeKey)
valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid validator address")
}
slashesStore := prefix.NewStore(store, types.GetValidatorSlashEventPrefix(valAddr))
events, pageRes, err := query.GenericFilteredPaginate(k.cdc, slashesStore, req.Pagination, func(key []byte, result *types.ValidatorSlashEvent) (*types.ValidatorSlashEvent, error) {
if result.ValidatorPeriod < req.StartingHeight || result.ValidatorPeriod > req.EndingHeight {
return nil, nil
}
return result, nil
}, func() *types.ValidatorSlashEvent {
return &types.ValidatorSlashEvent{}
})
if err != nil {
return nil, err
}
slashes := []types.ValidatorSlashEvent{}
for _, event := range events {
slashes = append(slashes, *event)
}
return &types.QueryValidatorSlashesResponse{Slashes: slashes, Pagination: pageRes}, nil
}
// DelegationRewards the total rewards accrued by a delegation
func (k Keeper) DelegationRewards(c context.Context, req *sdkdistr.QueryDelegationRewardsRequest) (*sdkdistr.QueryDelegationRewardsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty delegator address")
}
if req.ValidatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty validator address")
}
ctx := sdk.UnwrapSDKContext(c)
valAdr, err := sdk.ValAddressFromBech32(req.ValidatorAddress)
if err != nil {
return nil, err
}
val := k.stakingKeeper.Validator(ctx, valAdr)
if val == nil {
return nil, sdkerrors.Wrap(sdkdistr.ErrNoValidatorExists, req.ValidatorAddress)
}
delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
if err != nil {
return nil, err
}
del := k.stakingKeeper.Delegation(ctx, delAdr, valAdr)
if del == nil {
return nil, sdkdistr.ErrNoDelegationExists
}
endingPeriod := k.IncrementValidatorPeriod(ctx, val)
rewards := k.CalculateDelegationRewards(ctx, val, del, endingPeriod)
return &sdkdistr.QueryDelegationRewardsResponse{Rewards: rewards}, nil
}
// DelegationTotalRewards the total rewards accrued by a each validator
func (k Keeper) DelegationTotalRewards(c context.Context, req *types.QueryDelegationTotalRewardsRequest) (*types.QueryDelegationTotalRewardsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty delegator address")
}
ctx := sdk.UnwrapSDKContext(c)
total := sdk.DecCoins{}
var delRewards []types.DelegationDelegatorReward
delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
if err != nil {
return nil, err
}
k.stakingKeeper.IterateDelegations(
ctx, delAdr,
func(_ int64, del sdkstaking.DelegationI) (stop bool) {
valAddr := del.GetValidatorAddr()
val := k.stakingKeeper.Validator(ctx, valAddr)
endingPeriod := k.IncrementValidatorPeriod(ctx, val)
delReward := k.CalculateDelegationRewards(ctx, val, del, endingPeriod)
delRewards = append(delRewards, types.NewDelegationDelegatorReward(valAddr, delReward))
total = total.Add(delReward...)
return false
},
)
return &types.QueryDelegationTotalRewardsResponse{Rewards: delRewards, Total: total}, nil
}
// DelegatorValidators queries the validators list of a delegator
func (k Keeper) DelegatorValidators(c context.Context, req *types.QueryDelegatorValidatorsRequest) (*types.QueryDelegatorValidatorsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty delegator address")
}
ctx := sdk.UnwrapSDKContext(c)
delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
if err != nil {
return nil, err
}
var validators []string
k.stakingKeeper.IterateDelegations(
ctx, delAdr,
func(_ int64, del sdkstaking.DelegationI) (stop bool) {
validators = append(validators, del.GetValidatorAddr().String())
return false
},
)
return &types.QueryDelegatorValidatorsResponse{Validators: validators}, nil
}
// DelegatorWithdrawAddress queries Query/delegatorWithdrawAddress
func (k Keeper) DelegatorWithdrawAddress(c context.Context, req *types.QueryDelegatorWithdrawAddressRequest) (*types.QueryDelegatorWithdrawAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
if req.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "empty delegator address")
}
delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
if err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(c)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delAdr)
return &types.QueryDelegatorWithdrawAddressResponse{WithdrawAddress: withdrawAddr.String()}, nil
}
// CommunityPool queries the community pool coins
func (k Keeper) CommunityPool(c context.Context, req *types.QueryCommunityPoolRequest) (*types.QueryCommunityPoolResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
pool := k.GetFeePoolCommunityCoins(ctx)
return &types.QueryCommunityPoolResponse{Pool: pool}, nil
}
// TokenizeShareRecordReward returns estimated amount of reward from tokenize share record ownership
func (k Keeper) TokenizeShareRecordReward(c context.Context, req *types.QueryTokenizeShareRecordRewardRequest) (*types.QueryTokenizeShareRecordRewardResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
totalRewards := sdk.DecCoins{}
rewards := []types.TokenizeShareRecordReward{}
ownerAddr, err := sdk.AccAddressFromBech32(req.OwnerAddress)
if err != nil {
return nil, err
}
records := k.stakingKeeper.GetTokenizeShareRecordsByOwner(ctx, ownerAddr)
for _, record := range records {
valAddr, err := sdk.ValAddressFromBech32(record.Validator)
if err != nil {
return nil, err
}
moduleAddr := record.GetModuleAddress()
moduleBalance := k.bankKeeper.GetAllBalances(ctx, moduleAddr)
moduleBalanceDecCoins := sdk.NewDecCoinsFromCoins(moduleBalance...)
val := k.stakingKeeper.Validator(ctx, valAddr)
del := k.stakingKeeper.Delegation(ctx, moduleAddr, valAddr)
if val != nil && del != nil {
// withdraw rewards
endingPeriod := k.IncrementValidatorPeriod(ctx, val)
recordReward := k.CalculateDelegationRewards(ctx, val, del, endingPeriod)
rewards = append(rewards, types.TokenizeShareRecordReward{
RecordId: record.Id,
Reward: recordReward.Add(moduleBalanceDecCoins...),
})
totalRewards = totalRewards.Add(recordReward...)
}
if !moduleBalance.IsZero() {
rewards = append(rewards, types.TokenizeShareRecordReward{
RecordId: record.Id,
Reward: moduleBalanceDecCoins,
})
totalRewards = totalRewards.Add(moduleBalanceDecCoins...)
}
}
return &types.QueryTokenizeShareRecordRewardResponse{
Rewards: rewards,
Total: totalRewards,
}, nil
}