Skip to content

Commit ea16396

Browse files
mergify[bot]SpicyLemontac0turtle
authored
fix: Properly parse json in the wait-tx command. (backport #20631) (#20660)
Co-authored-by: Daniel Wedul <[email protected]> Co-authored-by: marbar3778 <[email protected]>
1 parent c5253e8 commit ea16396

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4141
## Improvements
4242

4343
* (x/authz,x/feegrant) [#20590](https://github.com/cosmos/cosmos-sdk/pull/20590) Provide updated keeper in depinject for authz and feegrant modules.
44+
* [#20631](https://github.com/cosmos/cosmos-sdk/pull/20631) Fix json parsing in the wait-tx command.
4445

4546
## [v0.50.7](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.7) - 2024-06-04
4647

client/rpc/tx.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ func WaitTxCmd() *cobra.Command {
102102
Short: "Wait for a transaction to be included in a block",
103103
Long: `Subscribes to a CometBFT WebSocket connection and waits for a transaction event with the given hash.`,
104104
Example: fmt.Sprintf(`By providing the transaction hash:
105-
$ %[1]sd q wait-tx [hash]
105+
$ %[1]s q wait-tx [hash]
106106
107107
Or, by piping a "tx" command:
108-
$ %[1]sd tx [flags] | %[1]sd q wait-tx
108+
$ %[1]s tx [flags] | %[1]s q wait-tx
109109
`, version.AppName),
110110
Args: cobra.MaximumNArgs(1),
111111
RunE: func(cmd *cobra.Command, args []string) error {
@@ -200,13 +200,21 @@ $ %[1]sd tx [flags] | %[1]sd q wait-tx
200200
}
201201

202202
func parseHashFromInput(in []byte) ([]byte, error) {
203-
var resultTx coretypes.ResultTx
204-
if err := json.Unmarshal(in, &resultTx); err == nil {
203+
// The content of in is expected to be the result of a tx command which should be using GenerateOrBroadcastTxCLI.
204+
// That outputs a sdk.TxResponse as either the json or yaml. As json, we can't unmarshal it back into that struct,
205+
// though, because the height field ends up quoted which confuses json.Unmarshal (because it's for an int64 field).
206+
207+
// Try to find the txhash from json ouptut.
208+
resultTx := make(map[string]json.RawMessage)
209+
if err := json.Unmarshal(in, &resultTx); err == nil && len(resultTx["txhash"]) > 0 {
205210
// input was JSON, return the hash
206-
return resultTx.Hash, nil
211+
hash := strings.Trim(strings.TrimSpace(string(resultTx["txhash"])), `"`)
212+
if len(hash) > 0 {
213+
return hex.DecodeString(hash)
214+
}
207215
}
208216

209-
// try to parse the hash from the output of a tx command
217+
// Try to find the txhash from yaml output.
210218
lines := strings.Split(string(in), "\n")
211219
for _, line := range lines {
212220
if strings.HasPrefix(line, "txhash:") {

0 commit comments

Comments
 (0)