Estimate Gas Cost

Hey

I have changed the script to be able to estimate gas price. It’s correct?

async function workIfNeeded(signer, jobs) {
  for (const job of jobs) {
    const contract = new ethers.Contract(job.address, ABIs[job.name], signer);
    if (isWorkable = await contract[job.workableFn]()) {
      console.log(`${job.name} is workable`);
      var overrideOptions = {}
      try {
        const estimateGas = await metaKeeperContract.estimateGas.work(job.addr);
        const gasLimitCalculated = await estimateGas();
        overrideOptions = {
          gasLimit: gasLimitCalculated * 1.5
        }
      } catch(error) {
        console.error(error);
      }
      const tx = await contract[job.workFn](overrideOptions);
      console.log(`${job.name} worked: ${tx.hash}`);
    } else {
      console.log(`${job.name} is not workable`);
    }
  }
}
3 Likes

Hey @bolo, ethers.js should take care of estimating the gas limit for you before sending the tx. If you’re getting a “failed to estimate gas” from ethers, it’s often because the tx would revert, not because an issue in the estimation. Still, at a first glance your snippet looks ok - though It’s not clear where the metaKeeperContract contract comes from, and I don’t understand why you are evaluating estimateGas() which should be a resolved promise with a number.

By the way, if your gas estimation fails, this code snippet may help in retrieving the underlying revert reason:

2 Likes

If my transaction who was started at and failed
16 hrs 42 mins ago (Nov-10-2020 01:46:50 AM +UTC)

This one who started at 16 hrs 42 mins ago (Nov-10-2020 01:46:41 AM +UTC) who as successful

If I understood correctly, this transaction was faster than mine because use 34 Gwei and me only 31.

that’s correct?

Still, at a first glance your snippet looks ok - though It’s not clear where the metaKeeperContract contract comes from, and I don’t understand why you are evaluating estimateGas() which should be a resolved promise with a number.

wrong copy and past. I have updated my script

1 Like

I have notice also for this job “Keep3rV1Oracle”, when I have tried to estimate the gas for work(). I have got this error.

“gas required exceeds allowance (12487683) or always failing transaction”

it’s mean I am not allowed to run work()?

 ERROR	Error: processing response error (body={"jsonrpc":"2.0","id":44,"error":{"code":-32000,"message":"gas required exceeds allowance (12487683) or always failing transaction"}}, error={"code":-32000}, requestBody="{\"method\":\"eth_estimateGas\",\"params\":[{\"from\":\"0xd849511eba9f7daf7c45dcba182714a3d8ffe792\",\"to\":\"0x73353801921417f465377c8d898c6f4c0270282c\",\"data\":\"0x322e9f04\"}],\"id\":44,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://mainnet.infura.io/v3/4d613481619d49629d4a816115309fb2", code=SERVER_ERROR, version=web/5.0.2)
        at Logger.makeError (/opt/nodejs/node_modules/@ethersproject/logger/lib/index.js:179:21)
        at Logger.throwError (/opt/nodejs/node_modules/@ethersproject/logger/lib/index.js:188:20)
        at /opt/nodejs/node_modules/@ethersproject/web/lib/index.js:244:32
        at step (/opt/nodejs/node_modules/@ethersproject/web/lib/index.js:33:23)
        at Object.next (/opt/nodejs/node_modules/@ethersproject/web/lib/index.js:14:53)
        at fulfilled (/opt/nodejs/node_modules/@ethersproject/web/lib/index.js:5:58)
        at processTicksAndRejections (internal/process/task_queues.js:97:5) {
      reason: 'processing response error',
      code: 'SERVER_ERROR',
      body: {
        jsonrpc: '2.0',
        id: 44,
        error: {
          code: -32000,
          message: 'gas required exceeds allowance (12487683) or always failing transaction'
        }
      },
      error: Error: gas required exceeds allowance (12487683) or always failing transaction
          at getResult (/opt/nodejs/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:70:21)
          at /opt/nodejs/node_modules/@ethersproject/web/lib/index.js:222:46
          at step (/opt/nodejs/node_modules/@ethersproject/web/lib/index.js:33:23)
          at Object.next (/opt/nodejs/node_modules/@ethersproject/web/lib/index.js:14:53)
          at fulfilled (/opt/nodejs/node_modules/@ethersproject/web/lib/index.js:5:58)
          at processTicksAndRejections (internal/process/task_queues.js:97:5) {
        code: -32000,
        data: undefined
      },
      requestBody: '{"method":"eth_estimateGas","params":[{"from":"0xd849511eba9f7daf7c45dcba182714a3d8ffe792","to":"0x73353801921417f465377c8d898c6f4c0270282c","data":"0x322e9f04"}],"id":44,"jsonrpc":"2.0"}',
      requestMethod: 'POST',
      url: 'https://mainnet.infura.io/v3/4d613481619d49629d4a816115309fb2'
    }
    2020-11-10T19:22:47.838Z	INFO	UniswapV2SlidingOracle is not workable
    2020-11-10T19:22:48.143Z	INFO	HegicPoolKeep3r is not workable
    2020-11-10T19:22:48.462Z	INFO	YearnV1EarnKeep3r is not workable
    2020-11-10T19:22:48.463Z	AUTOTASK COMPLETE
1 Like

Hi @Bolo,

I assume that the transaction is reverting on:

        require(worked, "UniswapV2Oracle: !work");

That we are back to the job competition issue.

Also see: Job competition and Autotask frequency.

I have got my answer. I need to have at least 200 keep3r to be able to run work()

1 Like

A post was split to a new topic: How can I check in the script if my relayer has enough eth to pay tx fee?