Skip to content

Commit df85d46

Browse files
devin-ai-integration[bot]Jayant
andcommitted
feat(entropy): add Set Custom Gas Limits guide
- Add comprehensive guide for using custom gas limits with Pyth Entropy - Show requestV2(uint32 gasLimit) and getFeeV2(uint32 gasLimit) usage - Include gas limit constraints, best practices, and troubleshooting - Build upon existing EVM guide as requested - Update navigation to include new guide Co-Authored-By: Jayant <[email protected]>
1 parent f652234 commit df85d46

File tree

2 files changed

+200
-1
lines changed

2 files changed

+200
-1
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"evm": "in EVM contracts"
2+
"evm": "in EVM contracts",
3+
"set-custom-gas-limits": "Set Custom Gas Limits"
34
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import { Tab, Tabs, Callout } from "nextra/components";
2+
3+
# Set Custom Gas Limits
4+
5+
This guide shows how to set custom gas limits when requesting random numbers from Pyth Entropy. Custom gas limits are useful when your callback function requires more gas than the default provider limit, or when you want to optimize gas costs for simpler callbacks.
6+
7+
## Prerequisites
8+
9+
Before following this guide, you should first complete the basic setup from the [Generate Random Numbers in EVM Contracts](/entropy/generate-random-numbers/evm) guide. This guide builds upon that foundation and assumes you have:
10+
11+
- Installed the Pyth Entropy Solidity SDK
12+
- Set up your contract with the `IEntropyConsumer` interface
13+
- Implemented the basic `entropyCallback` function
14+
15+
## When to Use Custom Gas Limits
16+
17+
You might need custom gas limits in these scenarios:
18+
19+
- **Complex callback logic**: Your `entropyCallback` function performs computationally expensive operations
20+
- **Gas optimization**: You want to use less gas for simple callbacks to reduce fees
21+
- **Multiple operations**: Your callback needs to perform multiple state changes or external calls
22+
- **Integration requirements**: Your application has specific gas requirements for reliability
23+
24+
## Implementation
25+
26+
### 1. Use requestV2 with Gas Limit Parameter
27+
28+
Instead of the basic `requestV2()` method, use the variant that accepts a `gasLimit` parameter:
29+
30+
```solidity copy
31+
function requestRandomNumberWithCustomGas(uint32 customGasLimit) external payable {
32+
// Calculate the fee for the custom gas limit
33+
uint256 fee = entropy.getFeeV2(customGasLimit);
34+
35+
// Request random number with custom gas limit
36+
uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
37+
38+
// Store the sequence number for tracking if needed
39+
}
40+
```
41+
42+
### 2. Calculate Fees with Custom Gas Limit
43+
44+
When using custom gas limits, you must use the `getFeeV2` variant that accepts a `gasLimit` parameter:
45+
46+
```solidity copy
47+
// Get fee for custom gas limit
48+
uint256 fee = entropy.getFeeV2(customGasLimit);
49+
50+
// NOT: uint256 fee = entropy.getFeeV2(); // This uses default gas limit
51+
```
52+
53+
### 3. Complete Example
54+
55+
Here's a complete example showing how to implement custom gas limits:
56+
57+
```solidity copy
58+
pragma solidity ^0.8.0;
59+
60+
import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
61+
import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
62+
63+
contract CustomGasLimitExample is IEntropyConsumer {
64+
IEntropyV2 public entropy;
65+
mapping(uint64 => bool) public processedRequests;
66+
67+
constructor(address entropyAddress) {
68+
entropy = IEntropyV2(entropyAddress);
69+
}
70+
71+
// Request with custom gas limit for complex callback
72+
function requestComplexRandomNumber() external payable {
73+
uint32 customGasLimit = 200000; // Higher limit for complex operations
74+
uint256 fee = entropy.getFeeV2(customGasLimit);
75+
76+
require(msg.value >= fee, "Insufficient fee");
77+
78+
uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
79+
// Store sequence number if needed for tracking
80+
}
81+
82+
// Request with lower gas limit for simple callback
83+
function requestSimpleRandomNumber() external payable {
84+
uint32 customGasLimit = 50000; // Lower limit for simple operations
85+
uint256 fee = entropy.getFeeV2(customGasLimit);
86+
87+
require(msg.value >= fee, "Insufficient fee");
88+
89+
uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
90+
}
91+
92+
// Complex callback that requires more gas
93+
function entropyCallback(
94+
uint64 sequenceNumber,
95+
address provider,
96+
bytes32 randomNumber
97+
) internal override {
98+
// Prevent duplicate processing
99+
require(!processedRequests[sequenceNumber], "Already processed");
100+
processedRequests[sequenceNumber] = true;
101+
102+
// Complex operations that require more gas
103+
for (uint i = 0; i < 10; i++) {
104+
// Simulate complex state changes
105+
// This would require more gas than the default limit
106+
}
107+
108+
// Use the random number for your application logic
109+
uint256 randomValue = uint256(randomNumber);
110+
// Your application logic here...
111+
}
112+
113+
function getEntropy() internal view override returns (address) {
114+
return address(entropy);
115+
}
116+
}
117+
```
118+
119+
## Gas Limit Constraints
120+
121+
When setting custom gas limits, be aware of these constraints:
122+
123+
<Callout type="info" emoji="ℹ️">
124+
**Gas Limit Rules:**
125+
- Gas limits are automatically rounded up to the nearest multiple of 10,000
126+
- Example: 19,000 becomes 20,000, 25,500 becomes 30,000
127+
- The minimum gas limit is the provider's configured default limit
128+
- The maximum gas limit is 655,350,000 (uint16.max × 10,000)
129+
</Callout>
130+
131+
### Recommended Gas Limits
132+
133+
- **Simple callbacks**: 50,000 - 100,000 gas
134+
- **Moderate complexity**: 100,000 - 200,000 gas
135+
- **Complex operations**: 200,000 - 500,000 gas
136+
- **Very complex logic**: 500,000+ gas (use with caution)
137+
138+
## Best Practices
139+
140+
### 1. Estimate Gas Usage
141+
142+
Test your callback function to determine the actual gas usage:
143+
144+
```solidity copy
145+
// In your tests, measure gas usage
146+
uint256 gasStart = gasleft();
147+
// Your callback logic here
148+
uint256 gasUsed = gasStart - gasleft();
149+
console.log("Gas used:", gasUsed);
150+
```
151+
152+
### 2. Add Safety Buffer
153+
154+
Always add a safety buffer to your estimated gas usage:
155+
156+
```solidity copy
157+
uint32 estimatedGas = 150000;
158+
uint32 safetyBuffer = 20000;
159+
uint32 customGasLimit = estimatedGas + safetyBuffer;
160+
```
161+
162+
### 3. Handle Gas Limit Errors
163+
164+
Be prepared to handle cases where your gas limit is insufficient:
165+
166+
<Callout type="warning" emoji="⚠️">
167+
If your callback runs out of gas, the entropy provider will not be able to
168+
complete the callback. Always test your gas limits thoroughly and include
169+
adequate safety margins.
170+
</Callout>
171+
172+
### 4. Consider Fee Implications
173+
174+
Higher gas limits result in higher fees. Balance your gas needs with cost considerations:
175+
176+
```solidity copy
177+
// Compare fees for different gas limits
178+
uint256 defaultFee = entropy.getFeeV2();
179+
uint256 customFee = entropy.getFeeV2(customGasLimit);
180+
uint256 additionalCost = customFee - defaultFee;
181+
```
182+
183+
## Troubleshooting
184+
185+
If you're experiencing issues with custom gas limits:
186+
187+
1. **Callback not executing**: Your gas limit might be too low
188+
2. **High fees**: Consider optimizing your callback or using a lower gas limit
189+
3. **Reverts**: Check that your gas limit doesn't exceed the maximum allowed
190+
191+
For more debugging help, see the [Debug Callback Failures](/entropy/debug-callback-failures) guide.
192+
193+
## Additional Resources
194+
195+
- [Generate Random Numbers in EVM Contracts](/entropy/generate-random-numbers/evm) - Basic setup guide
196+
- [Best Practices](/entropy/best-practices) - General optimization tips
197+
- [Current Fees](/entropy/current-fees) - Fee information for different chains
198+
- [Contract Addresses](/entropy/contract-addresses) - Entropy contract deployments

0 commit comments

Comments
 (0)