2019-02-04 07:48:26 -08:00
|
|
|
package rest
|
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
|
|
|
|
2018-10-04 04:00:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-09-26 06:29:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2019-02-04 07:48:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/utils"
|
2018-09-26 06:29:39 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-09-02 11:20:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-10-04 04:00:24 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-09-07 10:04:58 -07:00
|
|
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
2018-08-22 04:38:55 -07:00
|
|
|
)
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-09-26 06:29:39 -07:00
|
|
|
// Building / Sending utilities
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// WriteGenerateStdTxResponse writes response for the generate only mode.
|
2019-02-14 08:53:36 -08:00
|
|
|
func WriteGenerateStdTxResponse(w http.ResponseWriter, cdc *codec.Codec,
|
|
|
|
cliCtx context.CLIContext, br rest.BaseReq, msgs []sdk.Msg) {
|
2019-02-01 17:04:13 -08:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, client.DefaultGasAdjustment)
|
2019-01-29 11:22:47 -08:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:04:13 -08:00
|
|
|
simAndExec, gas, err := client.ParseGas(br.Gas)
|
2019-01-29 11:22:47 -08:00
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
txBldr := authtxb.NewTxBuilder(
|
2019-02-04 07:48:26 -08:00
|
|
|
utils.GetTxEncoder(cdc), 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 {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, client.ErrInvalidGasAdjustment.Error())
|
2019-02-01 17:04:13 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
txBldr, err = utils.EnrichWithGas(txBldr, cliCtx, msgs)
|
2019-02-01 17:04:13 -08:00
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2019-02-01 17:04:13 -08:00
|
|
|
return
|
|
|
|
}
|
2019-02-19 10:15:39 -08:00
|
|
|
|
|
|
|
if br.Simulate {
|
|
|
|
rest.WriteSimulationResponse(w, cdc, txBldr.Gas())
|
|
|
|
return
|
|
|
|
}
|
2019-02-01 17:04:13 -08:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:35:19 -08:00
|
|
|
stdMsg, err := txBldr.BuildSignMsg(msgs)
|
2019-01-29 11:22:47 -08:00
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2019-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := cdc.MarshalJSON(auth.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
|
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
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-01-29 11:22:47 -08:00
|
|
|
return
|
|
|
|
}
|