@@ -28,15 +28,18 @@ You might need custom gas limits in these scenarios:
28
28
Instead of the basic ` requestV2() ` method, use the variant that accepts a ` gasLimit ` parameter:
29
29
30
30
``` solidity copy
31
- function requestRandomNumberWithCustomGas(uint32 customGasLimit) external payable {
31
+ function requestRandomNumberWithCustomGas(
32
+ uint32 customGasLimit
33
+ ) external payable {
32
34
// Calculate the fee for the custom gas limit
33
35
uint256 fee = entropy.getFeeV2(customGasLimit);
34
-
36
+
35
37
// Request random number with custom gas limit
36
- uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
37
-
38
+ uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
39
+
38
40
// Store the sequence number for tracking if needed
39
41
}
42
+
40
43
```
41
44
42
45
### 2. Calculate Fees with Custom Gas Limit
@@ -63,32 +66,32 @@ import { IEntropyV2 } from "@pythnetwork/entropy-sdk-solidity/IEntropyV2.sol";
63
66
contract CustomGasLimitExample is IEntropyConsumer {
64
67
IEntropyV2 public entropy;
65
68
mapping(uint64 => bool) public processedRequests;
66
-
69
+
67
70
constructor(address entropyAddress) {
68
71
entropy = IEntropyV2(entropyAddress);
69
72
}
70
-
73
+
71
74
// Request with custom gas limit for complex callback
72
75
function requestComplexRandomNumber() external payable {
73
76
uint32 customGasLimit = 200000; // Higher limit for complex operations
74
77
uint256 fee = entropy.getFeeV2(customGasLimit);
75
-
78
+
76
79
require(msg.value >= fee, "Insufficient fee");
77
-
78
- uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
80
+
81
+ uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
79
82
// Store sequence number if needed for tracking
80
83
}
81
-
84
+
82
85
// Request with lower gas limit for simple callback
83
86
function requestSimpleRandomNumber() external payable {
84
87
uint32 customGasLimit = 50000; // Lower limit for simple operations
85
88
uint256 fee = entropy.getFeeV2(customGasLimit);
86
-
89
+
87
90
require(msg.value >= fee, "Insufficient fee");
88
-
89
- uint64 sequenceNumber = entropy.requestV2{value: fee}(customGasLimit);
91
+
92
+ uint64 sequenceNumber = entropy.requestV2{ value: fee }(customGasLimit);
90
93
}
91
-
94
+
92
95
// Complex callback that requires more gas
93
96
function entropyCallback(
94
97
uint64 sequenceNumber,
@@ -98,40 +101,40 @@ contract CustomGasLimitExample is IEntropyConsumer {
98
101
// Prevent duplicate processing
99
102
require(!processedRequests[sequenceNumber], "Already processed");
100
103
processedRequests[sequenceNumber] = true;
101
-
104
+
102
105
// Complex operations that require more gas
103
106
for (uint i = 0; i < 10; i++) {
104
107
// Simulate complex state changes
105
108
// This would require more gas than the default limit
106
109
}
107
-
110
+
108
111
// Use the random number for your application logic
109
112
uint256 randomValue = uint256(randomNumber);
110
113
// Your application logic here...
111
114
}
112
-
115
+
113
116
function getEntropy() internal view override returns (address) {
114
117
return address(entropy);
115
118
}
116
119
}
120
+
117
121
```
118
122
119
123
## Gas Limit Constraints
120
124
121
125
When setting custom gas limits, be aware of these constraints:
122
126
123
127
<Callout type = " info" emoji = " ℹ️" >
124
- ** Gas Limit Rules:**
125
- - Gas limits are automatically rounded up to the nearest multiple of 10,000
126
- - Example: 19,000 becomes 20,000, 25,500 becomes 30,000
127
- - The minimum gas limit is the provider's configured default limit
128
- - The maximum gas limit is 655,350,000 (uint16.max × 10,000)
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)
129
132
</Callout >
130
133
131
134
### Recommended Gas Limits
132
135
133
136
- ** Simple callbacks** : 50,000 - 100,000 gas
134
- - ** Moderate complexity** : 100,000 - 200,000 gas
137
+ - ** Moderate complexity** : 100,000 - 200,000 gas
135
138
- ** Complex operations** : 200,000 - 500,000 gas
136
139
- ** Very complex logic** : 500,000+ gas (use with caution)
137
140
@@ -164,8 +167,8 @@ uint32 customGasLimit = estimatedGas + safetyBuffer;
164
167
Be prepared to handle cases where your gas limit is insufficient:
165
168
166
169
<Callout type = " warning" emoji = " ⚠️" >
167
- If your callback runs out of gas, the entropy provider will not be able to
168
- complete the callback. Always test your gas limits thoroughly and include
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
169
172
adequate safety margins.
170
173
</Callout >
171
174
0 commit comments