2019-02-04 07:48:26 -08:00
|
|
|
package rest
|
2018-08-22 04:38:55 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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-10-24 06:19:48 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/keyerror"
|
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
|
|
|
|
|
|
|
|
// CompleteAndBroadcastTxREST implements a utility function that facilitates
|
2019-01-29 11:22:47 -08:00
|
|
|
// sending a series of messages in a signed tx. In addition, it will handle
|
|
|
|
// tx gas simulation and estimation.
|
2018-09-26 06:29:39 -07:00
|
|
|
//
|
2019-01-29 11:22:47 -08:00
|
|
|
// NOTE: Also see CompleteAndBroadcastTxCLI.
|
2019-02-14 08:53:36 -08:00
|
|
|
func CompleteAndBroadcastTxREST(w http.ResponseWriter, cliCtx context.CLIContext,
|
|
|
|
baseReq rest.BaseReq, msgs []sdk.Msg, cdc *codec.Codec) {
|
2019-01-18 08:45:20 -08:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, baseReq.GasAdjustment, client.DefaultGasAdjustment)
|
2018-12-19 16:26:33 -08:00
|
|
|
if !ok {
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
simAndExec, gas, err := client.ParseGas(baseReq.Gas)
|
2018-12-19 16:26:33 -08:00
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// derive the from account address and name from the Keybase
|
|
|
|
fromAddress, fromName, err := context.GetFromFields(baseReq.From)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
cliCtx = cliCtx.WithFromName(fromName).WithFromAddress(fromAddress)
|
2019-01-18 08:45:20 -08:00
|
|
|
txBldr := authtxb.NewTxBuilder(
|
2019-02-04 07:48:26 -08:00
|
|
|
utils.GetTxEncoder(cdc), baseReq.AccountNumber,
|
2019-01-29 11:22:47 -08:00
|
|
|
baseReq.Sequence, gas, gasAdj, baseReq.Simulate,
|
2019-01-18 08:45:20 -08:00
|
|
|
baseReq.ChainID, baseReq.Memo, baseReq.Fees, baseReq.GasPrices,
|
|
|
|
)
|
2018-12-19 16:26:33 -08:00
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
txBldr, err = utils.PrepareTxBuilder(txBldr, cliCtx)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseReq.Simulate || simAndExec {
|
|
|
|
if gasAdj < 0 {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, client.ErrInvalidGasAdjustment.Error())
|
2018-12-19 16:26:33 -08:00
|
|
|
return
|
|
|
|
}
|
2018-09-26 06:29:39 -07:00
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
txBldr, err = utils.EnrichWithGas(txBldr, cliCtx, msgs)
|
2018-09-26 06:29:39 -07:00
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-10 06:26:34 -08:00
|
|
|
if baseReq.Simulate {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteSimulationResponse(w, cdc, txBldr.Gas())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
txBytes, err := txBldr.BuildAndSign(cliCtx.GetFromName(), baseReq.Password, msgs)
|
2018-10-24 06:19:48 -07:00
|
|
|
if keyerror.IsErrKeyNotFound(err) {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
2018-10-24 06:19:48 -07:00
|
|
|
return
|
|
|
|
} else if keyerror.IsErrWrongPassword(err) {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
2018-10-24 06:19:48 -07:00
|
|
|
} else if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-10-24 06:19:48 -07:00
|
|
|
return
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cliCtx.BroadcastTx(txBytes)
|
|
|
|
if err != nil {
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-09-26 06:29:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
|
2018-09-26 06:29:39 -07:00
|
|
|
}
|
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-01 17:04:13 -08:00
|
|
|
if simAndExec {
|
|
|
|
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-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-01-29 11:22:47 -08:00
|
|
|
w.Write(output)
|
|
|
|
return
|
|
|
|
}
|