Skip to content

Commit 57002c5

Browse files
authored
Merge pull request #3023 from pyth-network/chore(dev-docs)-entropy-edits
chore(dev-docs) Entropy Guide Edits - Part 1
2 parents 27c35f9 + 2eb1ee8 commit 57002c5

19 files changed

+671
-155
lines changed
Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
---
22
title: Contract Addresses
3-
description: Entropy contract addresses on different networks
3+
description: Pyth Entropy contract addresses on EVM networks
44
---
55

6-
# Entropy Contract Addresses on EVM
6+
import { EntropyTable } from "../../../src/components/EntropyTable";
77

88
## Mainnets
99

1010
The Entropy contract is deployed on the following mainnet chains:
1111

12-
| Network | Contract Address | Gas Limit | Provider |
13-
| --------- | -------------------------------------------- | --------- | -------------------------------------------- |
14-
| Ethereum | `0x98046Bd286715D3B0BC227Dd7a956b83D8978603` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
15-
| Arbitrum | `0x23f0e8FAeE7bbb405E7A7C3d60138FCfd43d7509` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
16-
| Avalanche | `0x36825bf3Fbdf5a29E2d5148bfe7Dcf7B5639e320` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
17-
| Base | `0x98046Bd286715D3B0BC227Dd7a956b83D8978603` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
18-
| BNB Chain | `0x98046Bd286715D3B0BC227Dd7a956b83D8978603` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
19-
| Optimism | `0x98046Bd286715D3B0BC227Dd7a956b83D8978603` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
20-
| Polygon | `0x98046Bd286715D3B0BC227Dd7a956b83D8978603` | 100,000 | `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506` |
12+
<EntropyTable isMainnet={true} />
2113

2214
**The default provider for above mainnet chains is `0x52DeaA1c84233F7bb8C8A45baeDE41091c616506`.**
2315

@@ -30,18 +22,8 @@ The default provider fulfills the request by sending a transaction with a gas li
3022

3123
The Entropy contract is deployed on the following testnet chains:
3224

33-
| Network | Contract Address | Gas Limit | Provider |
34-
| ---------------- | -------------------------------------------- | --------- | -------------------------------------------- |
35-
| Ethereum Sepolia | `0x41c9e39574F40Ad34c79f1C99B66A45eFB830d4c` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
36-
| Arbitrum Sepolia | `0x36825bf3Fbdf5a29E2d5148bfe7Dcf7B5639e320` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
37-
| Avalanche Fuji | `0x36825bf3Fbdf5a29E2d5148bfe7Dcf7B5639e320` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
38-
| Base Sepolia | `0x41c9e39574F40Ad34c79f1C99B66A45eFB830d4c` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
39-
| BNB Testnet | `0x41c9e39574F40Ad34c79f1C99B66A45eFB830d4c` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
40-
| Optimism Sepolia | `0x41c9e39574F40Ad34c79f1C99B66A45eFB830d4c` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
41-
| Polygon Amoy | `0x41c9e39574F40Ad34c79f1C99B66A45eFB830d4c` | 100,000 | `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344` |
25+
<EntropyTable isMainnet={false} />
4226

4327
**The default provider for above testnet chains is `0x6CC14824Ea2918f5De5C2f75A9Da968ad4BD6344`.**
4428

4529
The default provider on testnet has reveal delays identical to the corresponding mainnet chains to ensure consistent environment.
46-
47-
The default provider fulfills the request by sending a transaction with a gas limit as mentioned in above table. Entropy callbacks the consumer as part of this transaction.

apps/developer-hub/content/docs/entropy/debug-callback-failures.mdx

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ title: Debug Callback Failures
33
description: How to identify and resolve issues with Entropy callbacks
44
---
55

6-
# Debug Callback Failures
7-
8-
> 🔍 **Quick Debug Tool**
9-
> Use the [Entropy Explorer](https://entropy-debugger.pyth.network/) to quickly diagnose and resolve callback issues.
6+
import { DynamicCodeBlock } from "fumadocs-ui/components/dynamic-codeblock";
107

118
This guide explains how to identify and resolve issues with the Entropy callback.
129
The intended audience for this guide is developers who have made an Entropy random number request, but their application hasn't received a callback.
1310

11+
> 🔍 **Quick Debug Tool**
12+
>
13+
> Use the [Entropy Explorer](https://entropy-debugger.pyth.network/) to quickly diagnose and resolve callback issues.
14+
1415
## Dependencies
1516

1617
This guide uses [Foundry](https://book.getfoundry.sh/getting-started/installation) to submit transactions to the blockchain.
@@ -22,45 +23,56 @@ Developers can run the Entropy callback themselves to see the reason for the fai
2223
To run the callback, invoke the `revealWithCallback` function on the Entropy contract on your blockchain.
2324
The function has the following signature:
2425

25-
```solidity
26-
function revealWithCallback(
26+
<DynamicCodeBlock
27+
lang="solidity"
28+
code={`function revealWithCallback(
2729
address provider,
2830
uint64 sequenceNumber,
29-
bytes32 userRandomNumber,
30-
bytes32 providerRevelation
31+
bytes32 userContribution,
32+
bytes32 providerContribution
3133
)
32-
```
34+
`}
35+
/>
3336

34-
This call requires the chain ID, contract address, and four arguments.
37+
This call requires the chain ID, contract address, and four other arguments.
3538
The chain ID and contract address can be retrieved from [Contract Addresses](contract-addresses).
3639
Export these values as environment variables for later use:
3740

38-
```bash
39-
export CHAIN_ID=blast
41+
<DynamicCodeBlock
42+
lang="bash"
43+
code={`export CHAIN_ID=blast
4044
export ENTROPY_ADDRESS=0x5744Cbf430D99456a0A8771208b674F27f8EF0Fb
41-
```
45+
`}
46+
/>
4247

43-
Three of the arguments can be retrieved from the request transaction's event logs.
48+
Three of the other arguments can be retrieved from the request transaction's event logs.
4449
Look at the event logs of the request transaction in a block explorer.
4550
You should see a `RequestedWithCallback` event emitted from the Entropy contract.
4651

4752
Copy the following values from the event into environment variables:
4853

49-
```bash
50-
export PROVIDER=0x52DeaA1c84233F7bb8C8A45baeDE41091c616506
54+
<DynamicCodeBlock
55+
lang="bash"
56+
code={`export PROVIDER=0x52DeaA1c84233F7bb8C8A45baeDE41091c616506
5157
export SEQUENCE_NUMBER=12345
5258
export USER_RANDOM_NUMBER=0x1234...
53-
```
59+
`}
60+
/>
5461

55-
The fourth argument (provider revelation) must be retrieved from the provider's API.
62+
The fourth argument (provider contribution) must be retrieved from the provider's API.
5663
This value becomes available after the reveal delay has passed.
5764

5865
## Common Issues
5966

67+
There are a few common issues that can cause the callback to fail.
68+
6069
### Gas Limit Exceeded
6170

6271
If your callback function uses too much gas, the transaction will fail. Check the gas limit for your chain on the [contract addresses](contract-addresses) page and ensure your callback function uses less gas.
6372

73+
> 💡 **Tip**
74+
> Refer to the [Set Custom Gas Limits](set-custom-gas-limits) guide to set a custom gas limit for your callback function.
75+
6476
### Callback Function Errors
6577

6678
Your callback function might contain logic that throws an error. Review your callback implementation for:
@@ -73,3 +85,4 @@ Your callback function might contain logic that throws an error. Review your cal
7385
### Transaction Timing
7486

7587
Make sure you're attempting the callback after the reveal delay has passed. The reveal delay varies by network and helps prevent MEV attacks.
88+
Refer to the [Contract Addresses](contract-addresses) page for the reveal delay for each network.
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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+
import { DynamicCodeBlock } from "fumadocs-ui/components/dynamic-codeblock";
8+
9+
This guide explains how to integrate Pyth Entropy into EVM Contracts to generate on-chain random numbers.
10+
The intended audience for this guide is developers of any application that needs on-chain randomness, such as NFT mints or games.
11+
12+
## Install the SDK
13+
14+
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.
15+
Install the SDK using your package manager:
16+
17+
<Tabs items={['hardhat', 'foundry']}>
18+
<Tab value="hardhat">
19+
<DynamicCodeBlock lang="shell"
20+
code={`npm install @pythnetwork/entropy-sdk-solidity`} />
21+
</Tab>
22+
<Tab value="foundry">
23+
<DynamicCodeBlock lang="shell" code={`npm init -y
24+
npm install @pythnetwork/entropy-sdk-solidity
25+
`} />
26+
27+
Then add the following line to your `remappings.txt` file:
28+
29+
<DynamicCodeBlock
30+
lang="text"
31+
code={`@pythnetwork/entropy-sdk-solidity/=node_modules/@pythnetwork/entropy-sdk-solidity
32+
`}
33+
/>
34+
35+
</Tab>
36+
</Tabs>
37+
38+
## Setup
39+
40+
The Solidity SDK exports two interfaces:
41+
42+
- [`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.
43+
- [`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.
44+
You will need the address of an Entropy contract on your blockchain.
45+
Consult the current [Entropy contract addresses](../contract-addresses) to find the address on your chain.
46+
Once you have a contract address, instantiate an `console.log("IEntropyV2"){:bash}` contract in your solidity contract:
47+
48+
<DynamicCodeBlock lang="solidity"
49+
code={`import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
50+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
51+
52+
// @param entropyAddress The address of the entropy contract.
53+
contract YourContract is IEntropyConsumer {
54+
IEntropyV2 public entropy;
55+
56+
constructor(address entropyAddress) {
57+
entropy = IEntropyV2(entropyAddress);
58+
}
59+
}
60+
`} />
61+
62+
## Usage
63+
64+
To generate a random number, follow these steps.
65+
66+
<Steps>
67+
<Step>
68+
### 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+
<DynamicCodeBlock lang="solidity"
80+
code={`function requestRandomNumber() external payable {
81+
uint256 fee = entropy.getFeeV2();
82+
83+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
84+
}
85+
`} />
86+
87+
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.
88+
89+
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.
90+
91+
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).
92+
93+
</Step>
94+
<Step>
95+
96+
### Implement the Entropy callback
97+
98+
<DynamicCodeBlock lang="solidity"
99+
code={`pragma solidity ^0.8.0;
100+
101+
import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
102+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
103+
104+
contract YourContract is IEntropyConsumer {
105+
IEntropyV2 entropy;
106+
107+
// @param entropyAddress The address of the entropy contract.
108+
constructor(address entropyAddress) {
109+
entropy = IEntropyV2(entropyAddress);
110+
}
111+
112+
function requestRandomNumber() external payable {
113+
// Get the fee for the request
114+
uint256 fee = entropy.getFeeV2();
115+
116+
// Request the random number with the callback
117+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
118+
// Store the sequence number to identify the callback request
119+
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+
</Step>
147+
</Steps>
148+
149+
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.
150+
151+
<Callout variant="warning">
152+
The `entropyCallback` function on your contract should **never** return an
153+
error. If it returns an error, the keeper will not be able to invoke the
154+
callback. If you are having problems receiving the callback, please see
155+
[Debugging Callback Failures](/entropy/debug-callback-failures).
156+
</Callout>
157+
158+
## Additional Resources
159+
160+
You may find these additional resources helpful while integrating Pyth Entropy into your EVM contract.
161+
162+
### Debug Callback Failures
163+
164+
Check how to [Debug Callback Failures](../debug-callback-failures) if you are having trouble getting the callback to run.
165+
166+
### Pyth Entropy Contract Addresses
167+
168+
Consult the [Entropy contract addresses](../contract-addresses) to find the Entropy contract address on your chain.
169+
170+
### Current Fees
171+
172+
Check the [Current Fees](../current-fees) to find the current fee for each provider on your chain.
173+
174+
### Best Practices
175+
176+
Check out the [Best Practices](../best-practices) guide for tips to limit gas usage, or generate multiple random numbers in a single transaction.
177+
178+
### Randomness providers
179+
180+
Some methods on Entropy require selecting a **randomness provider**. The randomness provider is a third-party
181+
who participates in the generation process. Each provider is identified by an address and hosts
182+
a keeper service for fullfilling requests.
183+
184+
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:
185+
186+
<DynamicCodeBlock
187+
lang="solidity"
188+
code={`address provider = entropy.getDefaultProvider();
189+
`}
190+
/>

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

0 commit comments

Comments
 (0)