Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 2dca11d

Browse files
feat(Entropy): add different variants of request v2 (#823)
* add different variants of request v2 * add variants of request in reference material * changed title to request callback variants
1 parent 904b202 commit 2dca11d

File tree

3 files changed

+134
-15
lines changed

3 files changed

+134
-15
lines changed

pages/entropy/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"newWindow": true
4848
},
4949
"examples": "Example Applications",
50+
"request-callback-variants": "Request Callback Variants",
5051

5152
"-- Understanding Entropy": {
5253
"title": "Understanding Entropy",

pages/entropy/generate-random-numbers/evm.mdx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,7 @@ function requestRandomNumber() external payable {
8282

8383
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.
8484

85-
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.
86-
87-
<Callout type="info" emoji="ℹ️">
88-
Users can also request a random number with a custom gas limit by passing a **custom `gasLimit`** to the `requestV2(uint32 gasLimit){:bash}` call.
89-
For example,
90-
```solidity copy
91-
function requestRandomNumber(uint32 gasLimit) external payable {
92-
uint256 fee = entropy.getFeeV2();
93-
94-
uint64 sequenceNumber = entropy.requestV2{ value: fee }(gasLimit);
95-
}
96-
97-
````
98-
</Callout>
85+
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.
9986

10087
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).
10188

@@ -147,7 +134,7 @@ contract YourContract is IEntropyConsumer {
147134
}
148135
}
149136
150-
````
137+
```
151138

152139
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.
153140

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Request Callback Variants
2+
3+
The [`IEntropyV2`](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/entropy_sdk/solidity/IEntropyV2.sol) interface provides multiple variants of the `requestV2` function:
4+
5+
## 1. Basic Request
6+
7+
```solidity copy
8+
function requestV2() external payable returns (uint64 assignedSequenceNumber);
9+
10+
```
11+
12+
This is the simplest variant that requests entropy using the default randomness provider and default gas settings. Uses in-contract PRNG for the user contribution to randomness.
13+
14+
Use this when you want the most straightforward implementation and don't need to customize provider or gas parameters.
15+
16+
### Example
17+
18+
```solidity copy
19+
function requestBasicRandomNumber() external payable {
20+
// Get the fee for the default provider and gas limit
21+
uint256 fee = entropy.getFeeV2();
22+
23+
require(msg.value >= fee, "Insufficient fee");
24+
25+
// Request randomness with default settings
26+
uint64 sequenceNumber = entropy.requestV2{ value: fee }();
27+
}
28+
29+
```
30+
31+
## 2. Request with Gas Limit
32+
33+
```solidity copy
34+
function requestV2(
35+
uint32 gasLimit
36+
) external payable returns (uint64 assignedSequenceNumber);
37+
38+
```
39+
40+
This variant allows you to specify a custom gas limit for the callback function execution. Uses in-contract PRNG for the user contribution.
41+
42+
Use this when your `entropyCallback` function has complex logic that requires more gas than the default amount, or when you want to optimize gas usage by setting a lower limit for simple callbacks.
43+
44+
### Example
45+
46+
```solidity copy
47+
function requestWithCustomGas() external payable {
48+
uint32 customGasLimit = 150000; // Custom gas limit for callback
49+
50+
// Get fee for the custom gas limit
51+
uint256 fee = entropy.getFeeV2(customGasLimit);
52+
53+
require(msg.value >= fee, "Insufficient fee");
54+
55+
// Request with custom gas limit
56+
uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
57+
}
58+
59+
```
60+
61+
## 3. Request with Provider and Gas Limit
62+
63+
```solidity copy
64+
function requestV2(
65+
address provider,
66+
uint32 gasLimit
67+
) external payable returns (uint64 assignedSequenceNumber);
68+
69+
```
70+
71+
This variant allows you to specify both a custom randomness provider and a custom gas limit for the callback. Uses in-contract PRNG for the user contribution.
72+
73+
Use this when you need full control over both the randomness source and the gas allocation for your callback execution.
74+
75+
### Example
76+
77+
```solidity copy
78+
function requestWithProviderAndGas() external payable {
79+
address preferredProvider = 0x1234567890123456789012345678901234567890; // Your chosen provider
80+
uint32 customGasLimit = 100000;
81+
82+
// Get fee for the specific provider and gas limit
83+
uint256 fee = entropy.getFeeV2(preferredProvider, customGasLimit);
84+
85+
require(msg.value >= fee, "Insufficient fee");
86+
87+
// Request with specific provider and gas limit
88+
uint64 sequenceNumber = entropy.requestV2{ value: fee }(
89+
preferredProvider,
90+
customGasLimit
91+
);
92+
}
93+
94+
```
95+
96+
## 4. Request with Provider, User Randomness and Gas Limit
97+
98+
```solidity copy
99+
function requestV2(
100+
address provider,
101+
bytes32 userRandomNumber,
102+
uint32 gasLimit
103+
) external payable returns (uint64 assignedSequenceNumber);
104+
105+
```
106+
107+
This is the most complete variant that allows you to specify a custom randomness provider, a custom gas limit for the callback, and provide your own user contribution to the randomness. The `userRandomNumber` parameter allows you to contribute your own randomness to the entropy generation process instead of relying on the in-contract PRNG.
108+
109+
Use this when you need maximum control over all aspects of the randomness generation, including providing your own source of entropy for the user contribution.
110+
111+
### Example
112+
113+
```solidity copy
114+
function requestWithFullControl(bytes32 userContribution) external payable {
115+
address preferredProvider = 0x1234567890123456789012345678901234567890;
116+
uint32 customGasLimit = 120000;
117+
118+
// Get fee for the specific provider and gas limit
119+
uint256 fee = entropy.getFeeV2(preferredProvider, customGasLimit);
120+
121+
require(msg.value >= fee, "Insufficient fee");
122+
123+
// Request with full customization
124+
uint64 sequenceNumber = entropy.requestV2{ value: fee }(
125+
preferredProvider,
126+
userContribution, // Your own randomness contribution
127+
customGasLimit
128+
);
129+
}
130+
131+
```

0 commit comments

Comments
 (0)