0

During my investigation, I would like a piece of advice, please.

I am trying to get a random number generated by the VRF of the oracle chainlink from the front coded in ReactJS. I would like to get the random number thanks to an event event GenerateRandomResult.

here's the function needed implemented in a smart contract of my application:

contract X is AccessControl, VRFConsumerBase {


bytes32 internal keyHash;
    uint256 internal fee;
    uint256 public randomResult;

    event GenerateRandomResult(uint256 ramdomNumber);


    constructor(address pawnToken_address)
        VRFConsumerBase(
            0x8C7382F9D8f56b33781fE506E897a4F1e2d17255, // VRF Coordinator
            0x326C977E6efc84E512bB9C30f76E30c160eD06FB  // LINK Token
        )

        keyHash = 0x6e75b569a01ef56d18cab6a8e71e6600d6ce853834d4a5748b720d06f878b3a4;
        fee = 0.0001 * 10 ** 18; // 0.1 LINK (Varies by network)


    /**
     * @notice Requests randomness
     * @return requestId the id of the request for the oracle
     */
    function getRandomNumber() public returns (bytes32 requestId) {
        require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
        return requestRandomness(keyHash, fee);
    }

    /**
     * @notice Callback function used by VRF Coordinator
     * @param requestId the id of the request for the oracle
     * @param randomness randomness must be requested from an oracle, which generates a number and a cryptographic proof
     */
    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        randomResult = randomness % 6 + 1;

        emit GenerateRandomResult(randomResult);
    }

Can you tell me if the event/emit is well positioned in my smart contract?

When I test with remix these functions, everything works fine!

Of course, I "feed" the deployed contract with LINK of my Metamask wallet to run it on Polygon framework.

Now, to connect the front (I use Ether.js) & use the previous event event GenerateRandomResult in ReactJS with the back in Solidity, I try to use this:

 async function rollDiceFunction() {
    if (BoardSC == null) return;

    //TODO: Replace by the call at the oracle

    BoardSC.on("GenerateRandomResult", (ramdomNumber) => {
        console.log('ramdomNumber: ', ramdomNumber);
    });

where BoardSC is an instance of my smart contract MonopolyBoard.sol but it doesn't work. I'll try to use this writing now:

 filter = {
        address: THE_ADDRESS_OF_YOUR_CONTRACT,
        topics: [
            // the name of the event, parnetheses containing the data type of each event, no spaces
            utils.id("Transfer(address,address,uint256)")
        ]
    }
    provider.on(filter, () => {
        // do whatever you want here
        // I'm pretty sure this returns a promise, so don't forget to resolve it
    })

Have you got any clue to suggest me to continue my developments?

0

Your Answer

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

Browse other questions tagged or ask your own question.