Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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;
}
134 changes: 134 additions & 0 deletions target_chains/ethereum/contracts/forge-test/EntropyAuthorized.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,138 @@ contract EntropyAuthorized is Test, EntropyTestUtils {
vm.expectRevert(EntropyErrors.Unauthorized.selector);
random.acceptAdmin();
}

function testWithdrawFeeByAdmin() public {
// Register provider1 first
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, "");

// First accrue some fees through requests
vm.prank(admin);
random.setPythFee(10);

// Make a few requests to accrue fees
bytes32 userCommitment = random.constructUserCommitment(
bytes32(uint256(42))
);
vm.deal(address(this), 50);
for (uint i = 0; i < 5; i++) {
random.request{value: 10}(provider1, userCommitment, false);
}
assertEq(random.getAccruedPythFees(), 50);

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

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

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

function testWithdrawFeeByOwner() public {
// Register provider1 first
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
vm.prank(provider1);
random.register(0, hashChain[0], hex"0100", 100, "");

// First accrue some fees through requests
vm.prank(admin);
random.setPythFee(10);

// Make a few requests to accrue fees
bytes32 userCommitment = random.constructUserCommitment(
bytes32(uint256(42))
);
vm.deal(address(this), 50);
for (uint i = 0; i < 5; i++) {
random.request{value: 10}(provider1, userCommitment, false);
}
assertEq(random.getAccruedPythFees(), 50);

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

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

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

function testWithdrawFeeByUnauthorized() public {
// Register provider1 first
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
vm.prank(provider1);
random.register(0, hashChain[0], hex"0100", 100, "");

// First accrue some fees through requests
vm.prank(admin);
random.setPythFee(10);

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

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

function testWithdrawFeeInsufficientBalance() public {
// Register provider1 first
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
vm.prank(provider1);
random.register(0, hashChain[0], hex"0100", 100, "");

// First accrue some fees through requests
vm.prank(admin);
random.setPythFee(10);

// Make a few requests to accrue fees
bytes32 userCommitment = random.constructUserCommitment(
bytes32(uint256(42))
);
vm.deal(address(this), 50);
for (uint i = 0; i < 5; i++) {
random.request{value: 10}(provider1, userCommitment, false);
}
assertEq(random.getAccruedPythFees(), 50);

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

function testWithdrawFeeToZeroAddress() public {
// Register provider1 first
bytes32[] memory hashChain = generateHashChain(provider1, 0, 100);
vm.prank(provider1);
random.register(0, hashChain[0], hex"0100", 100, "");

// First accrue some fees through requests
vm.prank(admin);
random.setPythFee(10);

// Make a few requests to accrue fees
bytes32 userCommitment = random.constructUserCommitment(
bytes32(uint256(42))
);
vm.deal(address(this), 50);
for (uint i = 0; i < 5; i++) {
random.request{value: 10}(provider1, userCommitment, false);
}
assertEq(random.getAccruedPythFees(), 50);

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