2020-01-24 08:40:56 -08:00
|
|
|
package client
|
2018-08-22 04:38:55 -07:00
|
|
|
|
|
|
|
import (
|
2019-03-19 09:52:43 -07:00
|
|
|
"log"
|
2018-08-22 04:38:55 -07:00
|
|
|
"net/http"
|
2018-09-02 11:20:14 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-05-28 01:44:04 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2018-09-02 11:20:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-15 14:55:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/errors"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2019-06-15 05:34:11 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-08-22 04:38:55 -07:00
|
|
|
)
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// WriteGenerateStdTxResponse writes response for the generate only mode.
|
2020-06-01 05:46:03 -07:00
|
|
|
func WriteGenerateStdTxResponse(w http.ResponseWriter, clientCtx client.Context, br rest.BaseReq, msgs []sdk.Msg) {
|
2019-05-28 01:44:04 -07:00
|
|
|
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
|
2019-01-29 11:22:47 -08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:44:04 -07:00
|
|
|
simAndExec, gas, err := flags.ParseGas(br.Gas)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-15 05:34:11 -07:00
|
|
|
txBldr := types.NewTxBuilder(
|
2020-06-01 05:46:03 -07:00
|
|
|
GetTxEncoder(clientCtx.Codec), br.AccountNumber, br.Sequence, gas, gasAdj,
|
2019-01-29 11:22:47 -08:00
|
|
|
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
|
|
|
|
)
|
|
|
|
|
2019-02-19 10:15:39 -08:00
|
|
|
if br.Simulate || simAndExec {
|
2019-02-01 17:04:13 -08:00
|
|
|
if gasAdj < 0 {
|
2020-04-15 14:55:02 -07:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, errors.ErrorInvalidGasAdjustment.Error())
|
2019-02-01 17:04:13 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
txBldr, err = EnrichWithGas(txBldr, clientCtx, msgs)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-02-01 17:04:13 -08:00
|
|
|
return
|
|
|
|
}
|
2019-02-19 10:15:39 -08:00
|
|
|
|
|
|
|
if br.Simulate {
|
2020-06-01 05:46:03 -07:00
|
|
|
rest.WriteSimulationResponse(w, clientCtx.Codec, txBldr.Gas())
|
2019-02-19 10:15:39 -08:00
|
|
|
return
|
|
|
|
}
|
2019-02-01 17:04:13 -08:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
stdMsg, err := txBldr.BuildSignMsg(msgs)
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckBadRequestError(w, err) {
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:20:50 -07:00
|
|
|
output, err := clientCtx.JSONMarshaler.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
|
2020-04-01 00:50:22 -07:00
|
|
|
if rest.CheckInternalServerError(w, err) {
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-03 20:52:08 -08:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2019-03-19 09:52:43 -07:00
|
|
|
if _, err := w.Write(output); err != nil {
|
|
|
|
log.Printf("could not write response: %v", err)
|
|
|
|
}
|
2019-06-06 08:43:15 -07:00
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
}
|