9
9
"github.com/gnolang/gno/pkgs/amino"
10
10
"github.com/gnolang/gno/pkgs/bft/rpc/client"
11
11
ctypes "github.com/gnolang/gno/pkgs/bft/rpc/core/types"
12
+ keysclient "github.com/gnolang/gno/pkgs/crypto/keys/client"
12
13
"github.com/gnolang/gno/pkgs/std"
13
14
)
14
15
@@ -26,6 +27,14 @@ type TxResponse struct {
26
27
GasUsed int64 `json:"gas_used,omitempty"`
27
28
}
28
29
30
+ type SimulateResponse struct {
31
+ GasInfo TxGasResult `json:"gas_info"`
32
+ }
33
+
34
+ type TxGasResult struct {
35
+ GasUsed int64 `json:"gas_used"`
36
+ }
37
+
29
38
func TxsHandler (cli client.ABCIClient ) http.HandlerFunc {
30
39
return func (w http.ResponseWriter , r * http.Request ) {
31
40
var params map [string ]json.RawMessage
@@ -115,6 +124,40 @@ func ProtoTxsHandler(cli client.ABCIClient) http.HandlerFunc {
115
124
}
116
125
}
117
126
127
+ func SimulateTxHandler (cli client.ABCIClient ) http.HandlerFunc {
128
+ return func (w http.ResponseWriter , r * http.Request ) {
129
+ var params struct {
130
+ TxBytes string `json:"tx_bytes"`
131
+ }
132
+ err := json .NewDecoder (r .Body ).Decode (& params )
133
+ if err != nil {
134
+ writeError (w , fmt .Errorf ("%s, %s" , "unmarshaling json params" , err .Error ()))
135
+ return
136
+ }
137
+
138
+ txBz , err := base64 .StdEncoding .DecodeString (params .TxBytes )
139
+ if err != nil {
140
+ writeError (w , fmt .Errorf ("%s, %s" , "cannot decode tx" , err .Error ()))
141
+ return
142
+ }
143
+ response , err := keysclient .SimulateTx (cli , txBz )
144
+ if err != nil {
145
+ writeError (w , fmt .Errorf ("%s, %s" , "cannot simulate tx" , err .Error ()))
146
+ return
147
+ }
148
+
149
+ result := & SimulateResponse {
150
+ GasInfo : TxGasResult {
151
+ GasUsed : response .DeliverTx .GasUsed ,
152
+ },
153
+ }
154
+
155
+ w .Header ().Set ("Content-Type" , "application/json" )
156
+ w .WriteHeader (http .StatusOK )
157
+ json .NewEncoder (w ).Encode (result )
158
+ }
159
+ }
160
+
118
161
func getCodeLog (res * ctypes.ResultBroadcastTxCommit ) (uint32 , string ) {
119
162
if res .CheckTx .IsErr () {
120
163
return 1 , res .CheckTx .Log
0 commit comments