github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/client/rest/query.go (about)

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/gorilla/mux"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/client/utils"
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    16  	genutilrest "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/genutil/client/rest"
    17  )
    18  
    19  // query accountREST Handler
    20  func QueryAccountRequestHandlerFn(storeName string, cliCtx context.CLIContext) http.HandlerFunc {
    21  	return func(w http.ResponseWriter, r *http.Request) {
    22  		vars := mux.Vars(r)
    23  		bech32addr := vars["address"]
    24  
    25  		addr, err := sdk.AccAddressFromBech32(bech32addr)
    26  		if err != nil {
    27  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    28  			return
    29  		}
    30  
    31  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    32  		if !ok {
    33  			return
    34  		}
    35  
    36  		accGetter := types.NewAccountRetriever(cliCtx)
    37  
    38  		account, height, err := accGetter.GetAccountWithHeight(addr)
    39  		if err != nil {
    40  			// TODO: Handle more appropriately based on the error type.
    41  			// Ref: https://github.com/cosmos/cosmos-sdk/issues/4923
    42  			if err := accGetter.EnsureExists(addr); err != nil {
    43  				cliCtx = cliCtx.WithHeight(height)
    44  				rest.PostProcessResponse(w, cliCtx, types.BaseAccount{})
    45  				return
    46  			}
    47  
    48  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    49  			return
    50  		}
    51  
    52  		cliCtx = cliCtx.WithHeight(height)
    53  		rest.PostProcessResponse(w, cliCtx, account)
    54  	}
    55  }
    56  
    57  // QueryTxsHandlerFn implements a REST handler that searches for transactions.
    58  // Genesis transactions are returned if the height parameter is set to zero,
    59  // otherwise the transactions are searched for by events.
    60  func QueryTxsRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    61  	return func(w http.ResponseWriter, r *http.Request) {
    62  		err := r.ParseForm()
    63  		if err != nil {
    64  			rest.WriteErrorResponse(
    65  				w, http.StatusBadRequest,
    66  				fmt.Sprintf("failed to parse query parameters: %s", err),
    67  			)
    68  			return
    69  		}
    70  
    71  		// if the height query param is set to zero, query for genesis transactions
    72  		heightStr := r.FormValue("height")
    73  		if heightStr != "" {
    74  			if height, err := strconv.ParseInt(heightStr, 10, 64); err == nil && height == 0 {
    75  				genutilrest.QueryGenesisTxs(cliCtx, w)
    76  				return
    77  			}
    78  		}
    79  
    80  		var (
    81  			events      []string
    82  			txs         []sdk.TxResponse
    83  			page, limit int
    84  		)
    85  
    86  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    87  		if !ok {
    88  			return
    89  		}
    90  
    91  		if len(r.Form) == 0 {
    92  			rest.PostProcessResponseBare(w, cliCtx, txs)
    93  			return
    94  		}
    95  
    96  		events, page, limit, err = rest.ParseHTTPArgs(r)
    97  		if err != nil {
    98  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    99  			return
   100  		}
   101  
   102  		searchResult, err := utils.QueryTxsByEvents(cliCtx, events, page, limit)
   103  		if err != nil {
   104  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   105  			return
   106  		}
   107  
   108  		rest.PostProcessResponseBare(w, cliCtx, searchResult)
   109  	}
   110  }
   111  
   112  // QueryTxRequestHandlerFn implements a REST handler that queries a transaction
   113  // by hash in a committed block.
   114  func QueryTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   115  	return func(w http.ResponseWriter, r *http.Request) {
   116  		vars := mux.Vars(r)
   117  		hashHexStr := vars["hash"]
   118  
   119  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   120  		if !ok {
   121  			return
   122  		}
   123  
   124  		output, err := utils.QueryTx(cliCtx, hashHexStr)
   125  		if err != nil {
   126  			if strings.Contains(err.Error(), hashHexStr) {
   127  				rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
   128  				return
   129  			}
   130  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
   131  			return
   132  		}
   133  
   134  		if output.Empty() {
   135  			rest.WriteErrorResponse(w, http.StatusNotFound, fmt.Sprintf("no transaction found with hash %s", hashHexStr))
   136  		}
   137  
   138  		rest.PostProcessResponseBare(w, cliCtx, output)
   139  	}
   140  }