Skip to content

Commit 93d836b

Browse files
authored
Merge pull request #47 from euler-xyz/final-clean
clean
2 parents 84d6939 + c2d071e commit 93d836b

File tree

5 files changed

+27
-74
lines changed

5 files changed

+27
-74
lines changed

src/EulerSwap.sol

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ contract EulerSwap is IEulerSwap, EVCUtil {
152152
}
153153
}
154154

155+
/// @inheritdoc IEulerSwap
155156
function getReserves() external view returns (uint112, uint112, uint32) {
156157
return (reserve0, reserve1, status);
157158
}
158159

159-
/// @notice Returns the address of the Ethereum Vault Connector (EVC) used by this contract.
160-
/// @return The address of the EVC contract.
160+
/// @inheritdoc IEulerSwap
161161
function EVC() external view override(EVCUtil, IEulerSwap) returns (address) {
162162
return address(evc);
163163
}
@@ -189,6 +189,13 @@ contract EulerSwap is IEulerSwap, EVCUtil {
189189
}
190190
}
191191

192+
/// @notice Withdraws assets from a vault, first using available balance and then borrowing if needed
193+
/// @param vault The address of the vault to withdraw from
194+
/// @param amount The total amount of assets to withdraw
195+
/// @param to The address that will receive the withdrawn assets
196+
/// @dev This function first checks if there's an existing balance in the vault.
197+
/// @dev If there is, it withdraws the minimum of the requested amount and available balance.
198+
/// @dev If more assets are needed after withdrawal, it enables the controller and borrows the remaining amount.
192199
function withdrawAssets(address vault, uint256 amount, address to) internal {
193200
uint256 balance = myBalance(vault);
194201

@@ -204,6 +211,14 @@ contract EulerSwap is IEulerSwap, EVCUtil {
204211
}
205212
}
206213

214+
/// @notice Deposits assets into a vault and automatically repays any outstanding debt
215+
/// @param vault The address of the vault to deposit into
216+
/// @param amount The amount of assets to deposit
217+
/// @return The amount of assets successfully deposited
218+
/// @dev This function attempts to deposit assets into the specified vault.
219+
/// @dev If the deposit fails with E_ZeroShares error, it safely returns 0 (this happens with very small amounts).
220+
/// @dev After successful deposit, if the user has any outstanding controller-enabled debt, it attempts to repay it.
221+
/// @dev If all debt is repaid, the controller is automatically disabled to reduce gas costs in future operations.
207222
function depositAssets(address vault, uint256 amount) internal returns (uint256) {
208223
try IEVault(vault).deposit(amount, eulerAccount) {}
209224
catch (bytes memory reason) {
@@ -237,10 +252,16 @@ contract EulerSwap is IEulerSwap, EVCUtil {
237252
}
238253
}
239254

255+
/// @notice Retrieves the current debt amount for the pool's eulerAccount
256+
/// @param vault The address of the vault to check for debt
257+
/// @return The amount of debt that the Euler account has in the specified vault
240258
function myDebt(address vault) internal view returns (uint256) {
241259
return IEVault(vault).debtOf(eulerAccount);
242260
}
243261

262+
/// @notice Calculates the asset balance of the pool's eulerAccount
263+
/// @param vault The address of the vault to check for balance
264+
/// @return The amount of assets that the Euler account has deposited in the specified vault
244265
function myBalance(address vault) internal view returns (uint256) {
245266
uint256 shares = IEVault(vault).balanceOf(eulerAccount);
246267
return shares == 0 ? 0 : IEVault(vault).convertToAssets(shares);

src/EulerSwapPeriphery.sol

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,6 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
6767
return calcLimits(IEulerSwap(eulerSwap), checkTokens(IEulerSwap(eulerSwap), tokenIn, tokenOut));
6868
}
6969

70-
/// @inheritdoc IEulerSwapPeriphery
71-
function fInverse(uint256 y, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c)
72-
external
73-
pure
74-
returns (uint256)
75-
{
76-
// A component of the quadratic formula: a = 2 * c
77-
uint256 A = 2 * c;
78-
79-
// B component of the quadratic formula
80-
int256 B = int256((px * (y - y0) + py - 1) / py) - int256((x0 * (2 * c - 1e18) + 1e18 - 1) / 1e18);
81-
82-
// B^2 component, using FullMath for overflow safety
83-
uint256 absB = B < 0 ? uint256(-B) : uint256(B);
84-
uint256 squaredB = Math.mulDiv(absB, absB, 1e18, Math.Rounding.Ceil);
85-
86-
// 4 * A * C component of the quadratic formula
87-
uint256 AC4 = Math.mulDiv(
88-
Math.mulDiv(4 * c, (1e18 - c), 1e18, Math.Rounding.Ceil),
89-
Math.mulDiv(x0, x0, 1e18, Math.Rounding.Ceil),
90-
1e18,
91-
Math.Rounding.Ceil
92-
);
93-
94-
// Discriminant: b^2 + 4ac, scaled up to maintain precision
95-
uint256 discriminant = (squaredB + AC4) * 1e18;
96-
97-
// Square root of the discriminant (rounded up)
98-
uint256 sqrt = Math.sqrt(discriminant);
99-
sqrt = (sqrt * sqrt < discriminant) ? sqrt + 1 : sqrt;
100-
101-
// Compute and return x = fInverse(y) using the quadratic formula
102-
return Math.mulDiv(uint256(int256(sqrt) - B), 1e18, A, Math.Rounding.Ceil);
103-
}
104-
10570
/// @dev Internal function to execute a token swap through EulerSwap
10671
/// @param eulerSwap The EulerSwap contract address to execute the swap through
10772
/// @param tokenIn The address of the input token being swapped

src/interfaces/IEulerSwap.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ interface IEulerSwap {
5151
function equilibriumReserve0() external view returns (uint112);
5252
function equilibriumReserve1() external view returns (uint112);
5353
function feeMultiplier() external view returns (uint256);
54+
/// @notice Returns the current reserves of the pool
55+
/// @return reserve0 The amount of asset0 in the pool
56+
/// @return reserve1 The amount of asset1 in the pool
57+
/// @return status The status of the pool (0 = unactivated, 1 = unlocked, 2 = locked)
5458
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 status);
5559

5660
// Curve Accessors

src/interfaces/IEulerSwapPeriphery.sol

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,4 @@ interface IEulerSwapPeriphery {
2424

2525
/// @notice Max amount the pool can buy of tokenIn and sell of tokenOut
2626
function getLimits(address eulerSwap, address tokenIn, address tokenOut) external view returns (uint256, uint256);
27-
28-
/**
29-
* @notice Computes the inverse of the `f()` function for the EulerSwap liquidity curve.
30-
* @dev Solves for `x` given `y` using the quadratic formula derived from the liquidity curve:
31-
* x = (-b + sqrt(b^2 + 4ac)) / 2a
32-
* Utilises mulDiv to avoid overflow and ensures precision with upward rounding.
33-
*
34-
* @param y The y-coordinate input value (must be greater than `y0`).
35-
* @param px Price factor for the x-axis (scaled by 1e18, between 1e18 and 1e36).
36-
* @param py Price factor for the y-axis (scaled by 1e18, between 1e18 and 1e36).
37-
* @param x0 Reference x-value on the liquidity curve (≤ 2^112 - 1).
38-
* @param y0 Reference y-value on the liquidity curve (≤ 2^112 - 1).
39-
* @param c Curve parameter shaping liquidity concentration (scaled by 1e18, between 0 and 1e18).
40-
*
41-
* @return x The computed x-coordinate on the liquidity curve.
42-
*
43-
* @custom:precision Uses rounding up to maintain precision in all calculations.
44-
* @custom:safety FullMath handles potential overflow in the b^2 computation.
45-
* @custom:requirement Input `y` must be strictly greater than `y0`; otherwise, the function will revert.
46-
*/
47-
function fInverse(uint256 y, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c)
48-
external
49-
pure
50-
returns (uint256);
5127
}

test/EulerSwapPeriphery.t.sol

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,4 @@ contract EulerSwapPeripheryTest is EulerSwapTestBase {
7777
periphery.swapExactOut(address(eulerSwap), address(assetTST), address(assetTST2), amountOut * 2, amountIn);
7878
vm.stopPrank();
7979
}
80-
81-
function test_fInverseFuzz(uint256 x) public view {
82-
x = bound(x, 2, 50e18 - 1); // note that it fails if 1 used as minimum, not an issue since only used in periphery
83-
uint256 y = eulerSwapHarness.exposedF(x, 1e18, 1e18, 50e18, 50e18, 0.85e18);
84-
uint256 outX = periphery.fInverse(y, 1e18, 1e18, 50e18, 50e18, 0.85e18);
85-
86-
// Ensure x is within the expected range
87-
assertGe(outX, x); // Asserts xOut >= x
88-
assertLe(outX, x + 1); // Asserts xOut <= x + 1
89-
90-
// Alternative using assertApproxEqAbs for absolute difference within 1
91-
assertApproxEqAbs(x, outX, 1);
92-
}
9380
}

0 commit comments

Comments
 (0)