|
| 1 | +# Request Callback Variants |
| 2 | + |
| 3 | +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: |
| 4 | + |
| 5 | +## 1. Basic Request |
| 6 | + |
| 7 | +```solidity copy |
| 8 | +function requestV2() external payable returns (uint64 assignedSequenceNumber); |
| 9 | +
|
| 10 | +``` |
| 11 | + |
| 12 | +This is the simplest variant that requests entropy using the default randomness provider and default gas settings. Uses in-contract PRNG for the user contribution to randomness. |
| 13 | + |
| 14 | +Use this when you want the most straightforward implementation and don't need to customize provider or gas parameters. |
| 15 | + |
| 16 | +### Example |
| 17 | + |
| 18 | +```solidity copy |
| 19 | +function requestBasicRandomNumber() external payable { |
| 20 | + // Get the fee for the default provider and gas limit |
| 21 | + uint256 fee = entropy.getFeeV2(); |
| 22 | +
|
| 23 | + require(msg.value >= fee, "Insufficient fee"); |
| 24 | +
|
| 25 | + // Request randomness with default settings |
| 26 | + uint64 sequenceNumber = entropy.requestV2{ value: fee }(); |
| 27 | +} |
| 28 | +
|
| 29 | +``` |
| 30 | + |
| 31 | +## 2. Request with Gas Limit |
| 32 | + |
| 33 | +```solidity copy |
| 34 | +function requestV2( |
| 35 | + uint32 gasLimit |
| 36 | +) external payable returns (uint64 assignedSequenceNumber); |
| 37 | +
|
| 38 | +``` |
| 39 | + |
| 40 | +This variant allows you to specify a custom gas limit for the callback function execution. Uses in-contract PRNG for the user contribution. |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +### Example |
| 45 | + |
| 46 | +```solidity copy |
| 47 | +function requestWithCustomGas() external payable { |
| 48 | + uint32 customGasLimit = 150000; // Custom gas limit for callback |
| 49 | +
|
| 50 | + // Get fee for the custom gas limit |
| 51 | + uint256 fee = entropy.getFeeV2(customGasLimit); |
| 52 | +
|
| 53 | + require(msg.value >= fee, "Insufficient fee"); |
| 54 | +
|
| 55 | + // Request with custom gas limit |
| 56 | + uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit); |
| 57 | +} |
| 58 | +
|
| 59 | +``` |
| 60 | + |
| 61 | +## 3. Request with Provider and Gas Limit |
| 62 | + |
| 63 | +```solidity copy |
| 64 | +function requestV2( |
| 65 | + address provider, |
| 66 | + uint32 gasLimit |
| 67 | +) external payable returns (uint64 assignedSequenceNumber); |
| 68 | +
|
| 69 | +``` |
| 70 | + |
| 71 | +This variant allows you to specify both a custom randomness provider and a custom gas limit for the callback. Uses in-contract PRNG for the user contribution. |
| 72 | + |
| 73 | +Use this when you need full control over both the randomness source and the gas allocation for your callback execution. |
| 74 | + |
| 75 | +### Example |
| 76 | + |
| 77 | +```solidity copy |
| 78 | +function requestWithProviderAndGas() external payable { |
| 79 | + address preferredProvider = 0x1234567890123456789012345678901234567890; // Your chosen provider |
| 80 | + uint32 customGasLimit = 100000; |
| 81 | +
|
| 82 | + // Get fee for the specific provider and gas limit |
| 83 | + uint256 fee = entropy.getFeeV2(preferredProvider, customGasLimit); |
| 84 | +
|
| 85 | + require(msg.value >= fee, "Insufficient fee"); |
| 86 | +
|
| 87 | + // Request with specific provider and gas limit |
| 88 | + uint64 sequenceNumber = entropy.requestV2{ value: fee }( |
| 89 | + preferredProvider, |
| 90 | + customGasLimit |
| 91 | + ); |
| 92 | +} |
| 93 | +
|
| 94 | +``` |
| 95 | + |
| 96 | +## 4. Request with Provider, User Randomness and Gas Limit |
| 97 | + |
| 98 | +```solidity copy |
| 99 | +function requestV2( |
| 100 | + address provider, |
| 101 | + bytes32 userRandomNumber, |
| 102 | + uint32 gasLimit |
| 103 | +) external payable returns (uint64 assignedSequenceNumber); |
| 104 | +
|
| 105 | +``` |
| 106 | + |
| 107 | +This is the most complete variant that allows you to specify a custom randomness provider, a custom gas limit for the callback, and provide your own user contribution to the randomness. The `userRandomNumber` parameter allows you to contribute your own randomness to the entropy generation process instead of relying on the in-contract PRNG. |
| 108 | + |
| 109 | +Use this when you need maximum control over all aspects of the randomness generation, including providing your own source of entropy for the user contribution. |
| 110 | + |
| 111 | +### Example |
| 112 | + |
| 113 | +```solidity copy |
| 114 | +function requestWithFullControl(bytes32 userContribution) external payable { |
| 115 | + address preferredProvider = 0x1234567890123456789012345678901234567890; |
| 116 | + uint32 customGasLimit = 120000; |
| 117 | +
|
| 118 | + // Get fee for the specific provider and gas limit |
| 119 | + uint256 fee = entropy.getFeeV2(preferredProvider, customGasLimit); |
| 120 | +
|
| 121 | + require(msg.value >= fee, "Insufficient fee"); |
| 122 | +
|
| 123 | + // Request with full customization |
| 124 | + uint64 sequenceNumber = entropy.requestV2{ value: fee }( |
| 125 | + preferredProvider, |
| 126 | + userContribution, // Your own randomness contribution |
| 127 | + customGasLimit |
| 128 | + ); |
| 129 | +} |
| 130 | +
|
| 131 | +``` |
0 commit comments