Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT - do not merge] Applying third-party content guidelines to VRF PR #476

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: "How to use Randomizer.ai's verifiable random functions (VRFs) in Arbitrum"
sidebar_label: Use verifiable random functions (VRFs) in Arbitrum
description: Learn how to generate verifiable, unpredictable random numbers in your Arbitrum smart contracts by using Randomizer.ai's verifiable random functions (VRFs) protocol.
reader-audience: developers who want to build on Ethereum/Arbitrum
content-type: how-to
---

:::info Community member contribution

The following [third-party document](../../contribute#new-third-party-docs-protocol) was contributed by [@DeanPress](https://twitter.com/Deanpress). Give them a shoutout if you find it useful!

:::

[Verifiable random functions (VRFs)](./verifiable-random-functions) are cryptographic methods to generate random numbers in a way that is verifiable and unpredictable. That means anyone can verify that a given number was generated correctly, but no one can predict the output before it's generated, even if they know the input.

VRFs can offer a **reliable source of randomness** for decentralized apps (dApps), such as games (e.g. card dealing, XP gain, treasure opening, hit/dodge rates), generating NFT traits, and airdrops.

## Randomizer VRF

[Randomizer](https://randomizer.ai/) is a decentralized VRF protocol on Arbitrum. It uses native ETH for fee settlement, makes fast callbacks to smart contracts, and offers an [npm module](https://www.npmjs.com/package/@randomizer.ai/realtime-client) to bring real-time results to dApps.

### Requesting a random number from Randomizer

Here’s an example of how to use the Randomizer VRF with your Arbitrum smart contract:

```solidity
interface IRandomizer {
function request(uint256 callbackGasLimit) external returns (uint256);
function request(uint256 callbackGasLimit, uint256 confirmations) external returns (uint256);
function clientWithdrawTo(address _to, uint256 _amount) external;
}

// Coinflip contract
contract CoinFlip {
// Using Randomizer's Arbitrum Goerli address
IRandomizer public randomizer = IRandomizer(0x923096Da90a3b60eb7E12723fA2E1547BA9236Bc);
address public owner;

// Stores each game to the player
mapping(uint256 => address) public flipToAddress;

// Events
event Win(address winner);
event Lose(address loser);

modifier onlyOwner() {
require(msg.sender == owner, "Sender is not owner");
_;
}

// The coin flip containing the random request
function flip() external returns (uint256) {
// Get the latest randomizer contract from the testnet proxy
// Request a random number from the randomizer contract (50k callback limit)
uint256 id = randomizer.request(50000);
// You can also do randomizer.request(50000, 20) to get a callback after 20 confirmations for increased finality security (you can do 1-40 confirmations).
// Store the flip ID and the player address
flipToAddress[id] = msg.sender;
// Return the flip ID
return id;
}

// Callback function called by the randomizer contract when the random value is generated
function randomizerCallback(uint256 _id, bytes32 _value) external {
//Callback can only be called by randomizer
require(msg.sender == address(randomizer), "Caller not Randomizer");
// Get the player address from the flip ID
address player = flipToAddress[_id];
// Convert the random bytes to a number between 0 and 99
uint256 random = uint256(_value) % 100;
// If the random number is less than 50, the player wins
if (random < 50) {
emit Win(player);
} else {
emit Lose(player);
}
}

// Allows the owner to withdraw their deposited randomizer funds
function randomizerWithdraw(uint256 amount) external onlyOwner {
randomizer.clientWithdrawTo(msg.sender, amount);
}
}
```

### Randomizer contract addresses

- **Arbitrum One:** `0x5b8bB80f2d72D0C85caB8fB169e8170A05C94bAF`
- **Arbitrum Goerli Testnet:** `0x923096Da90a3b60eb7E12723fA2E1547BA9236Bc`

### Full documentation

Refer to [Randomizer's documentation](https://randomizer.ai/docs) for more examples and instructions on how to integrate common features like having the end-user pay the VRF fee, refunding users for overdraft fees, and real-time results.

### Full dApp example

Refer to [Randomizer's Coinflip game source code](https://github.com/RandomizerAi/coinflip-example) for a full-stack example dApp (front-end and smart contract) that integrates real-time results, user payments for fees, and user refunds for overdraft fees.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: 'Verifiable random functions (VRFs): A conceptual overview'
sidebar_label: Verifiable random functions (VRF)
description: A high level description of what random numbers and VRFs are
reader-audience: developers who want to build on Ethereum/Arbitrum
content-type: concept
---

:::info Community member contribution

The following [third-party document](../../contribute#new-third-party-docs-protocol) was contributed by [@DeanPress](https://twitter.com/Deanpress). Give them a shoutout if you find it useful!

:::

**Random numbers** and **verifiable random functions** (VRFs) are key aspects of cryptographic systems, playing crucial roles in many blockchain-based applications. In this conceptual overview, we'll explore what random numbers and VRFs are, how they work, and some general use cases for them.

Note that Solidity doesn't offer native support for VRFs, but you can use a third-party VRF protocol like [Randomizer.ai](https://randomizer.ai/) to integrate this functionality into your smart contracts. Refer to [How to use Randomizer.ai's VRFs on Arbitrum](./how-to-use-vrfs) for instructions.

### What are random numbers and VRFs?

Random numbers are numbers generated in a manner that the values are statistically random and equally likely to be any of the possible outcomes. In the context of blockchain, they are used for many purposes, such as determining the outcomes in a decentralized lottery or generating NFT traits.

Verifiable random functions (VRFs) are cryptographic methods to generate random numbers in a way that is verifiable and unpredictable. That means anyone can verify that a given number was generated correctly, but no one can predict the output before it's generated, even if they know the input.

### Types of random number generation

Random numbers can be generated in different ways, depending on the level of security and decentralization required:

- **Pseudorandom number generators (PRNGs):** These generate numbers that appear to be random and unpredictable. They start with a seed value and produce a sequence of numbers using a deterministic algorithm. The same seed will always produce the same sequence of numbers, so they are not truly random.
- **True random number generators (TRNGs):** These generate numbers from a truly random physical process, such as atmospheric noise or radioactive decay. They do not use a deterministic algorithm, so they can't be reproduced. However, these are difficult to implement in a decentralized manner, since they require a source of physical randomness.
- **Verifiable random functions:** As mentioned above, VRFs are a cryptographic method of generating verifiable and unpredictable random numbers. They combine the advantages of PRNGs and TRNGs by ensuring that the numbers are both unpredictable and verifiable.

### How do VRFs work?

A VRF is a function that takes an input and produces a random output along with a proof. The same input will always produce the same output and proof, but without the proof, it's computationally infeasible to predict the output from the input. The proof can be used by anyone to verify that the output was generated correctly from the input.

```mermaid
sequenceDiagram
participant ClientSC as Client Smart Contract
participant VRFProtocol as VRF Protocol Smart Contract
participant Prover

ClientSC->>VRFProtocol: Request VRF with seed
VRFProtocol->>Prover: Forward seed to Prover
Prover-->>VRFProtocol: Sign seed and return VRF proofs
VRFProtocol-->>ClientSC: Generate bytes32 hash from proofs and Callback
```

### Use cases for random numbers and VRFs

Random numbers and VRFs are used in various applications across different industries. Some general use cases include:

- **Decentralized gaming:** Random numbers are used in many blockchain-based games to create unpredictable game outcomes (e.g. shuffling cards, hit rates in PvP games, treasure chest opening, XP gain). VRFs can make these outcomes verifiable, increasing trust in the game.
- **NFT minting:** VRFs can be used during the NFT minting process to randomly assign attributes, rarity or metadata to the NFTs, ensuring the fairness and unpredictability of the minting process.
- **On-chain Random Governance:** For DAOs or other on-chain governance models, VRFs can be used to randomly select a subset of members for decision-making to ensure a fair representation and prevent collusion.

In summary, random numbers and VRFs are key components of the blockchain ecosystem, ensuring fairness, security, and transparency in various applications.
11 changes: 11 additions & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ const sidebars = {
type: 'doc',
id: 'for-devs/contribute',
},
{
type: 'category',
label: 'Third-party content',
collapsed: true,
items: [
{
type: 'autogenerated',
dirName: 'for-devs/third-party-content',
},
],
},
],
bridgeTokensSidebar: [
{
Expand Down