* Add pagination types and helper func * Update API * Add pagination to queryBalance * Add QueryBalance to use Paginate * Update GetAllBalances usage * Add tests for QueryAllBalances * Fix bank get balance querier tests * Add pagination test setup * revert simapp changes * Fix pagenation tests * Add more tests for pagination * Add offset nullable * Add more tests * Fix paginate for offset * Add grpc queryconn for query * Fix paginate * Fix maxlimit * Fix queryClient * Fix pagination tests * refacor * Fix grpc tests * Revert * review changes * Fix lint * Update types/query/pagination.go * Fix review suggestions * Remove maxLimit and use defaultLimit * change example paginate as a testable fun * Fix review comments * Merge master * Add bank query.pb.go * Add missing import * Add pageReq to queryBalance * Add example for pagenate * Fix cli tests * Remove example for pagination * Update paginate example Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
130 lines
3.3 KiB
Go
130 lines
3.3 KiB
Go
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
|
)
|
|
|
|
// QueryBalancesRequestHandlerFn returns a REST handler that queries for all
|
|
// account balances or a specific balance by denomination.
|
|
func QueryBalancesRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
bech32addr := vars["address"]
|
|
|
|
addr, err := sdk.AccAddressFromBech32(bech32addr)
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
ctx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
var (
|
|
params interface{}
|
|
route string
|
|
)
|
|
|
|
// TODO: Remove once client-side Protobuf migration has been completed.
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
|
|
var marshaler codec.JSONMarshaler
|
|
|
|
if ctx.JSONMarshaler != nil {
|
|
marshaler = ctx.JSONMarshaler
|
|
} else {
|
|
marshaler = ctx.Codec
|
|
}
|
|
|
|
denom := r.FormValue("denom")
|
|
if denom == "" {
|
|
params = types.NewQueryAllBalancesRequest(addr, nil)
|
|
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllBalances)
|
|
} else {
|
|
params = types.NewQueryBalanceRequest(addr, denom)
|
|
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)
|
|
}
|
|
|
|
bz, err := marshaler.MarshalJSON(params)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
res, height, err := ctx.QueryWithData(route, bz)
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
ctx = ctx.WithHeight(height)
|
|
rest.PostProcessResponse(w, ctx, res)
|
|
}
|
|
}
|
|
|
|
// HTTP request handler to query the total supply of coins
|
|
func totalSupplyHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
params := types.NewQueryTotalSupplyParams(page, limit)
|
|
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
|
|
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
rest.PostProcessResponse(w, clientCtx, res)
|
|
}
|
|
}
|
|
|
|
// HTTP request handler to query the supply of a single denom
|
|
func supplyOfHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
denom := mux.Vars(r)["denom"]
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
params := types.NewQuerySupplyOfParams(denom)
|
|
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
return
|
|
}
|
|
|
|
res, height, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
|
|
if rest.CheckInternalServerError(w, err) {
|
|
return
|
|
}
|
|
|
|
clientCtx = clientCtx.WithHeight(height)
|
|
rest.PostProcessResponse(w, clientCtx, res)
|
|
}
|
|
}
|