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-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-08-26 02:39:38 -07:00
|
|
|
if !rest.ReadRESTReq(w, r, clientCtx.LegacyAmino, &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
|
|
|
}
|
|
|
|
}
|