简体   繁体   English

Solidity:抽象合约并覆盖 VRFConsumerBase 编译器/版本/导入问题

[英]Solidity: Abstract contract and override VRFConsumerBase compiler/version/import issues

I want to pull the RNG from VRFConsumerBase.sol by specifying it in the contract via is VRFConsumerBase and overriding the fulfillRandomness function.我想通过is VRFConsumerBase在合同中指定它并覆盖fulfillRandomness function,从而从VRFConsumerBase.sol中提取 RNG。

Using pragma solidity ^0.6.6;使用pragma solidity ^0.6.6; and v0.6 of the VRF as well as AggregatorV3Interface.sol and Ownable.sol I get two errors either in the editor or when compiling:和 VRF 的Ownable.sol以及AggregatorV3Interface.solv0.6我在编辑器或编译时遇到两个错误:

  1. Contract "Lottery" should be marked as abstract.

  2. Function has override specified but does not override anything.



When switching the version of VRFConsumerBase.sol from v0.6 to v0.7 both issues disappear.当将VRFConsumerBase.sol的版本从v0.7 v0.6 ,这两个问题都会消失。

Then while compiling I get prompted version and compiler issues.然后在编译时我得到提示版本和编译器问题。 I upgrade to the specified version 0.7.0 which then prompts multiple other compiler version errors of the other imports.我升级到指定版本0.7.0 ,然后提示其他导入的多个其他编译器版本错误。

I fix all of them settling on 0.7.0 which has my code look good without any visible errors.我修复了所有这些解决方案, 0.7.0我的代码看起来不错,没有任何可见错误。

Then I compile again and the initial abstract and override errors show in the terminal as CompilerErrors but NOT in the editor anymore.然后我再次编译,初始抽象和覆盖错误在终端中显示为CompilerErrors不再在编辑器中显示。

I guess it is a compiler version and imported code version conflict but after trying back and forth multiple times it seems like a paradox as either the versions don't interact or when they do I get prompted the initial errors again.我猜这是一个编译器版本和导入的代码版本冲突,但在多次来回尝试之后,这似乎是一个悖论,因为版本不交互或者当它们交互时我再次提示初始错误。



Here my slightly stripped Lottery.sol code snippet throwing the errors:在这里,我略微剥离的Lottery.sol代码片段抛出了错误:
(focus on: contract/constructor/fulfillRandomness) (重点:契约/构造函数/fulfillRandomness)

// SPDX-License-Identifier: MIT

pragma solidity ^0.6.6;
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";

contract Lottery is VRFConsumerBase, Ownable {
    address payable[] public players;
    address public recentWinner;
    uint256 public usdEntryFee;
    AggregatorV3Interface internal ethUsdPriceFeed;
    uint256 public fee;
    bytes32 public keyHash;

    enum LOTTERY_STATE {
        OPEN,
        CLOSED,
        CALCULATING_WINNER
    }

    LOTTERY_STATE public lottery_state;

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

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

    function fulfillRandomness(uint256 _requestId, bytes32 _randomness)
        internal
        override
    {
        // WINNER SELECTION
        uint256 indexOfWinner = uint256(_randomness) % players.length;
        recentWinner = players[indexOfWinner];
        payable(recentWinner).transfer(address(this).balance);
    }
}

(This is from lesson 7 of the freeCodeCamp Solidity Python tutorial) (这是来自 freeCodeCamp Solidity Python 教程的第 7 课)

As I just found out for anyone encountering a similar issue at some point it has nothing to do with versions of any code/import/VRF and it was simply a mistake defining types in正如我刚刚发现的那样,任何在某个时候遇到类似问题的人都与任何代码/导入/VRF 的版本无关,这只是在中定义类型的错误

function fulfillRandomness(uint256 _requestId, bytes32 _randomness) {}

as it should actually be:实际上应该是:

function fulfillRandomness(bytes32 _requestId, uint256 _randomness) {}

meaning rather look through your code and other topics posted on the issue.意思是查看您的代码和发布在该问题上的其他主题。

(abstract was fixed as override was fixed which was fixed by making the two functions basically look the same) (抽象是固定的,因为覆盖是固定的,这是通过使两个函数看起来基本相同来固定的)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM