1

我尝试了您在此处(在其他问题中)和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 文件有点不同,但这也没有帮助。

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

4

3 回答 3

1

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

pragma solidity >=0.4.22 <0.9.0;

由于我们是从导入的openzeppelin,我访问了 github repo 并看到它使用pragma solidity ^0.8.0;

所以将我的设置更改为

 pragma solidity >=0.4.22 <0.8.0;

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

在此处输入图像描述

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

于 2022-01-02T02:17:08.563 回答
1

错误的解决方案是

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

改用这个,希望这有帮助

于 2022-02-08T13:18:12.297 回答
0

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

function FulfillRandomness(bytes32 _requestId, uint256 _randomness)
        internal
        override
于 2022-01-02T07:45:23.560 回答