ParserError:源文件需要不同的编译器版本

Ros*_*rns 3 python ethereum solidity smartcontracts brownie

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

从输出:

Retrieving compiler information:
Compiler using remote version: 'v0.8.0+commit.c7dfd78e', solidity version: 0.8.0+commit.c7dfd78e.Emscripten.clang
Run Code Online (Sandbox Code Playgroud)

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

终端错误:

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>
Run Code Online (Sandbox Code Playgroud)

我的 .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 {
        //$50 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
        //$50, 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;
    }
}

Run Code Online (Sandbox Code Playgroud)

我还尝试用谷歌搜索一些解决方案,因此我的 settings.json 文件有点不同,但这也没有帮助。

设置.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
}
Run Code Online (Sandbox Code Playgroud)

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

布朗尼配置.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'
Run Code Online (Sandbox Code Playgroud)

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

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
Run Code Online (Sandbox Code Playgroud)

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

终端:

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> 
Run Code Online (Sandbox Code Playgroud)

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

小智 7

错误的解决办法是

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.9.0;
Run Code Online (Sandbox Code Playgroud)

改用这个,希望有帮助