set-custom-gas-limits.mdx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ---
  2. title: Set Custom Gas Limits
  3. description: How to set custom gas limits for Entropy callbacks
  4. ---
  5. # Set Custom Gas Limits
  6. 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.
  7. ## When to Use Custom Gas Limits
  8. ### Use Higher Gas Limits When:
  9. - Your callback function performs complex calculations
  10. - You need to update multiple storage variables
  11. - Your callback interacts with other contracts
  12. - You're implementing complex game logic
  13. ### Use Lower Gas Limits When:
  14. - Your callback function is simple (e.g., just stores a single value)
  15. - You want to optimize for cost
  16. - You want to prevent potential gas griefing
  17. ## Implementation
  18. ### Using EntropyV2 Interface
  19. ```solidity
  20. import "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
  21. contract MyContract is IEntropyConsumer {
  22. IEntropyV2 entropy;
  23. function requestWithCustomGas() external payable {
  24. uint32 customGasLimit = 200000; // Adjust based on your needs
  25. // Get fee for custom gas limit
  26. uint256 fee = entropy.getFeeV2(customGasLimit);
  27. require(msg.value >= fee, "Insufficient fee");
  28. // Request with custom gas limit
  29. uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
  30. }
  31. }
  32. ```
  33. ### Full Control Request
  34. ```solidity
  35. function requestWithFullControl() external payable {
  36. address provider = entropy.getDefaultProvider();
  37. uint32 gasLimit = 150000;
  38. bytes32 userRandomNumber = keccak256(abi.encode(block.timestamp, msg.sender));
  39. uint256 fee = entropy.getFeeV2(provider, gasLimit);
  40. require(msg.value >= fee, "Insufficient fee");
  41. uint64 sequenceNumber = entropy.requestV2{value: fee}(
  42. provider,
  43. gasLimit,
  44. userRandomNumber
  45. );
  46. }
  47. ```
  48. ## Gas Limit Constraints
  49. Each network has different gas limit constraints:
  50. | Network | Min Gas Limit | Max Gas Limit | Default |
  51. | --------- | ------------- | ------------- | ------- |
  52. | Ethereum | 50,000 | 100,000 | 100,000 |
  53. | Arbitrum | 50,000 | 100,000 | 100,000 |
  54. | Avalanche | 50,000 | 100,000 | 100,000 |
  55. | Base | 50,000 | 100,000 | 100,000 |
  56. | BNB Chain | 50,000 | 100,000 | 100,000 |
  57. | Optimism | 50,000 | 100,000 | 100,000 |
  58. | Polygon | 50,000 | 100,000 | 100,000 |
  59. ## Best Practices
  60. 1. **Test your callback function** to estimate gas usage before setting custom limits
  61. 2. **Add a buffer** of 10-20% to your estimated gas usage for safety
  62. 3. **Use the minimum necessary** gas limit to reduce costs
  63. 4. **Consider network differences** - some networks may have different gas costs for similar operations