github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/client/rest/broadcast.go (about)

     1  package rest
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    10  )
    11  
    12  // BroadcastReq defines a tx broadcasting request.
    13  type BroadcastReq struct {
    14  	Tx   types.StdTx `json:"tx" yaml:"tx"`
    15  	Mode string      `json:"mode" yaml:"mode"`
    16  }
    17  
    18  // BroadcastTxRequest implements a tx broadcasting handler that is responsible
    19  // for broadcasting a valid and signed tx to a full node. The tx can be
    20  // broadcasted via a sync|async|block mechanism.
    21  func BroadcastTxRequest(cliCtx context.CLIContext) http.HandlerFunc {
    22  	return func(w http.ResponseWriter, r *http.Request) {
    23  		var req BroadcastReq
    24  
    25  		body, err := ioutil.ReadAll(r.Body)
    26  		if err != nil {
    27  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    28  			return
    29  		}
    30  
    31  		err = cliCtx.Codec.UnmarshalJSON(body, &req)
    32  		if err != nil {
    33  			rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    34  			return
    35  		}
    36  
    37  		txBytes, err := cliCtx.Codec.MarshalBinaryLengthPrefixed(req.Tx)
    38  		if err != nil {
    39  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    40  			return
    41  		}
    42  
    43  		cliCtx = cliCtx.WithBroadcastMode(req.Mode)
    44  
    45  		res, err := cliCtx.BroadcastTx(txBytes)
    46  		if err != nil {
    47  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    48  			return
    49  		}
    50  
    51  		rest.PostProcessResponseBare(w, cliCtx, res)
    52  	}
    53  }