125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
// query accountREST Handler
|
|
func QueryAccountRequestHandlerFn(storeName string, cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
bech32addr := vars["address"]
|
|
|
|
addr, err := sdk.AccAddressFromBech32(bech32addr)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
accGetter := types.NewAccountRetriever(cliCtx)
|
|
// the query will return empty account if there is no data
|
|
if err := accGetter.EnsureExists(addr); err != nil {
|
|
rest.PostProcessResponse(w, cliCtx, types.BaseAccount{})
|
|
return
|
|
}
|
|
|
|
account, height, err := accGetter.GetAccountWithHeight(addr)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
rest.PostProcessResponse(w, cliCtx, account)
|
|
}
|
|
}
|
|
|
|
// QueryTxsByEventsRequestHandlerFn implements a REST handler that searches for
|
|
// transactions by events.
|
|
func QueryTxsByEventsRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var (
|
|
tags []string
|
|
txs []sdk.TxResponse
|
|
page, limit int
|
|
)
|
|
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest,
|
|
sdk.AppendMsgToErr("could not parse query parameters", err.Error()))
|
|
return
|
|
}
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
if len(r.Form) == 0 {
|
|
rest.PostProcessResponse(w, cliCtx, txs)
|
|
return
|
|
}
|
|
|
|
tags, page, limit, err = rest.ParseHTTPArgs(r)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
searchResult, err := utils.QueryTxsByEvents(cliCtx, tags, page, limit)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, searchResult)
|
|
}
|
|
}
|
|
|
|
// QueryTxRequestHandlerFn implements a REST handler that queries a transaction
|
|
// by hash in a committed block.
|
|
func QueryTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
hashHexStr := vars["hash"]
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
output, err := utils.QueryTx(cliCtx, hashHexStr)
|
|
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))
|
|
}
|
|
|
|
rest.PostProcessResponse(w, cliCtx, output)
|
|
}
|
|
}
|