Skip to content

Commit bebb212

Browse files
committed
improve devd q txs-in-block
1 parent 2aeba67 commit bebb212

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

cmd/query/txs.go

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package query
22

33
import (
44
"context"
5+
"encoding/hex"
6+
"encoding/json"
57
"fmt"
8+
"regexp"
69
"strconv"
10+
"strings"
711

812
"github.com/bcdevtools/devd/v3/cmd/flags"
913

@@ -28,15 +32,71 @@ func GetQueryTxsInBlockCommand() *cobra.Command {
2832
resBlock, err := tendermintRpcHttpClient.Block(context.Background(), &blockNumber)
2933
utils.ExitOnErr(err, "failed to get block")
3034

31-
for _, tx := range resBlock.Block.Txs {
32-
fmt.Println(tx.Hash())
33-
}
34-
3535
resBlockResult, err := tendermintRpcHttpClient.BlockResults(context.Background(), &blockNumber)
3636
utils.ExitOnErr(err, "failed to get block results")
3737

38-
for _, txResult := range resBlockResult.TxsResults {
39-
fmt.Println(txResult.Log)
38+
orTextEmpty := func(input any) any {
39+
if input == nil {
40+
return "(empty)"
41+
}
42+
if str, ok := input.(string); ok && str == "" {
43+
return "(empty)"
44+
}
45+
return input
46+
}
47+
48+
tryExtractMsgTypeFromRawTx := func(input []byte) []string {
49+
matches := regexp.MustCompile(`/[a-z\d]+(\.[a-z\d]+)+\.Msg[a-zA-Z\d]+`).FindAll(input, -1)
50+
var result []string
51+
for _, match := range matches {
52+
result = append(result, string(match))
53+
}
54+
return result
55+
}
56+
57+
for i, tx := range resBlock.Block.Txs {
58+
if i > 0 {
59+
utils.PrintlnStdErr("====================================")
60+
}
61+
txResult := resBlockResult.TxsResults[i]
62+
txResult.Events = utils.ResolveBase64Events(txResult.Events)
63+
fmt.Println("Index:", i)
64+
fmt.Println("Hash:", strings.ToUpper(hex.EncodeToString(tx.Hash())))
65+
if strings.Contains(string(tx), "/ethermint.evm.v1.MsgEthereumTx") {
66+
L1:
67+
for _, event := range txResult.Events {
68+
if event.Type != "ethereum_tx" {
69+
continue
70+
}
71+
72+
for _, attr := range event.Attributes {
73+
if string(attr.Key) == "ethereumTxHash" {
74+
fmt.Println("EvmHash:", attr.Value)
75+
break L1
76+
}
77+
}
78+
}
79+
}
80+
fmt.Println("Type:", func() string {
81+
if msgsType := tryExtractMsgTypeFromRawTx(tx); len(msgsType) > 0 {
82+
return strings.Join(msgsType, ", ")
83+
}
84+
return "(failed to extract msg type)"
85+
}())
86+
fmt.Println("Code:", txResult.Code)
87+
fmt.Println("Gas:", txResult.GasUsed, "/", txResult.GasWanted)
88+
fmt.Println("Data:", strings.ReplaceAll(string(txResult.Data), "\n", ""))
89+
fmt.Println("Log:", orTextEmpty(txResult.Log))
90+
fmt.Println("Events:", func() string {
91+
if len(txResult.Events) == 0 {
92+
return orTextEmpty("").(string)
93+
}
94+
bz, err := json.Marshal(txResult.Events)
95+
utils.ExitOnErr(err, "failed to marshal events")
96+
return string(bz)
97+
}())
98+
fmt.Println("Info:", orTextEmpty(txResult.Info))
99+
fmt.Println("Code-space:", orTextEmpty(txResult.Codespace))
40100
}
41101
},
42102
}

cmd/tx/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"bytes"
55
"encoding/hex"
66
"fmt"
7-
ethtypes "github.com/ethereum/go-ethereum/core/types"
87
"math/big"
98
"regexp"
109
"strings"
1110

11+
ethtypes "github.com/ethereum/go-ethereum/core/types"
12+
1213
"github.com/bcdevtools/devd/v3/cmd/utils"
1314
"github.com/spf13/cobra"
1415
)

0 commit comments

Comments
 (0)