-
Notifications
You must be signed in to change notification settings - Fork 13
/
query_server.go
61 lines (48 loc) · 1.75 KB
/
query_server.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
package keeper
import (
"context"
"errors"
"cosmossdk.io/collections"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/hyle-org/hyle/x/zktx"
)
var _ zktx.QueryServer = queryServer{}
// NewQueryServerImpl returns an implementation of the module QueryServer.
func NewQueryServerImpl(k Keeper) zktx.QueryServer {
return queryServer{k}
}
type queryServer struct {
k Keeper
}
// Handler for the contract contract query method
func (qs queryServer) Contract(ctx context.Context, req *zktx.ContractRequest) (*zktx.ContractResponse, error) {
contract, err := qs.k.Contracts.Get(ctx, req.ContractName)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return &zktx.ContractResponse{Contract: zktx.Contract{}}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}
return &zktx.ContractResponse{Contract: contract}, nil
}
// Handler for the contract contract query method
func (qs queryServer) ContractList(ctx context.Context, req *zktx.ContractListRequest) (*zktx.ContractListResponse, error) {
contractList := make([]string, 0)
qs.k.Contracts.Walk(ctx, nil, func(key string, contract zktx.Contract) (stop bool, err error) {
contractList = append(contractList, key)
return false, nil
})
return &zktx.ContractListResponse{Contracts: contractList}, nil
}
// Params defines the handler for the Query/Params RPC method.
func (qs queryServer) Params(ctx context.Context, req *zktx.QueryParamsRequest) (*zktx.QueryParamsResponse, error) {
params, err := qs.k.Params.Get(ctx)
if err != nil {
if errors.Is(err, collections.ErrNotFound) {
return &zktx.QueryParamsResponse{Params: zktx.Params{}}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}
return &zktx.QueryParamsResponse{Params: params}, nil
}