ParserError: Source file requires different compiler version

ParserError: Source file requires different compiler version

我尝试了您在此处(在其他问题中)和 https://github.com/smartcontractkit/full-blockchain-solidity-course-py/discussions/522 的讨论中提到的所有内容,但是它并没有解决我的问题,我还注意到当前的编译器版本仍然存在(当前的编译器是 0.6.12+commit.27d51765.Windows.msvc)。但是当我右击 select Solidty:Compiler 信息时,它显示 0.8.0.

来自输出:

Retrieving compiler information:
Compiler using remote version: 'v0.8.0+commit.c7dfd78e', solidity version: 0.8.0+commit.c7dfd78e.Emscripten.clang

不确定这是否与我面临的问题有关。无论如何,从 运行 brownie 编译时我看到的问题开始。我收到以下错误:

终端错误:

PS D:\Python projects\Solidity dev\demo\smartcontract-lottery> brownie compile
INFO: Could not find files for the given pattern(s).
Brownie v1.17.2 - Python development framework for Ethereum

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

C:/Users/rosne/.brownie/packages/OpenZeppelin/openzeppelin-contracts@4.3.0/contracts/access/Ownable.sol:3:1: ParserError: Source file requires different compiler version (current compiler is 0.6.12+commit.27d51765.Windows.msvc) - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.8.0;
^---------------------^

C:/Users/rosne/.brownie/packages/smartcontractkit/chainlink-brownie-contracts@0.2.1/contracts/src/v0.8/VRFConsumerBase.sol:2:1: ParserError: Source file requires different compiler version (current compiler is 0.6.12+commit.27d51765.Windows.msvc) - note that nightly builds are considered to be strictly less than the released version
pragma solidity ^0.8.0;
^---------------------^

PS D:\Python projects\Solidity dev\demo\smartcontract-lottery>

我的 .sol 文件是 Lottery.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.8/VRFConsumerBase.sol";

contract Lottery is VRFConsumerBase, Ownable {
    uint256 usdEntryFee;
    address payable[] public players;
    address payable public recentWinner;
    uint256 public randomness;
    AggregatorV3Interface internal ethUsdPriceFeed;
    enum LOTTERY_STATE {
        OPEN,
        CLOSED,
        CALCULATING_WINNER
    }

    LOTTERY_STATE public lottery_state;
    uint256 public fee;
    bytes32 public keyhash;

    constructor(
        address _priceFeedAddress,
        address _vrfCoordinator,
        address _link,
        uint256 _fee,
        bytes32 _keyhash
    ) public VRFConsumerBase(_vrfCoordinator, _link) {
        usdEntryFee = 50 * (10**18);
        ethUsdPriceFeed = AggregatorV3Interface(_priceFeedAddress);
        lottery_state = LOTTERY_STATE.CLOSED;
        fee = _fee;
        keyhash = _keyhash;
    }

    function enter() public payable {
        // min
        require(lottery_state == LOTTERY_STATE.OPEN);
        require(msg.value >= getEntranceFee(), "Not enough ETH!");
        players.push(payable(msg.sender));
    }

    function getEntranceFee() public view returns (uint256) {
        (, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
        uint256 adjustedPrice = uint256(price) * 10**12; //18 decimals
        //, 2000 ETH
        //50/2000
        //50*10000/2000
        uint256 costToEnter = (usdEntryFee * 10**18) / adjustedPrice;
        return costToEnter;
    }

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

    function endLottery() public onlyOwner {
        lottery_state = LOTTERY_STATE.CALCULATING_WINNER;
        bytes32 requestId = requestRandomness(keyhash, fee);
    }

    function FulfillRandomness(bytes32 _requestId, uint256 _randomness)
        internal
        override
    {
        require(
            lottery_state == LOTTERY_STATE.CALCULATING_WINNER,
            "you arent there yet!"
        );

        require(_randomness > 0, "random not found");
        uint256 indexOfWinner = _randomness % players.length;
        recentWinner = players[indexOfWinner];
        recentWinner.transfer(address(this).balance);

        //reset

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

我也尝试 google 一些解决方案,所以我的 settings.json 文件有点不同,但这也没有帮助。

settings.json:

{
    "solidity.compileUsingRemoteVersion": "v0.8.0+commit.c7dfd78e",
    "solidity.defaultCompiler": "remote",
    "solidity.compileUsingLocalVersion": "d:\Python projects\Solidity dev\demo\smartcontract-lottery\soljson-v0.8.0+commit.c7dfd78e.js"
    // "solidity.compileUsingRemoteVersion": "v0.7.4+commit.3f05b770",
    // "solidity.enableLocalNodeCompiler": false
}

在 brownie-config.yaml 中,我也尝试了从旧到最新的所有 openzepplin 合约版本(4.4.0、4.3.0、4.3.2 等),但同样的错误。

布朗尼-config.yaml

dependencies:
  - smartcontractkit/chainlink-brownie-contracts@1.1.1
  - OpenZeppelin/openzeppelin-contracts@4.3.0
compiler:
  solc:
    remappings:
      - '@chainlink=smartcontractkit/chainlink-brownie-contracts@0.2.1'
      - '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.3.0'
networks:
  mainnet-fork:
    eth_usd_price_feed: '0xaEA2808407B7319A31A383B6F8B60f04BCa23cE2'

我还尝试将 lottery.sol 文件中的编译器更改为

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

现在我得到一个不同的错误。 完全迷失在这里:(

终端机:

INFO: Could not find files for the given pattern(s).
Brownie v1.17.2 - Python development framework for Ethereum

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.Windows.msvc) - note that nightly builds are considered to be strictly less than the released version
 --> C:/Users/rosne/.brownie/packages/smartcontractkit/chainlink-brownie-contracts@0.2.1/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol:2:1:
  |
2 | pragma solidity ^0.6.0;
  | ^^^^^^^^^^^^^^^^^^^^^^^

PS D:\Python projects\Solidity dev\demo\smartcontract-lottery> 

我是 solidity 编程的新手,这是我学习的第一门课程,我不想轻易放弃,非常感谢任何帮助。

我遇到了同样的问题。我有这个编译器设置:

pragma solidity >=0.4.22 <0.9.0;

由于我们是从 openzeppelin 导入的,所以我查看了 github 存储库,发现它使用 pragma solidity ^0.8.0;

所以将我的设置更改为

 pragma solidity >=0.4.22 <0.8.0;

编译后我搜索了“>=0.6.0 <0.8.0”并得到了那些文件。他们都来自openzeppelin

我没有在 brownie-yaml 文件中添加 solc 版本

我设法找到了问题,这是因为函数名称应该是 fullfillRandomness 而不是 FullfillRandomness。 一个大写的F就出这么多问题

function FulfillRandomness(bytes32 _requestId, uint256 _randomness)
        internal
        override

错误的解决方法是

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;

改用这个,希望对你有帮助