-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsimulate.go
42 lines (34 loc) · 1.2 KB
/
simulate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cosmos
import (
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
)
// RegisterSimulateRoute registers the route on the router.
func RegisterSimulateRoute(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/txs/simulate", SimulateRequestHandlerFn(cliCtx)).Methods("POST")
}
// SimulateReq defines the properties of a send request's body.
type SimulateReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Msgs []sdk.Msg `json:"msgs" yaml:"msgs"`
}
// SimulateRequestHandlerFn - http request handler to simulate msgs.
func SimulateRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req SimulateReq
if !rest.ReadRESTReq(w, r, cliCtx.Codec, &req) {
return
}
req.BaseReq = req.BaseReq.Sanitize()
if !req.BaseReq.ValidateBasic(w) {
return
}
// force simulate to true to use the following function in simulate only mode.
req.BaseReq.Simulate = true
utils.WriteGenerateStdTxResponse(w, cliCtx, req.BaseReq, req.Msgs)
}
}