0

I'm trying to estimate gas with the following ethers function on the Polygon blockchain:

await provider.estimateGas({
  // Wrapped ETH address
  to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",

  // `function deposit() payable`
  data: "0xd0e30db0",

  // 1 ether
  value: parseEther("1.0")
});
// { BigNumber: "27938" }

Found here: ethers provider.estimateGas

The above function returns a value (typically 21064). However, it's to an unrelated contract that appears to be WETH on the Ethereum blockchain (and I'm not entirely sure what the address is on the Polygon blockchain).

According to the minimal documentation, I assume "0xd0e30db0" translates to the string "deposit()" (or the entire string "function deposit() payable")? (which exists on that contract)

My main question is, how would someone go about converting the function name (e.g. "deposit()") to the hex value (e.g. "0xd0e30db0") required in the data parameter? Is there another function or tool somewhere?

If I'm way off, please set me straight and any help is always much appreciated.

UPDATE: It seems the above code only works with the address above and doesn't even require the data parameter. I've tried numerous other contracts on ethereum and matic and none seem to return a gas estimate, except the above contract. I'm also using alchemy.io app servers if that makes a difference. Anyone know what the trick is? There error is:

Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":43,\"error\":{\"code\":-32000,\"message\":\"execution reverted\"}}","error":{"code":-32000},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"to\":\"0x7f268357a8c2552623316e2562d90e642bb538e5\"}],\"id\":43,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"https://polygon-mainnet.g.alchemy.com/v2/my-api-key-here"}, method="estimateGas", transaction={"to":"0x7f268357A8c2552623316e2562D90e642bB538E5","accessList":null}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.6.3)
    at Logger.makeError (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\logger\src.ts\index.ts:261:28)
    at Logger.throwError (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\logger\src.ts\index.ts:273:20)
    at checkError (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:108:16)
    at AlchemyProvider.<anonymous> (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:590:20)
    at step (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
  code: 'UNPREDICTABLE_GAS_LIMIT',
  error: Error: processing response error (body="{\"jsonrpc\":\"2.0\",\"id\":43,\"error\":{\"code\":-32000,\"message\":\"execution reverted\"}}", error={"code":-32000}, requestBody="{\"method\":\"eth_estimateGas\",\"params\":[{\"to\":\"0x7f268357a8c2552623316e2562d90e642bb538e5\"}],\"id\":43,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://polygon-mainnet.g.alchemy.com/v2/my-api-key-here", code=SERVER_ERROR, version=web/5.6.0)
      at Logger.makeError (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\logger\src.ts\index.ts:261:28)
      at Logger.throwError (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\logger\src.ts\index.ts:273:20)
      at C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\src.ts\index.ts:329:28
      at step (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\lib\index.js:33:23)
      at Object.next (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\lib\index.js:14:53)
      at fulfilled (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\lib\index.js:5:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    reason: 'processing response error',
    code: 'SERVER_ERROR',
    body: '{"jsonrpc":"2.0","id":43,"error":{"code":-32000,"message":"execution reverted"}}',
    error: Error: execution reverted
        at getResult (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:125:28)
        at processJsonFunc (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\src.ts\index.ts:371:22)
        at C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\src.ts\index.ts:308:42
        at step (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\lib\index.js:33:23)
        at Object.next (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\lib\index.js:14:53)
        at fulfilled (C:\Users\user1\Desktop\test-project\node_modules\@ethersproject\web\lib\index.js:5:58)
        at processTicksAndRejections (node:internal/process/task_queues:96:5) {
      code: -32000,
      data: undefined
    },
    requestBody: '{"method":"eth_estimateGas","params":[{"to":"0x7f268357a8c2552623316e2562d90e642bb538e5"}],"id":43,"jsonrpc":"2.0"}',
    requestMethod: 'POST',
    url: 'https://polygon-mainnet.g.alchemy.com/v2/my-api-key-here'
  },
  method: 'estimateGas',
  transaction: {
    to: '0x7f268357A8c2552623316e2562D90e642bB538E5',
    accessList: null
  }
}
2
  • MethodId is in abi code of contract but not specified with function names in it so you have to figure it out. etherscan API is one way to get it for each function in smart contract Jul 23, 2022 at 19:10
  • The address 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 is an EOA in the polygon chain, it is a real contract on ethereum mainnet.
    – Ismael
    Jul 24, 2022 at 15:27

1 Answer 1

0

I got what appears to be what I'm looking for with provider.getGasPrice:

// The gas price (in wei)...
gasPrice = await provider.getGasPrice()
// { BigNumber: "19732309040" }

// ...often this gas price is easier to understand or
// display to the user in gwei
utils.formatUnits(gasPrice, "gwei")
// '19.73230904'

From here: Ethers getGasPrice

Also, you're supposed to be able to parse Slow, Average and Agressive out from provider.getFeeData in the section right below the provider.getGasPrice section in the link above:

// The gas price (in wei)...
feeData = await provider.getFeeData()
// {
//   gasPrice: { BigNumber: "19732309040" },
//   maxFeePerGas: { BigNumber: "38964618080" },
//   maxPriorityFeePerGas: { BigNumber: "1500000000" }
// }

// ...often these values are easier to understand or
// display to the user in gwei
utils.formatUnits(feeData.maxFeePerGas, "gwei")
// '38.96461808'

However, I have only been able to get feeData.gasPrice out of that and it's pretty close to provider.getGasPrice. The other two, feeData.maxFeePerGas and feeData.maxPriorityFeePerGas return errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.