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

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/x/common"
     9  
    10  	"github.com/gorilla/mux"
    11  
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    13  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    14  	"github.com/fibonacci-chain/fbc/x/farm/types"
    15  )
    16  
    17  func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
    18  	// get the current state of the all farm pools
    19  	r.HandleFunc(
    20  		"/farm/pools",
    21  		queryPoolsHandlerFn(cliCtx),
    22  	).Methods("GET")
    23  
    24  	// get a single pool info by the farm pool's name
    25  	r.HandleFunc(
    26  		"/farm/pool/{poolName}",
    27  		queryPoolHandlerFn(cliCtx),
    28  	).Methods("GET")
    29  
    30  	// get the current earnings of an account in a farm pool
    31  	r.HandleFunc(
    32  		"/farm/earnings/{poolName}/{accAddr}",
    33  		queryEarningsHandlerFn(cliCtx),
    34  	).Methods("GET")
    35  
    36  	// get the white list info
    37  	r.HandleFunc(
    38  		"/farm/whitelist",
    39  		queryWhitelistHandlerFn(cliCtx),
    40  	).Methods("GET")
    41  
    42  	// get the current farm parameter values
    43  	r.HandleFunc(
    44  		"/farm/parameters",
    45  		queryParamsHandlerFn(cliCtx),
    46  	).Methods("GET")
    47  
    48  	// get the names of all farm pools that the account has locked coins in
    49  	r.HandleFunc(
    50  		"/farm/account/{accAddr}",
    51  		queryAccountHandlerFn(cliCtx),
    52  	).Methods("GET")
    53  }
    54  
    55  func queryAccountHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    56  	return func(w http.ResponseWriter, r *http.Request) {
    57  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    58  		if !ok {
    59  			return
    60  		}
    61  
    62  		accAddr, err := sdk.AccAddressFromBech32(mux.Vars(r)["accAddr"])
    63  		if err != nil {
    64  			common.HandleErrorMsg(w, cliCtx, common.CodeCreateAddrFromBech32Failed, err.Error())
    65  		}
    66  
    67  		jsonBytes, err := cliCtx.Codec.MarshalJSON(types.NewQueryAccountParams(accAddr))
    68  		if err != nil {
    69  			common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorCodecFails)
    70  			return
    71  		}
    72  
    73  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount)
    74  		res, height, err := cliCtx.QueryWithData(route, jsonBytes)
    75  		if err != nil {
    76  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
    77  			return
    78  		}
    79  
    80  		cliCtx = cliCtx.WithHeight(height)
    81  		rest.PostProcessResponse(w, cliCtx, res)
    82  	}
    83  }
    84  
    85  func queryEarningsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
    86  	return func(w http.ResponseWriter, r *http.Request) {
    87  		varsMap := mux.Vars(r)
    88  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
    89  		if !ok {
    90  			return
    91  		}
    92  
    93  		accAddr, err := sdk.AccAddressFromBech32(varsMap["accAddr"])
    94  		if err != nil {
    95  			common.HandleErrorMsg(w, cliCtx, common.CodeCreateAddrFromBech32Failed, err.Error())
    96  		}
    97  
    98  		jsonBytes, err := cliCtx.Codec.MarshalJSON(types.NewQueryPoolAccountParams(varsMap["poolName"], accAddr))
    99  		if err != nil {
   100  			common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorCodecFails)
   101  			return
   102  		}
   103  
   104  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryEarnings)
   105  		res, height, err := cliCtx.QueryWithData(route, jsonBytes)
   106  		if err != nil {
   107  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   108  			return
   109  		}
   110  
   111  		cliCtx = cliCtx.WithHeight(height)
   112  		rest.PostProcessResponse(w, cliCtx, res)
   113  	}
   114  }
   115  
   116  func queryPoolHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   117  	return func(w http.ResponseWriter, r *http.Request) {
   118  		poolName := mux.Vars(r)["poolName"]
   119  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   120  		if !ok {
   121  			return
   122  		}
   123  
   124  		params := types.NewQueryPoolParams(poolName)
   125  
   126  		jsonBytes, err := cliCtx.Codec.MarshalJSON(params)
   127  		if err != nil {
   128  			common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorCodecFails)
   129  			return
   130  		}
   131  
   132  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryPool)
   133  		res, height, err := cliCtx.QueryWithData(route, jsonBytes)
   134  		if err != nil {
   135  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   136  			return
   137  		}
   138  
   139  		cliCtx = cliCtx.WithHeight(height)
   140  		rest.PostProcessResponse(w, cliCtx, res)
   141  	}
   142  }
   143  
   144  func queryPoolsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   145  	return func(w http.ResponseWriter, r *http.Request) {
   146  		_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
   147  		if err != nil {
   148  			common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorArgsWithLimit)
   149  			return
   150  		}
   151  
   152  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   153  		if !ok {
   154  			return
   155  		}
   156  
   157  		params := types.NewQueryPoolsParams(page, limit)
   158  		jsonBytes, err := cliCtx.Codec.MarshalJSON(params)
   159  		if err != nil {
   160  			common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorCodecFails)
   161  			return
   162  		}
   163  
   164  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryPools)
   165  		res, height, err := cliCtx.QueryWithData(route, jsonBytes)
   166  		if err != nil {
   167  			common.HandleErrorResponseV2(w, http.StatusInternalServerError, common.ErrorABCIQueryFails)
   168  			return
   169  		}
   170  
   171  		cliCtx = cliCtx.WithHeight(height)
   172  		rest.PostProcessResponse(w, cliCtx, res)
   173  	}
   174  }
   175  
   176  func queryWhitelistHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   177  	return func(w http.ResponseWriter, r *http.Request) {
   178  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   179  		if !ok {
   180  			return
   181  		}
   182  
   183  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryWhitelist)
   184  		res, height, err := cliCtx.QueryWithData(route, nil)
   185  		if err != nil {
   186  			sdkErr := common.ParseSDKError(err.Error())
   187  			common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   188  			return
   189  		}
   190  
   191  		cliCtx = cliCtx.WithHeight(height)
   192  		rest.PostProcessResponse(w, cliCtx, res)
   193  	}
   194  }
   195  
   196  func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
   197  	return func(w http.ResponseWriter, r *http.Request) {
   198  		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
   199  		if !ok {
   200  			return
   201  		}
   202  
   203  		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters)
   204  		res, height, err := cliCtx.QueryWithData(route, nil)
   205  		if err != nil {
   206  			sdkErr := common.ParseSDKError(err.Error())
   207  			common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
   208  			return
   209  		}
   210  
   211  		cliCtx = cliCtx.WithHeight(height)
   212  		rest.PostProcessResponse(w, cliCtx, res)
   213  	}
   214  }