From 0b647a047e3653b94d8edc63caf0b072090c64ad Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 21 May 2024 13:50:11 -0400 Subject: [PATCH 01/18] Add baseline files --- .../direct-funding/get-a-random-number.mdx | 130 +++++++++++++ .../v2-5/subscription/get-a-random-number.mdx | 171 ++++++++++++++++++ 2 files changed, 301 insertions(+) create mode 100644 src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx create mode 100644 src/content/vrf/v2-5/subscription/get-a-random-number.mdx diff --git a/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx b/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx new file mode 100644 index 00000000000..2169ed6889d --- /dev/null +++ b/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx @@ -0,0 +1,130 @@ +--- +section: vrf +date: Last Modified +title: "Get a Random Number" +whatsnext: + { + "Security Considerations": "/vrf/v2/security", + "Best Practices": "/vrf/v2/best-practices", + "Migrating from VRF v1": "/vrf/v2/direct-funding/migration-from-v1", + "Supported Networks": "/vrf/v2/direct-funding/supported-networks", + } +metadata: + description: "How to generate a random number inside a smart contract using Chainlink VRF v2 - Direct funding method." +--- + +import VrfCommon from "@features/vrf/v2/common/VrfCommon.astro" +import { Aside, CodeSample } from "@components" + + + +This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2 without managing a subscription. To explore more applications of VRF, refer to our [blog](https://blog.chain.link/). + +## Requirements + +This guide assumes that you know how to create and deploy smart contracts on Ethereum testnets using the following tools: + +- [The Remix IDE](https://remix.ethereum.org/) +- [MetaMask](https://metamask.io/) +- [Sepolia testnet ETH](/resources/link-token-contracts/#sepolia-testnet) + +If you are new to developing smart contracts on Ethereum, see the [Getting Started](/getting-started/conceptual-overview) guide to learn the basics. + +## Create and deploy a VRF v2 compatible contract + +For this example, use the [VRFv2DirectFundingConsumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2DirectFundingConsumer.sol) sample contract. This contract imports the following dependencies: + +- `VRFV2WrapperConsumerBase.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFV2WrapperConsumerBase.sol) +- `ConfirmedOwner.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/shared/access/ConfirmedOwner.sol) + +The contract also includes pre-configured values for the necessary request parameters such as `callbackGasLimit`, `requestConfirmations`, the number of random words `numWords`, the VRF v2 Wrapper address `wrapperAddress`, and the LINK token address `linkAddress`. You can change these parameters if you want to experiment on different testnets. + +Build and deploy the contract on Sepolia. + +1. Open the [`VRFv2DirectFundingConsumer.sol` contract](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2DirectFundingConsumer.sol) in Remix. + + {/* prettier-ignore */} + + +1. On the **Compile** tab in Remix, compile the `VRFv2DirectFundingConsumer` contract. + +1. Configure your deployment. On the **Deploy** tab in Remix, select the **Injected Web3 Environment** and select the `VRFv2DirectFundingConsumer` contract from the contract list. + +1. Click the **Deploy** button to deploy your contract onchain. MetaMask opens and asks you to confirm the transaction. + +1. After you deploy your contract, copy the address from the **Deployed Contracts** list in Remix. Before you can request randomness from VRF v2, you must fund your consuming contract with enough LINK tokens in order to request for randomness. Next, [fund your contract](#fund-your-contract). + +## Fund Your Contract + +Requests for randomness will fail unless your consuming contract has enough LINK. + +1. [Acquire testnet LINK](/resources/acquire-link). +1. [Fund your contract with testnet LINK](/resources/fund-your-contract). For this example, funding your contract with 2 LINK should be sufficient. + +## Request random values + +The deployed contract requests random values from Chainlink VRF, receives those values, builds a struct `RequestStatus` containing them, and stores the struct in a mapping `s_requests`. Run the `requestRandomWords()` function on your contract to start the request. + +1. Return to Remix and view your deployed contract functions in the **Deployed Contracts** list. + +1. Click the `requestRandomWords()` function to send the request for random values to Chainlink VRF. MetaMask opens and asks you to confirm the transaction. + + + + After you approve the transaction, Chainlink VRF processes your request. Chainlink VRF fulfills the request and returns the random values to your contract in a callback to the `fulfillRandomWords()` function. At this point, a new key `requestId` is added to the mapping `s_requests`. Depending on current testnet conditions, it might take a few minutes for the callback to return the requested random values to your contract. + +1. To fetch the request ID of your request, call `lastRequestId()`. + +1. After the oracle returns the random values to your contract, the mapping `s_requests` is updated. The received random values are stored in `s_requests[_requestId].randomWords`. + +1. Call `getRequestStatus()` and specify the `requestId` to display the random words. + + + +## Analyzing the contract + +In this example, the consuming contract uses static configuration parameters. + + + +The parameters define how your requests will be processed. You can find the values for your network in the [Supported networks](/vrf/v2/direct-funding/supported-networks) page. + +- `uint32 callbackGasLimit`: The limit for how much gas to use for the callback request to your contract's `fulfillRandomWords()` function. It must be less than the `maxGasLimit` limit on the coordinator contract minus the `wrapperGasOverhead`. See the [VRF v2 Direct funding limits](/vrf/v2/direct-funding/#limits) for more details. Adjust this value for larger requests depending on how your `fulfillRandomWords()` function processes and stores the received random values. If your `callbackGasLimit` is not sufficient, the callback will fail and your consuming contract is still charged for the work done to generate your requested random values. + +- `uint16 requestConfirmations`: How many confirmations the Chainlink node should wait before responding. The longer the node waits, the more secure the random value is. It must be greater than the `minimumRequestBlockConfirmations` limit on the coordinator contract. + +- `uint32 numWords`: How many random values to request. If you can use several random values in a single callback, you can reduce the amount of gas that you spend per random value. The total cost of the callback request depends on how your `fulfillRandomWords()` function processes and stores the received random values, so adjust your `callbackGasLimit` accordingly. + +The contract includes the following functions: + +- `requestRandomWords()`: Takes your specified parameters and submits the request to the VRF v2 Wrapper contract. + +- `fulfillRandomWords()`: Receives random values and stores them with your contract. + +- `getRequestStatus()`: Retrive request details for a given `_requestId`. + +- `withdrawLink()`: At any time, the owner of the contract can withdraw outstanding LINK balance from it. + + + +## Clean up + +After you are done with this contract, you can retrieve the remaining testnet LINK to use with other examples. + +1. Call `withdrawLink()` function. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, the remaining LINK will be transferred from your consuming contract to your wallet address. diff --git a/src/content/vrf/v2-5/subscription/get-a-random-number.mdx b/src/content/vrf/v2-5/subscription/get-a-random-number.mdx new file mode 100644 index 00000000000..702b1de2fa0 --- /dev/null +++ b/src/content/vrf/v2-5/subscription/get-a-random-number.mdx @@ -0,0 +1,171 @@ +--- +section: vrf +date: Last Modified +title: "Get a Random Number" +whatsnext: + { + "Programmatic Subscription": "/vrf/v2/subscription/examples/programmatic-subscription", + "Subscription Manager UI": "/vrf/v2/subscription/ui", + "Security Considerations": "/vrf/v2/security", + "Best Practices": "/vrf/v2/best-practices", + "Migrating from VRF v1 to v2": "/vrf/v2/subscription/migration-from-v1", + "Supported Networks": "/vrf/v2/subscription/supported-networks", + } +metadata: + description: "How to generate a random number inside a smart contract using Chainlink VRF." +--- + +import VrfCommon from "@features/vrf/v2/common/VrfCommon.astro" +import { Aside, CodeSample } from "@components" + + + +This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2. For more advanced examples with programmatic subscription configuration, see the [Programmatic Subscription](/vrf/v2/subscription/examples/programmatic-subscription) page. To explore more applications of VRF, refer to our [blog](https://blog.chain.link/). + + + +## Requirements + +This guide assumes that you know how to create and deploy smart contracts on Ethereum testnets using the following tools: + +- [The Remix IDE](https://remix.ethereum.org/) +- [MetaMask](https://metamask.io/) +- [Sepolia testnet ETH](/resources/link-token-contracts/#sepolia-testnet) + +If you are new to developing smart contracts on Ethereum, see the [Getting Started](/getting-started/conceptual-overview) guide to learn the basics. + +## Create and fund a subscription + +For this example, create a new subscription on the Sepolia testnet. + +1. Open MetaMask and set it to use the Sepolia testnet. The [Subscription Manager](/vrf/v2/subscription/ui) detects your network based on the active network in MetaMask. + +1. Check MetaMask to make sure you have testnet ETH and LINK on Sepolia. You can get testnet ETH and LINK at [faucets.chain.link](https://faucets.chain.link/sepolia/). + +1. Open the Subscription Manager at [vrf.chain.link](https://vrf.chain.link). + {/* prettier-ignore */} + + + +1. Click **Create Subscription** and follow the instructions to create a new subscription account. If you connect your wallet to the Subscription Manager, the **Admin address** for your subscription is prefilled and not editable. Optionally, you can enter an email address and a project name for your subscription, and both of these are private. MetaMask opens and asks you to confirm payment to create the account onchain. After you approve the transaction, the network confirms the creation of your subscription account onchain. + +1. After the subscription is created, click **Add funds** and follow the instructions to fund your subscription. + + - For your request to go through, you need to fund your subscription with enough LINK to meet your [minimum subscription balance](/vrf/v2/subscription#minimum-subscription-balance) to serve as a buffer against gas volatility. For this example, a balance of 12 LINK is sufficient. (After your request is processed, it costs around 3 LINK, and that amount will be deducted from your subscription balance.) + - MetaMask opens to confirm the LINK transfer to your subscription. After you approve the transaction, the network confirms the transfer of your LINK token to your subscription account. + +1. After you add funds, click **Add consumer**. A page opens with your account details and subscription ID. + +1. Record your subscription ID, which you need for your consuming contract. You will add the consuming contract to your subscription later. + +You can always find your subscription IDs, balances, and consumers at [vrf.chain.link](https://vrf.chain.link/). + +Now that you have a funded subscription account and your subscription ID, [create and deploy a VRF v2 compatible contract](#create-and-deploy-a-vrf-v2-compatible-contract). + +## Create and deploy a VRF v2 compatible contract + +For this example, use the [VRFv2Consumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2Consumer.sol) sample contract. This contract imports the following dependencies: + +- `VRFConsumerBaseV2.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFConsumerBaseV2.sol) +- `VRFCoordinatorV2Interface.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/interfaces/VRFCoordinatorV2Interface.sol) +- `ConfirmedOwner.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/shared/access/ConfirmedOwner.sol) + +The contract also includes pre-configured values for the necessary request parameters such as `vrfCoordinator` address, gas lane `keyHash`, `callbackGasLimit`, `requestConfirmations` and number of random words `numWords`. You can change these parameters if you want to experiment on different testnets, but for this example you only need to specify `subscriptionId` when you deploy the contract. + +Build and deploy the contract on Sepolia. + +1. Open the [`VRFv2Consumer.sol` contract](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2Consumer.sol) in Remix. + + {/* prettier-ignore */} + + +1. On the **Compile** tab in Remix, compile the `VRFv2Consumer.sol` contract. + +1. Configure your deployment. On the **Deploy** tab in Remix, select the **Injected Provider** environment, select the `VRFv2Consumer` contract from the contract list, and specify your `subscriptionId` so the constructor can set it. + + ![Example showing the deploy button with the subscriptionID field filled in Remix](/images/vrf/deployWithSubscriptionId.png) + +1. Click the **Deploy** button to deploy your contract onchain. MetaMask opens and asks you to confirm the transaction. + +1. After you deploy your contract, copy the address from the **Deployed Contracts** list in Remix. Before you can request randomness from VRF v2, you must add this address as an approved consuming contract on your subscription account. + + ![Example showing the contract address listed under the Contracts list in Remix](/images/vrf/getContractAddress.png) + +1. Open the Subscription Manager at [vrf.chain.link](https://vrf.chain.link/) and click the ID of your new subscription under the **My Subscriptions** list. The subscription details page opens. + +1. Under the **Consumers** section, click **Add consumer**. + +1. Enter the address of your consuming contract that you just deployed and click **Add consumer**. MetaMask opens and asks you to confirm the transaction. + +Your example contract is deployed and approved to use your subscription balance to pay for VRF v2 requests. Next, [request random values](#request-random-values) from Chainlink VRF. + +## Request random values + +The deployed contract requests random values from Chainlink VRF, receives those values, builds a struct `RequestStatus` containing them and stores the struct in a mapping `s_requests`. Run the `requestRandomWords()` function on your contract to start the request. + +1. Return to Remix and view your deployed contract functions in the **Deployed Contracts** list. + +1. Click the `requestRandomWords()` function to send the request for random values to Chainlink VRF. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, Chainlink VRF processes your request. Chainlink VRF fulfills the request and returns the random values to your contract in a callback to the `fulfillRandomWords()` function. At this point, a new key `requestId` is added to the mapping `s_requests`. + + Depending on current testnet conditions, it might take a few minutes for the callback to return the requested random values to your contract. You can see a list of pending requests for your subscription ID at [vrf.chain.link](https://vrf.chain.link/). + +1. To fetch the request ID of your request, call `lastRequestId()`. + +1. After the oracle returns the random values to your contract, the mapping `s_requests` is updated: The received random values are stored in `s_requests[_requestId].randomWords`. + +1. Call `getRequestStatus()` specifying the `requestId` to display the random words. + +You deployed a simple contract that can request and receive random values from Chainlink VRF. To see more advanced examples where the contract can complete the entire process including subscription setup and management, see the [Programmatic Subscription](/vrf/v2/subscription/examples/programmatic-subscription) page. + + + +## Analyzing the contract + +In this example, your MetaMask wallet is the subscription owner and you created a consuming contract to use that subscription. The consuming contract uses static configuration parameters. + + + +The parameters define how your requests will be processed. You can find the values for your network in the [Configuration](/vrf/v2/subscription/supported-networks) page. + +- `uint64 s_subscriptionId`: The subscription ID that this contract uses for funding requests. + +- `bytes32 keyHash`: The gas lane key hash value, which is the maximum gas price you are willing to pay for a request in wei. It functions as an ID of the offchain VRF job that runs in response to requests. + +- `uint32 callbackGasLimit`: The limit for how much gas to use for the callback request to your contract's `fulfillRandomWords()` function. It must be less than the `maxGasLimit` limit on the coordinator contract. Adjust this value for larger requests depending on how your `fulfillRandomWords()` function processes and stores the received random values. If your `callbackGasLimit` is not sufficient, the callback will fail and your subscription is still charged for the work done to generate your requested random values. + +- `uint16 requestConfirmations`: How many confirmations the Chainlink node should wait before responding. The longer the node waits, the more secure the random value is. It must be greater than the `minimumRequestBlockConfirmations` limit on the coordinator contract. + +- `uint32 numWords`: How many random values to request. If you can use several random values in a single callback, you can reduce the amount of gas that you spend per random value. The total cost of the callback request depends on how your `fulfillRandomWords()` function processes and stores the received random values, so adjust your `callbackGasLimit` accordingly. + +The contract includes the following functions: + +- `requestRandomWords()`: Takes your specified parameters and submits the request to the VRF coordinator contract. + +- `fulfillRandomWords()`: Receives random values and stores them with your contract. + +- `getRequestStatus()`: Retrive request details for a given `_requestId`. + + + +## Clean up + +After you are done with this contract and the subscription, you can retrieve the remaining testnet LINK to use with other examples. + +1. Open the Subscription Manager at [vrf.chain.link](https://vrf.chain.link/) and click the ID of your new subscription under the **My Subscriptions** list. The subscription details page opens. + +1. On your subscription details page, expand the **Actions** menu and select **Cancel subscription**. A field displays, prompting you to add the wallet address you want to send the remaining funds to. + +1. Enter your wallet address and click **Cancel subscription**. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, Chainlink VRF closes your subscription account and sends the remaining LINK to your wallet. + +## Vyper example + +You must import the `VRFCoordinatorV2` Vyper interface. You can find it [here](https://github.com/smartcontractkit/apeworx-starter-kit/blob/main/contracts/interfaces/VRFCoordinatorV2.vy). +You can find a `VRFConsumerV2` example [here](https://github.com/smartcontractkit/apeworx-starter-kit/blob/main/contracts/VRFConsumerV2.vy). Read the _**apeworx-starter-kit**_ [README](https://github.com/smartcontractkit/apeworx-starter-kit) to learn how to run the example. From cfdf5a19c7adac97b94d11610545409901ae636a Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 21 May 2024 14:42:39 -0400 Subject: [PATCH 02/18] sub: updated most refs --- .../v2-5/subscription/get-a-random-number.mdx | 66 ++++++++----------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/src/content/vrf/v2-5/subscription/get-a-random-number.mdx b/src/content/vrf/v2-5/subscription/get-a-random-number.mdx index 702b1de2fa0..16a9411270b 100644 --- a/src/content/vrf/v2-5/subscription/get-a-random-number.mdx +++ b/src/content/vrf/v2-5/subscription/get-a-random-number.mdx @@ -4,25 +4,21 @@ date: Last Modified title: "Get a Random Number" whatsnext: { - "Programmatic Subscription": "/vrf/v2/subscription/examples/programmatic-subscription", - "Subscription Manager UI": "/vrf/v2/subscription/ui", - "Security Considerations": "/vrf/v2/security", - "Best Practices": "/vrf/v2/best-practices", - "Migrating from VRF v1 to v2": "/vrf/v2/subscription/migration-from-v1", - "Supported Networks": "/vrf/v2/subscription/supported-networks", + "Security Considerations": "/vrf/v2-5/security", + "Best Practices": "/vrf/v2-5/best-practices", + "Migrating from V2": "/vrf/v2-5/subscription/migration-from-v2", + "Supported Networks": "/vrf/v2-5/supported-networks", } metadata: - description: "How to generate a random number inside a smart contract using Chainlink VRF." + description: "How to generate a random number inside a smart contract using Chainlink VRF V2.5." --- -import VrfCommon from "@features/vrf/v2/common/VrfCommon.astro" +import Vrf2_5Common from "@features/vrf/v2-5/Vrf2_5Common.astro" import { Aside, CodeSample } from "@components" - +This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2.5. For more advanced examples with programmatic subscription configuration, see the [Programmatic Subscription](/vrf/v2/subscription/examples/programmatic-subscription) page. To explore more applications of VRF, refer to our [blog](https://blog.chain.link/). -This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2. For more advanced examples with programmatic subscription configuration, see the [Programmatic Subscription](/vrf/v2/subscription/examples/programmatic-subscription) page. To explore more applications of VRF, refer to our [blog](https://blog.chain.link/). - - + ## Requirements @@ -38,7 +34,7 @@ If you are new to developing smart contracts on Ethereum, see the [Getting Start For this example, create a new subscription on the Sepolia testnet. -1. Open MetaMask and set it to use the Sepolia testnet. The [Subscription Manager](/vrf/v2/subscription/ui) detects your network based on the active network in MetaMask. +1. Open MetaMask and set it to use the Sepolia testnet. The [Subscription Manager](https://vrf.chain.link) detects your network based on the active network in MetaMask. 1. Check MetaMask to make sure you have testnet ETH and LINK on Sepolia. You can get testnet ETH and LINK at [faucets.chain.link](https://faucets.chain.link/sepolia/). @@ -53,8 +49,8 @@ For this example, create a new subscription on the Sepolia testnet. 1. After the subscription is created, click **Add funds** and follow the instructions to fund your subscription. - - For your request to go through, you need to fund your subscription with enough LINK to meet your [minimum subscription balance](/vrf/v2/subscription#minimum-subscription-balance) to serve as a buffer against gas volatility. For this example, a balance of 12 LINK is sufficient. (After your request is processed, it costs around 3 LINK, and that amount will be deducted from your subscription balance.) - - MetaMask opens to confirm the LINK transfer to your subscription. After you approve the transaction, the network confirms the transfer of your LINK token to your subscription account. + - For your request to go through, you need to fund your subscription with enough testnet funds to meet your [minimum subscription balance](/vrf/v2-5/overview/subscription#minimum-subscription-balance) to serve as a buffer against gas volatility. For this example, a balance of 12 LINK is sufficient. (After your request is processed, it costs around 3 LINK, and that amount will be deducted from your subscription balance.) + - MetaMask opens to confirm the token transfer to your subscription. After you approve the transaction, the network confirms the transfer of your testnet funds to your subscription account. 1. After you add funds, click **Add consumer**. A page opens with your account details and subscription ID. @@ -62,34 +58,33 @@ For this example, create a new subscription on the Sepolia testnet. You can always find your subscription IDs, balances, and consumers at [vrf.chain.link](https://vrf.chain.link/). -Now that you have a funded subscription account and your subscription ID, [create and deploy a VRF v2 compatible contract](#create-and-deploy-a-vrf-v2-compatible-contract). +Now that you have a funded subscription account and your subscription ID, [create and deploy a VRF compatible contract](#create-and-deploy-a-vrf-compatible-contract). -## Create and deploy a VRF v2 compatible contract +## Create and deploy a VRF compatible contract -For this example, use the [VRFv2Consumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2Consumer.sol) sample contract. This contract imports the following dependencies: +For this example, use the [SubscriptionConsumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/v2-5/SubscriptionConsumer.sol) sample contract. This contract imports the following dependencies: -- `VRFConsumerBaseV2.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFConsumerBaseV2.sol) -- `VRFCoordinatorV2Interface.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/interfaces/VRFCoordinatorV2Interface.sol) -- `ConfirmedOwner.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/shared/access/ConfirmedOwner.sol) +- `VRFConsumerBaseV2Plus.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol) +- `VRFV2PlusClient.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol) The contract also includes pre-configured values for the necessary request parameters such as `vrfCoordinator` address, gas lane `keyHash`, `callbackGasLimit`, `requestConfirmations` and number of random words `numWords`. You can change these parameters if you want to experiment on different testnets, but for this example you only need to specify `subscriptionId` when you deploy the contract. Build and deploy the contract on Sepolia. -1. Open the [`VRFv2Consumer.sol` contract](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2Consumer.sol) in Remix. +1. Open the [SubscriptionConsumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/v2-5/SubscriptionConsumer.sol) in Remix. {/* prettier-ignore */} - + -1. On the **Compile** tab in Remix, compile the `VRFv2Consumer.sol` contract. +1. On the **Compile** tab in Remix, compile the `SubscriptionConsumer.sol` contract. -1. Configure your deployment. On the **Deploy** tab in Remix, select the **Injected Provider** environment, select the `VRFv2Consumer` contract from the contract list, and specify your `subscriptionId` so the constructor can set it. +1. Configure your deployment. On the **Deploy** tab in Remix, select the **Injected Provider** environment, select the `SubscriptionConsumer` contract from the contract list, and specify your `subscriptionId` so the constructor can set it. ![Example showing the deploy button with the subscriptionID field filled in Remix](/images/vrf/deployWithSubscriptionId.png) 1. Click the **Deploy** button to deploy your contract onchain. MetaMask opens and asks you to confirm the transaction. -1. After you deploy your contract, copy the address from the **Deployed Contracts** list in Remix. Before you can request randomness from VRF v2, you must add this address as an approved consuming contract on your subscription account. +1. After you deploy your contract, copy the address from the **Deployed Contracts** list in Remix. Before you can request randomness from VRF v2.5, you must add this address as an approved consuming contract on your subscription account. ![Example showing the contract address listed under the Contracts list in Remix](/images/vrf/getContractAddress.png) @@ -99,7 +94,7 @@ Build and deploy the contract on Sepolia. 1. Enter the address of your consuming contract that you just deployed and click **Add consumer**. MetaMask opens and asks you to confirm the transaction. -Your example contract is deployed and approved to use your subscription balance to pay for VRF v2 requests. Next, [request random values](#request-random-values) from Chainlink VRF. +Your example contract is deployed and approved to use your subscription balance to pay for VRF v2.5 requests. Next, [request random values](#request-random-values) from Chainlink VRF. ## Request random values @@ -121,18 +116,18 @@ You deployed a simple contract that can request and receive random values from C ## Analyzing the contract In this example, your MetaMask wallet is the subscription owner and you created a consuming contract to use that subscription. The consuming contract uses static configuration parameters. - + -The parameters define how your requests will be processed. You can find the values for your network in the [Configuration](/vrf/v2/subscription/supported-networks) page. +The parameters define how your requests will be processed. You can find the values for your network in the [Configuration](/vrf/v2-5/supported-networks) page. -- `uint64 s_subscriptionId`: The subscription ID that this contract uses for funding requests. +- `uint256 s_subscriptionId`: The subscription ID that this contract uses for funding requests. - `bytes32 keyHash`: The gas lane key hash value, which is the maximum gas price you are willing to pay for a request in wei. It functions as an ID of the offchain VRF job that runs in response to requests. @@ -152,20 +147,15 @@ The contract includes the following functions: ## Clean up -After you are done with this contract and the subscription, you can retrieve the remaining testnet LINK to use with other examples. +After you are done with this contract and the subscription, you can retrieve the remaining testnet tokens to use with other examples. 1. Open the Subscription Manager at [vrf.chain.link](https://vrf.chain.link/) and click the ID of your new subscription under the **My Subscriptions** list. The subscription details page opens. 1. On your subscription details page, expand the **Actions** menu and select **Cancel subscription**. A field displays, prompting you to add the wallet address you want to send the remaining funds to. 1. Enter your wallet address and click **Cancel subscription**. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, Chainlink VRF closes your subscription account and sends the remaining LINK to your wallet. - -## Vyper example - -You must import the `VRFCoordinatorV2` Vyper interface. You can find it [here](https://github.com/smartcontractkit/apeworx-starter-kit/blob/main/contracts/interfaces/VRFCoordinatorV2.vy). -You can find a `VRFConsumerV2` example [here](https://github.com/smartcontractkit/apeworx-starter-kit/blob/main/contracts/VRFConsumerV2.vy). Read the _**apeworx-starter-kit**_ [README](https://github.com/smartcontractkit/apeworx-starter-kit) to learn how to run the example. From 61293bd15407caf355e62911dea001c1cfe8307a Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 21 May 2024 14:55:00 -0400 Subject: [PATCH 03/18] fix link --- src/content/vrf/v2-5/subscription/get-a-random-number.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/vrf/v2-5/subscription/get-a-random-number.mdx b/src/content/vrf/v2-5/subscription/get-a-random-number.mdx index 16a9411270b..3d92d324a6d 100644 --- a/src/content/vrf/v2-5/subscription/get-a-random-number.mdx +++ b/src/content/vrf/v2-5/subscription/get-a-random-number.mdx @@ -6,7 +6,7 @@ whatsnext: { "Security Considerations": "/vrf/v2-5/security", "Best Practices": "/vrf/v2-5/best-practices", - "Migrating from V2": "/vrf/v2-5/subscription/migration-from-v2", + "Migrating from V2": "/vrf/v2-5/migration-from-v2", "Supported Networks": "/vrf/v2-5/supported-networks", } metadata: From 8a7f843bdeac4621667f2a576db9a2fd742dba89 Mon Sep 17 00:00:00 2001 From: thedriftofwords Date: Tue, 21 May 2024 17:28:39 -0400 Subject: [PATCH 04/18] df: updated most refs --- .../direct-funding/get-a-random-number.mdx | 68 +++++++++++-------- .../vrf/v2-5/overview/direct-funding.mdx | 2 +- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx b/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx index 2169ed6889d..f5815def1c9 100644 --- a/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx +++ b/src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx @@ -4,21 +4,19 @@ date: Last Modified title: "Get a Random Number" whatsnext: { - "Security Considerations": "/vrf/v2/security", - "Best Practices": "/vrf/v2/best-practices", - "Migrating from VRF v1": "/vrf/v2/direct-funding/migration-from-v1", - "Supported Networks": "/vrf/v2/direct-funding/supported-networks", + "Security Considerations": "/vrf/v2-5/security", + "Best Practices": "/vrf/v2-5/best-practices", + "Migrating from V2": "/vrf/v2-5/migration-from-v2", + "Supported Networks": "/vrf/v2/supported-networks", } metadata: - description: "How to generate a random number inside a smart contract using Chainlink VRF v2 - Direct funding method." + description: "How to generate a random number inside a smart contract using Chainlink VRF v2.5 - Direct funding method." --- -import VrfCommon from "@features/vrf/v2/common/VrfCommon.astro" +import Vrf2_5Common from "@features/vrf/v2-5/Vrf2_5Common.astro" import { Aside, CodeSample } from "@components" - - -This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2 without managing a subscription. To explore more applications of VRF, refer to our [blog](https://blog.chain.link/). +This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2.5 without managing a subscription. To explore more applications of VRF, refer to our [blog](https://blog.chain.link/). ## Requirements @@ -30,36 +28,48 @@ This guide assumes that you know how to create and deploy smart contracts on Eth If you are new to developing smart contracts on Ethereum, see the [Getting Started](/getting-started/conceptual-overview) guide to learn the basics. -## Create and deploy a VRF v2 compatible contract +## Create and deploy a VRF compatible contract -For this example, use the [VRFv2DirectFundingConsumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2DirectFundingConsumer.sol) sample contract. This contract imports the following dependencies: +For this example, use the [DirectFundingConsumer.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/v2-5/DirectFundingConsumer.sol) sample contract. This contract imports the following dependencies: -- `VRFV2WrapperConsumerBase.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/VRFV2WrapperConsumerBase.sol) -- `ConfirmedOwner.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/shared/access/ConfirmedOwner.sol) +- `VRFV2PlusWrapperConsumerBase.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/dev/VRFV2PlusWrapperConsumerBase.sol) +- `VRFV2PlusClient.sol`[(link)](https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol) -The contract also includes pre-configured values for the necessary request parameters such as `callbackGasLimit`, `requestConfirmations`, the number of random words `numWords`, the VRF v2 Wrapper address `wrapperAddress`, and the LINK token address `linkAddress`. You can change these parameters if you want to experiment on different testnets. +The contract also includes pre-configured values for the necessary request parameters such as `callbackGasLimit`, `requestConfirmations`, the number of random words `numWords`, the VRF v2.5 Wrapper address `wrapperAddress`, and the LINK token address `linkAddress`. You can change these parameters if you want to experiment on different testnets. Build and deploy the contract on Sepolia. -1. Open the [`VRFv2DirectFundingConsumer.sol` contract](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/VRFv2DirectFundingConsumer.sol) in Remix. + + +1. Open the [`DirectFundingConsumer.sol` contract](https://remix.ethereum.org/#url=https://docs.chain.link/samples/VRF/v2-5/DirectFundingConsumer.sol) in Remix. {/* prettier-ignore */} - + -1. On the **Compile** tab in Remix, compile the `VRFv2DirectFundingConsumer` contract. +1. On the **Compile** tab in Remix, compile the `DirectFundingConsumer` contract. -1. Configure your deployment. On the **Deploy** tab in Remix, select the **Injected Web3 Environment** and select the `VRFv2DirectFundingConsumer` contract from the contract list. +1. Configure your deployment. On the **Deploy** tab in Remix, select the **Injected Web3 Environment** and select the `DirectFundingConsumer` contract from the contract list. 1. Click the **Deploy** button to deploy your contract onchain. MetaMask opens and asks you to confirm the transaction. -1. After you deploy your contract, copy the address from the **Deployed Contracts** list in Remix. Before you can request randomness from VRF v2, you must fund your consuming contract with enough LINK tokens in order to request for randomness. Next, [fund your contract](#fund-your-contract). +1. After you deploy your contract, copy the address from the **Deployed Contracts** list in Remix. Before you can request randomness from VRF v2.5, you must fund your consuming contract with enough tokens in order to request for randomness. Next, [fund your contract](#fund-your-contract). -## Fund Your Contract +## Fund your contract -Requests for randomness will fail unless your consuming contract has enough LINK. +Requests for randomness will fail unless your consuming contract has enough tokens. VRF V2.5 allows you to use either native tokens or LINK to pay for your requests. -1. [Acquire testnet LINK](/resources/acquire-link). -1. [Fund your contract with testnet LINK](/resources/fund-your-contract). For this example, funding your contract with 2 LINK should be sufficient. +1. [Acquire testnet LINK and Sepolia ETH](https://faucets.chain.link/sepolia). +1. [Fund your contract](/resources/fund-your-contract) with either testnet LINK or Sepolia ETH, depending on how you want to pay for your VRF requests. For this example, funding your contract with 2 LINK should be sufficient. ## Request random values @@ -69,7 +79,7 @@ The deployed contract requests random values from Chainlink VRF, receives those 1. Click the `requestRandomWords()` function to send the request for random values to Chainlink VRF. MetaMask opens and asks you to confirm the transaction. -