-
Notifications
You must be signed in to change notification settings - Fork 593
/
grpc_query.go
161 lines (129 loc) · 4.41 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
package keeper
import (
"context"
"fmt"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/ibc-go/v9/internal/validate"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
)
var (
_ types.QueryServer = (*Keeper)(nil)
_ types.QueryV2Server = (*Keeper)(nil)
)
// Denom implements the Query/Denom gRPC method
func (k Keeper) Denom(c context.Context, req *types.QueryDenomRequest) (*types.QueryDenomResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
hash, err := types.ParseHexHash(strings.TrimPrefix(req.Hash, "ibc/"))
if err != nil {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("invalid denom trace hash: %s, error: %s", hash.String(), err))
}
ctx := sdk.UnwrapSDKContext(c)
denom, found := k.GetDenom(ctx, hash)
if !found {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrap(types.ErrDenomNotFound, req.Hash).Error(),
)
}
return &types.QueryDenomResponse{
Denom: &denom,
}, nil
}
// Denoms implements the Query/Denoms gRPC method
func (k Keeper) Denoms(c context.Context, req *types.QueryDenomsRequest) (*types.QueryDenomsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var denoms types.Denoms
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.DenomKey)
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
var denom types.Denom
if err := k.cdc.Unmarshal(value, &denom); err != nil {
return err
}
denoms = append(denoms, denom)
return nil
})
if err != nil {
return nil, err
}
return &types.QueryDenomsResponse{
Denoms: denoms.Sort(),
Pagination: pageRes,
}, nil
}
// Params implements the Query/Params gRPC method
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
return &types.QueryParamsResponse{
Params: ¶ms,
}, nil
}
// DenomHash implements the Query/DenomHash gRPC method
func (k Keeper) DenomHash(c context.Context, req *types.QueryDenomHashRequest) (*types.QueryDenomHashResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
// Convert given request trace path to Denom struct to confirm the path in a valid denom trace format
denom := types.ExtractDenomFromPath(req.Trace)
if err := denom.Validate(); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
ctx := sdk.UnwrapSDKContext(c)
denomHash := denom.Hash()
found := k.HasDenom(ctx, denomHash)
if !found {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrap(types.ErrDenomNotFound, req.Trace).Error(),
)
}
return &types.QueryDenomHashResponse{
Hash: denomHash.String(),
}, nil
}
// EscrowAddress implements the EscrowAddress gRPC method
func (k Keeper) EscrowAddress(c context.Context, req *types.QueryEscrowAddressRequest) (*types.QueryEscrowAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
addr := types.GetEscrowAddress(req.PortId, req.ChannelId)
if err := validate.GRPCRequest(req.PortId, req.ChannelId); err != nil {
return nil, err
}
ctx := sdk.UnwrapSDKContext(c)
if !k.channelKeeper.HasChannel(ctx, req.PortId, req.ChannelId) {
return nil, status.Error(
codes.NotFound,
errorsmod.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", req.PortId, req.ChannelId).Error(),
)
}
return &types.QueryEscrowAddressResponse{
EscrowAddress: addr.String(),
}, nil
}
// TotalEscrowForDenom implements the TotalEscrowForDenom gRPC method.
func (k Keeper) TotalEscrowForDenom(c context.Context, req *types.QueryTotalEscrowForDenomRequest) (*types.QueryTotalEscrowForDenomResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
if err := sdk.ValidateDenom(req.Denom); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
amount := k.GetTotalEscrowForDenom(ctx, req.Denom)
return &types.QueryTotalEscrowForDenomResponse{
Amount: amount,
}, nil
}