Skip to content

Commit

Permalink
Fix the wait-tx command to properly parse the hash out of json.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Jun 11, 2024
1 parent a25adc2 commit 6d10c74
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions client/rpc/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,21 @@ $ %[1]s tx [flags] | %[1]s q wait-tx
}

func parseHashFromInput(in []byte) ([]byte, error) {
var resultTx coretypes.ResultTx
if err := json.Unmarshal(in, &resultTx); err == nil {
// The content of in is expected to be the result of a tx command which should be using GenerateOrBroadcastTxCLI.
// That outputs a sdk.TxResponse as either the json or yaml. As json, we can't unmarshal it back into that struct,
// though, because the height field ends up quoted which confuses json.Unmarshal (because it's for an int64 field).

// Try to find the txhash from json ouptut.
resultTx := make(map[string]json.RawMessage)
if err := json.Unmarshal(in, &resultTx); err == nil && len(resultTx["txhash"]) > 0 {
// input was JSON, return the hash
return resultTx.Hash, nil
hash := strings.Trim(strings.TrimSpace(string(resultTx["txhash"])), `"`)
if len(hash) > 0 {
return hex.DecodeString(hash)
}
}

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

0 comments on commit 6d10c74

Please sign in to comment.