Yvonnick U
Yvonnick U

Reputation: 63

Chainlink VRF : can't get a Random array

I'm trying to edit this demo: https://www.youtube.com/watch?v=JqZWariqh5s to get a array with ramdom number.

pragma solidity 0.6.6;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/VRFConsumerBase.sol";

contract RandomNumberConsumer is VRFConsumerBase {
    
    bytes32 internal keyHash;
    uint256 internal fee;
    uint256 public randomResult;
    
    constructor() VRFConsumerBase(
            0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
            0xa36085F69e2889c224210F603D836748e7dC0088  // LINK Token
        ) public
    {
        keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }
    

    function getRandomNumber(uint256 userProvidedSeed) public returns (bytes32 requestId) {
        return requestRandomness(keyHash, fee, userProvidedSeed);
    }

    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        randomResult = randomness.mod(50).add(1);
    }
    
    function getDraw(uint256 userProvidedSeed) public returns(uint256[] memory) {
         uint256[] memory draw = new uint256[](5);
         
         for(uint i = 0; i < 5; i++) {
             draw[i] = getRandomNumber(userProvidedSeed);
         }
         return draw;
    }
}

I get this error : contracts/RandomNumbers.sol:33:24: TypeError: Type bytes32 is not implicitly convertible to expected type uint256. draw[i] = getRandomNumber(userProvidedSeed); ^-------------------------------^

What am i doing wrong ?

Upvotes: 1

Views: 651

Answers (2)

Petr Hejda
Petr Hejda

Reputation: 43491

getRandomNumber() returns bytes32, but you're trying to assign this value to uint256.

You need to cast the bytes32 to uint256 first:

draw[i] = uint256(getRandomNumber(userProvidedSeed));

Note: requestRandomness() returns the request ID, not the actual random number. So if you want to get actual 5 random numbers from an oracle, you cannnot do it synchronously. You'll also need to update your fulfillRandomness() and randomResult to store more than one value.

Upvotes: 1

Patrick Collins
Patrick Collins

Reputation: 6131

You cannot convert a bytes32 to a uint256.

Your draw array is an array of uint256s. getRandomNumber returns a bytes32.

On this line:

draw[i] = getRandomNumber(userProvidedSeed);

You're setting draw[i] to a bytes32. draw[i] needs to be a uint256 and getRandomNumber(userProvidedSeed) returns a bytes32

See:

function getRandomNumber(uint256 userProvidedSeed) public returns (bytes32 requestId)

What you may want to do instead, is have your drawing happen in the fulfillrandomness function, since it looks like you want to do:

draw[i] = randomResult;

Upvotes: 1

Related Questions