1

First of all, I'm new to this smart contract world, and so I would like to apology if it's a stupid question. I'm programming with Remix and with Solidity as programming language.I have 2 files:

mainfile.sol:

[...]
function createSomething(string memory _name, uint _userProvidedSeed) payable public {
        require(msg.value >= productCost);
        //refound user if he/she pays more than the productCost value 
        msg.sender.transfer(msg.value - productCost);
        
        getRandomNumber(_userProvidedSeed);
        _createProduct(_name, randomResult);
    }
[...]

and RandomNumberConsumer.sol that is an oracle for generating a random number:

// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.6;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/master/evm-contracts/src/v0.6/VRFConsumerBase.sol"; //import of the VRFConsumerBase for generating random data

contract RandomNumberConsumer is VRFConsumerBase {
    
    bytes32 internal keyHash;
    uint256 internal fee;
    
    uint256 internal randomResult;
    
    event RequestRandomness(
        bytes32 indexed requestId,
        bytes32 keyHash,
        uint256 seed
    );

    event RequestRandomnessFulfilled(
        bytes32 indexed requestId,
        uint256 randomness
    );
    
    /**
     * Constructor inherits VRFConsumerBase
     * 
     * Network: Kovan
     * Chainlink VRF Coordinator address: 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9
     * LINK token address:                0xa36085F69e2889c224210F603D836748e7dC0088
     * Key Hash: 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4
     */
    constructor() 
        VRFConsumerBase(
            0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
            0xa36085F69e2889c224210F603D836748e7dC0088  // LINK Token
        ) public
    {
        keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }
    
    /** 
     * Requests randomness from a user-provided seed
     */
    function getRandomNumber(uint256 userProvidedSeed) internal returns (bytes32 requestId) {
        require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
        uint256 seed = uint256(keccak256(abi.encode(userProvidedSeed, blockhash(block.number)))); // Hash user seed and blockhash
        bytes32 _requestId = requestRandomness(keyHash, fee, seed);
        emit RequestRandomness(_requestId, keyHash, seed);
        return _requestId;
    }

    /**
     * Callback function used by VRF Coordinator
     */
    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        emit RequestRandomnessFulfilled(requestId, randomness);
        randomResult = randomness;
    }
}

the problem here, is that if I generate my product without the payment in ethereum, so by commenting "getRandomNumber(_userProvidedSeed);" it works perfectly and creates the product, obviously without the random number. When I run it without commenting the above function, remix transaction prompt writes me this:

transact to xxx.xxx errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

and in my opinion is a problem with my LINK balance which is 0 probably but generating the random number need to pay a fee to the oracle, as said in the getRandomNumber(..) function. Can anybody help me? Thanks a lot in advance :P

2
  • " if I generate my product without the payment in ethereum," > could you please explain this? Feb 18 at 23:25
  • read below for further informations, i was wrong by saying the phrase you quote :P sorry Feb 19 at 10:32
1

It looks like your contract doesn't have LINK funds to perform the random number generation. Note this require in function getRandomNumber():

require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");

In the same documentation from this example, there is an explanation on how to fund this contract with LINKs as a preliminary step:

https://docs.chain.link/docs/fund-your-contract

5
  • I think it's the right solution but using the chainlink faucet for the kovan testnet doesn't give me link token to my metamask address (connected to kovan obviously) :C Feb 19 at 9:55
  • btw appreciate a lot your help! thanks so much! Feb 19 at 9:55
  • ok, apparently you need to press "add manually a custom token" on metamask and copy-paste this address (on kovan) 0xa36085F69e2889c224210F603D836748e7dC0088 to add the right LINK token. Automatic search by name doesn't work properly :C Feb 19 at 10:04
  • btw was still not working because I should have pressed on remix enviroment->inject web3 and link to metamask then :p Feb 19 at 10:51
  • You are welcome! And you are right: you need to add the custom token addresses manually from testnets in Metamask. As a side note: some Faucets tend to be buggy, so you can also use Uniswap and swap the tokens you need. Feb 19 at 11:19

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.