Skip to content

Commit efe5487

Browse files
committed
alternate approach for protocol fee collection
1 parent 8109224 commit efe5487

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

src/EulerSwap.sol

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ contract EulerSwap is IEulerSwap, EVCUtil {
1616

1717
bytes32 public constant curve = bytes32("EulerSwap v1");
1818

19+
uint256 public constant protocolFee = 0.1e18;
20+
address public constant protocolFeeRecipient = address(0);
21+
1922
address public immutable vault0;
2023
address public immutable vault1;
2124
address public immutable asset0;
@@ -24,6 +27,8 @@ contract EulerSwap is IEulerSwap, EVCUtil {
2427
uint112 public immutable equilibriumReserve0;
2528
uint112 public immutable equilibriumReserve1;
2629
uint256 public immutable feeMultiplier;
30+
uint256 private immutable feeMultiplierProtocol;
31+
uint256 private immutable feeMultiplierLP;
2732

2833
uint256 public immutable priceX;
2934
uint256 public immutable priceY;
@@ -84,6 +89,8 @@ contract EulerSwap is IEulerSwap, EVCUtil {
8489
reserve0 = params.currReserve0;
8590
reserve1 = params.currReserve1;
8691
feeMultiplier = 1e18 - params.fee;
92+
feeMultiplierProtocol = 1e18 - (params.fee * protocolFee / 1e18);
93+
feeMultiplierLP = feeMultiplier * 1e18 / feeMultiplierProtocol;
8794

8895
// Curve params
8996

@@ -120,11 +127,8 @@ contract EulerSwap is IEulerSwap, EVCUtil {
120127

121128
// Deposit all available funds, adjust received amounts downward to collect fees
122129

123-
uint256 amount0In = IERC20(asset0).balanceOf(address(this));
124-
if (amount0In > 0) amount0In = depositAssets(vault0, amount0In) * feeMultiplier / 1e18;
125-
126-
uint256 amount1In = IERC20(asset1).balanceOf(address(this));
127-
if (amount1In > 0) amount1In = depositAssets(vault1, amount1In) * feeMultiplier / 1e18;
130+
uint256 amount0In = depositAssets(asset0, vault0);
131+
uint256 amount1In = depositAssets(asset1, vault1);
128132

129133
// Verify curve invariant is satisfied
130134

@@ -211,14 +215,23 @@ contract EulerSwap is IEulerSwap, EVCUtil {
211215
}
212216

213217
/// @notice Deposits assets into a vault and automatically repays any outstanding debt
218+
/// @param asset The address of the underlying asset
214219
/// @param vault The address of the vault to deposit into
215-
/// @param amount The amount of assets to deposit
216220
/// @return The amount of assets successfully deposited
217221
/// @dev This function attempts to deposit assets into the specified vault.
218222
/// @dev If the deposit fails with E_ZeroShares error, it safely returns 0 (this happens with very small amounts).
219223
/// @dev After successful deposit, if the user has any outstanding controller-enabled debt, it attempts to repay it.
220224
/// @dev If all debt is repaid, the controller is automatically disabled to reduce gas costs in future operations.
221-
function depositAssets(address vault, uint256 amount) internal returns (uint256) {
225+
function depositAssets(address asset, address vault) internal returns (uint256) {
226+
uint256 amount = IERC20(asset).balanceOf(address(this));
227+
if (amount == 0) return 0;
228+
229+
{
230+
uint256 feeAmount = amount - (amount * feeMultiplierProtocol / 1e18);
231+
IERC20(asset).transfer(protocolFeeRecipient, feeAmount);
232+
amount -= feeAmount;
233+
}
234+
222235
uint256 deposited;
223236

224237
if (IEVC(evc).isControllerEnabled(eulerAccount, vault)) {
@@ -238,13 +251,13 @@ contract EulerSwap is IEulerSwap, EVCUtil {
238251
try IEVault(vault).deposit(amount, eulerAccount) {}
239252
catch (bytes memory reason) {
240253
require(bytes4(reason) == EVKErrors.E_ZeroShares.selector, DepositFailure(reason));
241-
return deposited;
254+
amount = 0;
242255
}
243256

244257
deposited += amount;
245258
}
246259

247-
return deposited;
260+
return (deposited * feeMultiplierLP + (1e18 - 1)) / 1e18;
248261
}
249262

250263
/// @notice Approves tokens for a given vault, supporting both standard approvals and permit2

test/Fees.t.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ contract FeesTest is EulerSwapTestBase {
5858

5959
// Holder's NAV increased by fee amount, plus slightly extra because we are not at curve equilibrium point
6060

61-
assertGt(getHolderNAV(), origNav + int256(amountIn - amountInNoFees));
62-
assertEq(eTST.balanceOf(address(holder)), 10e18 + amountIn);
61+
uint256 protocolFeesCollected = assetTST.balanceOf(address(0));
62+
63+
assertGt(getHolderNAV() + int256(protocolFeesCollected), origNav + int256(amountIn - amountInNoFees));
64+
assertEq(eTST.balanceOf(address(holder)), 10e18 + amountIn - protocolFeesCollected);
6365
assertEq(eTST2.balanceOf(address(holder)), 10e18 - amountOut);
6466
}
6567

@@ -107,8 +109,10 @@ contract FeesTest is EulerSwapTestBase {
107109

108110
// Holder's NAV increased by fee amount, plus slightly extra because we are not at curve equilibrium point
109111

110-
assertGt(getHolderNAV(), origNav + int256(amountIn - amountInNoFees));
111-
assertEq(eTST.balanceOf(address(holder)), 10e18 + amountIn);
112+
uint256 protocolFeesCollected = assetTST.balanceOf(address(0));
113+
114+
assertGt(getHolderNAV() + int256(protocolFeesCollected), origNav + int256(amountIn - amountInNoFees));
115+
assertEq(eTST.balanceOf(address(holder)), 10e18 + amountIn - protocolFeesCollected);
112116
assertEq(eTST2.balanceOf(address(holder)), 10e18 - amountOut);
113117
}
114118
}

0 commit comments

Comments
 (0)