Skip to content

Commit

Permalink
Use a library for reward collection
Browse files Browse the repository at this point in the history
  • Loading branch information
enriquefynn committed Nov 28, 2023
1 parent 336d8e2 commit 6ff972e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
71 changes: 53 additions & 18 deletions src/FeeRewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,29 @@ pragma solidity ^0.8.13;

import "@openzeppelin/contracts/access/Ownable.sol";

contract RewardsCollector is Ownable {
library CalculateAndSendRewards {
// Fee denominator, if `feeNominator = 500`,
// the tax is 500/10000 = 5/100 = 5%.
uint32 public constant FEE_DENOMINATOR = 10000;
event CollectedReward(
address withdrawalCredential,
uint256 withdrawnAmount,
address owner,
uint256 ownerFee
);

// 1 - fee % will go to the user in this address.
address public withdrawalCredential;

// Fee's numerator.
uint32 public feeNumerator;

// Fee denominator, if `feeNumerator = 500`,
// the tax is 500/10000 = 5/100 = 5%.
uint32 public constant FEE_DENOMINATOR = 10000;

// Allow receiving MEV and other rewards.
receive() external payable {}

function collectRewards() public payable {
uint256 ownerAmount = (address(this).balance * feeNumerator) /
function calculateRewards(
uint32 feeNominator,
address owner,
address withdrawalCredential
) public {
uint256 ownerAmount = (address(this).balance * feeNominator) /
FEE_DENOMINATOR;
uint256 returnedAmount = address(this).balance - ownerAmount;
require(
ownerAmount != 0 || returnedAmount != 0,
"Nothing to distribute"
);
address owner = owner();
emit CollectedReward(
withdrawalCredential,
returnedAmount,
Expand All @@ -47,13 +40,55 @@ contract RewardsCollector is Ownable {
}("");
require(sent, "Failed to send Ether back to withdrawal credential");
}
}

contract RewardsCollector {
event CollectedReward(
address withdrawalCredential,
uint256 withdrawalFee,
address owner,
uint256 ownerFee
);

// 1 - fee % will go to the user in this address.
address public withdrawalCredential;

// Fee's numerator.
uint32 public feeNumerator;

// This is the contract that created the `RewardsCollector`.
// Do not use owner here because this contract is going to be
// created multiple times for each `withdrawal credential` and
// we don't need any function for the ownership except when changing
// the fee.
address public parentContract;

// Fee denominator, if `feeNumerator = 500`,
// the tax is 500/10000 = 5/100 = 5%.
uint32 public constant FEE_DENOMINATOR = 10000;

// Allow receiving MEV and other rewards.
receive() external payable {}

function collectRewards() public payable {
CalculateAndSendRewards.calculateRewards(
feeNumerator,
parentContract,
withdrawalCredential
);
}

constructor(address _withdrawalCredential, uint32 _feeNumerator) {
withdrawalCredential = _withdrawalCredential;
feeNumerator = _feeNumerator;
parentContract = msg.sender;
}

function changeFeeNumerator(uint32 _newFeeNumerator) public onlyOwner {
function changeFeeNumerator(uint32 _newFeeNumerator) public {
require(
msg.sender == parentContract,
"ChangeFee not called from parent contract"
);
feeNumerator = _newFeeNumerator;
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/FeeRewardsManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ contract ReentrantAttack {

contract ChangeOwnerContract {
fallback() external payable {
RewardsCollector(payable(msg.sender)).transferOwnership(address(0x200));
FeeRewardsManager(payable(msg.sender)).transferOwnership(
address(0x200)
);
}
}

Expand Down Expand Up @@ -47,7 +49,7 @@ contract FeeRewardsTest is Test {
// derived address has the parent's as owner
assertEq(
address(feeRewardsManager),
RewardsCollector(payable(addr)).owner()
RewardsCollector(payable(addr)).parentContract()
);

uint256 amountInContract = address(addr).balance;
Expand Down Expand Up @@ -155,7 +157,8 @@ contract FeeRewardsTest is Test {
rewards
);
uint256 chorusAmount = (address(collector).balance *
uint256(collector.feeNumerator())) / collector.FEE_DENOMINATOR();
uint256(collector.feeNumerator())) /
CalculateAndSendRewards.FEE_DENOMINATOR;
uint256 withdrawalCredentialsAmount = address(collector).balance -
chorusAmount;
uint256 chorusBalanceBefore = address(feeRewardsManager).balance;
Expand Down

0 comments on commit 6ff972e

Please sign in to comment.