Skip to content

Commit 551abdb

Browse files
committed
feat: refactor fee withdrawal tests to use setup helper function
1 parent 12a6da7 commit 551abdb

File tree

1 file changed

+27
-83
lines changed

1 file changed

+27
-83
lines changed

target_chains/ethereum/contracts/forge-test/EntropyAuthorized.t.sol

Lines changed: 27 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -175,134 +175,78 @@ contract EntropyAuthorized is Test, EntropyTestUtils {
175175
random.acceptAdmin();
176176
}
177177

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
180184
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
181185
vm.prank(provider1);
182186
random.register(0, hashChain[0], hex"0100", 100, "");
183187

184-
// First accrue some fees through requests
188+
// Set Pyth fee
185189
vm.prank(admin);
186-
random.setPythFee(10);
190+
random.setPythFee(feeAmount);
187191

188-
// Make a few requests to accrue fees
192+
// Make requests to accrue fees
189193
bytes32 userCommitment = random.constructUserCommitment(
190194
bytes32(uint256(42))
191195
);
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);
195199
}
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);
197208

198209
address targetAddress = address(123);
199210
uint128 withdrawAmount = 30;
200211

201212
vm.prank(admin);
202213
random.withdrawFee(targetAddress, withdrawAmount);
203214

204-
assertEq(random.getAccruedPythFees(), 20);
215+
assertEq(random.getAccruedPythFees(), totalFees - withdrawAmount);
205216
assertEq(targetAddress.balance, withdrawAmount);
206217
}
207218

208219
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);
227221

228222
address targetAddress = address(123);
229223
uint128 withdrawAmount = 30;
230224

231225
vm.prank(owner);
232226
random.withdrawFee(targetAddress, withdrawAmount);
233227

234-
assertEq(random.getAccruedPythFees(), 20);
228+
assertEq(random.getAccruedPythFees(), totalFees - withdrawAmount);
235229
assertEq(targetAddress.balance, withdrawAmount);
236230
}
237231

238232
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);
256234

257235
vm.prank(admin2);
258236
vm.expectRevert(EntropyErrors.Unauthorized.selector);
259237
random.withdrawFee(address(123), 30);
260238
}
261239

262240
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);
281242

282243
vm.prank(admin);
283244
vm.expectRevert(EntropyErrors.InsufficientFee.selector);
284-
random.withdrawFee(address(123), 60);
245+
random.withdrawFee(address(123), totalFees + 10);
285246
}
286247

287248
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);
306250

307251
vm.prank(admin);
308252
vm.expectRevert("targetAddress is zero address");

0 commit comments

Comments
 (0)