|
|
@@ -3,84 +3,210 @@ title: Set Custom Gas Limits
|
|
|
description: How to set custom gas limits for Entropy callbacks
|
|
|
---
|
|
|
|
|
|
-# Set Custom Gas Limits
|
|
|
+import { Step, Steps } from "fumadocs-ui/components/steps";
|
|
|
+import { DynamicCodeBlock } from "fumadocs-ui/components/dynamic-codeblock";
|
|
|
|
|
|
-By default, Pyth Entropy uses a predefined gas limit for callback functions. However, you may need to adjust this limit based on the complexity of your callback implementation.
|
|
|
+Custom gas limits are useful when your callback function requires more gas than the [default provider limit]TODO(../contract-addresses), or when you want to optimize gas costs for simpler callbacks.
|
|
|
|
|
|
-## When to Use Custom Gas Limits
|
|
|
+## Prerequisites
|
|
|
+
|
|
|
+Before following this guide, you should first complete the basic setup from the [Generate Random Numbers in EVM Contracts](./generate-random-numbers-evm.mdx) guide. This guide builds upon that foundation and assumes you have:
|
|
|
|
|
|
-### Use Higher Gas Limits When:
|
|
|
+- Installed the Pyth Entropy Solidity SDK
|
|
|
+- Set up your contract with the `IEntropyConsumer` interface
|
|
|
+- Implemented the basic `entropyCallback` function
|
|
|
|
|
|
-- Your callback function performs complex calculations
|
|
|
-- You need to update multiple storage variables
|
|
|
-- Your callback interacts with other contracts
|
|
|
-- You're implementing complex game logic
|
|
|
+## When to Use Custom Gas Limits
|
|
|
|
|
|
-### Use Lower Gas Limits When:
|
|
|
+You might need custom gas limits in these scenarios:
|
|
|
|
|
|
-- Your callback function is simple (e.g., just stores a single value)
|
|
|
-- You want to optimize for cost
|
|
|
-- You want to prevent potential gas griefing
|
|
|
+- **Complex callback logic**: Your `entropyCallback` function performs computationally expensive operations
|
|
|
+- **Gas optimization**: You want to use less gas for simple callbacks to reduce fees
|
|
|
+- **Multiple operations**: Your callback needs to perform multiple state changes or external calls
|
|
|
+- **Integration requirements**: Your application has specific gas requirements for reliability
|
|
|
|
|
|
## Implementation
|
|
|
|
|
|
-### Using EntropyV2 Interface
|
|
|
+### 1. Use requestV2 with Gas Limit Parameter
|
|
|
|
|
|
-```solidity
|
|
|
-import "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
|
|
|
+Instead of the basic `requestV2()` method, use the variant that accepts a `gasLimit` parameter:
|
|
|
|
|
|
-contract MyContract is IEntropyConsumer {
|
|
|
- IEntropyV2 entropy;
|
|
|
+<DynamicCodeBlock lang="solidity"
|
|
|
+code={`function requestRandomNumberWithCustomGas(
|
|
|
+ uint32 customGasLimit
|
|
|
+) external payable {
|
|
|
+ // Calculate the fee for the custom gas limit
|
|
|
+ uint256 fee = entropy.getFeeV2(customGasLimit);
|
|
|
|
|
|
- function requestWithCustomGas() external payable {
|
|
|
- uint32 customGasLimit = 200000; // Adjust based on your needs
|
|
|
+// Request random number with custom gas limit
|
|
|
+uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
|
|
|
|
|
|
- // Get fee for custom gas limit
|
|
|
- uint256 fee = entropy.getFeeV2(customGasLimit);
|
|
|
- require(msg.value >= fee, "Insufficient fee");
|
|
|
+// Store the sequence number for tracking if needed
|
|
|
+}
|
|
|
+`} />
|
|
|
|
|
|
- // Request with custom gas limit
|
|
|
- uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
|
|
|
- }
|
|
|
+### 2. Calculate Fees with Custom Gas Limit
|
|
|
+
|
|
|
+When using custom gas limits, you must use the `getFeeV2` variant that accepts a `gasLimit` parameter:
|
|
|
+
|
|
|
+<DynamicCodeBlock lang="solidity"
|
|
|
+code={`// Get fee for custom gas limit
|
|
|
+uint256 fee = entropy.getFeeV2(customGasLimit);
|
|
|
+
|
|
|
+// NOT: uint256 fee = entropy.getFeeV2(); // This uses default gas limit
|
|
|
+`} />
|
|
|
+
|
|
|
+### 3. Complete Example
|
|
|
+
|
|
|
+Here's a complete example showing how to implement custom gas limits:
|
|
|
+
|
|
|
+<DynamicCodeBlock lang="solidity"
|
|
|
+code={`pragma solidity ^0.8.0;
|
|
|
+
|
|
|
+import { IEntropyConsumer } from "@pythnetwork/entropy-sdk-solidity/IEntropyConsumer.sol";
|
|
|
+import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
|
|
|
+
|
|
|
+contract CustomGasLimitExample is IEntropyConsumer {
|
|
|
+IEntropyV2 public entropy;
|
|
|
+mapping(uint64 => bool) public processedRequests;
|
|
|
+
|
|
|
+constructor(address entropyAddress) {
|
|
|
+entropy = IEntropyV2(entropyAddress);
|
|
|
}
|
|
|
-```
|
|
|
|
|
|
-### Full Control Request
|
|
|
+// Request with custom gas limit for complex callback
|
|
|
+function requestComplexRandomNumber() external payable {
|
|
|
+uint32 customGasLimit = 200000; // Higher limit for complex operations
|
|
|
+uint256 fee = entropy.getFeeV2(customGasLimit);
|
|
|
+
|
|
|
+ require(msg.value >= fee, "Insufficient fee");
|
|
|
+
|
|
|
+ uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
|
|
|
+ // Store sequence number if needed for tracking
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
-```solidity
|
|
|
-function requestWithFullControl() external payable {
|
|
|
- address provider = entropy.getDefaultProvider();
|
|
|
- uint32 gasLimit = 150000;
|
|
|
- bytes32 userRandomNumber = keccak256(abi.encode(block.timestamp, msg.sender));
|
|
|
+// Request with lower gas limit for simple callback
|
|
|
+function requestSimpleRandomNumber() external payable {
|
|
|
+uint32 customGasLimit = 50000; // Lower limit for simple operations
|
|
|
+uint256 fee = entropy.getFeeV2(customGasLimit);
|
|
|
|
|
|
- uint256 fee = entropy.getFeeV2(provider, gasLimit);
|
|
|
require(msg.value >= fee, "Insufficient fee");
|
|
|
|
|
|
- uint64 sequenceNumber = entropy.requestV2{value: fee}(
|
|
|
- provider,
|
|
|
- gasLimit,
|
|
|
- userRandomNumber
|
|
|
- );
|
|
|
+ uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
|
|
|
+
|
|
|
}
|
|
|
-```
|
|
|
+
|
|
|
+// Complex callback that requires more gas
|
|
|
+function entropyCallback(
|
|
|
+uint64 sequenceNumber,
|
|
|
+address provider,
|
|
|
+bytes32 randomNumber
|
|
|
+) internal override {
|
|
|
+// Prevent duplicate processing
|
|
|
+require(!processedRequests[sequenceNumber], "Already processed");
|
|
|
+processedRequests[sequenceNumber] = true;
|
|
|
+
|
|
|
+ // Complex operations that require more gas
|
|
|
+ for (uint i = 0; i < 10; i++) {
|
|
|
+ // Simulate complex state changes
|
|
|
+ // This would require more gas than the default limit
|
|
|
+ }
|
|
|
+
|
|
|
+ // Use the random number for your application logic
|
|
|
+ uint256 randomValue = uint256(randomNumber);
|
|
|
+ // Your application logic here...
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+function getEntropy() internal view override returns (address) {
|
|
|
+return address(entropy);
|
|
|
+}
|
|
|
+}
|
|
|
+`} />
|
|
|
|
|
|
## Gas Limit Constraints
|
|
|
|
|
|
-Each network has different gas limit constraints:
|
|
|
+When setting custom gas limits, be aware of these constraints:
|
|
|
+
|
|
|
+<Callout variant="info" header="Gas Limit Rules">
|
|
|
+ Gas limits are automatically rounded up to the nearest multiple of **10,000**.
|
|
|
+ Example: 19,000 becomes 20,000 25,500 becomes 30,000. The minimum gas limit is
|
|
|
+ the provider's configured default limit. The maximum gas limit is 655,350,000
|
|
|
+ (`uint16.max` \* 10,000).
|
|
|
+</Callout>
|
|
|
+
|
|
|
+### Recommended Gas Limits
|
|
|
|
|
|
-| Network | Min Gas Limit | Max Gas Limit | Default |
|
|
|
-| --------- | ------------- | ------------- | ------- |
|
|
|
-| Ethereum | 50,000 | 100,000 | 100,000 |
|
|
|
-| Arbitrum | 50,000 | 100,000 | 100,000 |
|
|
|
-| Avalanche | 50,000 | 100,000 | 100,000 |
|
|
|
-| Base | 50,000 | 100,000 | 100,000 |
|
|
|
-| BNB Chain | 50,000 | 100,000 | 100,000 |
|
|
|
-| Optimism | 50,000 | 100,000 | 100,000 |
|
|
|
-| Polygon | 50,000 | 100,000 | 100,000 |
|
|
|
+- **Simple callbacks**: 50,000 - 100,000 gas
|
|
|
+- **Moderate complexity**: 100,000 - 200,000 gas
|
|
|
+- **Complex operations**: 200,000 - 500,000 gas
|
|
|
+- **Very complex logic**: 500,000+ gas (use with caution)
|
|
|
|
|
|
## Best Practices
|
|
|
|
|
|
-1. **Test your callback function** to estimate gas usage before setting custom limits
|
|
|
-2. **Add a buffer** of 10-20% to your estimated gas usage for safety
|
|
|
-3. **Use the minimum necessary** gas limit to reduce costs
|
|
|
-4. **Consider network differences** - some networks may have different gas costs for similar operations
|
|
|
+### 1. Estimate Gas Usage
|
|
|
+
|
|
|
+Test your callback function to determine the actual gas usage:
|
|
|
+
|
|
|
+<DynamicCodeBlock
|
|
|
+ lang="solidity"
|
|
|
+ code={`// In your tests, measure gas usage
|
|
|
+uint256 gasStart = gasleft();
|
|
|
+// Your callback logic here
|
|
|
+uint256 gasUsed = gasStart - gasleft();
|
|
|
+console.log("Gas used:", gasUsed);
|
|
|
+`}
|
|
|
+/>
|
|
|
+
|
|
|
+### 2. Add Safety Buffer
|
|
|
+
|
|
|
+Always add a safety buffer to your estimated gas usage:
|
|
|
+
|
|
|
+<DynamicCodeBlock
|
|
|
+ lang="solidity"
|
|
|
+ code={`uint32 estimatedGas = 150000;
|
|
|
+uint32 safetyBuffer = 20000;
|
|
|
+uint32 customGasLimit = estimatedGas + safetyBuffer;
|
|
|
+`}
|
|
|
+/>
|
|
|
+
|
|
|
+### 3. Handle Gas Limit Errors
|
|
|
+
|
|
|
+Be prepared to handle cases where your gas limit is insufficient:
|
|
|
+
|
|
|
+<Callout variant="warning">
|
|
|
+ If your callback **runs out of gas**, the entropy provider will **not** be
|
|
|
+ able to complete the callback. Always test your gas limits thoroughly and
|
|
|
+ include adequate safety margins.
|
|
|
+</Callout>
|
|
|
+
|
|
|
+### 4. Consider Fee Implications
|
|
|
+
|
|
|
+Higher gas limits result in higher fees. Balance your gas needs with cost considerations:
|
|
|
+
|
|
|
+<DynamicCodeBlock
|
|
|
+ lang="solidity"
|
|
|
+ code={`// Compare fees for different gas limits
|
|
|
+uint256 defaultFee = entropy.getFeeV2();
|
|
|
+uint256 customFee = entropy.getFeeV2(customGasLimit);
|
|
|
+uint256 additionalCost = customFee - defaultFee;
|
|
|
+`}
|
|
|
+/>
|
|
|
+
|
|
|
+## Troubleshooting
|
|
|
+
|
|
|
+If you're experiencing issues with custom gas limits:
|
|
|
+
|
|
|
+1. **Callback not executing**: Your gas limit might be too low
|
|
|
+2. **High fees**: Consider optimizing your callback or using a lower gas limit
|
|
|
+3. **Reverts**: Check that your gas limit doesn't exceed the maximum allowed
|
|
|
+
|
|
|
+For more debugging help, see the [Debug Callback Failures](/entropy/debug-callback-failures) guide.
|
|
|
+
|
|
|
+## Additional Resources
|
|
|
+
|
|
|
+- [Generate Random Numbers in EVM Contracts](/entropy/generate-random-numbers/evm) - Basic setup guide
|
|
|
+- [Best Practices](/entropy/best-practices) - General optimization tips
|
|
|
+- [Current Fees](/entropy/current-fees) - Fee information for different chains
|
|
|
+- [Contract Addresses](/entropy/contract-addresses) - Entropy contract deployments
|