Skip to content

Commit

Permalink
Move /bank/balances/{address} REST endpoint into /x/bank (#4570)
Browse files Browse the repository at this point in the history
Closes: #4560
  • Loading branch information
Alessio Treglia authored Jun 18, 2019
1 parent a797495 commit 1e7c4dd
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 140 deletions.
1 change: 1 addition & 0 deletions .pending/features/sdk/4570-Move-bank-balan
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4570 Move /bank/balances/{address} REST handler to x/bank/client/rest. The exposed interface is unchanged.
2 changes: 0 additions & 2 deletions x/auth/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ var (
NewParams = types.NewParams
ParamKeyTable = types.ParamKeyTable
DefaultParams = types.DefaultParams
NewQueryAccountParams = types.NewQueryAccountParams
NewStdTx = types.NewStdTx
CountSubKeys = types.CountSubKeys
NewStdFee = types.NewStdFee
Expand Down Expand Up @@ -75,7 +74,6 @@ type (
FeeCollectionKeeper = types.FeeCollectionKeeper
GenesisState = types.GenesisState
Params = types.Params
QueryAccountParams = types.QueryAccountParams
StdSignMsg = types.StdSignMsg
StdTx = types.StdTx
StdFee = types.StdFee
Expand Down
44 changes: 0 additions & 44 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,50 +64,6 @@ func QueryAccountRequestHandlerFn(
}
}

// query accountREST Handler
func QueryBalancesRequestHandlerFn(
storeName string, decoder types.AccountDecoder, cliCtx context.CLIContext,
) 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 err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

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

res, _, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

// the query will return empty if there is no data for this account
if len(res) == 0 {
rest.PostProcessResponse(w, cliCtx, sdk.Coins{})
return
}

// decode the value
account, err := decoder(res)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

rest.PostProcessResponse(w, cliCtx, account.GetCoins())
}
}

// QueryTxsByTagsRequestHandlerFn implements a REST handler that searches for
// transactions by tags.
func QueryTxsByTagsRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
Expand Down
6 changes: 0 additions & 6 deletions x/auth/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string)
"/auth/accounts/{address}",
QueryAccountRequestHandlerFn(storeName, context.GetAccountDecoder(cliCtx.Codec), cliCtx),
).Methods("GET")

// TODO: Change path or mount under x/bank if possible
r.HandleFunc(
"/bank/balances/{address}",
QueryBalancesRequestHandlerFn(storeName, context.GetAccountDecoder(cliCtx.Codec), cliCtx),
).Methods("GET")
}

// RegisterTxRoutes registers all transaction routes on the provided router.
Expand Down
8 changes: 5 additions & 3 deletions x/auth/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/x/auth/types"
)

func Test_queryAccount(t *testing.T) {
Expand All @@ -20,13 +22,13 @@ func Test_queryAccount(t *testing.T) {
require.NotNil(t, err)
require.Nil(t, res)

req.Data = input.cdc.MustMarshalJSON(NewQueryAccountParams([]byte("")))
req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams([]byte("")))
res, err = queryAccount(input.ctx, req, input.ak)
require.NotNil(t, err)
require.Nil(t, res)

_, _, addr := types.KeyTestPubAddr()
req.Data = input.cdc.MustMarshalJSON(NewQueryAccountParams(addr))
req.Data = input.cdc.MustMarshalJSON(types.NewQueryAccountParams(addr))
res, err = queryAccount(input.ctx, req, input.ak)
require.NotNil(t, err)
require.Nil(t, res)
Expand Down
7 changes: 3 additions & 4 deletions x/auth/types/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ const (
QueryAccount = "account"
)

// defines the params for query: "custom/acc/account"
// QueryAccountParams defines the params for querying accounts.
type QueryAccountParams struct {
Address sdk.AccAddress
}

// NewQueryAccountParams creates a new instance of QueryAccountParams.
func NewQueryAccountParams(addr sdk.AccAddress) QueryAccountParams {
return QueryAccountParams{
Address: addr,
}
return QueryAccountParams{Address: addr}
}
1 change: 1 addition & 0 deletions x/bank/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
CodeInvalidInputsOutputs = types.CodeInvalidInputsOutputs
ModuleName = types.ModuleName
RouterKey = types.RouterKey
QuerierRoute = types.QuerierRoute
DefaultParamspace = types.DefaultParamspace
)

Expand Down
54 changes: 54 additions & 0 deletions x/bank/client/rest/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package rest

import (
"net/http"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/internal/types"
)

// query accountREST Handler
func QueryBalancesRequestHandlerFn(cliCtx context.CLIContext) 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 err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

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

params := types.NewQueryBalanceParams(addr)
bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, _, err := cliCtx.QueryWithData("custom/bank/balances", bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

// the query will return empty if there is no data for this account
if len(res) == 0 {
rest.PostProcessResponse(w, cliCtx, sdk.Coins{})
return
}

rest.PostProcessResponse(w, cliCtx, res)
}
}
8 changes: 1 addition & 7 deletions x/bank/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"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/auth/client/utils"
Expand All @@ -17,6 +16,7 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/bank/accounts/{address}/transfers", SendRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/bank/balances/{address}", QueryBalancesRequestHandlerFn(cliCtx)).Methods("GET")
}

// SendReq defines the properties of a send request's body.
Expand All @@ -25,12 +25,6 @@ type SendReq struct {
Amount sdk.Coins `json:"amount"`
}

var moduleCdc = codec.New()

func init() {
types.RegisterCodec(moduleCdc)
}

// SendRequestHandlerFn - http request handler to send coins to a address.
func SendRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading

0 comments on commit 1e7c4dd

Please sign in to comment.