solidity icon indicating copy to clipboard operation
solidity copied to clipboard

InternalCompilerError: Internal compiler error

Open Xardesso opened this issue 1 year ago • 2 comments

Hi i creating arbitrage bot using Solidity in Hardhat and i created smart contract which take aave flashloan make 2 swaps and repay flashloan itself and i got error when i tried to compile smart contract

Solidity version ^0.6.12
Hardhat version ^2.19.4
Operating System Windows 11

Code:

// SPDX-License-Identifier: MIT

pragma solidity ^0.6.12;
import "@aave/protocol-v2/contracts/interfaces/ILendingPool.sol";
import "@aave/protocol-v2/contracts/interfaces/ILendingPoolAddressesProvider.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/Context.sol";

contract ArbitrageBot1 is Context, ReentrancyGuard {
    using SafeMath for uint256;

    ILendingPoolAddressesProvider provider;
    ILendingPool lendingPool;
    IUniswapV2Router02 uniswapRouter;
    IUniswapV2Router02 sushiSwapRouter;
    address private owner;

    constructor(
        address _provider,
        address _uniswapRouter,
        address _sushiSwapRouter
    ) public {
        provider = ILendingPoolAddressesProvider(_provider);
        lendingPool = ILendingPool(provider.getLendingPool());
        uniswapRouter = IUniswapV2Router02(_uniswapRouter);
        sushiSwapRouter = IUniswapV2Router02(_sushiSwapRouter);
        owner = msg.sender;
    }

    modifier OnlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function borrowAndSwap(
        uint256 _amount,
        uint256 amountOutExpected,
        uint256 amountOutExpected2,
        address coin1,
        address coin2
    ) external nonReentrant {
        lendingPool.borrow(coin1, _amount, 1, 0, address(this));

        address[] memory path2 = new address[](2);
        path2[0] = coin1;
        path2[1] = coin2;

        sushiSwapRouter.swapExactTokensForTokens(
            _amount,
            amountOutExpected,
            path2,
            address(this),
            block.timestamp
        );

        address[] memory path = new address[](2);
        path[0] = coin2;
        path[1] = coin1;

        uniswapRouter.swapExactTokensForTokens(
            IERC20(coin2).balanceOf(address(this)),
            amountOutExpected2,
            path,
            address(this),
            block.timestamp
        );

        // Get the borrow rate
        DataTypes.ReserveData memory reserveData = lendingPool.getReserveData(
            coin1
        );
        uint256 borrowRate = reserveData.currentVariableBorrowRate;

        // Calculate the amount of interest
        uint256 interest = _amount.mul(borrowRate).div(1e27);

        // Calculate the total amount to repay
        uint256 amountToRepay = _amount.add(interest);

        // Approve the lending pool to transfer the repayment amount
        IERC20(coin1).approve(address(lendingPool), amountToRepay);

        // Repay the borrowed amount
        lendingPool.repay(coin1, amountToRepay, 1, address(this));
    }

    function withdrawWETH(address outCoin) external OnlyOwner {
        require(
            IERC20(outCoin).balanceOf(address(this)) > 0,
            "No funds to withdraw"
        );
        IERC20(outCoin).transfer(
            owner,
            IERC20(outCoin).balanceOf(address(this))
        );
    }}

And error:

 $ npx hardhat compile 
InternalCompilerError: Internal compiler error (C:\projects\solidity\libsolidity\codegen\CompilerUtils.cpp:1441)
Error HH600: Compilation failed

For more info go to https://hardhat.org/HH600 or run Hardhat with --show-stack-traces

Xardesso avatar Dec 31 '23 18:12 Xardesso

We always consider an internal compiler error (ICE) to be a bug (usually a low priority one), so thanks for the report. @Xardesso can you please post the exact version of the compiler you were using? ^0.6.12 just means higher than 0.6.12, so I assume you weren't using 0.6.x, since those are fairly old.

nikola-matic avatar Jan 10 '24 09:01 nikola-matic

Well ^0.6.12 also means <0.7.0 and 0.6.12 was the last in the 0.6.x series, so it must have been 0.6.12.

It looks like this assert: https://github.com/ethereum/solidity/blob/27d51765c0623c9f6aef7c00214e9fe705c331b1/libsolidity/codegen/CompilerUtils.cpp#L1439-L1441

Looks like it might have already been reported in #12114 but that issue was closed by the user because they found a workaround. The ICE might still be there though - we should try to reproduce on 0.8.23 (but with abicoder v1 since it's no longer the default).

cameel avatar Jan 10 '24 11:01 cameel