无法编译多个Solidity版本

15
我正在尝试通过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"
      }
    ]
  }
   ...
5个回答

7

我有一个类似的问题。

在我的情况下,我的合约使用了pragma solidity version ^0.8.0

为了解决这个问题,我在我的hardhat.config.js文件中添加了这些代码(通常是在现有module.exports中)。

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

我刚刚删除了版本号前面的 "^" 符号。

7

解决方案:

从每个接口中获取我感兴趣的函数的签名,并在我的接口上使用 pragma solidity ^0.8.0 将它们放置。


2
是的,这个可以工作,可惜编译器让我们跳过这些荒谬的步骤,因为它认为一个函数可能不存在于特定的地址。 - Alexander Barry

5

只需尝试在hardhat.config.js文件中进行设置即可。

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

};

点击此处查看更多信息!!!


3
你好!这跟我在最初的帖子中尝试的有什么不同吗?(指它的最后一部分) - dNyrM

2
我发现来自《Hardhat FAQ》的信息很有用:
在某些情况下,您可能会遇到具有pragma版本^0.7.0并导入具有^0.6.0的合同的情况。这是无法编译的
如果^0.6.0文件来自依赖项,则一种可能的解决方法是升级该依赖项(假设较新版本使用了更高版本的solidity)。或者,您可能需要降低项目中合同的pragma版本。

0
这个问题的一个解决方案是在Contracts文件夹下的"Lock.sol"。那里的pragma solidity版本往往与其他文件不同,因此您必须使它们相同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接