|
| 1 | +package query |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "math/big" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/bcdevtools/devd/v3/cmd/flags" |
| 11 | + "github.com/bcdevtools/devd/v3/cmd/utils" |
| 12 | + "github.com/ethereum/go-ethereum" |
| 13 | + "github.com/ethereum/go-ethereum/common" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func GetQueryEvmRpcEthCallCommand() *cobra.Command { |
| 18 | + const flagFrom = "from" |
| 19 | + const flagValue = "value" |
| 20 | + |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "eth_call [contract address] [call data]", |
| 23 | + Short: "Call `eth_call` of EVM RPC: executes a new EVM message call immediately without creating a transaction on the block chain", |
| 24 | + Args: cobra.ExactArgs(2), |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + ethClient, _ := flags.MustGetEthClient(cmd) |
| 27 | + |
| 28 | + contractAddress := common.HexToAddress(args[0]) |
| 29 | + callData, err := hex.DecodeString(strings.TrimPrefix(strings.ToLower(args[1]), "0x")) |
| 30 | + utils.ExitOnErr(err, "failed to decode call data") |
| 31 | + |
| 32 | + contextHeight := func() *big.Int { |
| 33 | + height, err := flags.ReadFlagBlockNumberOrNil(cmd, flags.FlagHeight) |
| 34 | + utils.ExitOnErr(err, "failed to parse block number") |
| 35 | + if height != nil && height.Sign() == 1 { |
| 36 | + utils.PrintfStdErr("INF: using block number: %s\n", height.String()) |
| 37 | + } |
| 38 | + return height |
| 39 | + }() |
| 40 | + |
| 41 | + result, err := ethClient.CallContract(context.Background(), ethereum.CallMsg{ |
| 42 | + From: func() common.Address { |
| 43 | + from, _ := cmd.Flags().GetString(flagFrom) |
| 44 | + if from == "" { |
| 45 | + return common.Address{} |
| 46 | + } |
| 47 | + fromAddr := common.HexToAddress(from) |
| 48 | + utils.PrintfStdErr("INF: using from: %s\n", fromAddr) |
| 49 | + return fromAddr |
| 50 | + }(), |
| 51 | + To: &contractAddress, |
| 52 | + Gas: func() uint64 { |
| 53 | + gas, err := flags.ReadFlagShortIntOrHexOrZero(cmd, flags.FlagGas) |
| 54 | + utils.ExitOnErr(err, "failed to parse gas") |
| 55 | + if gas > 0 { |
| 56 | + utils.PrintfStdErr("INF: using gas: %d\n", gas) |
| 57 | + if gas < 21000 { |
| 58 | + utils.PrintlnStdErr("WARN: gas is less than 21000, it may not enough for the call") |
| 59 | + } |
| 60 | + } |
| 61 | + return gas |
| 62 | + }(), |
| 63 | + GasPrice: func() *big.Int { |
| 64 | + gasPrice, err := flags.ReadFlagShortIntOrHexOrNil(cmd, flags.FlagGasPrices) |
| 65 | + utils.ExitOnErr(err, "failed to parse gas price") |
| 66 | + if gasPrice != nil && gasPrice.Sign() == 1 { |
| 67 | + utils.PrintfStdErr("INF: using gas-price: %s\n", gasPrice) |
| 68 | + } |
| 69 | + return gasPrice |
| 70 | + }(), |
| 71 | + GasFeeCap: nil, |
| 72 | + GasTipCap: nil, |
| 73 | + Value: func() *big.Int { |
| 74 | + value, err := flags.ReadFlagShortIntOrHexOrNil(cmd, flagValue) |
| 75 | + utils.ExitOnErr(err, "failed to parse value") |
| 76 | + if value != nil && value.Sign() == 1 { |
| 77 | + utils.PrintfStdErr("INF: using value: %s\n", value) |
| 78 | + } |
| 79 | + return value |
| 80 | + }(), |
| 81 | + Data: callData, |
| 82 | + AccessList: nil, |
| 83 | + }, contextHeight) |
| 84 | + utils.ExitOnErr(err, "failed to call contract") |
| 85 | + |
| 86 | + fmt.Println("0x" + hex.EncodeToString(result)) |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + cmd.Flags().String(flags.FlagEvmRpc, "", flags.FlagEvmRpcDesc) |
| 91 | + cmd.Flags().StringP(flagFrom, "f", "", "the address from which the transaction is sent") |
| 92 | + cmd.Flags().StringP(flags.FlagGas, "g", "", "the integer of gas provided for the transaction execution, support short int and hex") |
| 93 | + cmd.Flags().StringP(flags.FlagGasPrices, "p", "", "the integer of gasPrice used for each paid gas encoded as hexadecimal, support short int and hex") |
| 94 | + cmd.Flags().StringP(flagValue, "v", "", "the integer of value sent with this transaction encoded as hexadecimal, support short int and hex") |
| 95 | + cmd.Flags().StringP(flags.FlagHeight, "h", "latest", "the context height of the block to exec, accept \"latest\"/short int/hex") |
| 96 | + |
| 97 | + return cmd |
| 98 | +} |
0 commit comments