1

I have tried using @chainlink/contracts many ways (truffle, hardhat, remix).

In all cases I run into an error that seems to be some bug deep inside on the chainlink library:

> Compilation warnings encountered:

    Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.
   --> project:/contracts/CCGG_all.sol:139:3:
    |
139 |   constructor() VRFConsumerBase(__vrfCoordinatorAddress, __linkTokenAddress) public {
    |   ^ (Relevant source part starts here and spans across multiple lines).

,Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.
  --> project:/contracts/Simple-Chainlink.sol:27:3:
   |
27 |   constructor() VRFConsumerBase(__vrfCoordinatorAddress, __linkTokenAddress) public {
   |   ^ (Relevant source part starts here and spans across multiple lines).


TypeError: Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
   --> @chainlink/contracts/src/v0.8/VRFConsumerBase.sol:167:5:
    |
167 |     LINK.transferAndCall(vrfCoordinator, _fee, abi.encode(_keyHash, USER_SEED_PLACEHOLDER));
    |     ^^^^

,TypeError: Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
   --> @chainlink/contracts/src/v0.8/VRFConsumerBase.sol:167:26:
    |
167 |     LINK.transferAndCall(vrfCoordinator, _fee, abi.encode(_keyHash, USER_SEED_PLACEHOLDER));
    |                          ^^^^^^^^^^^^^^

I have not found any solution for this... is there some problem with the chainlink library itself? Why does it not work??? Verified that these error happen on solidity compiler 0.6.x and 0.8x

Here is an example of some code that will produce this error (taken straight from chainlink docs):

// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;
pragma experimental ABIEncoderV2;

import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";

contract Example is VRFConsumerBase {
 
  //   on Kovan Network
  //   address vrfCoordinatorAddress  = 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9;
  //   address linkTokenAddress       = 0xa36085F69e2889c224210F604D836748e7dC0088;
  //   address oracleKeyhash          = 0x6c3699283bda56ad74f6b855546425b68d482e983852a7a82979cc4807b641f4;

  //   on Rinkeby Network
  address __vrfCoordinatorAddress  = 0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B;
  address __linkTokenAddress       = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709;
  bytes32 __oracleKeyhash          = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
  
  uint256 fee;
  bytes32 keyHash;

  constructor() VRFConsumerBase(__vrfCoordinatorAddress, __linkTokenAddress) public {

      keyHash = __oracleKeyhash;

      fee = 0.1 * 10 ** 18; // 0.1 Link
      
  }
  
  function getRandomNumber(uint256 userProvidedSeed) public returns (bytes32 requestId) {
      return requestRandomness(keyHash, fee);
  }
  
  function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
    
    // something  
  }
  
}

2 Answers 2

2

You can read immutable variables in the constructor in Solidity v0.8.8 and above:

Compiler Features

  • ...
  • Immutable variables can be read at construction time once they are initialized.
5
  • thanks! on v0.8.8 it now compiles but "fails gas estimation" when I try to call the "getRandomNumber" function... 🤔
    – Jim
    Oct 20, 2021 at 2:41
  • That's a different problem situation for which you should consider posting another question (if you don't find an answer on StackExchange). Oct 20, 2021 at 9:14
  • Also, don't use v0.8.8. Use v0.8.9, which patches a bug in the user defined value types. Oct 20, 2021 at 9:16
  • the code still doesn't work on v0.8.9, I always get "gas estimation failed errors"... what am I doing wrong here?
    – Jim
    Oct 20, 2021 at 16:03
  • 1
    The comment about v0.8.9 was not meant to solve your gas estimation error. Please, open a new question for that, and add lots of details. Oct 20, 2021 at 16:08
1

What you are seeing are warnings, not errors. They do not prevent the contract from compiling.

In addition to this, your code isn't the same as the VRF code sample in the Chainlink docs. The version in the docs doesn't read immutable variables in the constructor, everything is hardcoded.

Finally, as previously stated, if you change your compiler to v0.8.8, you should be able to stop this warning from occuring

2
  • Did you read the full output Harry? It's one warning and two errors.
    – Jim
    Oct 20, 2021 at 1:08
  • @Jim I tried your sample code in remix on 0.8, 0.87 and 0.88 and I get no errors. Please double check and update your question Nov 22, 2021 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.