Skip to content

Commit

Permalink
fix: withdraw not redeeming profits
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChin committed Nov 19, 2024
1 parent 1a3b739 commit cff55c9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 15 additions & 7 deletions src/contracts/Grateful.sol
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
if (assetsToWithdraw > initialDepositToWithdraw) {
profit = assetsToWithdraw - initialDepositToWithdraw;
performanceFeeAmount = calculatePerformanceFee(profit);
assetsToWithdraw -= performanceFeeAmount;
}

// Update user's shares and deposits before external calls
Expand All @@ -531,13 +530,22 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
userDeposits[msg.sender][_token] = 0;
}

// Withdraw performance fee to fee recipient (owner)
if (performanceFeeAmount > 0) {
vault.withdraw(performanceFeeAmount, owner(), address(this));
}
// Redeem shares to Grateful contract
vault.redeem(sharesToWithdraw, address(this), address(this));

IERC20 token = IERC20(_token);

// Withdraw assets to user
vault.withdraw(assetsToWithdraw, msg.sender, address(this));
if (profit > 0) {
// Transfer performance fee to owner
token.safeTransfer(owner(), performanceFeeAmount);

// Transfer remaining assets to merchant
uint256 merchantAmount = assetsToWithdraw - performanceFeeAmount;
token.safeTransfer(msg.sender, merchantAmount);
} else {
// No profit, transfer all assets to merchant
token.safeTransfer(msg.sender, assetsToWithdraw);
}

// Emit an event for the withdrawal
emit Withdrawal(msg.sender, _token, assetsToWithdraw, performanceFeeAmount);
Expand Down
6 changes: 0 additions & 6 deletions test/integration/Grateful.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ contract IntegrationGrateful is IntegrationBase {

IERC20 token = IERC20(tokenAddr);

// Flow correctly works only if vault has any deposit
_approveAndPay(_user, _merchant2, tokenAddr, amount, _YIELDING_FUNDS);

// Capture owner's initial balance before payment
uint256 ownerInitialBalance = token.balanceOf(_owner);

Expand Down Expand Up @@ -202,9 +199,6 @@ contract IntegrationGrateful is IntegrationBase {

IERC20 token = IERC20(tokenAddr);

// Flow correctly works only if vault has any deposit
_approveAndPay(_user, _merchant2, tokenAddr, 1, _YIELDING_FUNDS);

// Capture owner's initial balance before payment
uint256 ownerInitialBalance = token.balanceOf(_owner);

Expand Down

0 comments on commit cff55c9

Please sign in to comment.