How to Mock Chainlink VRF Coordinator V2 and Aggregator V3 With Truffle (0.8.0) | by Doğukan Akkaya | May, 2022

Regionally take a look at your good contracts

On this story, I’ll present you how you can make native checks of your good contracts that use Aggregator V3 and VRF Coordinator V2 in Truffle utilizing mock contracts.

First, let’s init a recent Truffle undertaking and set up Chainlink contracts.

truffle init vrf-aggregator-mock
cd vrf-aggregator-mock
npm i @chainlink/contracts

Subsequent, we’ll must create a take a look at listing and mock contracts underneath the contracts listing the place we’ll put our mock contracts for testing functions.

mkdir contracts/take a look at
contact contracts/take a look at/VRFCoordinatorV2Mock.sol
contact contracts/take a look at/MockV3Aggregator.sol

Home windows natively don’t assist contact command, so you possibly can manually create these recordsdata.

Now we additionally want another contract which is we’ll use these mocks from.

Create a file named Mock.sol (or no matter your contract is, I’ll simply persist with Mock) underneath the contracts listing:

Configuration to Use Ganache as Improvement Community

Open truffle-config.js and add a growth community.

growth:     host: "",    port: 7545,    network_id: "*",    websockets: true,

Websockets should be true as a result of we’ll use occasions to retrieve random phrases which might be created by the VRF Coordinator and HttpProvider doesn’t assist it.

Create a file named 2_deploy_mock.js underneath the migrations listing.

Now let’s test all the things is alright by working the deploy command.

truffle deploy
> Complete deployments: 4
> Last value: 0.06800202 ETH

Create a file named mock.js underneath the take a look at listing (not underneath the contracts/take a look at):

Now let’s run our take a look at.

truffle take a look at

If all the things is okay, you need to be seeing one thing like that:

Contract: Mock
✔ ought to return random phrases (1166ms)
✔ ought to return value feed
2 passing (1s)

That’s it, yay!. We mocked VRF Coordinator V2 and Aggregator V3.

More Posts