Skip to content

Commit

Permalink
Wire up bank tx REST routes
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Apr 28, 2020
1 parent 026a8ea commit d0cd45a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 27 deletions.
7 changes: 3 additions & 4 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ func BroadcastTx(ctx context.CLIContext, txf Factory, msgs ...sdk.Msg) error {
// WriteGeneratedTxResponse writes a generated unsigned transaction to the
// provided http.ResponseWriter. It will simulate gas costs if requested by the
// BaseReq. Upon any error, the error will be written to the http.ResponseWriter.
func WriteGeneratedTxResponse(
ctx context.CLIContext, w http.ResponseWriter, txg context.TxGenerator, br rest.BaseReq, msgs ...sdk.Msg,
) {
func WriteGeneratedTxResponse(ctx context.CLIContext, w http.ResponseWriter, br rest.BaseReq, msgs ...sdk.Msg) {

gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
if !ok {
Expand All @@ -145,7 +143,8 @@ func WriteGeneratedTxResponse(
WithGasAdjustment(gasAdj).
WithMemo(br.Memo).
WithChainID(br.ChainID).
WithSimulateAndExecute(br.Simulate)
WithSimulateAndExecute(br.Simulate).
WithTxGenerator(ctx.TxGenerator)

if br.Simulate || simAndExec {
if gasAdj < 0 {
Expand Down
5 changes: 4 additions & 1 deletion types/module/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package module

import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/gorilla/mux"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/context"
)

type ClientModule interface {
NewTxCmd(ctx context.CLIContext) *cobra.Command
NewQueryCmd(ctx context.CLIContext) *cobra.Command
NewRESTRoutes(ctx context.CLIContext, rtr *mux.Router)
}
5 changes: 2 additions & 3 deletions x/bank/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
)

// RegisterHandlers registers all x/bank transaction and query HTTP REST handlers
// on the provided mux router.
func RegisterHandlers(ctx context.CLIContext, m codec.Marshaler, txg context.TxGenerator, r *mux.Router) {
r.HandleFunc("/bank/accounts/{address}/transfers", NewSendRequestHandlerFn(ctx, m, txg)).Methods("POST")
func RegisterHandlers(ctx context.CLIContext, r *mux.Router) {
r.HandleFunc("/bank/accounts/{address}/transfers", NewSendRequestHandlerFn(ctx)).Methods("POST")
r.HandleFunc("/bank/balances/{address}", QueryBalancesRequestHandlerFn(ctx)).Methods("GET")
}

Expand Down
7 changes: 2 additions & 5 deletions x/bank/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
Expand All @@ -22,10 +21,8 @@ type SendReq struct {

// NewSendRequestHandlerFn returns an HTTP REST handler for creating a MsgSend
// transaction.
func NewSendRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg context.TxGenerator) http.HandlerFunc {
func NewSendRequestHandlerFn(ctx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx = ctx.WithMarshaler(m)

vars := mux.Vars(r)
bech32Addr := vars["address"]

Expand All @@ -50,7 +47,7 @@ func NewSendRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg cont
}

msg := types.NewMsgSend(fromAddr, toAddr, req.Amount)
tx.WriteGeneratedTxResponse(ctx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(ctx, w, req.BaseReq, msg)
}
}

Expand Down
4 changes: 4 additions & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,7 @@ func (am AppModuleBasic) NewTxCmd(ctx context.CLIContext) *cobra.Command {
func (am AppModuleBasic) NewQueryCmd(ctx context.CLIContext) *cobra.Command {
return nil
}

func (am AppModuleBasic) NewRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
rest.RegisterHandlers(ctx, rtr)
}
10 changes: 5 additions & 5 deletions x/distribution/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func newWithdrawDelegatorRewardsHandlerFn(cliCtx context.CLIContext, m codec.Mar
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msgs...)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msgs...)
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ func newWithdrawDelegationRewardsHandlerFn(cliCtx context.CLIContext, m codec.Ma
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func newSetDelegatorWithdrawalAddrHandlerFn(cliCtx context.CLIContext, m codec.M
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ func newWithdrawValidatorRewardsHandlerFn(cliCtx context.CLIContext, m codec.Mar
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msgs...)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msgs...)
}
}

Expand All @@ -204,7 +204,7 @@ func newFundCommunityPoolHandlerFn(cliCtx context.CLIContext, m codec.Marshaler,
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/gov/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newPostProposalHandlerFn(cliCtx context.CLIContext, txg context.TxGenerator
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ func newDepositHandlerFn(cliCtx context.CLIContext, txg context.TxGenerator) htt
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func newVoteHandlerFn(cliCtx context.CLIContext, txg context.TxGenerator) http.H
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/slashing/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewUnjailRequestHandlerFn(ctx context.CLIContext, m codec.Marshaler, txg co
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
tx.WriteGeneratedTxResponse(ctx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(ctx, w, req.BaseReq, msg)
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/staking/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func newPostDelegationsHandlerFn(cliCtx context.CLIContext, m codec.Marshaler, t
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ func newPostRedelegationsHandlerFn(cliCtx context.CLIContext, m codec.Marshaler,
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ func newPostUnbondingDelegationsHandlerFn(cliCtx context.CLIContext, m codec.Mar
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/upgrade/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func newPostPlanHandler(cliCtx context.CLIContext, txg context.TxGenerator, newM
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down Expand Up @@ -137,7 +137,7 @@ func newCancelPlanHandler(cliCtx context.CLIContext, txg context.TxGenerator, ne
return
}

tx.WriteGeneratedTxResponse(cliCtx, w, txg, req.BaseReq, msg)
tx.WriteGeneratedTxResponse(cliCtx, w, req.BaseReq, msg)
}
}

Expand Down

0 comments on commit d0cd45a

Please sign in to comment.