2018-03-10 09:33:05 -08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/gorilla/mux"
|
2019-02-04 07:48:26 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-03-25 11:23:34 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2018-03-10 09:33:05 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-01-24 08:40:56 -08:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2018-03-10 09:33:05 -08:00
|
|
|
)
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
// SendReq defines the properties of a send request's body.
|
|
|
|
type SendReq struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
|
|
|
|
Amount sdk.Coins `json:"amount" yaml:"amount"`
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// NewSendRequestHandlerFn returns an HTTP REST handler for creating a MsgSend
|
|
|
|
// transaction.
|
2020-06-01 05:46:03 -07:00
|
|
|
func NewSendRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2020-03-25 11:23:34 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bech32Addr := vars["address"]
|
|
|
|
|
|
|
|
toAddr, err := sdk.AccAddressFromBech32(bech32Addr)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req SendReq
|
2020-06-01 05:46:03 -07:00
|
|
|
if !rest.ReadRESTReq(w, r, clientCtx.JSONMarshaler, &req) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.BaseReq = req.BaseReq.Sanitize()
|
|
|
|
if !req.BaseReq.ValidateBasic(w) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2020-03-25 11:23:34 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := types.NewMsgSend(fromAddr, toAddr, req.Amount)
|
2020-06-01 05:46:03 -07:00
|
|
|
tx.WriteGeneratedTxResponse(clientCtx, w, req.BaseReq, msg)
|
2020-03-25 11:23:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Deprecated
|
|
|
|
//
|
|
|
|
// TODO: Remove once client-side Protobuf migration has been completed.
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-09-26 06:29:39 -07:00
|
|
|
// SendRequestHandlerFn - http request handler to send coins to a address.
|
2020-03-25 11:23:34 -07:00
|
|
|
//
|
|
|
|
// TODO: Remove once client-side Protobuf migration has been completed.
|
|
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
|
2020-06-01 05:46:03 -07:00
|
|
|
func SendRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2018-03-10 09:33:05 -08:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2018-09-26 06:29:39 -07:00
|
|
|
bech32Addr := vars["address"]
|
2018-06-05 21:53:04 -07:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
toAddr, err := sdk.AccAddressFromBech32(bech32Addr)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2018-06-05 21:53:04 -07:00
|
|
|
return
|
|
|
|
}
|
2018-03-10 09:33:05 -08:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
var req SendReq
|
2020-06-01 05:46:03 -07:00
|
|
|
if !rest.ReadRESTReq(w, r, clientCtx.Codec, &req) {
|
2018-08-31 10:04:42 -07:00
|
|
|
return
|
|
|
|
}
|
2018-08-22 04:38:55 -07:00
|
|
|
|
2018-12-19 16:26:33 -08:00
|
|
|
req.BaseReq = req.BaseReq.Sanitize()
|
|
|
|
if !req.BaseReq.ValidateBasic(w) {
|
2018-09-02 11:20:14 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-19 10:15:39 -08:00
|
|
|
fromAddr, err := sdk.AccAddressFromBech32(req.BaseReq.From)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
msg := types.NewMsgSend(fromAddr, toAddr, req.Amount)
|
2020-06-01 05:46:03 -07:00
|
|
|
authclient.WriteGenerateStdTxResponse(w, clientCtx, req.BaseReq, []sdk.Msg{msg})
|
2018-03-10 09:33:05 -08:00
|
|
|
}
|
|
|
|
}
|