2018-03-10 09:33:05 -08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2019-06-15 05:34:11 -07:00
|
|
|
"fmt"
|
2018-03-10 09:33:05 -08:00
|
|
|
"net/http"
|
2019-08-01 11:56:50 -07:00
|
|
|
"strconv"
|
2019-06-15 05:34:11 -07:00
|
|
|
"strings"
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-03-10 09:33:05 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-06-01 05:46:03 -07:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-08-01 11:56:50 -07:00
|
|
|
genutilrest "github.com/cosmos/cosmos-sdk/x/genutil/client/rest"
|
2018-03-10 09:33:05 -08:00
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// query accountREST Handler
|
2020-06-01 05:46:03 -07:00
|
|
|
func QueryAccountRequestHandlerFn(storeName string, clientCtx client.Context) http.HandlerFunc {
|
2018-03-10 09:33:05 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2018-06-05 21:53:04 -07:00
|
|
|
bech32addr := vars["address"]
|
2018-03-14 03:43:31 -07:00
|
|
|
|
2018-07-09 16:06:05 -07:00
|
|
|
addr, err := sdk.AccAddressFromBech32(bech32addr)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
2019-06-07 06:02:07 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-13 07:22:16 -07:00
|
|
|
accGetter := types.AccountRetriever{}
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
account, height, err := accGetter.GetAccountWithHeight(clientCtx, addr)
|
2018-03-10 09:33:05 -08:00
|
|
|
if err != nil {
|
2019-08-19 07:58:11 -07:00
|
|
|
// TODO: Handle more appropriately based on the error type.
|
|
|
|
// Ref: https://github.com/cosmos/cosmos-sdk/issues/4923
|
2020-06-01 05:46:03 -07:00
|
|
|
if err := accGetter.EnsureExists(clientCtx, addr); err != nil {
|
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, clientCtx, types.BaseAccount{})
|
2019-08-19 07:58:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, clientCtx, account)
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
}
|
2018-09-29 20:42:12 -07:00
|
|
|
|
2019-08-01 11:56:50 -07:00
|
|
|
// QueryTxsHandlerFn implements a REST handler that searches for transactions.
|
|
|
|
// Genesis transactions are returned if the height parameter is set to zero,
|
|
|
|
// otherwise the transactions are searched for by events.
|
2020-06-01 05:46:03 -07:00
|
|
|
func QueryTxsRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2019-06-15 05:34:11 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
2019-12-27 09:57:54 -08:00
|
|
|
rest.WriteErrorResponse(
|
|
|
|
w, http.StatusBadRequest,
|
|
|
|
fmt.Sprintf("failed to parse query parameters: %s", err),
|
|
|
|
)
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-01 11:56:50 -07:00
|
|
|
// if the height query param is set to zero, query for genesis transactions
|
|
|
|
heightStr := r.FormValue("height")
|
|
|
|
if heightStr != "" {
|
|
|
|
if height, err := strconv.ParseInt(heightStr, 10, 64); err == nil && height == 0 {
|
2020-06-01 05:46:03 -07:00
|
|
|
genutilrest.QueryGenesisTxs(clientCtx, w)
|
2019-08-01 11:56:50 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
events []string
|
|
|
|
txs []sdk.TxResponse
|
|
|
|
page, limit int
|
|
|
|
)
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
2019-06-15 05:34:11 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Form) == 0 {
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, txs)
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-01 11:56:50 -07:00
|
|
|
events, page, limit, err = rest.ParseHTTPArgs(r)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
searchResult, err := authclient.QueryTxsByEvents(clientCtx, events, page, limit, "")
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-06-15 05:34:11 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, searchResult)
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryTxRequestHandlerFn implements a REST handler that queries a transaction
|
|
|
|
// by hash in a committed block.
|
2020-06-01 05:46:03 -07:00
|
|
|
func QueryTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2019-06-15 05:34:11 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
hashHexStr := vars["hash"]
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
2019-06-15 05:34:11 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
output, err := authclient.QueryTx(clientCtx, hashHexStr)
|
2019-06-15 05:34:11 -07:00
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), hashHexStr) {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if output.Empty() {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusNotFound, fmt.Sprintf("no transaction found with hash %s", hashHexStr))
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, output)
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 07:33:56 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
func queryParamsHandler(clientCtx client.Context) http.HandlerFunc {
|
2020-02-26 07:33:56 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
2020-02-26 07:33:56 -08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
|
2020-06-01 05:46:03 -07:00
|
|
|
res, height, err := clientCtx.QueryWithData(route, nil)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2020-02-26 07:33:56 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, clientCtx, res)
|
2020-02-26 07:33:56 -08:00
|
|
|
}
|
|
|
|
}
|