Skip to content

Commit

Permalink
feat: add views for fees and rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
patcito committed Aug 15, 2024
1 parent 1f79a5a commit f22a747
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/FoldCaptiveStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
function compound() public isInitialized {
collectPositionFees();

uint256 fee0Owed = (token0FeesPerLiquidity - balances[msg.sender].token0FeeDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;
uint256 fee1Owed = (token1FeesPerLiquidity - balances[msg.sender].token1FeeDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;
(uint256 fee0Owed, uint256 fee1Owed) = owedFees();

INonfungiblePositionManager.IncreaseLiquidityParams memory params = INonfungiblePositionManager
.IncreaseLiquidityParams({
Expand All @@ -243,14 +240,23 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
emit Compounded(msg.sender, liquidity, fee0Owed, fee1Owed);
}

/// @notice User-specific function to view fees owed on the singular position
function owedFees() public view returns (uint256, uint256) {
uint256 fee0Owed = ((token0FeesPerLiquidity -
balances[msg.sender].token0FeeDebt) * balances[msg.sender].amount) /
liquidityUnderManagement;
uint256 fee1Owed = ((token1FeesPerLiquidity -
balances[msg.sender].token1FeeDebt) * balances[msg.sender].amount) /
liquidityUnderManagement;

return (fee0Owed, fee1Owed);
}

/// @notice User-specific function to collect fees on the singular position
function collectFees() public isInitialized {
collectPositionFees();

uint256 fee0Owed = (token0FeesPerLiquidity - balances[msg.sender].token0FeeDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;
uint256 fee1Owed = (token1FeesPerLiquidity - balances[msg.sender].token1FeeDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;
(uint256 fee0Owed, uint256 fee1Owed) = owedFees();

token0.transfer(msg.sender, fee0Owed);
token1.transfer(msg.sender, fee1Owed);
Expand All @@ -261,10 +267,16 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
emit FeesCollected(msg.sender, fee0Owed, fee1Owed);
}

/// @notice User-specific function to view rewards owed on the singular position
function owedRewards() public view returns (uint256) {
return
((rewardsPerLiquidity - balances[msg.sender].rewardDebt) *
balances[msg.sender].amount) / liquidityUnderManagement;
}

/// @notice User-specific Rewards for Protocol Rewards
function collectRewards() public isInitialized {
uint256 rewardsOwed = (rewardsPerLiquidity - balances[msg.sender].rewardDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;
uint256 rewardsOwed = owedRewards();

WETH9.transfer(msg.sender, rewardsOwed);

Expand Down Expand Up @@ -373,7 +385,8 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
amount1Max: uint128(amount1)
});

(uint256 amount0Collected, uint256 amount1Collected) = positionManager.collect(collectParams);
(uint256 amount0Collected, uint256 amount1Collected) = positionManager
.collect(collectParams);

if (amount0Collected != amount0 || amount1Collected != amount1) {
revert WithdrawFailed();
Expand Down
36 changes: 36 additions & 0 deletions test/UnitTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,42 @@ contract UnitTests is BaseCaptiveTest {
assertEq(amount, liq / 4);
}

/// @dev Ensure that owed fees are returned correctly.
function testOwedFees() public {
testAddLiquidity();
uint256 owedReards = foldCaptiveStaking.owedRewards();
(uint256 amount, uint256 rewardDebt, , ) = foldCaptiveStaking.balances(
User01
);
uint256 rewardOwedCheck = ((foldCaptiveStaking.rewardsPerLiquidity() -
rewardDebt) * amount) /
foldCaptiveStaking.liquidityUnderManagement();

assertEq(rewardOwedCheck, owedReards);
}

/// @dev Ensure that owed fees are returned correctly.
function testOwedRewards() public {
testAddLiquidity();
(uint256 fee0Owed, uint256 fee1Owed) = foldCaptiveStaking.owedFees();
(
uint256 amount,
,
uint256 token0FeeDebt,
uint256 token1FeeDebt
) = foldCaptiveStaking.balances(User01);
uint256 fee0OwedCheck = ((foldCaptiveStaking.token0FeesPerLiquidity() -
token0FeeDebt) * amount) /
foldCaptiveStaking.liquidityUnderManagement();

uint256 fee1OwedCheck = ((foldCaptiveStaking.token1FeesPerLiquidity() -
token1FeeDebt) * amount) /
foldCaptiveStaking.liquidityUnderManagement();

assertEq(fee0OwedCheck, fee0Owed);
assertEq(fee1OwedCheck, fee1Owed);
}

/// @dev Ensure fees are accrued correctly and distributed proportionately.
function testFeesAccrue() public {
testAddLiquidity();
Expand Down

0 comments on commit f22a747

Please sign in to comment.