/
grpc_query.go
135 lines (107 loc) · 3.87 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
package keeper
import (
"context"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
appparams "github.com/celinium-network/celinium/app/params"
"github.com/celinium-network/celinium/x/liquidstake/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Querier struct {
Keeper
}
var _ types.QueryServer = Querier{}
// ProxyDelegation implements types.QueryServer
func (k Querier) ProxyDelegation(goCtx context.Context, req *types.QueryProxyDelegationRequest) (*types.QueryProxyDelegationResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.ChainID == "" {
return nil, status.Errorf(codes.InvalidArgument, "empty chainID %s", req.ChainID)
}
ctx := sdk.UnwrapSDKContext(goCtx)
if _, found := k.GetSourceChain(ctx, req.ChainID); !found {
return nil, status.Errorf(codes.InvalidArgument, "unknown chainID %s", req.ChainID)
}
recordID, found := k.GetChianProxyDelegationID(ctx, req.ChainID, req.Epoch)
if !found {
return nil, status.Errorf(codes.InvalidArgument, "chain%s no record in epoch %d", req.ChainID, req.Epoch)
}
record, found := k.GetProxyDelegation(ctx, recordID)
if !found {
return nil, status.Errorf(codes.Internal, "chain%s no record in epoch %d", req.ChainID, req.Epoch)
}
return &types.QueryProxyDelegationResponse{
Record: *record,
}, nil
}
// EpochProxyUnbonding implements types.QueryServer
func (k Querier) EpochProxyUnbonding(goCtx context.Context, req *types.QueryEpochProxyUnbondingRequest) (
*types.QueryEpochProxyUnbondingResponse, error,
) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.ChainID == "" {
return nil, status.Errorf(codes.InvalidArgument, "empty chainID %s", req.ChainID)
}
ctx := sdk.UnwrapSDKContext(goCtx)
unbondings, found := k.GetEpochProxyUnboundings(ctx, req.Epoch)
if !found || len(unbondings.Unbondings) == 0 {
return nil, status.Errorf(codes.Internal, "no unbondings in epoch %d", req.Epoch)
}
var chainUnbonding types.ProxyUnbonding
for _, unbonding := range unbondings.Unbondings {
if strings.Compare(req.ChainID, unbonding.ChainID) != 0 {
continue
}
chainUnbonding = unbonding
}
return &types.QueryEpochProxyUnbondingResponse{
ChainUnbonding: chainUnbonding,
}, nil
}
// SourceChain implements types.QueryServer
func (k Querier) SourceChain(goCtx context.Context, req *types.QuerySourceChainRequest) (*types.QuerySourceChainResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
if req.ChainID == "" {
return nil, status.Errorf(codes.InvalidArgument, "empty chainID %s", req.ChainID)
}
ctx := sdk.UnwrapSDKContext(goCtx)
sourceChain, found := k.GetSourceChain(ctx, req.ChainID)
if !found {
return nil, status.Errorf(codes.InvalidArgument, "unknown chainID %s", req.ChainID)
}
return &types.QuerySourceChainResponse{
SourceChain: *sourceChain,
}, nil
}
// UserUnbonding implements types.QueryServer
func (k Querier) UserUnbonding(goCtx context.Context, req *types.QueryUserUnbondingRequest) (*types.QueryUserUnbondingResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
_, found := k.GetSourceChain(ctx, req.ChainID)
if !found {
return nil, status.Errorf(codes.InvalidArgument, "unknown chainID %s", req.ChainID)
}
curEpoch, found := k.epochKeeper.GetEpochInfo(ctx, appparams.UndelegationEpochIdentifier)
if !found {
return nil, status.Errorf(codes.Internal, "undelegation epoch not start")
}
var userUnbondings []types.UserUnbonding
for i := uint64(0); i < uint64(curEpoch.CurrentEpoch); i++ {
record, found := k.GetUserUnbonding(ctx, req.ChainID, i, req.User)
if !found {
continue
}
userUnbondings = append(userUnbondings, *record)
}
return &types.QueryUserUnbondingResponse{
UserUnbondings: userUnbondings,
}, nil
}