From 6bd7321993f1677dd60aa3f7ea53f47a21c5af70 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 7 May 2024 18:26:32 -0400 Subject: [PATCH 01/13] baseline for subscription mock contract page --- .../vrf/v2-5/subscription/test-locally.mdx | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/content/vrf/v2-5/subscription/test-locally.mdx diff --git a/src/content/vrf/v2-5/subscription/test-locally.mdx b/src/content/vrf/v2-5/subscription/test-locally.mdx new file mode 100644 index 00000000000..7dbc3b969f0 --- /dev/null +++ b/src/content/vrf/v2-5/subscription/test-locally.mdx @@ -0,0 +1,159 @@ +--- +section: vrf +title: "Local testing using a Mock contract" +metadata: + description: "Example contract for generating random words using the VRF v2 subscription 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 [RandomNumberConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberConsumer.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. Call the VRFCoordinatorV2Mock [createSubscription function](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol#L194) to create a new subscription. +1. Call the VRFCoordinatorV2Mock [fundSubscription function](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol#L156) to fund your newly created subscription. **Note**: You can fund with an arbitrary amount. +1. Deploy your VRF consumer contract. +1. Call the VRFCoordinatorV2Mock [addConsumer function](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2Mock.sol#L230) to add your consumer contract to your subscription. +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. + +## Testing + +### Open the contracts on RemixIDE + +Open _VRFCoordinatorV2Mock_ and compile in Remix: + + + +Open _VRFv2Consumer_ and compile in Remix: + + + +Your RemixIDE file explorer should display _VRFCoordinatorV2Mock.sol_ and _VRFv2Consumer.sol_: + + + +### 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. Once deployed, you should see the _VRFCoordinatorV2Mock_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Create and fund a subscription + +1. Click on `createSubscription` to create a new subscription. + +1. In the RemixIDE console, read your transaction decoded output to find the subscription ID. In this example, the subscription ID is _1_. + + + +1. Click on `fundSubscription` to fund your subscription. In this example, you can set the `_subid` to `1` (which is your newly created subscription ID) and the `_amount` to `1000000000000000000`. + +### Deploy the VRF consumer contract + +1. In the file explorer, open _VRFv2Consumer.sol_. + +1. Under _DEPLOY & RUN TRANSACTIONS_, select _RandomNumberConsumerV2_. + + + +1. Under _DEPLOY_, fill in `SUBSCRIPTIONID` with your subscription ID, `vrfCoordinator` with the deployed _VRFCoordinatorV2Mock_ address and, _KEYHASH_ with an arbitrary `bytes32` (In this example, you can set the _KEYHASH_ to `0xd89b2bf150e3b9e13446986e571fb9cab24b13cea0a43ea20a6049a85cc807cc`). + +1. Click on _transact_ to deploy the _RandomNumberConsumerV2_ contract. + +1. After the consumer contract is deployed, you should see the _RandomNumberConsumerV2_ contract under _Deployed Contracts_. + + + +1. Note the address of the deployed contract. + +### Add the consumer contract to your subscription + +1. Under _Deployed Contracts_, open the functions list of your deployed _VRFCoordinatorV2Mock_ contract. + +1. Click on _addConsumer_ and fill in the `_subid` with your subscription ID and `_consumer` with your deployed consumer contract address. + + + +1. Click on _transact_. + +### Request random words + +1. Under _Deployed Contracts_, open the functions list of your deployed _RandomNumberConsumerV2_ contract. + +1. Click on `requestRandomWords`. + + + +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 your consumer contract address. + + + +1. Click on `transact`. + +### Check the results + +1. Under _Deployed Contracts_, open the functions list of your deployed _RandomNumberConsumerV2_ contract. + +1. Click on `s_requestId` to display the last request ID. In this example, the output is _1_. + + + +1. Each time you make a VRF request, your consumer contract requests two random words. After the request is fulfilled, the two random words are stored in the `s_randomWords` array. You can check the stored random words by reading the two first indexes of the `s_randomWords` array. To do so, click on the _s_randomWords_ function and: + + 1. Fill in the index with _0_ then click on _call_ to read the first random word. + + {" "} + + + + 1. Fill in the index with _1_ then click on _call_ to read the second random word. + + {" "} + + + +## 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 [RandomNumberConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberConsumer.spec.js). From 729a1cbf0afa926be3523ccccb3fdfdd1c5c0d33 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 7 May 2024 18:40:20 -0400 Subject: [PATCH 02/13] Updated some refs --- .../vrf/v2-5/subscription/test-locally.mdx | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/content/vrf/v2-5/subscription/test-locally.mdx b/src/content/vrf/v2-5/subscription/test-locally.mdx index 7dbc3b969f0..c3fc883694c 100644 --- a/src/content/vrf/v2-5/subscription/test-locally.mdx +++ b/src/content/vrf/v2-5/subscription/test-locally.mdx @@ -2,16 +2,14 @@ section: vrf title: "Local testing using a Mock contract" metadata: - description: "Example contract for generating random words using the VRF v2 subscription method on your local blockchain using a mock contract." + description: "Example contract for generating random words using the VRF v2.5 subscription 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 [RandomNumberConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberConsumer.spec.js). +This guide explains how to test Chainlink VRF v2.5 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 [RandomNumberConsumer unit tests](https://github.com/smartcontractkit/hardhat-starter-kit/blob/main/test/unit/RandomNumberConsumer.spec.js).