Skip to content

Commit 18b9165

Browse files
committed
improve and simplify fee split calculation, change feeMultiplier to just fee
1 parent efe5487 commit 18b9165

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

src/EulerSwap.sol

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@ 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-
2219
address public immutable vault0;
2320
address public immutable vault1;
2421
address public immutable asset0;
2522
address public immutable asset1;
2623
address public immutable eulerAccount;
2724
uint112 public immutable equilibriumReserve0;
2825
uint112 public immutable equilibriumReserve1;
29-
uint256 public immutable feeMultiplier;
30-
uint256 private immutable feeMultiplierProtocol;
31-
uint256 private immutable feeMultiplierLP;
26+
uint256 public immutable fee;
27+
uint256 public immutable protocolFee;
28+
address public immutable protocolFeeRecipient;
3229

3330
uint256 public immutable priceX;
3431
uint256 public immutable priceY;
@@ -88,9 +85,9 @@ contract EulerSwap is IEulerSwap, EVCUtil {
8885
equilibriumReserve1 = params.equilibriumReserve1;
8986
reserve0 = params.currReserve0;
9087
reserve1 = params.currReserve1;
91-
feeMultiplier = 1e18 - params.fee;
92-
feeMultiplierProtocol = 1e18 - (params.fee * protocolFee / 1e18);
93-
feeMultiplierLP = feeMultiplier * 1e18 / feeMultiplierProtocol;
88+
fee = params.fee;
89+
protocolFee = 0.1e18;
90+
protocolFeeRecipient = address(0);
9491

9592
// Curve params
9693

@@ -226,10 +223,16 @@ contract EulerSwap is IEulerSwap, EVCUtil {
226223
uint256 amount = IERC20(asset).balanceOf(address(this));
227224
if (amount == 0) return 0;
228225

226+
uint256 feeAmount = amount * fee / 1e18;
227+
229228
{
230-
uint256 feeAmount = amount - (amount * feeMultiplierProtocol / 1e18);
231-
IERC20(asset).transfer(protocolFeeRecipient, feeAmount);
232-
amount -= feeAmount;
229+
uint256 protocolFeeAmount = feeAmount * protocolFee / 1e18;
230+
231+
if (protocolFeeAmount != 0) {
232+
IERC20(asset).transfer(protocolFeeRecipient, protocolFeeAmount);
233+
amount -= protocolFeeAmount;
234+
feeAmount -= protocolFeeAmount;
235+
}
233236
}
234237

235238
uint256 deposited;
@@ -257,7 +260,7 @@ contract EulerSwap is IEulerSwap, EVCUtil {
257260
deposited += amount;
258261
}
259262

260-
return (deposited * feeMultiplierLP + (1e18 - 1)) / 1e18;
263+
return deposited > feeAmount ? deposited - feeAmount : 0;
261264
}
262265

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

src/EulerSwapFactory.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
2323
address indexed asset1,
2424
address vault0,
2525
address vault1,
26-
uint256 indexed feeMultiplier,
26+
uint256 indexed fee,
2727
address eulerAccount,
2828
uint256 reserve0,
2929
uint256 reserve1,
@@ -70,7 +70,7 @@ contract EulerSwapFactory is IEulerSwapFactory, EVCUtil {
7070
pool.asset1(),
7171
params.vault0,
7272
params.vault1,
73-
pool.feeMultiplier(),
73+
pool.fee(),
7474
params.eulerAccount,
7575
params.currReserve0,
7676
params.currReserve1,

src/EulerSwapPeriphery.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
105105
);
106106
require(amount <= type(uint112).max, SwapLimitExceeded());
107107

108-
uint256 feeMultiplier = eulerSwap.feeMultiplier();
108+
uint256 fee = eulerSwap.fee();
109109
(uint112 reserve0, uint112 reserve1,) = eulerSwap.getReserves();
110110

111-
// exactIn: decrease received amountIn, rounding down
112-
if (exactIn) amount = amount * feeMultiplier / 1e18;
111+
// exactIn: decrease effective amountIn
112+
if (exactIn) amount = amount - (amount * fee / 1e18);
113113

114114
bool asset0IsInput = checkTokens(eulerSwap, tokenIn, tokenOut);
115115
(uint256 inLimit, uint256 outLimit) = calcLimits(eulerSwap, asset0IsInput);
@@ -124,8 +124,8 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
124124
require(amount <= outLimit && quote <= inLimit, SwapLimitExceeded());
125125
}
126126

127-
// exactOut: increase required quote(amountIn), rounding up
128-
if (!exactIn) quote = (quote * 1e18 + (feeMultiplier - 1)) / feeMultiplier;
127+
// exactOut: inflate required amountIn
128+
if (!exactIn) quote = (quote * 1e18) / (1e18 - fee);
129129

130130
return quote;
131131
}

src/interfaces/IEulerSwap.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ interface IEulerSwap {
5050
function eulerAccount() external view returns (address);
5151
function equilibriumReserve0() external view returns (uint112);
5252
function equilibriumReserve1() external view returns (uint112);
53-
function feeMultiplier() external view returns (uint256);
53+
function fee() external view returns (uint256);
54+
function protocolFee() external view returns (uint256);
55+
function protocolFeeRecipient() external view returns (address);
5456
/// @notice Returns the current reserves of the pool
5557
/// @return reserve0 The amount of asset0 in the pool
5658
/// @return reserve1 The amount of asset1 in the pool

0 commit comments

Comments
 (0)