Skip to content

Commit

Permalink
Add query REST endpoint for buyback module balance
Browse files Browse the repository at this point in the history
  • Loading branch information
haasted committed Oct 20, 2020
1 parent 876c062 commit 51d610e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
37 changes: 37 additions & 0 deletions x/buyback/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package rest

import (
"fmt"
"net/http"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"

"github.com/e-money/em-ledger/x/buyback/internal/types"
)

func RegisterQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/buyback/balance", queryBalanceHandlerFn(cliCtx)).Methods("GET")
}

func queryBalanceHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
5 changes: 4 additions & 1 deletion x/buyback/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/e-money/em-ledger/x/buyback/client/cli"
"github.com/e-money/em-ledger/x/buyback/client/rest"
"github.com/e-money/em-ledger/x/buyback/internal/keeper"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +50,9 @@ func (amb AppModuleBasic) ValidateGenesis(json.RawMessage) error {
return nil
}

func (amb AppModuleBasic) RegisterRESTRoutes(context.CLIContext, *mux.Router) {}
func (amb AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, r *mux.Router) {
rest.RegisterQueryRoutes(ctx, r)
}

func (amb AppModuleBasic) GetTxCmd(*codec.Codec) *cobra.Command {
return nil
Expand Down

0 comments on commit 51d610e

Please sign in to comment.