AAVE Supply Pool – Resolving ‘ERC20: Transfer Amount Exceeds Allowance’ Error in AAVE Supply Pool

aaveethers.jshardhatsolidity

I'm trying write a contract that takes a transfer from a user which then deposits the sent tokens into an AAVE lending pool (I'm using USDT for testing on Georli testnet). I was able to get the funds into the contract via a standard ERC20 approval and transferFrom and confirmed that the contract received the funds. When I run the supply function though it says "Fail with error 'ERC20: transfer amount exceeds allowance'". I know that is typically the error you get when the funds haven't been approved for spending but the funds are already in the contract. You can see this on etherscan https://goerli.etherscan.io/address/0xcb4C0F8DcFd58736bfbC946185845B87857a89DD
The fund transaction goes through and the tokens are in the contract, but it won't let me supply the same amount. Here is the code for those two relevant functions

    function fund(address sender, uint256 amount) public {
        // Set initial amount for funder
        IERC20(i_assetAddress).transferFrom(sender, address(this), amount);
        s_funders[sender] = amount;
    }

    function supplyLiquidity(uint256 amount) public {
        IPool(i_poolAddress).supply(i_assetAddress, amount, address(this), 0);
    }

And here is the revelant typescript testing code I've been using.

              fundMe = await fundMeContract.connect(deployer)
              const approveTx = await assetToken.approve(fundMe.address, fundValueWithDecimals)
              await approveTx.wait(1)

              const fundTx = await fundMe.fund(deployer.address, fundValueWithDecimals)
              await fundTx.wait(1)

              const fundAmount = await fundMe.getFundAmount(deployer.address)

              const supplyTx = await fundMe.supplyLiquidity(fundValueWithDecimals)
              await supplyTx.wait(1)

              console.log(`fundAmount: ${fundAmount} | fundValue: ${fundAmount}`)
              assert.equal(fundAmount.toString(), fundValueWithDecimals.toString())

I'm not really sure what I'm missing here… Is it possible that I have to somehow let the contract approve itself to spend? That doesn't sound right but I'm not sure what else it could be. Any help would be appreciated.

Best Answer

Figured it out: You do need to approve the Lending Pool to access the tokens from the contract. Here's the function I wrote in the contract

function fund(address sender, uint256 amount) public {
        // Set initial amount for funder
        IERC20(i_assetAddress).transferFrom(sender, address(this), amount);
        approveOtherContract(IERC20(i_assetAddress), i_poolAddress);
        IPool(i_poolAddress).supply(i_assetAddress, amount, address(this), 0);

        s_funders[sender] += amount;
    }

    function approveOtherContract(IERC20 token, address recipient) public {
        token.approve(recipient, 1e18);
    }
Related Topic