简体   繁体   English

solidity: 错误信息 源文件需要不同的编译器版本

[英]solidity : error information Source file requires different compiler version

When compiling the lottery.sol, met this compiler error, as a rookie to development, didn't quite understand the error, what's the solution to solve this compiler version issue, it's really frustrating...在编译lottery.sol的时候遇到了这个编译错误,作为一个开发菜鸟,对这个错误不是很理解,请问这个编译版本问题有什么办法解决,真是郁闷。。。

Compiling contracts...
  Solc version: 0.6.12
  Optimizer: Enabled  Runs: 200
  EVM Version: Istanbul
CompilerError: solc returned the following errors:

/Users/liwei/.brownie/packages/smartcontractkit/chainlink-brownie-contracts@1.1.1/contracts/src/v0.6/vendor/SafeMathChainlink.sol:2:1: ParserError: Source file requires different compiler version (current compiler is 0.6.12+commit.27d51765.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
pragma solidity >=0.7.0 <0.9.0;

lottery.sol彩票.sol

// SPDX-License-Identifier:MIT
pragma solidity ^0.6.0;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";
import "@chainlink/contracts/src/v0.6/vendor/SafeMathChainlink.sol";

contract Lottery is VRFConsumerBase, Ownable {
    AggregatorV3Interface internal ethUsdPriceFeed;
    uint256 usdEntryFee;
    address[] public players;
    bytes32 internal keyHash; //public key to generate randomness,uniquely identify the chainlink node.
    uint256 internal fee;
    uint256 public randomness;

    enum LOTTERY_STATE {
        OPEN,
        CLOSED,
        CALCULATING_WINNER
    }
    LOTTERY_STATE public lottery_state;

    constructor(
        address _priceFeedAddress,
        address _VRFCoordinator, //contract to validate the random number
        address _link, //address to pay oracle gas
        uint256 _fee,
        bytes32 _keyhash
    ) public VRFConsumerBase(_vrfCoordinator, _link) {
        usdEntryFee = 50 * (10**18);
        ethUsdPriceFeed = AggregatorV3Interface(_priceFeedAddress);
        lottery_state = CLOSED;
        fee = _fee;
        keyhash = _keyhash;
    }

    function enter() public payable {
        require(lottery_state == OPEN);
        // require amount>=entrance fee ($50 in Ether)
        require(msg.value >= getEntranceFee(), "Not enought ETH!");
        // push player's address into the the array
        players.push(msg.sender);
    }

    function getEntranceFee() public view returns (uint256) {
        (, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
        uint256 adjustedPrice = uint256(price) * (10**10); // 18 decimals
        // $50,$2,000 / ETH
        // 50/2,000
        // 50 *100000 / 20000
        uint256 costToEnter = (usdEntryFee * 10**18) / adjustedPrice; //usdEntryFee has additional 18 decimals
        return costToEnter;
    }

    function startLottery() public {
        require(lottery_state == CLOSED, "Can't start a new lottery yet");
        lottery_state = LOTTERY_STATE.OPEN;
    }

    function endLottery() public {
        lottery_state = LOTTERY_STATE.CLOSED;
        require(
            LINK.balanceOf(address(this)) >= fee,
            "Not enough LINK - fill contract with faucet"
        );
        bytes32 requestId = requestRandomness(keyHash, fee); // make the inital request for randomness
    }

    function fulfillRandomness(bytes32 _requestId, uint256 _randomness)
        internal
        override
    {
        require(
            lottery_state == LOTTERY_STATE.CALCULATING_WINNER,
            "You are n't there yet !"
        );
        require(_randomness > 0, "radnom-not-found !");
        uint256 indexOfWinner = _radonmness % players.length; // get the remainder,Modulo pattern
        recentWinner = players[indexOfWinner];
        rencentWinner.tranfer(address(this).balance);

        // reset the players array

        players = new address payable[](0);
        lottery_state = LOTTERY_STATE.CLOSED;
        randomness = _randomness;
    }
}

After changing the compiler version as the error information suggested, it suggested switching the compiler version back.按照错误信息提示更改编译器版本后,提示将编译器版本切换回来。

New compatible solc version available: 0.8.11
Compiling contracts...
  Solc version: 0.8.11
  Optimizer: Enabled  Runs: 200
  EVM Version: Istanbul
CompilerError: solc returned the following errors:

ParserError: Source file requires different compiler version (current compiler is 0.8.11+commit.d7f03943.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
 --> /Users/liwei/.brownie/packages/OpenZeppelin/openzeppelin-contracts@3.4.0/contracts/access/Ownable.sol:3:1:
  |
3 | pragma solidity >=0.6.0 <0.8.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


ParserError: Source file requires different compiler version (current compiler is 0.8.11+commit.d7f03943.Darwin.appleclang) - note that nightly builds are considered to be strictly less than the released version
 --> /Users/liwei/.brownie/packages/smartcontractkit/chainlink-brownie-contracts@1.1.1/contracts/src/v0.6/VRFConsumerBase.sol:2:1:
  |
2 | pragma solidity ^0.6.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^

(base) liwei@liweideMacBook-Pro smartcontract-lottery2022 % 

It pointed out that SafeMathChainlink.sol use another compiler version, which is incompatible with the one you're using.它指出SafeMathChainlink.sol使用了另一个编译器版本,该版本与您正在使用的版本不兼容。

So here are something you could try:所以这里有一些你可以尝试的东西:

  • Import the SafeMathChainlink.sol complied with 0.6.x instead of 0.7-0.9 .导入符合0.6.x而不是0.7-0.9SafeMathChainlink.sol
  • Change the compiler to 0.7.x and looks for the VRFConsumerBase.sol complied with 0.7.x (Might be something like: @chainlink/contracts/src/v0.6/VRFConsumerBase.sol )将编译器更改为0.7.x并查找符合0.7.x VRFConsumerBase.sol可能类似于: @chainlink/contracts/src/v0.6/VRFConsumerBase.sol
  • If the two things above is impossible, than you can create VRFConsumerBase.sol or VRFConsumerBase.sol yourself with the compiler version you desired to use.如果以上两件事是不可能的,那么您可以使用您希望使用的编译器版本自己创建VRFConsumerBase.solVRFConsumerBase.sol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Solidity:抽象合约并覆盖 VRFConsumerBase 编译器/版本/导入问题 - Solidity: Abstract contract and override VRFConsumerBase compiler/version/import issues 在不同源文件中声明的同名类没有编译器/链接器错误 - No compiler/linker error for same name classes declared in different source files keil编译器错误-无法打开源输入文件-但文件存在 - keil compiler error - can not open source input file - but file exists Solidity 和 truffle 的编译器问题 - compiler problem with solidity and truffle 本机编译器与 web 编译器的不同错误 - Different error on native compiler vs web compiler 无法编译需要 C99 编译器 (AFAIU) 的 pyethash python package。 错误 - 无法打开包含文件:'alloca.h' - Cannot compile pyethash python package which requires C99 compiler (AFAIU). Error - Cannot open include file: 'alloca.h' C ++编译器模板错误信息 - 解码错误信息的工具 - C++ compiler template error information - tool to decode the error information 使用具有不同编译器版本的boost库 - Using boost library with different compiler version Java 8编译器错误 - 如何获取更多信息? - Java 8 compiler error - how to get more information? SOLIDITY REMIX 编译器,在部署我的合约后收到此错误(无效的 BigNumber 字符串) - SOLIDITY REMIX compiler, after deploying my contract getting this ERROR (Invalid BigNumber string)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM