/
grpc_query.go
274 lines (216 loc) · 10.7 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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/persistenceOne/pstake-native/v2/x/lscosmos/types"
)
var _ types.QueryServer = Keeper{}
// HostChainParams queries the host chain params
func (k Keeper) HostChainParams(c context.Context, in *types.QueryHostChainParamsRequest) (*types.QueryHostChainParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
hostChainParams := k.GetHostChainParams(ctx)
return &types.QueryHostChainParamsResponse{
HostChainParams: hostChainParams,
}, nil
}
// DelegationState queries the current delegation state
func (k Keeper) DelegationState(c context.Context, request *types.QueryDelegationStateRequest) (*types.QueryDelegationStateResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
delegationState := k.GetDelegationState(ctx)
return &types.QueryDelegationStateResponse{
DelegationState: delegationState,
}, nil
}
// AllowListedValidators queries the current allow listed validators set
func (k Keeper) AllowListedValidators(c context.Context, request *types.QueryAllowListedValidatorsRequest) (*types.QueryAllowListedValidatorsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
allowListedValidators := k.GetAllowListedValidators(ctx)
return &types.QueryAllowListedValidatorsResponse{
AllowListedValidators: allowListedValidators,
}, nil
}
// CValue computes and returns the c value
func (k Keeper) CValue(c context.Context, request *types.QueryCValueRequest) (*types.QueryCValueResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
cValue := k.GetCValue(ctx)
return &types.QueryCValueResponse{
CValue: cValue,
}, nil
}
// ModuleState queries the current module state
func (k Keeper) ModuleState(c context.Context, request *types.QueryModuleStateRequest) (*types.QueryModuleStateResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
moduleState := k.GetModuleState(ctx)
return &types.QueryModuleStateResponse{
ModuleState: moduleState,
}, nil
}
// IBCTransientStore queries the current IBC transient store
func (k Keeper) IBCTransientStore(c context.Context, request *types.QueryIBCTransientStoreRequest) (*types.QueryIBCTransientStoreResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ibcTransientStore := k.GetIBCTransientStore(ctx)
return &types.QueryIBCTransientStoreResponse{
IBCTransientStore: ibcTransientStore,
}, nil
}
// Unclaimed queries the unclaimed entries corresponding to the input delegator address in types.QueryUnclaimedRequest
func (k Keeper) Unclaimed(c context.Context, request *types.QueryUnclaimedRequest) (*types.QueryUnclaimedResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
// get delegator account address from request
delegatorAddress, err := sdk.AccAddressFromBech32(request.DelegatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
var queryResponse types.QueryUnclaimedResponse
delegatorUnbondingEpochEntries := k.IterateDelegatorUnbondingEpochEntry(ctx, delegatorAddress)
for _, entry := range delegatorUnbondingEpochEntries {
unbondingEpochCValue := k.GetUnbondingEpochCValue(ctx, entry.EpochNumber)
// sort for all the cases
if unbondingEpochCValue.IsMatured && unbondingEpochCValue.EpochNumber > 0 {
// append to ready to claim entries
queryResponse.Unclaimed = append(queryResponse.Unclaimed, unbondingEpochCValue)
}
}
return &queryResponse, nil
}
// FailedUnbondings queries the failed unbonding entries corresponding to the input delegator address in
// types.QueryUnclaimedRequest
func (k Keeper) FailedUnbondings(c context.Context, request *types.QueryFailedUnbondingsRequest) (*types.QueryFailedUnbondingsResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
// get delegator account address from request
delegatorAddress, err := sdk.AccAddressFromBech32(request.DelegatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
var queryResponse types.QueryFailedUnbondingsResponse
delegatorUnbondingEpochEntries := k.IterateDelegatorUnbondingEpochEntry(ctx, delegatorAddress)
for _, entry := range delegatorUnbondingEpochEntries {
unbondingEpochCValue := k.GetUnbondingEpochCValue(ctx, entry.EpochNumber)
if unbondingEpochCValue.IsFailed && unbondingEpochCValue.EpochNumber > 0 {
// append to failed entries for which stkAtom should be claimed again
queryResponse.FailedUnbondings = append(queryResponse.FailedUnbondings, unbondingEpochCValue)
}
}
return &queryResponse, nil
}
// PendingUnbondings queries the pending unbonding entries corresponding to the input delegator address in
// types.QueryUnclaimedRequest
func (k Keeper) PendingUnbondings(c context.Context, request *types.QueryPendingUnbondingsRequest) (*types.QueryPendingUnbondingsResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
// get delegator account address from request
delegatorAddress, err := sdk.AccAddressFromBech32(request.DelegatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
var queryResponse types.QueryPendingUnbondingsResponse
delegatorUnbondingEpochEntries := k.IterateDelegatorUnbondingEpochEntry(ctx, delegatorAddress)
for _, entry := range delegatorUnbondingEpochEntries {
unbondingEpochCValue := k.GetUnbondingEpochCValue(ctx, entry.EpochNumber)
if !unbondingEpochCValue.IsFailed && !unbondingEpochCValue.IsMatured && unbondingEpochCValue.EpochNumber > 0 {
// append to in progress entries
queryResponse.PendingUnbondings = append(queryResponse.PendingUnbondings, unbondingEpochCValue)
}
}
return &queryResponse, nil
}
// UnbondingEpochCValue queries the unbonding epoch c value details corresponding to the input epoch number
// in types.QueryUnbondingEpochCValueRequest
func (k Keeper) UnbondingEpochCValue(c context.Context, request *types.QueryUnbondingEpochCValueRequest) (*types.QueryUnbondingEpochCValueResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.EpochNumber <= 0 {
return nil, status.Error(codes.InvalidArgument, "epoch number less than equal to 0")
}
ctx := sdk.UnwrapSDKContext(c)
unbondingEpochCValue := k.GetUnbondingEpochCValue(ctx, request.EpochNumber)
return &types.QueryUnbondingEpochCValueResponse{UnbondingEpochCValue: unbondingEpochCValue}, nil
}
// HostAccountUndelegation queries the host account undelegation details corresponding to the input epoch number
// in types.QueryHostAccountUndelegationRequest
func (k Keeper) HostAccountUndelegation(c context.Context, request *types.QueryHostAccountUndelegationRequest) (*types.QueryHostAccountUndelegationResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.EpochNumber <= 0 {
return nil, status.Error(codes.InvalidArgument, "epoch number less than equal to 0")
}
ctx := sdk.UnwrapSDKContext(c)
hostAccountUndelegation, err := k.GetHostAccountUndelegationForEpoch(ctx, request.EpochNumber)
if err != nil {
return nil, err
}
return &types.QueryHostAccountUndelegationResponse{HostAccountUndelegation: hostAccountUndelegation}, nil
}
// DelegatorUnbondingEpochEntry queries the delegator unbonding epoch entry details corresponding to the
// input epoch number and delegator address in types.QueryDelegatorUnbondingEpochEntryRequest
func (k Keeper) DelegatorUnbondingEpochEntry(c context.Context, request *types.QueryDelegatorUnbondingEpochEntryRequest) (*types.QueryDelegatorUnbondingEpochEntryResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.EpochNumber <= 0 {
return nil, status.Error(codes.InvalidArgument, "epoch number less than equal to 0")
}
if request.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
// get delegator account address from request
delegatorAddress, err := sdk.AccAddressFromBech32(request.DelegatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
unbondingEpochEntry := k.GetDelegatorUnbondingEpochEntry(ctx, delegatorAddress, request.EpochNumber)
return &types.QueryDelegatorUnbondingEpochEntryResponse{DelegatorUnbodingEpochEntry: unbondingEpochEntry}, nil
}
// HostAccounts queries the host accounts
func (k Keeper) HostAccounts(c context.Context, request *types.QueryHostAccountsRequest) (*types.QueryHostAccountsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
hostAccounts := k.GetHostAccounts(ctx)
return &types.QueryHostAccountsResponse{HostAccounts: hostAccounts}, nil
}
// DepositModuleAccount queries the deposit module account balance
func (k Keeper) DepositModuleAccount(c context.Context, request *types.QueryDepositModuleAccountRequest) (*types.QueryDepositModuleAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
balance := k.bankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(types.DepositModuleAccount), k.GetIBCDenom(ctx))
return &types.QueryDepositModuleAccountResponse{Balance: balance}, nil
}
// DelegatorUnbondingEpochEntries queries all the delegator unbonding epoch entries corresponding to
// the input delegator address in types.QueryAllDelegatorUnbondingEpochEntriesRequest
func (k Keeper) DelegatorUnbondingEpochEntries(c context.Context, request *types.QueryAllDelegatorUnbondingEpochEntriesRequest) (*types.QueryAllDelegatorUnbondingEpochEntriesResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if request.DelegatorAddress == "" {
return nil, status.Error(codes.InvalidArgument, "address cannot be empty")
}
ctx := sdk.UnwrapSDKContext(c)
// get delegator account address from request
delegatorAddress, err := sdk.AccAddressFromBech32(request.DelegatorAddress)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid address: %s", err.Error())
}
list := k.IterateDelegatorUnbondingEpochEntry(ctx, delegatorAddress)
return &types.QueryAllDelegatorUnbondingEpochEntriesResponse{DelegatorUnbondingEpochEntries: list}, nil
}