ethereum / go-ethereum

Go implementation of the Ethereum protocol
https://geth.ethereum.org
GNU Lesser General Public License v3.0
47.53k stars 20.12k forks source link

How to do STATIC CALL to simulate failures just like Metamask with Geth client? #27757

Closed kabaluyot closed 1 year ago

kabaluyot commented 1 year ago

Is it possible to most likely know if a contract call will fail just like how EtherJS or Metamask is doing it? It is to not waste gas fees.

Same like what's discussed here: https://ethereum.stackexchange.com/a/135938 .. Can't find much resources with Geth online. Thanks

jsvisa commented 1 year ago

AFAIK, you can use ethereum's eth_estimategas OR geth's debug_traceCall RPC to simulated execution.

kabaluyot commented 1 year ago

Hi @jsvisa , do you have a sample with debug_traceCall? I also see that geth client has estimate gas support

jsvisa commented 1 year ago

FYI:

{
  "jsonrpc": "2.0",
  "method": "debug_traceCall",
  "id": 67,
  "params": [
    {
      "from": "0x0C46dADBc6271511F25273cE856eCF799022dA63",
      "to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
      "value": "0x1BC16D674EC80000",
      "data": "0xb6f9de95000000000000000000000000000000000000000000000000000000dc2702dfd000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000c46dadbc6271511f25273ce856ecf799022da630000000000000000000000000000000000000000000000000000000064b818c30000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000005c72bb4774d70058b7761cc963782aa2826c7b81",
      "gas": "0x47CED",
       "gasPrice": "0xf3f0fd31a0"
    },
    "latest",
    {
      "timeout": "120s",
      "tracer": "callTracer",
      "tracerConfig": {
        "withLog": true
      }
    }

}
rjl493456442 commented 1 year ago

As @jsvisa pointed out, eth_estimateGas will return the error message if your contract fail to execute with the parameters provided.

eth_traceCall can provide more information, but more or less for debugging propose. I think eth_estimateGas should be enough for you.