Skip to content

Commit

Permalink
df: updated most refs
Browse files Browse the repository at this point in the history
  • Loading branch information
thedriftofwords committed May 21, 2024
1 parent 61293bd commit 8a7f843
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
68 changes: 39 additions & 29 deletions src/content/vrf/v2-5/direct-funding/get-a-random-number.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

<VrfCommon callout="directFunding" />

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

Expand All @@ -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.
<Aside type="caution" title="Outdated V2.5 direct funding interface and wrapper in chainlink 1.1.0 package">
The [`chainlink/contracts` 1.1.0 package](https://www.npmjs.com/package/@chainlink/contracts/v/1.1.0) currently has an outdated interface and wrapper for V2.5 direct funding. If you deploy contracts for direct funding with these outdated interfaces, your `requestRandomness` function will revert.

Use this interface and wrapper:

- `VRFV2PlusWrapperConsumerBase.sol`: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/dev/VRFV2PlusWrapperConsumerBase.sol
- `IVRFV2PlusWrapper.sol`: https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/vrf/dev/interfaces/IVRFV2PlusWrapper.sol

The `DirectFundingConsumer.sol` example code used in this tutorial includes the updated wrapper and interface code within the same file.

</Aside>

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 */}
<CodeSample src="samples/VRF/VRFv2DirectFundingConsumer.sol" showButtonOnly/>
<CodeSample src="samples/VRF/v2-5/DirectFundingConsumer.sol" showButtonOnly/>

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

Expand All @@ -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.

<Aside type="note" title="Set your gas limit in MetaMask">
<Aside type="note" title="Set your gas limit in MetaMask">
Remix IDE doesn't set the right gas limit, so you must [edit the
gas limit in MetaMask](https://support.metamask.io/hc/en-us/articles/360022895972) within the **Advanced gas controls** settings.

Expand All @@ -91,18 +101,18 @@ The deployed contract requests random values from Chainlink VRF, receives those

<Aside type="note" title="Note on Requesting or Cancelling Randomness">
Do not allow re-requesting or cancellation of randomness. For more information, see the [VRF Security
Considerations](/vrf/v2/security#do-not-allow-re-requesting-or-cancellation-of-randomness) page.
Considerations](/vrf/v2-5/security#do-not-allow-re-requesting-or-cancellation-of-randomness) page.
</Aside>

## Analyzing the contract

In this example, the consuming contract uses static configuration parameters.

<CodeSample src="samples/VRF/VRFv2DirectFundingConsumer.sol" />
<CodeSample src="samples/VRF/v2-5/DirectFundingConsumer.sol" />

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.
The parameters define how your requests will be processed. You can find the values for your network in the [Supported networks](/vrf/v2-5/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.
- `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.5 Direct funding limits](/vrf/v2-5/overview/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.

Expand All @@ -120,7 +130,7 @@ The contract includes the following functions:

<Aside type="note" title="Security Considerations">
Be sure to review your contracts to make sure they follow the best practices on the [security
considerations](/vrf/v2/security) page.
considerations](/vrf/v2-5/security) page.
</Aside>

## Clean up
Expand Down
2 changes: 1 addition & 1 deletion src/content/vrf/v2-5/overview/direct-funding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ After you submit your request, it is processed using the [Request & Receive Data

## Limits

You can see the configuration for each network on the [Supported networks](/vrf/v2/direct-funding/supported-networks) page. You can also view the full configuration for each VRF v2.5 Wrapper contract directly in Etherscan. As an example, view the [Ethereum Mainnet VRF v2.5 Wrapper contract](https://etherscan.io/address/0x02aae1A04f9828517b3007f83f6181900CaD910c#readContract) configuration by calling `getConfig` function.
You can see the configuration for each network on the [Supported networks](/vrf/v2-5/supported-networks) page. You can also view the full configuration for each VRF v2.5 Wrapper contract directly in Etherscan. As an example, view the [Ethereum Mainnet VRF v2.5 Wrapper contract](https://etherscan.io/address/0x02aae1A04f9828517b3007f83f6181900CaD910c#readContract) configuration by calling `getConfig` function.

- Each wrapper has a `maxNumWords` parameter that limits the maximum number of random values you can receive in each request.
- The maximum allowed `callbackGasLimit` value for your requests is defined in the [Coordinator contract supported networks](/vrf/v2-5/supported-networks) page. Because the VRF v2.5 Wrapper adds an overhead, your `callbackGasLimit` must not exceed `maxGasLimit - wrapperGasOverhead`. Learn more about [estimating costs](/vrf/v2-5/billing).

0 comments on commit 8a7f843

Please sign in to comment.