Skip to content

Commit

Permalink
feat: add oracle modules query client (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuguen authored Dec 8, 2022
1 parent 9160397 commit c684dde
Show file tree
Hide file tree
Showing 8 changed files with 605 additions and 65 deletions.
7 changes: 4 additions & 3 deletions proto/panacea/oracle/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ service Query {

// OracleRegistrations returns a list of OracleRegistration.
rpc OracleRegistrations(QueryOracleRegistrationsRequest) returns (QueryOracleRegistrationsResponse) {
option (google.api.http).get = "/panacea/oracle/v2/oracle_registrations";
option (google.api.http).get = "/panacea/oracle/v2/oracle-registrations/{unique_id}";
}

// OracleRegistration returns a OracleRegistration details.
rpc OracleRegistration(QueryOracleRegistrationRequest) returns (QueryOracleRegistrationResponse) {
option (google.api.http).get = "/panacea/oracle/v2/oracle_registrations/{unique_id}/{oracle_address}";
option (google.api.http).get = "/panacea/oracle/v2/oracle-registrations/{unique_id}/{oracle_address}";
}

// Params returns params of oracle module.
Expand Down Expand Up @@ -61,7 +61,8 @@ message QueryOracleResponse {

// QueryOracleRegistrationsRequest is the request type for the Query/OracleRegistrations RPC method.
message QueryOracleRegistrationsRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string unique_id = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

// QueryOracleRegistrationsResponse is the response type for the Query/OracleRegistrations RPC method.
Expand Down
4 changes: 4 additions & 0 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdGetOracles())
cmd.AddCommand(CmdGetOracle())
cmd.AddCommand(CmdGetOracleRegistrations())
cmd.AddCommand(CmdGetOracleRegistration())
cmd.AddCommand(CmdGetParams())

return cmd
Expand Down
72 changes: 72 additions & 0 deletions x/oracle/client/cli/queryOracle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/spf13/cobra"
)

func CmdGetOracles() *cobra.Command {
cmd := &cobra.Command{
Use: "oracles",
Short: "Query oracles info",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryOraclesRequest{
Pagination: pageReq,
}
res, err := queryClient.Oracles(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "oracles")
return cmd
}

func CmdGetOracle() *cobra.Command {
cmd := &cobra.Command{
Use: "oracle [oracle-address]",
Short: "Query a oracle info",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryOracleRequest{
OracleAddress: args[0],
}
res, err := queryClient.Oracle(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
75 changes: 75 additions & 0 deletions x/oracle/client/cli/queryOracleRegistration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
"github.com/spf13/cobra"
)

func CmdGetOracleRegistrations() *cobra.Command {
cmd := &cobra.Command{
Use: "oracle-registrations [unique-id]",
Short: "Query oracle-registrations info",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

req := &types.QueryOracleRegistrationsRequest{
UniqueId: args[0],
Pagination: pageReq,
}
res, err := queryClient.OracleRegistrations(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "oracle-registrations")

return cmd
}

func CmdGetOracleRegistration() *cobra.Command {
cmd := &cobra.Command{
Use: "oracle-registration [unique-id] [oracle-address]",
Short: "Query a oracle registration info",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.OracleRegistration(cmd.Context(), &types.QueryOracleRegistrationRequest{
UniqueId: args[0],
OracleAddress: args[1],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
97 changes: 82 additions & 15 deletions x/oracle/keeper/grpc_query_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,97 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/medibloc/panacea-core/v2/x/oracle/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) Oracles(ctx context.Context, request *types.QueryOraclesRequest) (*types.QueryOraclesResponse, error) {
//TODO implement me
panic("implement me")
func (k Keeper) Oracles(goCtx context.Context, request *types.QueryOraclesRequest) (*types.QueryOraclesResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
oracleStore := prefix.NewStore(store, types.OraclesKey)

var oracles []*types.Oracle
pageRes, err := query.Paginate(oracleStore, request.Pagination, func(_, value []byte) error {
var oracle types.Oracle
if err := k.cdc.UnmarshalLengthPrefixed(value, &oracle); err != nil {
return err
}

oracles = append(oracles, &oracle)
return nil
})

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryOraclesResponse{
Oracles: oracles,
Pagination: pageRes,
}, nil
}

func (k Keeper) Oracle(ctx context.Context, request *types.QueryOracleRequest) (*types.QueryOracleResponse, error) {
//TODO implement me
panic("implement me")
func (k Keeper) Oracle(goCtx context.Context, request *types.QueryOracleRequest) (*types.QueryOracleResponse, error) {
oracle, err := k.GetOracle(sdk.UnwrapSDKContext(goCtx), request.OracleAddress)
if err != nil {
return nil, err
}

return &types.QueryOracleResponse{
Oracle: oracle,
}, nil
}

func (k Keeper) OracleRegistrations(ctx context.Context, request *types.QueryOracleRegistrationsRequest) (*types.QueryOracleRegistrationsResponse, error) {
//TODO implement me
panic("implement me")
func (k Keeper) OracleRegistrations(goCtx context.Context, request *types.QueryOracleRegistrationsRequest) (*types.QueryOracleRegistrationsResponse, error) {
if request == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

store := ctx.KVStore(k.storeKey)
oracleRegistrationStore := prefix.NewStore(store, append(types.OracleRegistrationKey, []byte(request.UniqueId)...))

var oracleRegistrations []*types.OracleRegistration
pageRes, err := query.Paginate(oracleRegistrationStore, request.Pagination, func(_, value []byte) error {
var oracleRegistration types.OracleRegistration
if err := k.cdc.UnmarshalLengthPrefixed(value, &oracleRegistration); err != nil {
return err
}

oracleRegistrations = append(oracleRegistrations, &oracleRegistration)
return nil
})

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryOracleRegistrationsResponse{
OracleRegistrations: oracleRegistrations,
Pagination: pageRes,
}, nil
}

func (k Keeper) OracleRegistration(ctx context.Context, request *types.QueryOracleRegistrationRequest) (*types.QueryOracleRegistrationResponse, error) {
//TODO implement me
panic("implement me")
func (k Keeper) OracleRegistration(goCtx context.Context, request *types.QueryOracleRegistrationRequest) (*types.QueryOracleRegistrationResponse, error) {
oracleRegistration, err := k.GetOracleRegistration(sdk.UnwrapSDKContext(goCtx), request.UniqueId, request.OracleAddress)
if err != nil {
return nil, err
}

return &types.QueryOracleRegistrationResponse{OracleRegistration: oracleRegistration}, nil
}

func (k Keeper) Params(ctx context.Context, request *types.QueryOracleParamsRequest) (*types.QueryParamsResponse, error) {
//TODO implement me
panic("implement me")
func (k Keeper) Params(goCtx context.Context, _ *types.QueryOracleParamsRequest) (*types.QueryParamsResponse, error) {
params := k.GetParams(sdk.UnwrapSDKContext(goCtx))
return &types.QueryParamsResponse{Params: &params}, nil
}
Loading

0 comments on commit c684dde

Please sign in to comment.