Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ abstract contract EntropyGovernance is EntropyState {

event NewAdminProposed(address oldAdmin, address newAdmin);
event NewAdminAccepted(address oldAdmin, address newAdmin);
event FeeWithdrawn(address targetAddress, uint amount);

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

/**
* @dev Withdraw accumulated Pyth fees to a target address
*
* Calls {_authoriseAdminAction}.
*
* Emits a {FeeWithdrawn} event.
*/
function withdrawFee(address targetAddress, uint128 amount) external {
require(targetAddress != address(0), "targetAddress is zero address");
_authoriseAdminAction();

if (amount > _state.accruedPythFeesInWei)
revert EntropyErrors.InsufficientFee();

_state.accruedPythFeesInWei -= amount;

(bool success, ) = targetAddress.call{value: amount}("");
require(success, "Failed to withdraw fees");

emit FeeWithdrawn(targetAddress, amount);
}

function _authoriseAdminAction() internal virtual;
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,82 @@ contract EntropyAuthorized is Test, EntropyTestUtils {
vm.expectRevert(EntropyErrors.Unauthorized.selector);
random.acceptAdmin();
}

// Helper function to setup contract with fees
function setupContractWithFees(
uint128 feeAmount,
uint numRequests
) internal returns (uint128 totalFees) {
// Register provider1
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a lot of code duplication in these tests. I suggest adding a helper method that sets up the contract with fees so the test simply has to do the withdraw call.

vm.prank(provider1);
random.register(0, hashChain[0], hex"0100", 100, "");

// Set Pyth fee
vm.prank(admin);
random.setPythFee(feeAmount);

// Make requests to accrue fees
bytes32 userCommitment = random.constructUserCommitment(
bytes32(uint256(42))
);
vm.deal(address(this), feeAmount * numRequests);
for (uint i = 0; i < numRequests; i++) {
random.request{value: feeAmount}(provider1, userCommitment, false);
}

totalFees = uint128(feeAmount * numRequests);
assertEq(random.getAccruedPythFees(), totalFees);
return totalFees;
}

function testWithdrawFeeByAdmin() public {
uint128 totalFees = setupContractWithFees(10, 5);

address targetAddress = address(123);
uint128 withdrawAmount = 30;

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

assertEq(random.getAccruedPythFees(), totalFees - withdrawAmount);
assertEq(targetAddress.balance, withdrawAmount);
}

function testWithdrawFeeByOwner() public {
uint128 totalFees = setupContractWithFees(10, 5);

address targetAddress = address(123);
uint128 withdrawAmount = 30;

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

assertEq(random.getAccruedPythFees(), totalFees - withdrawAmount);
assertEq(targetAddress.balance, withdrawAmount);
}

function testWithdrawFeeByUnauthorized() public {
setupContractWithFees(10, 5);

vm.prank(admin2);
vm.expectRevert(EntropyErrors.Unauthorized.selector);
random.withdrawFee(address(123), 30);
}

function testWithdrawFeeInsufficientBalance() public {
uint128 totalFees = setupContractWithFees(10, 5);

vm.prank(admin);
vm.expectRevert(EntropyErrors.InsufficientFee.selector);
random.withdrawFee(address(123), totalFees + 10);
}

function testWithdrawFeeToZeroAddress() public {
setupContractWithFees(10, 5);

vm.prank(admin);
vm.expectRevert("targetAddress is zero address");
random.withdrawFee(address(0), 30);
}
}
Loading