Skip to content

Commit

Permalink
Add CLI for query evm tx by hash (#2053)
Browse files Browse the repository at this point in the history
* Add seid evm tx [hash] command

* Add log

* Fix

* Fix

* Fix import

* fxi

* Fix lint
  • Loading branch information
yzang2019 authored Jan 27, 2025
1 parent 2418932 commit eeb164d
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions x/evm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"errors"
"fmt"

"math/big"
"os"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/lib/ethapi"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/sei-protocol/sei-chain/evmrpc"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw1155"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw721"
Expand Down Expand Up @@ -48,6 +55,7 @@ func GetQueryCmd(_ string) *cobra.Command {
cmd.AddCommand(CmdQueryPointer())
cmd.AddCommand(CmdQueryPointerVersion())
cmd.AddCommand(CmdQueryPointee())
cmd.AddCommand(CmdQueryTxByHash())

return cmd
}
Expand Down Expand Up @@ -461,3 +469,36 @@ func CmdQueryPointee() *cobra.Command {

return cmd
}

func CmdQueryTxByHash() *cobra.Command {
cmd := &cobra.Command{
Use: "tx [hash]",
Short: "Query for a transaction by tx hash, same as eth_getTransactionByHash",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
hash := common.HexToHash(args[0])
rpc, err := cmd.Flags().GetString(FlagRPC)
if err != nil {
return err
}
ethClient, err := ethclient.Dial(rpc)
if err != nil {
return err
}
var response *ethapi.RPCTransaction
err = ethClient.Client().CallContext(context.Background(), &response, "eth_getTransactionByHash", hash)
if err != nil {
return err
} else if response == nil {
return ethereum.NotFound
}
result, err := json.MarshalIndent(response, "", " ")
fmt.Printf("%s\n", result)
return err
},
}

cmd.Flags().String(FlagRPC, fmt.Sprintf("http://%s:8545", evmrpc.LocalAddress), "RPC endpoint to send request to")

return cmd
}

0 comments on commit eeb164d

Please sign in to comment.