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