Skip to content

Commit c791fde

Browse files
committed
Add tx response fields
1 parent 07dce81 commit c791fde

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

cmd/handler/tx.go

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ import (
77
"net/http"
88

99
"github.com/gnolang/gno/pkgs/amino"
10-
abci "github.com/gnolang/gno/pkgs/bft/abci/types"
1110
"github.com/gnolang/gno/pkgs/bft/rpc/client"
1211
ctypes "github.com/gnolang/gno/pkgs/bft/rpc/core/types"
1312
"github.com/gnolang/gno/pkgs/std"
1413
)
1514

16-
type BroadcastResponse struct {
17-
Result abci.ResponseDeliverTx `json:"result"`
18-
Hash string `json:"hash"`
19-
Height int64 `json:"height"`
15+
type BroadcastTxResponse struct {
16+
TxResponse *TxResponse `json:"tx_response,omitempty"`
17+
}
18+
19+
type TxResponse struct {
20+
Height int64 `json:"height,omitempty"`
21+
TxHash string `json:"txhash,omitempty"`
22+
Code uint32 `json:"code,omitempty"`
23+
Data string `json:"data,omitempty"`
24+
RawLog string `json:"raw_log,omitempty"`
25+
GasWanted int64 `json:"gas_wanted,omitempty"`
26+
GasUsed int64 `json:"gas_used,omitempty"`
2027
}
2128

2229
func TxsHandler(cli client.ABCIClient) http.HandlerFunc {
@@ -48,20 +55,17 @@ func TxsHandler(cli client.ABCIClient) http.HandlerFunc {
4855
return
4956
}
5057

51-
if res.CheckTx.IsErr() {
52-
writeError(w, fmt.Errorf("transaction failed %#v\nlog %s", res, res.CheckTx.Log))
53-
return
54-
}
55-
56-
if res.DeliverTx.IsErr() {
57-
writeError(w, fmt.Errorf("transaction failed %#v\nlog %s", res, res.DeliverTx.Log))
58-
return
59-
}
60-
61-
result := BroadcastResponse{
62-
Hash: fmt.Sprintf("%X", res.Hash),
63-
Height: res.Height,
64-
Result: res.DeliverTx,
58+
code, log := getCodeLog(res)
59+
result := BroadcastTxResponse{
60+
TxResponse: &TxResponse{
61+
TxHash: fmt.Sprintf("%X", res.Hash),
62+
Height: res.Height,
63+
Code: code,
64+
Data: string(res.DeliverTx.Data),
65+
RawLog: log,
66+
GasWanted: res.DeliverTx.GasWanted,
67+
GasUsed: res.DeliverTx.GasUsed,
68+
},
6569
}
6670

6771
w.Header().Set("Content-Type", "application/json")
@@ -94,20 +98,17 @@ func ProtoTxsHandler(cli client.ABCIClient) http.HandlerFunc {
9498
return
9599
}
96100

97-
if res.CheckTx.IsErr() {
98-
writeError(w, fmt.Errorf("transaction failed %#v\nlog %s", res, res.CheckTx.Log))
99-
return
100-
}
101-
102-
if res.DeliverTx.IsErr() {
103-
writeError(w, fmt.Errorf("transaction failed %#v\nlog %s", res, res.DeliverTx.Log))
104-
return
105-
}
106-
107-
result := BroadcastResponse{
108-
Hash: fmt.Sprintf("%X", res.Hash),
109-
Height: res.Height,
110-
Result: res.DeliverTx,
101+
code, log := getCodeLog(res)
102+
result := BroadcastTxResponse{
103+
TxResponse: &TxResponse{
104+
TxHash: fmt.Sprintf("%X", res.Hash),
105+
Height: res.Height,
106+
Code: code,
107+
Data: string(res.DeliverTx.Data),
108+
RawLog: log,
109+
GasWanted: res.DeliverTx.GasWanted,
110+
GasUsed: res.DeliverTx.GasUsed,
111+
},
111112
}
112113

113114
w.Header().Set("Content-Type", "application/json")
@@ -116,6 +117,18 @@ func ProtoTxsHandler(cli client.ABCIClient) http.HandlerFunc {
116117
}
117118
}
118119

120+
func getCodeLog(res *ctypes.ResultBroadcastTxCommit) (uint32, string) {
121+
if res.CheckTx.IsErr() {
122+
return 1, res.CheckTx.Log
123+
}
124+
125+
if res.DeliverTx.IsErr() {
126+
return 2, res.DeliverTx.Log
127+
}
128+
129+
return 0, ""
130+
}
131+
119132
func BroadcastHandler(cli client.ABCIClient, tx std.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
120133
bz, err := amino.Marshal(tx)
121134
if err != nil {

0 commit comments

Comments
 (0)