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

     1  package utils
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/flags"
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/rest"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    12  )
    13  
    14  // WriteGenerateStdTxResponse writes response for the generate only mode.
    15  func WriteGenerateStdTxResponse(w http.ResponseWriter, cliCtx context.CLIContext, br rest.BaseReq, msgs []sdk.Msg) {
    16  	gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
    17  	if !ok {
    18  		return
    19  	}
    20  
    21  	simAndExec, gas, err := flags.ParseGas(br.Gas)
    22  	if err != nil {
    23  		rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    24  		return
    25  	}
    26  
    27  	txBldr := types.NewTxBuilder(
    28  		GetTxEncoder(cliCtx.Codec), br.AccountNumber, br.Sequence, gas, gasAdj,
    29  		br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
    30  	)
    31  
    32  	if br.Simulate || simAndExec {
    33  		if gasAdj < 0 {
    34  			rest.WriteErrorResponse(w, http.StatusBadRequest, errInvalidGasAdjustment.Error())
    35  			return
    36  		}
    37  
    38  		txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs)
    39  		if err != nil {
    40  			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    41  			return
    42  		}
    43  
    44  		if br.Simulate {
    45  			rest.WriteSimulationResponse(w, cliCtx.Codec, txBldr.Gas())
    46  			return
    47  		}
    48  	}
    49  
    50  	stdMsg, err := txBldr.BuildSignMsg(msgs)
    51  	if err != nil {
    52  		rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
    53  		return
    54  	}
    55  
    56  	output, err := cliCtx.Codec.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
    57  	if err != nil {
    58  		rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
    59  		return
    60  	}
    61  
    62  	w.Header().Set("Content-Type", "application/json")
    63  	if _, err := w.Write(output); err != nil {
    64  		log.Printf("could not write response: %v", err)
    65  	}
    66  
    67  }