Skip to content

Commit 19be573

Browse files
committed
chore(dev-docs) Entropy Guide Edits - 1
1 parent 332c3ea commit 19be573

File tree

8 files changed

+401
-97
lines changed

8 files changed

+401
-97
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
title: Generate Random Numbers onchain
3+
description: Learn how to integrate Pyth Entropy to generate random numbers in your dapp
4+
---
5+
6+
import { Step, Steps } from "fumadocs-ui/components/steps";
7+
8+
This guide explains how to integrate Pyth Entropy into EVM Contracts to generate on-chain random numbers.
9+
The intended audience for this guide is developers of any application that needs on-chain randomness, such as NFT mints or games.
10+
11+
## Install the SDK
12+
13+
Pyth Entropy has a [Solidity SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/entropy_sdk/solidity) that lets your contract interact with the Entropy contract.
14+
Install the SDK using your package manager:
15+
16+
<Tabs items={['hardhat', 'foundry']}>
17+
<Tab value="hardhat">
18+
```shell copy
19+
npm install @pythnetwork/entropy-sdk-solidity
20+
```
21+
</Tab>
22+
<Tab value="foundry">
23+
```shell copy
24+
npm init -y
25+
npm install @pythnetwork/entropy-sdk-solidity
26+
```
27+
28+
Then add the following line to your `remappings.txt` file:
29+
30+
```text copy
31+
@pythnetwork/entropy-sdk-solidity/=node_modules/@pythnetwork/entropy-sdk-solidity
32+
```
33+
34+
</Tab>
35+
</Tabs>
36+
37+
## Setup
38+
39+
The Solidity SDK exports two interfaces:
40+
41+
- [`IEntropyConsumer`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropyConsumer.sol) - The interface that your contract should implement. It makes sure that your contract is compliant with the Entropy contract.
42+
- [`IEntropyV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropyV2.sol) - The interface to interact with the Entropy contract.
43+
You will need the address of an Entropy contract on your blockchain.
44+
Consult the current [Entropy contract addresses](../contract-addresses) to find the address on your chain.
45+
Once you have a contract address, instantiate an `console.log("IEntropyV2"){:bash}` contract in your solidity contract:
46+
47+
```solidity copy
48+
import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
49+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
50+
51+
// @param entropyAddress The address of the entropy contract.
52+
contract YourContract is IEntropyConsumer {
53+
IEntropyV2 public entropy;
54+
55+
constructor(address entropyAddress) {
56+
entropy = IEntropyV2(entropyAddress);
57+
}
58+
}
59+
60+
```
61+
62+
## Usage
63+
64+
To generate a random number, follow these steps.
65+
66+
<Steps>
67+
<Step>
68+
### 1. Request a number from Entropy
69+
70+
Invoke the [`requestV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L83) method of the `IEntropyV2` interface.
71+
The `console.log("requestV2"){:bash}` method requires paying a fee in native gas tokens which is configured per-provider.
72+
73+
The fee differs for every chain and also varies over time depending on the chain's current gas price.
74+
The current value for each chain can be found on the [Current Fees](../current-fees) page.
75+
However, you should use the on-chain method [`getFeeV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L101) to compute the required fee and send it as the value of the `requestV2{:bash}` call.
76+
77+
These methods use the default randomness provider ([see here](#randomness-providers) for more info on providers).
78+
79+
```solidity copy
80+
function requestRandomNumber() external payable {
81+
uint256 fee = entropy.getFeeV2();
82+
83+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
84+
}
85+
86+
```
87+
88+
This method returns a sequence number and emits a [`Requested`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/EntropyEventsV2.sol#L30) event. You can store this sequence number to identify the request in next step.
89+
90+
Note that there are several variants of `requestV2` that allow the caller to configure the provider fulfilling the request and the gas limit for the callback. Refer [request callback variants](../request-callback-variants.mdx) for more details.
91+
92+
Please see the method documentation in the [IEntropyV2 interface](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropyV2.sol).
93+
94+
</Step>
95+
<Step>
96+
97+
### 2. Implement the Entropy callback
98+
99+
```solidity {31-45} copy
100+
pragma solidity ^0.8.0;
101+
102+
import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
103+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
104+
105+
contract YourContract is IEntropyConsumer {
106+
IEntropyV2 entropy;
107+
108+
// @param entropyAddress The address of the entropy contract.
109+
constructor(address entropyAddress) {
110+
entropy = IEntropyV2(entropyAddress);
111+
}
112+
113+
function requestRandomNumber() external payable {
114+
// Get the fee for the request
115+
uint256 fee = entropy.getFeeV2();
116+
117+
// Request the random number with the callback
118+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
119+
// Store the sequence number to identify the callback request
120+
}
121+
122+
// @param sequenceNumber The sequence number of the request.
123+
// @param provider The address of the provider that generated the random number. If your app uses multiple providers, you can use this argument to distinguish which one is calling the app back.
124+
// @param randomNumber The generated random number.
125+
// This method is called by the entropy contract when a random number is generated.
126+
// This method **must** be implemented on the same contract that requested the random number.
127+
// This method should **never** return an error -- if it returns an error, then the keeper will not be able to invoke the callback.
128+
// If you are having problems receiving the callback, the most likely cause is that the callback is erroring.
129+
// See the callback debugging guide here to identify the error https://docs.pyth.network/entropy/debug-callback-failures
130+
function entropyCallback(
131+
uint64 sequenceNumber,
132+
address provider,
133+
bytes32 randomNumber
134+
) internal override {
135+
// Implement your callback logic here.
136+
}
137+
138+
// This method is required by the IEntropyConsumer interface.
139+
// It returns the address of the entropy contract which will call the callback.
140+
function getEntropy() internal view override returns (address) {
141+
return address(entropy);
142+
}
143+
}
144+
145+
```
146+
147+
</Step>
148+
</Steps>
149+
150+
When the final random number is ready to use, the entropyCallback function will be called by the Entropy contract. This will happen in a separate transaction submitted by the requested provider.
151+
152+
<Callout type="info">
153+
The `entropyCallback` function on your contract should **never** return an
154+
error. If it returns an error, the keeper will not be able to invoke the
155+
callback. If you are having problems receiving the callback, please see
156+
[Debugging Callback Failures](/entropy/debug-callback-failures).
157+
</Callout>
158+
159+
## Additional Resources
160+
161+
You may find these additional resources helpful while integrating Pyth Entropy into your EVM contract.
162+
163+
### Debug Callback Failures
164+
165+
Check how to [Debug Callback Failures](../debug-callback-failures) if you are having trouble getting the callback to run.
166+
167+
### Pyth Entropy Contract Addresses
168+
169+
Consult the [Entropy contract addresses](../contract-addresses) to find the Entropy contract address on your chain.
170+
171+
### Current Fees
172+
173+
Check the [Current Fees](../current-fees) to find the current fee for each provider on your chain.
174+
175+
### Best Practices
176+
177+
Check out the [Best Practices](../best-practices) guide for tips to limit gas usage, or generate multiple random numbers in a single transaction.
178+
179+
### Randomness providers
180+
181+
Some methods on Entropy require selecting a **randomness provider**. The randomness provider is a third-party
182+
who participates in the generation process. Each provider is identified by an address and hosts
183+
a keeper service for fullfilling requests.
184+
185+
You can get the default provider's address by calling the [`getDefaultProvider`](https://github.com/pyth-network/pyth-crosschain/blob/f8ebeb6af31d98f94ce73edade6da2ebab7b2456/target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol#L94) method:
186+
187+
```solidity copy
188+
address provider = entropy.getDefaultProvider();
189+
```

apps/developer-hub/content/docs/entropy/generate-random-numbers.mdx

Lines changed: 0 additions & 10 deletions
This file was deleted.

apps/developer-hub/content/docs/entropy/how-to-guides/index.mdx

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
---
22
title: Entropy
3-
description: Random number Generator for Ethereum smart contracts
3+
description: Secure, Verifiable Random Number Generator for EVM-based smart contracts
44
icon: DiceSix
55
full: true
66
---
77

8-
# Entropy
8+
**Pyth Entropy** is an on-chain random number generator (RNG) designed for developers who need fair, unbiased, and cryptographically secure randomness.
9+
Whether you're building a blockchain game, NFT mint, lottery, or simulation, Entropy delivers randomness that is:
910

10-
Pyth Entropy allows developers to quickly and easily generate secure random numbers on the blockchain.
11-
Entropy's rapid response time allows developers to build applications such as NFT mints and games with responsive UX.
12-
Entropy also provides [strong security guarantees](protocol-design) to ensure that both users and application developers can trust that the results are random.
11+
- **Trustless & verifiable** - built on commit-reveal(TODO: link to commit-reveal).
12+
- **Low-latency** - randomness available within a few blocks(TODO: link to latency).
13+
- **Easy to integrate** - Permissionless Integration, Visual Tx Explorer(TODO: link to explorer).
14+
- **Cost-efficient** - designed for scalable production use(TODO: link to fees).
15+
- **Native gas fees** - pay with chain native token.
1316

14-
Pyth Entropy is currently available on several [EVM networks](contract-addresses).
15-
If you would like a deployment on another network, please [ask in Discord](https://discord.gg/invite/PythNetwork).
17+
## What's New in Entropy v2
18+
19+
Entropy v2 introduces several improvements and new features to make random number generation more flexible and efficient.
20+
See [What's New in Entropy v2](whats-new-entropyv2) for more details.
21+
(TODO: This can be displayed in a banner above) (TODO: Add aan infographic here)
1622

1723
## Getting Started
1824

1925
Using Pyth Entropy is permissionless and developers can integrate in a few minutes.
2026
Please see [How to Generate Random Numbers Using Pyth Entropy](generate-random-numbers) to start integrating Pyth Entropy into your application.
2127

22-
## Additional Resources
23-
24-
To learn more about how the protocol works, please see [Protocol design](protocol-design).
25-
2628
## Reference Material
2729

2830
- [Protocol design](protocol-design)
29-
- [Contract Addresses](contract-addresses)
31+
- [Contract Addresses/Supported Networks](contract-addresses)
3032
- [Error Codes](error-codes)
3133
- [Entropy Debugger](https://entropy-debugger.pyth.network/) - Interactive tool for diagnosing callback issues

apps/developer-hub/content/docs/entropy/meta.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
"description": "Random number Generator for Ethereum smart contracts",
55
"icon": "DiceSix",
66
"pages": [
7+
"---Introduction---",
78
"index",
8-
"generate-random-numbers",
9-
"create-your-first-entropy-app",
10-
"protocol-design",
9+
"whats-new-entropyv2",
10+
"---How-To Guides---",
11+
"generate-random-numbers-evm",
12+
"set-custom-gas-limits",
13+
"debug-callback-failures",
14+
"---Reference Material---",
1115
"contract-addresses",
1216
"best-practices",
1317
"fees",
18+
"protocol-design",
1419
"current-fees",
1520
"error-codes",
16-
"debug-callback-failures",
1721
"examples",
1822
"request-callback-variants",
19-
"set-custom-gas-limits",
20-
"whats-new-entropyv2",
21-
"---Guides---",
22-
"how-to-guides"
23+
"create-your-first-entropy-app"
2324
]
2425
}

0 commit comments

Comments
 (0)