@@ -175,134 +175,78 @@ contract EntropyAuthorized is Test, EntropyTestUtils {
175
175
random.acceptAdmin ();
176
176
}
177
177
178
- function testWithdrawFeeByAdmin () public {
179
- // Register provider1 first
178
+ // Helper function to setup contract with fees
179
+ function setupContractWithFees (
180
+ uint128 feeAmount ,
181
+ uint numRequests
182
+ ) internal returns (uint128 totalFees ) {
183
+ // Register provider1
180
184
bytes32 [] memory hashChain = generateHashChain (provider1, 0 , 100 );
181
185
vm.prank (provider1);
182
186
random.register (0 , hashChain[0 ], hex "0100 " , 100 , "" );
183
187
184
- // First accrue some fees through requests
188
+ // Set Pyth fee
185
189
vm.prank (admin);
186
- random.setPythFee (10 );
190
+ random.setPythFee (feeAmount );
187
191
188
- // Make a few requests to accrue fees
192
+ // Make requests to accrue fees
189
193
bytes32 userCommitment = random.constructUserCommitment (
190
194
bytes32 (uint256 (42 ))
191
195
);
192
- vm.deal (address (this ), 50 );
193
- for (uint i = 0 ; i < 5 ; i++ ) {
194
- random.request {value: 10 }(provider1, userCommitment, false );
196
+ vm.deal (address (this ), feeAmount * numRequests );
197
+ for (uint i = 0 ; i < numRequests ; i++ ) {
198
+ random.request {value: feeAmount }(provider1, userCommitment, false );
195
199
}
196
- assertEq (random.getAccruedPythFees (), 50 );
200
+
201
+ totalFees = uint128 (feeAmount * numRequests);
202
+ assertEq (random.getAccruedPythFees (), totalFees);
203
+ return totalFees;
204
+ }
205
+
206
+ function testWithdrawFeeByAdmin () public {
207
+ uint128 totalFees = setupContractWithFees (10 , 5 );
197
208
198
209
address targetAddress = address (123 );
199
210
uint128 withdrawAmount = 30 ;
200
211
201
212
vm.prank (admin);
202
213
random.withdrawFee (targetAddress, withdrawAmount);
203
214
204
- assertEq (random.getAccruedPythFees (), 20 );
215
+ assertEq (random.getAccruedPythFees (), totalFees - withdrawAmount );
205
216
assertEq (targetAddress.balance, withdrawAmount);
206
217
}
207
218
208
219
function testWithdrawFeeByOwner () public {
209
- // Register provider1 first
210
- bytes32 [] memory hashChain = generateHashChain (provider1, 0 , 100 );
211
- vm.prank (provider1);
212
- random.register (0 , hashChain[0 ], hex "0100 " , 100 , "" );
213
-
214
- // First accrue some fees through requests
215
- vm.prank (admin);
216
- random.setPythFee (10 );
217
-
218
- // Make a few requests to accrue fees
219
- bytes32 userCommitment = random.constructUserCommitment (
220
- bytes32 (uint256 (42 ))
221
- );
222
- vm.deal (address (this ), 50 );
223
- for (uint i = 0 ; i < 5 ; i++ ) {
224
- random.request {value: 10 }(provider1, userCommitment, false );
225
- }
226
- assertEq (random.getAccruedPythFees (), 50 );
220
+ uint128 totalFees = setupContractWithFees (10 , 5 );
227
221
228
222
address targetAddress = address (123 );
229
223
uint128 withdrawAmount = 30 ;
230
224
231
225
vm.prank (owner);
232
226
random.withdrawFee (targetAddress, withdrawAmount);
233
227
234
- assertEq (random.getAccruedPythFees (), 20 );
228
+ assertEq (random.getAccruedPythFees (), totalFees - withdrawAmount );
235
229
assertEq (targetAddress.balance, withdrawAmount);
236
230
}
237
231
238
232
function testWithdrawFeeByUnauthorized () public {
239
- // Register provider1 first
240
- bytes32 [] memory hashChain = generateHashChain (provider1, 0 , 100 );
241
- vm.prank (provider1);
242
- random.register (0 , hashChain[0 ], hex "0100 " , 100 , "" );
243
-
244
- // First accrue some fees through requests
245
- vm.prank (admin);
246
- random.setPythFee (10 );
247
-
248
- // Make a few requests to accrue fees
249
- bytes32 userCommitment = random.constructUserCommitment (
250
- bytes32 (uint256 (42 ))
251
- );
252
- vm.deal (address (this ), 50 );
253
- for (uint i = 0 ; i < 5 ; i++ ) {
254
- random.request {value: 10 }(provider1, userCommitment, false );
255
- }
233
+ setupContractWithFees (10 , 5 );
256
234
257
235
vm.prank (admin2);
258
236
vm.expectRevert (EntropyErrors.Unauthorized.selector );
259
237
random.withdrawFee (address (123 ), 30 );
260
238
}
261
239
262
240
function testWithdrawFeeInsufficientBalance () public {
263
- // Register provider1 first
264
- bytes32 [] memory hashChain = generateHashChain (provider1, 0 , 100 );
265
- vm.prank (provider1);
266
- random.register (0 , hashChain[0 ], hex "0100 " , 100 , "" );
267
-
268
- // First accrue some fees through requests
269
- vm.prank (admin);
270
- random.setPythFee (10 );
271
-
272
- // Make a few requests to accrue fees
273
- bytes32 userCommitment = random.constructUserCommitment (
274
- bytes32 (uint256 (42 ))
275
- );
276
- vm.deal (address (this ), 50 );
277
- for (uint i = 0 ; i < 5 ; i++ ) {
278
- random.request {value: 10 }(provider1, userCommitment, false );
279
- }
280
- assertEq (random.getAccruedPythFees (), 50 );
241
+ uint128 totalFees = setupContractWithFees (10 , 5 );
281
242
282
243
vm.prank (admin);
283
244
vm.expectRevert (EntropyErrors.InsufficientFee.selector );
284
- random.withdrawFee (address (123 ), 60 );
245
+ random.withdrawFee (address (123 ), totalFees + 10 );
285
246
}
286
247
287
248
function testWithdrawFeeToZeroAddress () public {
288
- // Register provider1 first
289
- bytes32 [] memory hashChain = generateHashChain (provider1, 0 , 100 );
290
- vm.prank (provider1);
291
- random.register (0 , hashChain[0 ], hex "0100 " , 100 , "" );
292
-
293
- // First accrue some fees through requests
294
- vm.prank (admin);
295
- random.setPythFee (10 );
296
-
297
- // Make a few requests to accrue fees
298
- bytes32 userCommitment = random.constructUserCommitment (
299
- bytes32 (uint256 (42 ))
300
- );
301
- vm.deal (address (this ), 50 );
302
- for (uint i = 0 ; i < 5 ; i++ ) {
303
- random.request {value: 10 }(provider1, userCommitment, false );
304
- }
305
- assertEq (random.getAccruedPythFees (), 50 );
249
+ setupContractWithFees (10 , 5 );
306
250
307
251
vm.prank (admin);
308
252
vm.expectRevert ("targetAddress is zero address " );
0 commit comments