Categories
Cryptocurrency Ethereum Mastering Development

Can’t estimate gas on Infura using web3 or axios

I’m trying to estimate gas required to transfer usdt using Infura node but always receive gas required exceeds allowance (12451164) or always failing transaction error. I have tried both using web3 and post (via axios) and both return same error.

Axios attempt:

export const estimateUsdtGas: EstimateGasType = async ({ web3 }, to, amount, wallet) => {
  const contract = new web3.eth.Contract(USDT_ABI, USDT_CONTRACT_ADDRESS)
  const gasPrice = await getGasPrice()
  const data = await contract.methods.transfer(to, '1').encodeABI()
  const body = {
    jsonrpc: '2.0',
    method: 'eth_estimateGas',
    params: [{
      value: web3.utils.numberToHex(0),
      gasPrice: web3.utils.numberToHex(gasPrice),
      to: USDT_CONTRACT_ADDRESS,
      from: wallet.address,
      data
    }],
    id: '1'
  }
  await axios.post(process.env.NODE_URL, body).then(res => {
    console.log(res.data)
    return res.data
  })
}

returns

{
 jsonrpc: '2.0',
 id: '122',
 error: {
   code: -32000,
   message: 'gas required exceeds allowance (12451164) or always failing transaction'
 }
}

web3 approach:

export const estimateUsdtGas: EstimateGasType = async ({ web3 }, to, amount) => {
  const contract = new web3.eth.Contract(USDT_ABI, USDT_CONTRACT_ADDRESS)
  return contract.methods.transfer(to, amount).estimateGas()
}

throws:

Error: Returned error: gas required exceeds allowance (12475588) or always failing transaction

USDT_ABI and contact address are correct, since if I hardcode gasLimit I am able to transfer.

Leave a Reply

Your email address will not be published. Required fields are marked *