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
}
}
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
is an EOA in the polygon chain, it is a real contract on ethereum mainnet.