smartcontractkit / hardhat-starter-kit

A repo for boilerplate code for testing, deploying, and shipping chainlink solidity code.
MIT License
1.21k stars 490 forks source link

Unit testing of randomness - callback from mock? #65

Closed andreasrs closed 2 years ago

andreasrs commented 2 years ago

Would it be possible to have the fulfillRandomness fire from the mock during tests, so that the following behaviour can be tested in unit-tests? This does not seem to happen in my project. It does not in your starter either.

The contract I would like to create really depends on this random value as its starting point, without alternative public entrypoints to accomodate for testing (the random value not being manipulated from outside factors is crucial when the contract is deployed on a real chain).

How are people testing this today?

PatrickAlphaC commented 2 years ago

Yes. 100%

We have an issue asking to add callbacks here: https://github.com/smartcontractkit/hardhat-starter-kit/issues/67

But in the meantime, can you check this for an example of how to do it in typescript? It should be "about" the same in JS. https://github.com/PatrickAlphaC/decentralized-raffle/blob/969bee4f5a1da41db32cd1dacb6bab4060f4048d/test/unit/raffle.spec.ts#L58

andreasrs commented 2 years ago

Yes. 100%

We have an issue asking to add callbacks here: #67

But in the meantime, can you check this for an example of how to do it in typescript? It should be "about" the same in JS. https://github.com/PatrickAlphaC/decentralized-raffle/blob/969bee4f5a1da41db32cd1dacb6bab4060f4048d/test/unit/raffle.spec.ts#L58

I think that looks good. That is very close to how my current code looks based on how I expected it to work, so if the test then executed the fulfillRandomness of the contract, it would be in a good spot I think! Let me know if you want help with testing anything down the line when you have added callbacks.


I have compiled something locally that works for my tests, by modifying the VRFCoordinatorMock to import the base and then modify function to do a call using interface instead of abi+call:

    function callBackWithRandomness(
        bytes32 requestId,
        uint256 randomness,
        address consumerContract
    ) public {
        VRFConsumerBase(consumerContract).rawFulfillRandomness(requestId, randomness);
    }
andrejrakic commented 2 years ago

Fixed in #80