Skip to content

Commit 304c172

Browse files
committed
Add simulateTx handler
1 parent 83e9245 commit 304c172

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

cmd/handler/tx.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/gnolang/gno/pkgs/amino"
1010
"github.com/gnolang/gno/pkgs/bft/rpc/client"
1111
ctypes "github.com/gnolang/gno/pkgs/bft/rpc/core/types"
12+
keysclient "github.com/gnolang/gno/pkgs/crypto/keys/client"
1213
"github.com/gnolang/gno/pkgs/std"
1314
)
1415

@@ -26,6 +27,14 @@ type TxResponse struct {
2627
GasUsed int64 `json:"gas_used,omitempty"`
2728
}
2829

30+
type SimulateResponse struct {
31+
GasInfo TxGasResult `json:"gas_info"`
32+
}
33+
34+
type TxGasResult struct {
35+
GasUsed int64 `json:"gas_used"`
36+
}
37+
2938
func TxsHandler(cli client.ABCIClient) http.HandlerFunc {
3039
return func(w http.ResponseWriter, r *http.Request) {
3140
var params map[string]json.RawMessage
@@ -115,6 +124,40 @@ func ProtoTxsHandler(cli client.ABCIClient) http.HandlerFunc {
115124
}
116125
}
117126

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+
118161
func getCodeLog(res *ctypes.ResultBroadcastTxCommit) (uint32, string) {
119162
if res.CheckTx.IsErr() {
120163
return 1, res.CheckTx.Log

0 commit comments

Comments
 (0)