EatTheBlocks Forum

Error: Transaction reverted: function call failed to execute

Hello!

I am using the following code as a reference to study Flash Loans. I have been struggling this the error Error: Transaction reverted: function call failed to execute exactly in the line of otherToken.deposit{value: amountRequired}();. I do not understand the error. Basically, I am depositing the tokens after the flash loan and transferring the tokens to the pair contract (paying back the loan), and send the leftover to my wallet. Have you had the same error before?

pragma solidity ^0.6.6;

import './UniswapV2Library.sol';

import './interfaces/IUniswapV2Router02.sol';

import './interfaces/IUniswapV2Pair.sol';

import './interfaces/IUniswapV2Factory.sol';

import './interfaces/IERC20.sol';

contract Arbitrage {

  address public pancakeFactory;

  uint constant deadline = 10 days;

  // Pointer to the bakeryRouter (Trade in BakerySwap LP)

  IUniswapV2Router02 public bakeryRouter;

  constructor(address _pancakeFactory, address _bakeryRouter) public {

    pancakeFactory = _pancakeFactory;  

    bakeryRouter = IUniswapV2Router02(_bakeryRouter);

  }

  function startArbitrage(

    address token0, 

    address token1, 

    uint amount0, 

    uint amount1

  ) external {

    address pairAddress = IUniswapV2Factory(pancakeFactory).getPair(token0, token1);

    require(pairAddress != address(0), 'This pool does not exist');

    IUniswapV2Pair(pairAddress).swap(

      amount0, 

      amount1, 

      address(this), 

      bytes('not empty')

    );

  }

  function pancakeCall(

    address _sender, 

    uint _amount0, 

    uint _amount1, 

    bytes calldata _data

  ) external {

    address[] memory path = new address[](2);

    uint amountToken = _amount0 == 0 ? _amount1 : _amount0;

    

    address token0 = IUniswapV2Pair(msg.sender).token0();

    address token1 = IUniswapV2Pair(msg.sender).token1();

    require(

      msg.sender == UniswapV2Library.pairFor(pancakeFactory, token0, token1), 

      'Unauthorized'

    ); 

    require(_amount0 == 0 || _amount1 == 0);

    path[0] = _amount0 == 0 ? token1 : token0;

    path[1] = _amount0 == 0 ? token0 : token1;

    IERC20 token = IERC20(_amount0 == 0 ? token1 : token0);

    

    token.approve(address(bakeryRouter), amountToken);

    uint amountRequired = UniswapV2Library.getAmountsIn(

      pancakeFactory, 

      amountToken, 

      path

    )[0];

    uint amountReceived = bakeryRouter.swapExactTokensForTokens(

      amountToken, 

      amountRequired, 

      path, 

      msg.sender, 

      deadline

    )[1];

    IERC20 otherToken = IERC20(_amount0 == 0 ? token0 : token1);
    otherToken.deposit{value: amountRequired}();

    otherToken.transfer(msg.sender, amountRequired);

    otherToken.transfer(tx.origin, amountReceived - amountRequired);

  }

}