python - 解析器错误 : Source file requires different compiler version

标签 python ethereum solidity smartcontracts brownie

我尝试了您在此处(在其他问题中)和 https://github.com/smartcontractkit/full-blockchain-solidity-course-py/discussions/522 的讨论中提到的所有内容,但是它并没有为我解决问题,我还注意到当前的编译器版本仍然存在(当前编译器是 0.6.12+commit.27d51765.Windows.msvc)。但是当我右键单击并选择 Solidty:Compiler information 时,它显示 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 compile 时看到的问题开始。我收到以下错误:

终端错误:

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 {
        //$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;
    }
}

我也尝试用谷歌搜索一些解决方案,所以我的 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 等),但同样的错误。

brownie-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 编程非常陌生,这是我学习的第一门类(class),我不想这么轻易放弃,非常感谢任何帮助。

最佳答案

错误的解决方法是

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

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

关于python - 解析器错误 : Source file requires different compiler version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70459922/

相关文章:

python - 来自 API 请求的 JSON 打印嵌套列表

javascript - 编译 Solidity 错误 - RangeError : Maximum call stack size exceeded

solidity - Solidity 如何使用元组(嵌套 abi)制作函数签名?

java - Web3j调用智能合约中的变量

python - 如何从 Excel 电子表格中仅读取满足特定条件的行到 Python 中?

python - 如何使用networkx在两个图之间执行树交叉?

node.js - 在安全帽上部署契约(Contract)或创建 NFT 时出现错误 : ProviderError: invalid sender

ethereum - Solidity:在映射中检索结构数组的值

django - 使用 web3.py 交易永远不会被挖掘

python - 您正在使用 staticfiles 应用程序,而没有将 STATIC_ROOT 设置设置为文件系统路径