Packages named utils, common, or misc provide clients with no sense of what the package contains. This makes it harder for clients to use the package and makes it harder for maintainers to keep the package focused. Over time, they accumulate dependencies that can make compilation significantly and unnecessarily slower, especially in large programs. And since such package names are generic, they are more likely to collide with other packages imported by client code, forcing clients to invent names to distinguish them. cit. https://blog.golang.org/package-names
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package client
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
// WriteGenerateStdTxResponse writes response for the generate only mode.
|
|
func WriteGenerateStdTxResponse(w http.ResponseWriter, cliCtx context.CLIContext, br rest.BaseReq, msgs []sdk.Msg) {
|
|
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
simAndExec, gas, err := flags.ParseGas(br.Gas)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
txBldr := types.NewTxBuilder(
|
|
GetTxEncoder(cliCtx.Codec), br.AccountNumber, br.Sequence, gas, gasAdj,
|
|
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
|
|
)
|
|
|
|
if br.Simulate || simAndExec {
|
|
if gasAdj < 0 {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, types.ErrorInvalidGasAdjustment.Error())
|
|
return
|
|
}
|
|
|
|
txBldr, err = EnrichWithGas(txBldr, cliCtx, msgs)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
if br.Simulate {
|
|
rest.WriteSimulationResponse(w, cliCtx.Codec, txBldr.Gas())
|
|
return
|
|
}
|
|
}
|
|
|
|
stdMsg, err := txBldr.BuildSignMsg(msgs)
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
output, err := cliCtx.Codec.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
|
|
if err != nil {
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if _, err := w.Write(output); err != nil {
|
|
log.Printf("could not write response: %v", err)
|
|
}
|
|
|
|
}
|