UnPapeur
UnPapeur

Reputation: 87

Retrieve the random number generated by Chainlink VRF in 1 transaction

I am trying to get a random number in solidity in 1 transaction with Chainlink VRF.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
contract RandomNumberConsumer is VRFConsumerBase {
    
    bytes32 internal keyHash;
    uint256 internal fee;
    
    uint256 internal randomResult;
    
    constructor() 
        VRFConsumerBase(
            0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
            0xa36085F69e2889c224210F603D836748e7dC0088  // LINK Token
        )
    {
        keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
        fee = 0.1 * 10 ** 18; // 0.1 LINK (Varies by network)
    }
    function getRandomNumber() public returns (bytes32 requestId) {
        require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
        return requestRandomness(keyHash, fee);
    }
    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        randomResult = randomness;
    }

    function letsGo() public {
        bytes32 requestId;
        requestId = getRandomNumber();
        fulfillRandomness(requestId, 123456);
    }
}

Is there a way in blockchain to get the randomResult?

I'm looking specifically at my letsGo function.

Upvotes: 0

Views: 1739

Answers (1)

Patrick Collins
Patrick Collins

Reputation: 6131

The chainlink VRF is a 2 transaction process.

The first transaction requests the random number, and the second to have the random number returned. You can't get the random number in 1 go.

You can read more about the basic request model in the documentation.

You're letsgo function will not work.

Upvotes: 2

Related Questions