Getting a execution reverted: ERC20: transfer amount exceeds balance error


I'm trying to withdraw funds from the Aave pool using the WETHGateway (on Polygon), however I'm getting a execution reverted: ERC20: transfer amount exceeds balance error.

Here is my contract:

contract openDEFI {
  address private GATEWAY = 0xbEadf48d62aCC944a06EEaE0A9054A90E5A7dc97;
  address private LENDINGPOOL = 0x8dFf5E27EA6b7AC08EbFdf9eB090F32ee9a30fcf;
  address private WETH = 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270;
  function deposit() public payable {
    IWETHGateway(GATEWAY).depositETH{value: msg.value}(LENDINGPOOL, msg.sender, 0);

  function withdraw(uint256 amount) public payable {
    IAToken aWETH = IAToken(ILendingPool(LENDINGPOOL).getReserveData(address(WETH)).aTokenAddress);
    aWETH.approve(GATEWAY, amount);
    IWETHGateway(GATEWAY).withdrawETH(LENDINGPOOL, amount, msg.sender);

Answers 3

The issue you're encountering, execution reverted: ERC20: transfer amount exceeds balance, typically indicates that the smart contract attempted to transfer more tokens than are currently available in the contract's or the user's balance. This error is a common safeguard within ERC20 tokens to prevent overdrawn transactions and is enforced by the Ethereum Virtual Machine (EVM) during contract execution.

To address the specific problem with your contract's withdraw function when interacting with the Aave pool via the WETHGateway on Polygon, let's analyze the steps involved and how the contract's logic could lead to such execution reverted errors:

  1. Approval Process: Before withdrawing, the aWETH.approve(GATEWAY, amount) line is executed to grant the WETHGateway permission to withdraw the specified amount of aWETH tokens on behalf of your contract. This operation assumes that the contract itself holds a sufficient balance of aWETH tokens.

  2. Balance Verification: The error suggests that when IWETHGateway(GATEWAY).withdrawETH(LENDINGPOOL, amount, msg.sender) is called, the contract may not have a sufficient balance of aWETH tokens to support the withdrawal request. This can happen if the amount specified exceeds the aWETH balance that the contract or the user has within Aave.

  3. Transaction Failed: Due to the EVM's strict execution rules, any violation such as attempting to transfer more than the available balance immediately reverts the entire transaction to ensure atomicity. This means no changes are made to the state, but gas used up to the point of failure is consumed.

How to Fix the Issue

  • Ensure Sufficient Balance: Before calling the withdraw function, confirm that the balance of aWETH tokens (or the relevant interest-bearing token version of the asset you're trying to withdraw) is sufficient. This check should account for both the contract's balance and potentially the user's balance, depending on the exact logic you wish to implement.

  • Debugging: Utilize smart contract development tools to simulate the transaction and pinpoint the exact cause of the error. Tools like Hardhat or Truffle offer testing frameworks that can help reproduce and diagnose issues with local blockchain simulation.

  • Contract Logic Adjustment: Consider adding a balance check within your contract before attempting the withdrawal. This could be a simple conditional check that only proceeds with the withdrawal if the balance check passes, otherwise, it could revert with a more informative error message.

  • Approval and Withdrawal Workflow: Double-check the approval and withdrawal workflow to ensure that the correct amounts are being approved and that the withdrawal request matches the available balance. It's also crucial to ensure that the contract calling the withdrawal has been properly granted permission to manage the user's tokens if the tokens are not directly held by the contract.

By addressing these potential issues, you can refine your smart contract's interaction with the Aave protocol and avoid common pitfalls related to ERC20 token transfers and the execution of complex transactions within the EVM environment.

I think its because atokens are held by msg.sender and you are trying to initiate withdraw from the contract.. You will need to transfer aTokens from sender to the contract.

The error message "execution reverted: ERC20: transfer amount exceeds balance" suggests that the account trying to withdraw the funds does not have sufficient balance in the Aave pool to complete the withdrawal.

There are a few things that you should check to troubleshoot this issue:

Verify that the account calling the withdraw function has enough balance in the Aave pool to cover the amount being withdrawn. You can check the balance of an account for a specific token using the getUserAccountData function of the LendingPool contract.

Make sure that the approve function call is executed before the withdrawETH function call, this allow to gateway contract to transfer the tokens.

Also check that you are passing the correct address for the WETH token, and that this token is supported by the LendingPool contract.

Verify that the LendingPool and Gateway contracts are deployed and active on the correct network and that you are interacting with the correct contract addresses

Check that the gas limit and gas price for the transaction are set high enough to complete the withdrawal.

If the error persists, you can try using a higher version of the contract and see if that resolves the issue.

It's worth mentioning that the above answer is based on the code you've provided, it's always a good idea to double check the documentation and the specific implementation of the platform you are working with.