Skip to content

Commit 12a6da7

Browse files
committed
feat: add withdrawFee function to EntropyGovernance contract
1 parent 871e7e8 commit 12a6da7

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

target_chains/ethereum/contracts/contracts/entropy/EntropyGovernance.sol

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract contract EntropyGovernance is EntropyState {
1717

1818
event NewAdminProposed(address oldAdmin, address newAdmin);
1919
event NewAdminAccepted(address oldAdmin, address newAdmin);
20+
event FeeWithdrawn(address targetAddress, uint amount);
2021

2122
/**
2223
* @dev Returns the address of the proposed admin.
@@ -92,5 +93,27 @@ abstract contract EntropyGovernance is EntropyState {
9293
emit DefaultProviderSet(oldDefaultProvider, newDefaultProvider);
9394
}
9495

96+
/**
97+
* @dev Withdraw accumulated Pyth fees to a target address
98+
*
99+
* Calls {_authoriseAdminAction}.
100+
*
101+
* Emits a {FeeWithdrawn} event.
102+
*/
103+
function withdrawFee(address targetAddress, uint128 amount) external {
104+
require(targetAddress != address(0), "targetAddress is zero address");
105+
_authoriseAdminAction();
106+
107+
if (amount > _state.accruedPythFeesInWei)
108+
revert EntropyErrors.InsufficientFee();
109+
110+
_state.accruedPythFeesInWei -= amount;
111+
112+
(bool success, ) = targetAddress.call{value: amount}("");
113+
require(success, "Failed to withdraw fees");
114+
115+
emit FeeWithdrawn(targetAddress, amount);
116+
}
117+
95118
function _authoriseAdminAction() internal virtual;
96119
}

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,138 @@ contract EntropyAuthorized is Test, EntropyTestUtils {
174174
vm.expectRevert(EntropyErrors.Unauthorized.selector);
175175
random.acceptAdmin();
176176
}
177+
178+
function testWithdrawFeeByAdmin() public {
179+
// Register provider1 first
180+
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
181+
vm.prank(provider1);
182+
random.register(0, hashChain[0], hex"0100", 100, "");
183+
184+
// First accrue some fees through requests
185+
vm.prank(admin);
186+
random.setPythFee(10);
187+
188+
// Make a few requests to accrue fees
189+
bytes32 userCommitment = random.constructUserCommitment(
190+
bytes32(uint256(42))
191+
);
192+
vm.deal(address(this), 50);
193+
for (uint i = 0; i < 5; i++) {
194+
random.request{value: 10}(provider1, userCommitment, false);
195+
}
196+
assertEq(random.getAccruedPythFees(), 50);
197+
198+
address targetAddress = address(123);
199+
uint128 withdrawAmount = 30;
200+
201+
vm.prank(admin);
202+
random.withdrawFee(targetAddress, withdrawAmount);
203+
204+
assertEq(random.getAccruedPythFees(), 20);
205+
assertEq(targetAddress.balance, withdrawAmount);
206+
}
207+
208+
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);
227+
228+
address targetAddress = address(123);
229+
uint128 withdrawAmount = 30;
230+
231+
vm.prank(owner);
232+
random.withdrawFee(targetAddress, withdrawAmount);
233+
234+
assertEq(random.getAccruedPythFees(), 20);
235+
assertEq(targetAddress.balance, withdrawAmount);
236+
}
237+
238+
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+
}
256+
257+
vm.prank(admin2);
258+
vm.expectRevert(EntropyErrors.Unauthorized.selector);
259+
random.withdrawFee(address(123), 30);
260+
}
261+
262+
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);
281+
282+
vm.prank(admin);
283+
vm.expectRevert(EntropyErrors.InsufficientFee.selector);
284+
random.withdrawFee(address(123), 60);
285+
}
286+
287+
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);
306+
307+
vm.prank(admin);
308+
vm.expectRevert("targetAddress is zero address");
309+
random.withdrawFee(address(0), 30);
310+
}
177311
}

0 commit comments

Comments
 (0)