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-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"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
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"
|
2019-06-15 05:34:11 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-03-10 09:33:05 -08:00
|
|
|
)
|
|
|
|
|
2019-06-14 02:52:28 -07:00
|
|
|
// AccountWithHeight wraps the embedded Account with the height it was queried
|
|
|
|
// at.
|
2019-06-13 06:54:17 -07:00
|
|
|
type AccountWithHeight struct {
|
|
|
|
types.Account
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
}
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// query accountREST Handler
|
2018-08-06 11:11:30 -07:00
|
|
|
func QueryAccountRequestHandlerFn(
|
2019-06-06 08:43:15 -07:00
|
|
|
storeName string, decoder types.AccountDecoder, cliCtx context.CLIContext,
|
2018-08-06 11:11:30 -07:00
|
|
|
) http.HandlerFunc {
|
2019-06-06 08:43:15 -07:00
|
|
|
|
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)
|
2018-03-10 09:33:05 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-07 06:02:07 -07:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
res, height, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
|
2018-03-10 09:33:05 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-10 10:07:21 -07:00
|
|
|
// the query will return empty account if there is no data
|
2018-03-14 03:43:31 -07:00
|
|
|
if len(res) == 0 {
|
2019-06-06 08:43:15 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, types.BaseAccount{})
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:27:50 -07:00
|
|
|
// decode the value
|
2018-04-18 21:49:24 -07:00
|
|
|
account, err := decoder(res)
|
2018-03-10 09:33:05 -08:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, AccountWithHeight{account, height})
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
}
|
2018-09-29 20:42:12 -07:00
|
|
|
|
|
|
|
// query accountREST Handler
|
|
|
|
func QueryBalancesRequestHandlerFn(
|
2019-06-06 08:43:15 -07:00
|
|
|
storeName string, decoder types.AccountDecoder, cliCtx context.CLIContext,
|
2018-09-29 20:42:12 -07:00
|
|
|
) http.HandlerFunc {
|
2019-06-06 08:43:15 -07:00
|
|
|
|
2018-09-29 20:42:12 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2018-09-30 03:50:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2018-09-29 20:42:12 -07:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bech32addr := vars["address"]
|
|
|
|
|
|
|
|
addr, err := sdk.AccAddressFromBech32(bech32addr)
|
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-09-29 20:42:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-07 06:02:07 -07:00
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-13 06:54:17 -07:00
|
|
|
res, _, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
|
2018-09-29 20:42:12 -07:00
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-09-29 20:42:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// the query will return empty if there is no data for this account
|
|
|
|
if len(res) == 0 {
|
2019-06-06 08:43:15 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, sdk.Coins{})
|
2018-09-29 20:42:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode the value
|
|
|
|
account, err := decoder(res)
|
|
|
|
if err != nil {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-09-29 20:42:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:43:15 -07:00
|
|
|
rest.PostProcessResponse(w, cliCtx, account.GetCoins())
|
2018-09-29 20:42:12 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 05:34:11 -07:00
|
|
|
|
|
|
|
// QueryTxsByTagsRequestHandlerFn implements a REST handler that searches for
|
|
|
|
// transactions by tags.
|
|
|
|
func QueryTxsByTagsRequestHandlerFn(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.QueryTxsByTags(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)
|
|
|
|
}
|
|
|
|
}
|