0

i want to transfer ALL the ether in my account to the smart contract. I am calculating the gasEstimate and then subtracting it from my total ether balance...but i get an ERROR saying code: -32000, message: 'gas required exceeds allowance (13179)'

const grantorEtherBalance: number = Number(
      ethers.utils.formatUnits(await provider.getBalance(grantorAddress), 'ether'),
    )
const wallet = new ethers.Wallet(privateKey, provider)
const psigner = wallet.connect(provider)

    //send the ether from grantor's wallet to smart contract
    const forGasEstimatetx = {
      to: '0xa013618172F9E3514843CBF3a45221479A2E4297',
      value: ethers.utils.parseEther('0'),
    }
const gasEstimate: any = await wallet.estimateGas(forGasEstimatetx)

    const sub = feeData * parseInt(gasEstimate)

    const sendingEther = grantorEtherBalance - sub

    const tx = {
      to: '0xa013618172F9E3514843CBF3a45221479A2E4297',
      value: ethers.utils.parseEther(sendingEther.toString()),
    }
    const transaction = await psigner.sendTransaction(tx)


    const receipt = await transaction.wait()
    console.log('receipt', receipt)```
3
  • 2
    you better transfer all the funds to another account (EOA) you are using, and then from there, transfer to the contract. Because when you call the contract with the balance of the calling account emptied many irregularities can happen. EstimateGas() function is not 100% precise, and in your case, it has failed to calculate the gas to clear the balance completely. Thats why it is better to empty and account into another EOA instead of a contract
    – Nulik
    Feb 3 at 0:17
  • Try setting the value to a non zero ether amount to calculate the estimation. Also, set the gas limit, and gas price in the final transaction. In some cases, in complex contracts, the gas limit might need to be greater due to the estimation being dependant on the contract state. It could happend to leave some ether behind due to gas refund.
    – Ismael
    Feb 11 at 5:33

0

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.

Browse other questions tagged or ask your own question.