1/5
--- title: Deploy a mock Chainlink VRF _Follow along with this video:_ --- ### Chainlink VRF mock contract A mock contract is a type of smart contract used in testing and development environments to simulate the behavior of real contracts. It allows us to create controlled and predictable scenarios for testing purposes without relying on actual external contracts or data sources. Moreover, it facilitates testing using Anvil, which is extremely fast and practical in comparison to a testnet. In the last lesson, we stopped on `HelperConfig.s.sol`: ```solidity function getOrCreateAnvilEthConfig() public returns (NetworkConfig memory anvilNetworkConfig) { // Check to see if we set an active network config if (activeNetworkConfig.vrfCoordinator != address(0)) { return activeNetworkConfig; } } ``` We need to treat the other side of the `(activeNetworkConfig.vrfCoordinatorV2 != address(0))` condition. What happens if that is false? If that is false we need to deploy a mock vrfCoordinatorV2_5 and pass its address inside a `NetworkConfig` that will be used on Anvil. Please use your Explorer on the left side to access the following path: `foundry-smart-contract-lottery-cu/lib/chainlink/contracts/src/v0.8/vrf/` Inside you'll find multiple folders, one of which is called `mocks`. Inside that folder, you can find the `VRFCoordinatorV2_5Mock` mock contract created by Chainlink. Add the following line in the imports section of `HelperConfig.s.sol`: ```solidity import {VRFCoordinatorV2_5Mock} from "chainlink/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol"; ``` Amazing! Now let's keep on working on the `getOrCreateAnvilEthConfig` function. We need to deploy the `vrfCoordinatorV2Mock`, but if we open it we'll see that its constructor requires some parameters: ```solidity contract VRFCoordinatorV2_5Mock is SubscriptionAPI, IVRFCoordinatorV2Plus { uint96 public immutable i_base_fee; uint96 public immutable i_gas_price; int256 public immutable i_wei_per_unit_link; } ``` The `i_base_fee` is the flat fee that VRF charges for the provided randomness. `i_gas_price` which is the gas consumed by the VRF node when calling your function. `i_wei_per_unit_link` is the LINK price in ETH in wei units. Given the way it's structured the callback gas is paid initially by the node which needs to be reimbursed. We add the following lines to the `getOrCreateAnvilEthConfig` function: ```solidity /* VRF Mock Values */ uint96 public constant MOCK_BASE_FEE = 0.25 ether; uint96 public constant MOCK_GAS_PRICE_LINK = 1e9; int256 public constant MOCK_WEI_PER_UNIT_LINK = 4e15; vm.startBroadcast(); VRFCoordinatorV2_5Mock vrfCoordinatorMock = new VRFCoordinatorV2_5Mock( MOCK_BASE_FEE, MOCK_GAS_PRICE_LINK, MOCK_WEI_PER_UNIT_LINK, ); vm.stopBroadcast(); ``` Amazing! Now that we have everything we need, let's perform the return, similar to what we did in `getSepoliaEthConfig`. ```solidity return NetworkConfig({ entranceFee: 0.01 ether, interval: 30, // 30 seconds vrfCoordinator: address(vrfCoordinatorMock), // gasLane value doesn't matter. gasLane: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae, subscriptionId: 0, callbackGasLimit: 500_000, }); ``` Great! Now this is fixed let's continue testing and deploying our Raffle contract.
The focus of this lesson is on deploying a mock Chainlink VRF, vital for testing smart contracts. It provides insights into setting up mock contracts, adjusting parameters, and the importance of Chainlink VRF in blockchain development.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Foundryup, Foundry Forge, and Anvil
Blockchain Oracles
How to create local Blockchain testnets
How to verify a smart contract
How to write and run smart contract tests
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Last updated on November 29, 2024
Solidity Developer
Foundry FundamentalsDuration: 2h 52min
Duration: 2h 56min
Duration: 26min
Duration: 5h 22min
Course Overview
About the course
Foundryup, Foundry Forge, and Anvil
Blockchain Oracles
How to create local Blockchain testnets
How to verify a smart contract
How to write and run smart contract tests
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Last updated on November 29, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar