无法编译多个 Solidity 版本

Failing to compile multiple Solidity versions

我正在尝试编译(通过 Hardhat)一个合约,该合约导入多个具有不同 Solidity 版本的接口,但我收到以下错误:

Error HH606: The project cannot be compiled, see reasons below.

These files and its dependencies cannot be compiled with your config. This can happen because they have incompatible Solidity pragmas, or don't match any of your configured Solidity compilers.

  * contracts/FlashLoaner.sol

Flashloaner.sol:

pragma solidity >=0.5.0 <=0.8.0;

import '@uniswap/v2-periphery/contracts/interfaces/IWETH.sol';
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import '@aave/protocol-v2/contracts/interfaces/ILendingPool.sol'; //---> Issue
import "hardhat/console.sol";


contract FlashLoaner {
    struct MyCustomData {
        address token;
        uint256 repayAmount;
    }

    address public logicContract;
    
    function execute(address _weth, address _contract) external view {
        console.log(_weth);
    }
}

问题出在 @aave/protocol-v2/contracts/interfaces/ILendingPool.sol。如果我将其注释掉,我的合约编译良好。

IlendingPool.sol: pragma solidity 0.6.12;

IERC20.sol: pragma solidity ^0.5.0;

IWETH.sol: pragma solidity >=0.5.0;

Hardhat.config:

module.exports = {
  solidity: {
    compilers: [
      {
        version: "0.5.7"
      },
      {
        version: "0.8.0"
      },
      {
        version: "0.6.12"
      }
    ]
  }
   ...

解决方案:

从每个接口中抓取我感兴趣的函数的签名,并将它们放在我自己的接口上 pragma solidity ^0.8.0

只需尝试在 hardhat.config.js

上设置
    module.exports = {   solidity: {
        compilers: [
          {
            version: "0.5.5",
          },
          {
            version: "0.6.7",
            settings: {},
          },
        ],   
}, 

};

see more!!!!

我遇到了类似的问题。

在我的例子中,我的合约使用了 pragma solidity 版本 ^0.8.0

为了解决这个问题,我将这些行添加到我的 hardhat.config.js(大多数情况下在现有的 module.exports 中)。

module.exports = {
  solidity: "0.8.0",
}

刚刚把版本前的“^”删掉了。 我希望它可以帮助别人。

我发现来自 A Hardhat FAQ 的信息很有用:

In some scenarios, you might have a contract with pragma version ^0.7.0 that imports a contract with ^0.6.0. This can never be compiled.

If the ^0.6.0 file comes from a dependency, one possible fix is to upgrade that dependency (assuming newer versions use a newer version of solidity). Alternatively, you might need to downgrade the pragma versions of the contracts in your project.