request-callback-variants.mdx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ---
  2. title: Request Callback Variants
  3. description: Different ways to request random numbers with Pyth Entropy
  4. ---
  5. 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:
  6. ## 1. Basic request
  7. ```solidity
  8. function requestV2() external payable returns (uint64 assignedSequenceNumber);
  9. ```
  10. This is the simplest variant that requests entropy using the default provider and default gas settings. Uses in-contract PRNG for the user contribution to randomness.
  11. Use this when you want the most straightforward implementation and don't need to customize provider or gas parameters.
  12. ### Example
  13. ```solidity
  14. function requestBasicRandomNumber() external payable {
  15. // Get the fee for the default provider and gas limit
  16. uint256 fee = entropy.getFeeV2();
  17. require(msg.value >= fee, "Insufficient fee");
  18. // Request randomness with default settings
  19. uint64 sequenceNumber = entropy.requestV2{value: fee}();
  20. }
  21. ```
  22. ## 2. Request with custom gas limit
  23. ```solidity
  24. function requestV2(
  25. uint32 gasLimit
  26. ) external payable returns (uint64 assignedSequenceNumber);
  27. ```
  28. This variant allows you to specify a custom gas limit for the callback function execution. Uses in-contract PRNG for the user contribution.
  29. 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.
  30. ### Example
  31. ```solidity
  32. function requestWithCustomGas() external payable {
  33. uint32 customGasLimit = 150000; // Custom gas limit for callback
  34. // Get fee for the custom gas limit
  35. uint256 fee = entropy.getFeeV2(customGasLimit);
  36. require(msg.value >= fee, "Insufficient fee");
  37. // Request with custom gas limit
  38. uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
  39. }
  40. ```
  41. ## 3. Request with custom provider
  42. ```solidity
  43. function requestV2(
  44. address provider
  45. ) external payable returns (uint64 assignedSequenceNumber);
  46. ```
  47. This variant allows you to specify a custom entropy provider instead of using the default one.
  48. ## 4. Full custom request
  49. ```solidity
  50. function requestV2(
  51. address provider,
  52. uint32 gasLimit,
  53. bytes32 userRandomNumber
  54. ) external payable returns (uint64 assignedSequenceNumber);
  55. ```
  56. This is the most flexible variant that allows you to specify all parameters: custom provider, custom gas limit, and user random number.