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

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/gorilla/mux"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/supply/internal/types"
    12  )
    13  
    14  // RegisterRoutes registers staking-related REST handlers to a router
    15  func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
    16  	registerQueryRoutes(cliCtx, r)
    17  }
    18  
    19  func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
    20  	// Query the total supply of coins
    21  	r.HandleFunc(
    22  		"/supply/total",
    23  		totalSupplyHandlerFn(cliCtx),
    24  	).Methods("GET")
    25  
    26  	// Query the supply of a single denom
    27  	r.HandleFunc(
    28  		"/supply/total/{denom}",
    29  		supplyOfHandlerFn(cliCtx),
    30  	).Methods("GET")
    31  }
    32  
    33  // HTTP request handler to query the total supply of coins
    34  func totalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    35  	return func(w http.ResponseWriter, r *http.Request) {
    36  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
    37  		if err != nil {
    38  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    39  			return
    40  		}
    41  
    42  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    43  		if !ok {
    44  			return
    45  		}
    46  
    47  		params := types.NewQueryTotalSupplyParams(page, limit)
    48  		bz, err := cliCtx.Codec.MarshalJSON(params)
    49  		if err != nil {
    50  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    51  			return
    52  		}
    53  
    54  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
    55  		if err != nil {
    56  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    57  			return
    58  		}
    59  
    60  		cliCtx = cliCtx.WithHeight(height)
    61  		rest.PostProcessResponse(w, cliCtx, res)
    62  	}
    63  }
    64  
    65  // HTTP request handler to query the supply of a single denom
    66  func supplyOfHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    67  	return func(w http.ResponseWriter, r *http.Request) {
    68  		denom := mux.Vars(r)["denom"]
    69  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    70  		if !ok {
    71  			return
    72  		}
    73  
    74  		params := types.NewQuerySupplyOfParams(denom)
    75  		bz, err := cliCtx.Codec.MarshalJSON(params)
    76  		if err != nil {
    77  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    78  			return
    79  		}
    80  
    81  		res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
    82  		if err != nil {
    83  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    84  			return
    85  		}
    86  
    87  		cliCtx = cliCtx.WithHeight(height)
    88  		rest.PostProcessResponse(w, cliCtx, res)
    89  	}
    90  }