From 39992b2528643e75a89ed959469249148d14d4e6 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Thu, 30 May 2024 18:16:59 -0400 Subject: [PATCH 01/14] baseline copy of v2 df mock --- .../vrf/v2-5/direct-funding/test-locally.mdx | 350 ++++++++++++++++++ 1 file changed, 350 insertions(+) create mode 100644 src/content/vrf/v2-5/direct-funding/test-locally.mdx diff --git a/src/content/vrf/v2-5/direct-funding/test-locally.mdx b/src/content/vrf/v2-5/direct-funding/test-locally.mdx new file mode 100644 index 00000000000..8fa3f022254 --- /dev/null +++ b/src/content/vrf/v2-5/direct-funding/test-locally.mdx @@ -0,0 +1,350 @@ +--- +section: vrf +title: "Local testing using a Mock contract" +metadata: + description: "Example contract for generating random words using the VRF v2 direct funding method on your local blockchain using a mock contract." +--- + +import VrfCommon from "@features/vrf/v2/common/VrfCommon.astro" +import ContentCommon from "@features/common/ContentCommon.astro" +import { CodeSample, ClickToZoom, Aside } from "@components" + + + +This guide explains how to test Chainlink VRF v2 on a [Remix IDE](https://remix-ide.readthedocs.io/en/latest/run.html#environment) sandbox blockchain environment. **Note**: You can reuse the same logic on another development environment, such as Hardhat or Truffle. For example, read the Hardhat Starter Kit [RandomNumberDirectFundingConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberDirectFundingConsumer.spec.js). + + + +## Benefits of local testing + + + +## Testing logic + +Complete the following tasks to test your VRF v2 consumer locally: + +1. Deploy the [VRFCoordinatorV2Mock](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol). This contract is a mock of the [VRFCoordinatorV2](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFCoordinatorV2.sol) contract. +1. Deploy the [MockV3Aggregator](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/tests/MockV3Aggregator.sol) contract. +1. Deploy the [LinkToken](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/shared/token/ERC677/LinkToken.sol) contract. +1. Deploy the [VRFV2Wrapper](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFV2Wrapper.sol) contract. +1. Call the VRFV2Wrapper [setConfig function](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFV2Wrapper.sol#L119) to set wrapper specific parameters. +1. Fund the VRFv2Wrapper subscription. +1. Call the VRFCoordinatorV2Mock [addConsumer function](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol#L230) to add the wrapper contract to your subscription. +1. Deploy your VRF consumer contract. +1. Fund your consumer contract with LINK tokens. +1. Request random words from your consumer contract. +1. Call the VRFCoordinatorV2Mock [fulfillRandomWords function](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol#L108) to fulfill your consumer contract request. + +## Prerequisites + +This guide will require you to finetune the gas limit when fulfilling requests. When writing, manually setting up the gas limits on RemixIDE is not supported, so you will use RemixIDE in conjunction with [Metamask](https://metamask.io/). +[Ganache](https://trufflesuite.com/ganache/) lets you quickly fire up a personal Ethereum blockchain. If you still need to install Ganache, follow the [official guide](https://trufflesuite.com/docs/ganache/quickstart/). + +1. Once Ganache is installed, click the _QUICKSTART_ button to start a local Ethereum node. + + + + **Note**: Make sure to note the RPC server. In this example, the RPC server is _http://127.0.0.1:7545/_. + +1. Follow the Metamask [official guide](https://support.metamask.io/hc/en-us/articles/360043227612-How-to-add-a-custom-network-RPC#h_01G63GGJ83DGDRCS2ZWXM37CV5) to add a custom network manually. + + + +1. Import a Ganache account into Metamask. + + 1. On Ganache, click on the _key_ symbol of the first account: + + + + 1. Copy the private key: + + + + 1. Follow the Metamask [official guide](https://support.metamask.io/hc/en-us/articles/360015489331-How-to-import-an-account) to import an account using a private key. + + 1. Your Metamask is connected to _Ganache_, and you should have access to the newly imported account. + + {" "} + +## Testing + +### Open the contracts on RemixIDE + +Open _VRFCoordinatorV2Mock_ and compile in Remix: + + + +Open _MockV3Aggregator_ and compile in Remix: + + + +Open _LinkToken_ and compile in Remix: + + + +Open _VRFV2Wrapper_ and compile in Remix: + + + +Open _RandomNumberDirectFundingConsumerV2_ and compile in Remix: + + + +Your RemixIDE file explorer should display the opened contracts: + + + +### Select the correct RemixIDE environment + +Under _DEPLOY & RUN TRANSACTIONS_: + +1. Set the Environment to _Injected Provider - Metamask_: + + + +1. On Metamask, connect your Ganache account to the Remix IDE. + + + +1. Click on Connect. The RemixIDE environment should be set to the correct environment, and the account should be the Ganache account. + + + +### Deploy VRFCoordinatorV2Mock + +1. Open _VRFCoordinatorV2Mock.sol_. + +1. Under _DEPLOY & RUN TRANSACTIONS_, select _VRFCoordinatorV2Mock_. + + + +1. Under _DEPLOY_, fill in the `_BASEFEE` and `_GASPRICELINK`. These variables are used in the _VRFCoordinatorV2Mock_ contract to represent the base fee and the gas price (in LINK tokens) for the VRF requests. You can set: `_BASEFEE=100000000000000000` and `_GASPRICELINK=1000000000`. + +1. Click on _transact_ to deploy the _VRFCoordinatorV2Mock_ contract. + +1. A Metamask popup will open. Click on _Confirm_. + +1. Once deployed, you should see the _VRFCoordinatorV2Mock_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Deploy MockV3Aggregator + +The _MockV3Aggregator_ contract is designed for testing purposes, allowing you to simulate an oracle price feed without interacting with the existing Chainlink network. + +1. Open _MockV3Aggregator.sol_. + +1. Under _DEPLOY & RUN TRANSACTIONS_, select _MockV3Aggregator_. + + + +1. Under _DEPLOY_, fill in `_DECIMALS` and `_INITIALANSWER`. These variables are used in the _MockV3Aggregator_ contract to represent the number of decimals the aggregator's answer should have and the most recent price feed answer. You can set: `_DECIMALS=18` and `_INITIALANSWER=3000000000000000` (We are considering that `1 LINK = 0.003 native gas tokens`). + +1. Click on _transact_ to deploy the _MockV3Aggregator_ contract. + +1. A Metamask popup will open. Click on _Confirm_. + +1. Once deployed, you should see the _MockV3Aggregator_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Deploy LinkToken + +The Chainlink VRF v2 direct funding method requires your consumer contract to pay for VRF requests in LINK. Therefore, you have to deploy the _LinkToken_ contract to your local blockchain. + +1. Open _LinkToken.sol_. + +1. Under _DEPLOY & RUN TRANSACTIONS_, select _LinkToken_. + + + +1. Under _DEPLOY_, click on _transact_ to deploy the _LinkToken_ contract. + +1. A Metamask popup will open. Click on _Confirm_. + +1. Once deployed, you should see the _LinkToken_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Deploy VRFV2Wrapper + +As the VRF v2 direct funding [end-to-end diagram](/vrf/v2/direct-funding#request-and-receive-data) explains, the _VRFV2Wrapper_ acts as a wrapper for the coordinator contract. + +1. Open _VRFV2Wrapper.sol_. + +1. Under _DEPLOY & RUN TRANSACTIONS_, select _VRFV2Wrapper_. + + + +1. Under _DEPLOY_, fill in `_LINK` with the _LinkToken_ contract address, `_LINKETHFEED` with the _MockV3Aggregator_ contract address, and `_COORDINATOR` with the _VRFCoordinatorV2Mock_ contract address. + +1. click on _transact_ to deploy the _VRFV2Wrapper_ contract. + +1. A Metamask popup will open. Click on _Confirm_. + +1. Once deployed, you should see the _VRFV2Wrapper_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Configure the VRFV2Wrapper + +1. Under _Deployed Contracts_, open the functions list of your deployed _VRFV2Wrapper_ contract. + +1. Click on `setConfig` and fill in `_wrapperGasOverhead` with `60000`, `_coordinatorGasOverhead` with `52000`, `_wrapperPremiumPercentage` with `10`, `_keyHash` with `0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc`, and `_maxNumWords` with `10`. **Note** on these variables: + + 1. `_wrapperGasOverhead`: This variable reflects the gas overhead of the wrapper's fulfillRandomWords function. The cost for this gas is passed to the user. + + 1. `_coordinatorGasOverhead`: This variable reflects the gas overhead of the coordinator's `fulfillRandomWords` function. The cost for this gas is billed to the _VRFV2Wrapper_ subscription and must, therefore, be included in the VRF v2 direct funding requests pricing. + + 1. `_wrapperPremiumPercentage`: This variable is the premium ratio in percentage. For example, a value of 0 indicates no premium. A value of 15 indicates a 15 percent premium. + + 1. `_keyHash`: The gas lane key hash value is the maximum gas price you are willing to pay for a request in wei. + + 1. `_maxNumWords`: This variable is the maximum number of words requested in a VRF v2 direct funding request. + + + +1. click on _transact_. + +1. A Metamask popup will open. Click on _Confirm_. + +### Fund the VRFV2Wrapper subscription + +When deployed, the _VRFV2Wrapper_ contract creates a new subscription and adds itself to the newly created subscription. If you started this guide from scratch, the subscription ID should be 1. + +1. Under _Deployed Contracts_, open the functions list of your deployed _VRFCoordinatorV2Mock_ contract. + +1. Click `fundSubscription` to fund the _VRFV2Wrapper_ subscription. In this example, you can set the `_subid` to `1` (which is your newly created subscription ID) and the `_amount` to `10000000000000000000` (10 LINK). + +1. A Metamask popup will open. Click on _Confirm_. + +### Deploy the VRF consumer contract + +1. In the file explorer, open _RandomNumberDirectFundingConsumerV2.sol_. + +1. Under _DEPLOY & RUN TRANSACTIONS_, select _RandomNumberDirectFundingConsumerV2_. + + + +1. Under _DEPLOY_, fill in `_LINKADDRESS_` with the _LinkToken_ contract address, and `_WRAPPERADDRESS_` with the deployed _VRFV2Wrapper_ address. + +1. Click on _transact_ to deploy the _RandomNumberDirectFundingConsumerV2_ contract. + +1. A Metamask popup will open. Click on _Confirm_. + +1. Once deployed, you should see the _RandomNumberDirectFundingConsumerV2_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Fund your VRF consumer contract + +1. Under _Deployed Contracts_, open the functions list of your deployed _LinkToken_ contract. + +1. Click on _transfer_ and fill in the `_to` with your consumer contract address and `_value` with LINK tokens amount. For this example, you can set the `_value` to `10000000000000000000` (10 LINK). + + + +1. Click on _transact_. + +1. A Metamask popup will open. Click on _Confirm_. + +### Request random words + +Request three random words. + +1. Under _Deployed Contracts_, open the functions list of your deployed _RandomNumberConsumerV2_ contract. + +1. In `requestRandomWords`, fill in `_callbackGasLimit` with `300000`, `_requestConfirmations` with `3` and `_numWords` with `3`. + + + +1. Click on _transact_. + +1. A Metamask popup will open. + + + +1. Click on _Confirm_. + +1. In the RemixIDE console, read your transaction logs to find the VRF request ID. In this example, the request ID is _1_. + + + +1. Note your request ID. + +### Fulfill the VRF request + +Because you are testing on a local blockchain environment, you must fulfill the VRF request yourself. + +1. Under _Deployed Contracts_, open the functions list of your deployed _VRFCoordinatorV2Mock_ contract. + +1. Click `fulfillRandomWords` and fill in `_requestId` with your VRF request ID and `_consumer` with the _VRFV2Wrapper_ contract address. + + + +1. Click on `transact`. + +1. A Metamask popup will open. + + + +1. Click on _Confirm_. + +1. In the RemixIDE console, read your transaction logs to find the random words. + + + +### Check the results + +1. Under _Deployed Contracts_, open the functions list of your deployed _RandomNumberDirectFundingConsumerV2_ contract. + +1. Click on `lastRequestId` to display the last request ID. In this example, the output is _1_. + + + +1. Click on `getRequestStatus` with `_requestId` equal to `1`: + + + +1. You will get the amount paid, the status, and the random words. + + + + ## Next steps + +This guide demonstrated how to test a VRF v2 consumer contract on your local blockchain. We made the guide on RemixIDE for learning purposes, but you can reuse the same [testing logic](#testing-logic) on another development environment, such as Truffle or Hardhat. For example, read the Hardhat Starter Kit [RandomNumberDirectFundingConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberDirectFundingConsumer.spec.js). From af0580af5b72a0e08d300e2c9ee10f782568601f Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Fri, 31 May 2024 11:38:59 -0400 Subject: [PATCH 02/14] Updated some refs --- .../vrf/v2-5/direct-funding/test-locally.mdx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/content/vrf/v2-5/direct-funding/test-locally.mdx b/src/content/vrf/v2-5/direct-funding/test-locally.mdx index 8fa3f022254..3161094c11a 100644 --- a/src/content/vrf/v2-5/direct-funding/test-locally.mdx +++ b/src/content/vrf/v2-5/direct-funding/test-locally.mdx @@ -11,7 +11,7 @@ import { CodeSample, ClickToZoom, Aside } from "@components" -This guide explains how to test Chainlink VRF v2 on a [Remix IDE](https://remix-ide.readthedocs.io/en/latest/run.html#environment) sandbox blockchain environment. **Note**: You can reuse the same logic on another development environment, such as Hardhat or Truffle. For example, read the Hardhat Starter Kit [RandomNumberDirectFundingConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberDirectFundingConsumer.spec.js). +This guide explains how to test Chainlink VRF v2 on a [Remix IDE](https://remix-ide.readthedocs.io/en/latest/run.html#environment) sandbox blockchain environment. **Note**: You can reuse the same logic on another development environment, such as Hardhat. For example, read the Hardhat Starter Kit [RandomNumberDirectFundingConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberDirectFundingConsumer.spec.js).