2019-06-18 06:44:09 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2020-01-30 13:31:16 -08:00
|
|
|
"fmt"
|
2019-06-18 06:44:09 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2020-03-25 10:22:29 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-06-18 06:44:09 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-06-18 06:44:09 -07:00
|
|
|
)
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
// QueryBalancesRequestHandlerFn returns a REST handler that queries for all
|
|
|
|
// account balances or a specific balance by denomination.
|
2020-03-25 10:22:29 -07:00
|
|
|
func QueryBalancesRequestHandlerFn(ctx context.CLIContext) http.HandlerFunc {
|
2019-06-18 06:44:09 -07:00
|
|
|
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)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-06-18 06:44:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:22:29 -07:00
|
|
|
ctx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, ctx, r)
|
2019-06-18 06:44:09 -07:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
var (
|
|
|
|
params interface{}
|
|
|
|
route string
|
|
|
|
)
|
|
|
|
|
2020-03-25 10:22:29 -07:00
|
|
|
// 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.Marshaler != nil {
|
|
|
|
marshaler = ctx.Marshaler
|
|
|
|
} else {
|
|
|
|
marshaler = ctx.Codec
|
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
denom := r.FormValue("denom")
|
|
|
|
if denom == "" {
|
|
|
|
params = types.NewQueryAllBalancesParams(addr)
|
|
|
|
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllBalances)
|
|
|
|
} else {
|
|
|
|
params = types.NewQueryBalanceParams(addr, denom)
|
|
|
|
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:22:29 -07:00
|
|
|
bz, err := marshaler.MarshalJSON(params)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-06-18 06:44:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:22:29 -07:00
|
|
|
res, height, err := ctx.QueryWithData(route, bz)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-06-18 06:44:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-25 10:22:29 -07:00
|
|
|
ctx = ctx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, ctx, res)
|
2019-06-18 06:44:09 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
// HTTP request handler to query the total supply of coins
|
|
|
|
func totalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
params := types.NewQueryTotalSupplyParams(page, limit)
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
|
|
|
|
if rest.CheckInternalServerError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP request handler to query the supply of a single denom
|
|
|
|
func supplyOfHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
denom := mux.Vars(r)["denom"]
|
|
|
|
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
params := types.NewQuerySupplyOfParams(denom)
|
|
|
|
bz, err := cliCtx.Codec.MarshalJSON(params)
|
|
|
|
if rest.CheckBadRequestError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
|
|
|
|
if rest.CheckInternalServerError(w, err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithHeight(height)
|
|
|
|
rest.PostProcessResponse(w, cliCtx, res)
|
|
|
|
}
|
|
|
|
}
|