ethereumsolidityethers.jshardhatmatic

Balancer flashloan transaction execution reverting on Polygon Mumbai Testnet


I'm trying to call the flashLoan() function on Balancer's vault contract but I keep getting an 'execution reverted' error when I view the transaction on polygonscan mumbai.

Here's my smart contract code:

// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.7;
pragma abicoder v2; 
import "@balancer-labs/v2-interfaces/contracts/vault/IFlashLoanRecipient.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

interface Vault {
    function flashLoan(
        IFlashLoanRecipient recipient,
        IERC20[] memory tokens,
        uint256[] memory amounts,
        bytes memory userData
    ) external;
}

contract FlashLoanSimple is IFlashLoanRecipient {
    using SafeMath for uint256;
    address internal constant vaultAddr = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;

    Vault vault = Vault(vaultAddr);

    function receiveFlashLoan(IERC20[] memory tokens,uint256[] memory amounts,uint256[] memory 
    feeAmounts,bytes memory userData) external override {   
        userData;
    
        for (uint i = 0; i < tokens.length; i++) {
            uint amountOwing = amounts[i].add(feeAmounts[i]);
            if(address(this).balance >= amountOwing) {
                IERC20(tokens[i]).approve(vaultAddr,amountOwing);
                IERC20(tokens[i]).transfer(vaultAddr, amountOwing);
            }             
        }
    }

    function executeFlashLoan(address _flTokenAddr,uint256 _flAmount) external {
        IERC20 flToken = IERC20(_flTokenAddr);
        IERC20[] memory flTokens = new IERC20[](1);
        uint256[] memory flAmounts = new uint256[](1);
        flTokens[0] = flToken;
        flAmounts[0] = _flAmount ;
        Vault(vaultAddr).flashLoan(IFlashLoanRecipient(address(this)),flTokens,flAmounts,"");
    }
}

and here is part of my script:

const maticProvider = new ethers.providers.JsonRpcProvider("https://polygon- 
mumbai.g.alchemy.com/v2/Q0iw05Ps4B9YV10FMtVUSBOzxNKVoV2x");
const private_key = "xxxxxxxxxxxxxxxxxxxxxxxx";
const signer = new ethers.Wallet(private_key, maticProvider);
const Flashloan = new ethers.Contract(flashloanAddress,FlashloanJson.abi,maticProvider);

export const flashloan = async (address: string, amountIn: BigNumber) => {
  const gasPrice = await maticProvider.getGasPrice();
  const tx = await Flashloan.connect(signer).executeFlashLoan(address,amountIn, {gasLimit: 
  15000000,gasPrice: gasPrice});
  return tx;
};

Any help would be greatly appreciated, I was getting an error related to gas before, but I'm no longer getting that error after I increased the gas limit. My contract is deployed on the Polygon Mumbai network and has a balance of 3 MATIC.


Solution

  • The problem was that I was using the Mumbai network, it only works when deployed on the mainnet.