Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

chain: Add CLI and REST query interface for request #1875

Merged
merged 3 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Chain

- (feat) [\#1875](https://github.com/bandprotocol/bandchain/pull/1875) Add CLI and REST query interface for request.
- (chore) [\#1869](https://github.com/bandprotocol/bandchain/pull/1869) Update new schema and source code url for all oracle scripts
- (chore) [\#1864](https://github.com/bandprotocol/bandchain/pull/1864) Remove unused query types.
- (impv) [\#1792](https://github.com/bandprotocol/bandchain/pull/1792) Request data message handler test
Expand Down
44 changes: 31 additions & 13 deletions chain/x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ func GetQueryCmd(storeKey string, cdc *codec.Codec) *cobra.Command {
RunE: client.ValidateCmd,
}
oracleCmd.AddCommand(flags.GetCommands(
GetCmdParams(storeKey, cdc),
GetCmdCounts(storeKey, cdc),
GetCmdDataSource(storeKey, cdc),
GetCmdOracleScript(storeKey, cdc),
GetQueryCmdParams(storeKey, cdc),
GetQueryCmdCounts(storeKey, cdc),
GetQueryCmdDataSource(storeKey, cdc),
GetQueryCmdOracleScript(storeKey, cdc),
GetQueryCmdRequest(storeKey, cdc),
)...)

return oracleCmd
}

// GetCmdParams implements the query parameters command.
func GetCmdParams(route string, cdc *codec.Codec) *cobra.Command {
// GetQueryCmdParams implements the query parameters command.
func GetQueryCmdParams(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
Expand All @@ -50,8 +50,8 @@ func GetCmdParams(route string, cdc *codec.Codec) *cobra.Command {
}
}

// GetCmdCounts implements the query counts command.
func GetCmdCounts(route string, cdc *codec.Codec) *cobra.Command {
// GetQueryCmdCounts implements the query counts command.
func GetQueryCmdCounts(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "counts",
Args: cobra.NoArgs,
Expand All @@ -68,8 +68,8 @@ func GetCmdCounts(route string, cdc *codec.Codec) *cobra.Command {
}
}

// GetCmdDataSource implements the query data source command.
func GetCmdDataSource(route string, cdc *codec.Codec) *cobra.Command {
// GetQueryCmdDataSource implements the query data source command.
func GetQueryCmdDataSource(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "data-source [id]",
Args: cobra.ExactArgs(1),
Expand All @@ -86,8 +86,8 @@ func GetCmdDataSource(route string, cdc *codec.Codec) *cobra.Command {
}
}

// GetCmdOracleScript implements the query oracle script command.
func GetCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command {
// GetQueryCmdOracleScript implements the query oracle script command.
func GetQueryCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "oracle-script [id]",
Args: cobra.ExactArgs(1),
Expand All @@ -103,3 +103,21 @@ func GetCmdOracleScript(route string, cdc *codec.Codec) *cobra.Command {
},
}
}

// GetQueryCmdRequest implements the query request command.
func GetQueryCmdRequest(route string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "request [id]",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryRequests, args[0]), nil)
if err != nil {
return err
}
var out types.QueryRequestResult
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
}
21 changes: 19 additions & 2 deletions chain/x/oracle/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func getDataSourceByIDHandler(cliCtx context.CLIContext, route string) http.Hand
return
}
vars := mux.Vars(r)
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryDataSources, vars[dataSourceIDTag]), nil)
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryDataSources, vars[idTag]), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand All @@ -85,7 +85,24 @@ func getOracleScriptByIDHandler(cliCtx context.CLIContext, route string) http.Ha
return
}
vars := mux.Vars(r)
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryOracleScripts, vars[oracleScriptIDTag]), nil)
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryOracleScripts, vars[idTag]), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func getRequestByIDHandler(cliCtx context.CLIContext, route string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
vars := mux.Vars(r)
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", route, types.QueryRequests, vars[idTag]), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
10 changes: 5 additions & 5 deletions chain/x/oracle/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

const (
dataSourceIDTag = "dataSourceIDTag"
oracleScriptIDTag = "oracleScriptIDTag"
dataHashTag = "dataHashTag"
idTag = "idTag"
dataHashTag = "dataHashTag"
)

func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) {
r.HandleFunc(fmt.Sprintf("/%s/params", storeName), getParamsHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/counts", storeName), getCountsHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/data/{%s}", storeName, dataHashTag), getDataByHashHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/data_sources/{%s}", storeName, dataSourceIDTag), getDataSourceByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/oracle_scripts/{%s}", storeName, oracleScriptIDTag), getOracleScriptByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/data_sources/{%s}", storeName, idTag), getDataSourceByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/oracle_scripts/{%s}", storeName, idTag), getOracleScriptByIDHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/requests/{%s}", storeName, idTag), getRequestByIDHandler(cliCtx, storeName)).Methods("GET")
}
1 change: 0 additions & 1 deletion chain/x/oracle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
_ "github.com/cosmos/cosmos-sdk/x/ibc/04-channel"
)

// NewHandler creates the msg handler of this module, as required by Cosmos-SDK standard.
Expand Down
21 changes: 21 additions & 0 deletions chain/x/oracle/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryDataSourceByID(ctx, path[1:], keeper)
case types.QueryOracleScripts:
return queryOracleScriptByID(ctx, path[1:], keeper)
case types.QueryRequests:
return queryRequestByID(ctx, path[1:], keeper)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown oracle query endpoint")
}
Expand Down Expand Up @@ -79,3 +81,22 @@ func queryOracleScriptByID(ctx sdk.Context, path []string, k Keeper) ([]byte, er
}
return codec.MarshalJSONIndent(types.ModuleCdc, oracleScript)
}

func queryRequestByID(ctx sdk.Context, path []string, k Keeper) ([]byte, error) {
if len(path) != 1 {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "request not specified")
}
id, err := strconv.ParseInt(path[0], 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, err.Error())
}
request, err := k.GetRequest(ctx, types.RequestID(id))
reports := k.GetReports(ctx, types.RequestID(id))
if err != nil {
return nil, err
}
return codec.MarshalJSONIndent(types.ModuleCdc, types.QueryRequestResult{
Request: request,
Reports: reports,
})
}
7 changes: 7 additions & 0 deletions chain/x/oracle/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
QueryData = "data"
QueryDataSources = "data_sources"
QueryOracleScripts = "oracle_scripts"
QueryRequests = "requests"
)

// QueryCountsResult is the struct for the result of query counts.
Expand All @@ -15,3 +16,9 @@ type QueryCountsResult struct {
OracleScriptCount int64 `json:"oracle_script_count"`
RequestCount int64 `json:"request_count"`
}

// QueryRequestResult is the struct for the result of request query.
type QueryRequestResult struct {
Request Request `json:"request"`
Reports []Report `json:"reports"`
}