Search code examples
compiler-errorsoverridingsolidityabstractchainlink

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.

Using 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:

  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.

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.

I fix all of them settling on 0.7.0 which has my code look good without any visible errors.

Then I compile again and the initial abstract and override errors show in the terminal as CompilerErrors but NOT in the editor anymore.

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:
(focus on: contract/constructor/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)


Solution

  • 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

    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)