|
| 1 | +package query |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/bcdevtools/devd/v3/cmd/types" |
| 12 | + "github.com/ethereum/go-ethereum/crypto" |
| 13 | + |
| 14 | + "github.com/bcdevtools/devd/v3/cmd/flags" |
| 15 | + "github.com/bcdevtools/devd/v3/cmd/utils" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +func GetQueryEvmRpcEthGetAccountCommand() *cobra.Command { |
| 20 | + cmd := &cobra.Command{ |
| 21 | + Use: "eth_getAccount [0xAddress/Bech32]", |
| 22 | + Aliases: []string{"evm-account"}, |
| 23 | + Short: "Query account using `eth_getAccount` via EVM RPC", |
| 24 | + Args: cobra.ExactArgs(1), |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + ethClient8545, evmRpc := flags.MustGetEthClient(cmd) |
| 27 | + |
| 28 | + evmAddrs, err := utils.GetEvmAddressFromAnyFormatAddress(args...) |
| 29 | + utils.ExitOnErr(err, "failed to get evm address from input") |
| 30 | + evmAddr := evmAddrs[0] |
| 31 | + |
| 32 | + contextHeight, err := flags.ReadFlagBlockNumberOrNil(cmd, flags.FlagHeight) |
| 33 | + utils.ExitOnErr(err, "failed to parse block number") |
| 34 | + |
| 35 | + var params []types.JsonRpcQueryParam |
| 36 | + |
| 37 | + paramsAddr, err := types.NewJsonRpcStringQueryParam(evmAddr.String()) |
| 38 | + utils.ExitOnErr(err, "failed to create json rpc query param") |
| 39 | + params = append(params, paramsAddr) |
| 40 | + |
| 41 | + paramsContext, err := types.NewJsonRpcStringQueryParam(func() string { |
| 42 | + if contextHeight == nil { |
| 43 | + return "latest" |
| 44 | + } |
| 45 | + return contextHeight.Text(16) |
| 46 | + }()) |
| 47 | + utils.ExitOnErr(err, "failed to create json rpc query param") |
| 48 | + params = append(params, paramsContext) |
| 49 | + |
| 50 | + bz, err := types.DoEvmRpcQuery( |
| 51 | + evmRpc, |
| 52 | + types.NewJsonRpcQueryBuilder( |
| 53 | + "eth_getAccount", |
| 54 | + params..., |
| 55 | + ), |
| 56 | + 0, |
| 57 | + ) |
| 58 | + |
| 59 | + utils.ExitOnErr(err, "failed to query account") |
| 60 | + |
| 61 | + codeHashOfEmpty := "0x" + hex.EncodeToString(crypto.Keccak256(nil)) |
| 62 | + |
| 63 | + accountInfoAsMap, err := getResultObjectFromEvmRpcResponse(bz) |
| 64 | + if err != nil && strings.Contains(err.Error(), "method eth_getAccount does not exist") { |
| 65 | + // fallback |
| 66 | + err = nil |
| 67 | + |
| 68 | + code, err := ethClient8545.CodeAt(context.Background(), evmAddr, contextHeight) |
| 69 | + utils.ExitOnErr(err, "failed to get code") |
| 70 | + |
| 71 | + balance, err := ethClient8545.BalanceAt(context.Background(), evmAddr, contextHeight) |
| 72 | + utils.ExitOnErr(err, "failed to get balance") |
| 73 | + |
| 74 | + nonce, err := ethClient8545.NonceAt(context.Background(), evmAddr, contextHeight) |
| 75 | + utils.ExitOnErr(err, "failed to get nonce") |
| 76 | + |
| 77 | + accountInfoAsMap = map[string]interface{}{ |
| 78 | + "codeHash": func() string { |
| 79 | + if len(code) == 0 { |
| 80 | + return codeHashOfEmpty |
| 81 | + } |
| 82 | + |
| 83 | + return "0x" + hex.EncodeToString(crypto.Keccak256(code)) |
| 84 | + }(), |
| 85 | + "balance": balance.String(), |
| 86 | + "nonce": nonce, |
| 87 | + } |
| 88 | + } |
| 89 | + utils.ExitOnErr(err, "failed to get result object from response") |
| 90 | + |
| 91 | + if codeHashRaw, found := accountInfoAsMap["codeHash"]; found { |
| 92 | + // normalize |
| 93 | + if codeHashStr, ok := codeHashRaw.(string); ok && codeHashStr == "0x" { |
| 94 | + accountInfoAsMap["codeHash"] = codeHashOfEmpty |
| 95 | + } |
| 96 | + } |
| 97 | + if codeHashRaw, found := accountInfoAsMap["codeHash"]; found { |
| 98 | + if codeHashStr, ok := codeHashRaw.(string); ok { |
| 99 | + isContract := codeHashStr != codeHashOfEmpty |
| 100 | + accountInfoAsMap["_isContract"] = isContract |
| 101 | + |
| 102 | + if !isContract { |
| 103 | + if nonceRaw, found := accountInfoAsMap["nonce"]; found { |
| 104 | + nonceStr := fmt.Sprintf("%v", nonceRaw) |
| 105 | + nonce, ok := new(big.Int).SetString(nonceStr, 10) |
| 106 | + if !ok { |
| 107 | + utils.PrintlnStdErr("ERR: failed to parse nonce:", nonceStr) |
| 108 | + } else { |
| 109 | + txSent := nonce |
| 110 | + if txSent.Sign() > 0 && txSent.Cmp(big.NewInt(1_000_000)) > 0 { |
| 111 | + txSent = new(big.Int).Mod(txSent, big.NewInt(1_000_000_000)) // Dymension RollApps increases nonce at fraud happened |
| 112 | + } |
| 113 | + accountInfoAsMap["_txSent"] = txSent.String() |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + bz, err = json.Marshal(accountInfoAsMap) |
| 121 | + utils.ExitOnErr(err, "failed to marshal account info") |
| 122 | + |
| 123 | + utils.TryPrintBeautyJson(bz) |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc) |
| 128 | + cmd.Flags().String(flags.FlagHeight, "", "query account info at specific height") |
| 129 | + |
| 130 | + return cmd |
| 131 | +} |
0 commit comments