2019-06-15 05:34:11 -07:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BroadcastReq defines a tx broadcasting request.
|
|
|
|
type BroadcastReq struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
Tx types.StdTx `json:"tx" yaml:"tx"`
|
|
|
|
Mode string `json:"mode" yaml:"mode"`
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BroadcastTxRequest implements a tx broadcasting handler that is responsible
|
|
|
|
// for broadcasting a valid and signed tx to a full node. The tx can be
|
|
|
|
// broadcasted via a sync|async|block mechanism.
|
|
|
|
func BroadcastTxRequest(cliCtx context.CLIContext) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req BroadcastReq
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cliCtx.Codec.UnmarshalJSON(body, &req)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithBroadcastMode(req.Mode)
|
|
|
|
|
|
|
|
res, err := cliCtx.BroadcastTx(txBytes)
|
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-03 06:56:15 -07:00
|
|
|
rest.PostProcessResponseBare(w, cliCtx, res)
|
2019-06-15 05:34:11 -07:00
|
|
|
}
|
|
|
|
}
|