jeudi 18 février 2021

How to pay an Oracle with Link token if I have payable with Ethereum values?

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




Aucun commentaire:

Enregistrer un commentaire