Skip to content

Commit 8a10948

Browse files
committed
Add tx decode
1 parent 4a65af3 commit 8a10948

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

cmd/handler/query.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package handler
22

33
import (
4+
"encoding/base64"
45
"encoding/json"
56
"fmt"
67
"net/http"
78
"strconv"
89
"strings"
910

11+
"github.com/gnolang/gno/pkgs/amino"
1012
"github.com/gnolang/gno/pkgs/bft/rpc/client"
13+
"github.com/gnolang/gno/pkgs/std"
1114

1215
"github.com/gorilla/mux"
1316
)
@@ -73,6 +76,40 @@ func AuthQueryHandler(cli client.ABCIClient) http.HandlerFunc {
7376
}
7477
}
7578

79+
func TxDecodeHandler(cli client.ABCIClient) http.HandlerFunc {
80+
return func(w http.ResponseWriter, r *http.Request) {
81+
params := r.URL.Query()
82+
txParam := params.Get("tx")
83+
if txParam == "" {
84+
writeError(w, fmt.Errorf("tx param is required"))
85+
return
86+
}
87+
88+
txData, err := base64.StdEncoding.DecodeString(txParam)
89+
if err != nil {
90+
writeError(w, err)
91+
return
92+
}
93+
94+
var tx std.Tx
95+
err = amino.Unmarshal(txData, &tx)
96+
if err != nil {
97+
writeError(w, err)
98+
return
99+
}
100+
101+
jsonData, err := amino.MarshalJSON(&tx)
102+
if err != nil {
103+
writeError(w, err)
104+
return
105+
}
106+
107+
w.WriteHeader(http.StatusOK)
108+
w.Header().Set("Content-Type", "application/json")
109+
fmt.Fprint(w, string(jsonData))
110+
}
111+
}
112+
76113
func BankQueryHandler(cli client.ABCIClient) http.HandlerFunc {
77114
return func(w http.ResponseWriter, r *http.Request) {
78115
vars := mux.Vars(r)

cmd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func main() {
4141
r.HandleFunc("/cosmos/bank/v1beta1/balances/{address}", handler.BankQueryHandler(cli))
4242
r.HandleFunc("/cosmos/staking/v1beta1/delegations/{address}", handler.StakingQueryHandler(cli))
4343
r.HandleFunc("/cosmos/staking/v1beta1/delegators/{address}/unbonding_delegations", handler.StakingUnbondingQueryHandler(cli))
44+
r.HandleFunc("/txs/decode", handler.TxDecodeHandler(cli)).Methods(http.MethodGet)
4445
r.HandleFunc("/txs", handler.TxsHandler(cli)).Methods(http.MethodPost)
4546

4647
fmt.Println("Running on port", apiPort)

0 commit comments

Comments
 (0)