Skip to content

Commit 1cd30ba

Browse files
authored
Merge pull request #21 from euler-xyz/renaming
Renaming & contracts cleaning
2 parents 4f031d4 + 4eabe47 commit 1cd30ba

11 files changed

+293
-410
lines changed

TODO

Lines changed: 0 additions & 40 deletions
This file was deleted.

docs/architecture.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ The EulerSwap contract tracks what it believes the reserves to be by caching the
5151

5252
### **1. Core contracts**
5353

54-
#### **Maglev contract**
54+
#### **EulerSwap contract**
5555

56-
The `Maglev` contract is the core of EulerSwap and is responsible for:
56+
The `EulerSwap` contract is the core of EulerSwap and is responsible for:
5757

5858
- Managing liquidity reserves.
5959
- Executing token swaps based on the EulerSwap curve.
@@ -76,20 +76,20 @@ The `Maglev` contract is the core of EulerSwap and is responsible for:
7676

7777
### **2. Periphery contracts**
7878

79-
#### **MaglevPeriphery contract**
79+
#### **EulerSwapPeriphery contract**
8080

81-
The `MaglevPeriphery` contract extends the functionality of the core Maglev contract by providing:
81+
The `EulerSwapPeriphery` contract extends the functionality of the core EulerSwap contract by providing:
8282

8383
- **Swap price quotations** before execution.
8484
- **Liquidity checks** to ensure solvency before transactions.
8585
- **Binary search mechanisms** for dynamic price calculation.
8686

8787
##### **Key functions**
8888

89-
- `quoteExactInput(address maglev, address tokenIn, address tokenOut, uint256 amountIn)`: Estimates the output amount for a given input.
90-
- `quoteExactOutput(address maglev, address tokenIn, address tokenOut, uint256 amountOut)`: Estimates the required input amount to receive a specified output.
91-
- `computeQuote(IMaglev maglev, address tokenIn, address tokenOut, uint256 amount, bool exactIn)`: A high-level function to compute swaps while enforcing fee multipliers.
92-
- `binarySearch(IMaglev maglev, uint112 reserve0, uint112 reserve1, uint256 amount, bool exactIn, bool asset0IsInput)`: Uses binary search to determine an optimal swap amount along the curve.
89+
- `quoteExactInput(address eulerSwap, address tokenIn, address tokenOut, uint256 amountIn)`: Estimates the output amount for a given input.
90+
- `quoteExactOutput(address eulerSwap, address tokenIn, address tokenOut, uint256 amountOut)`: Estimates the required input amount to receive a specified output.
91+
- `computeQuote(IEulerSwap eulerSwap, address tokenIn, address tokenOut, uint256 amount, bool exactIn)`: A high-level function to compute swaps while enforcing fee multipliers.
92+
- `binarySearch(IEulerSwap eulerSwap, uint112 reserve0, uint112 reserve1, uint256 amount, bool exactIn, bool asset0IsInput)`: Uses binary search to determine an optimal swap amount along the curve.
9393

9494
### **3. Vault integration**
9595

@@ -110,8 +110,8 @@ EulerSwap integrates with **Ethereum Vault Connector (EVC)** to enable collatera
110110

111111
EulerSwap’s architecture is designed for **efficient, secure, and collateral-backed trading** with a custom **swapping curve**. The system leverages:
112112

113-
- **Maglev** as the core AMM contract.
114-
- **MaglevPeriphery** for auxiliary quoting and validations.
113+
- **EulerSwap** as the core AMM contract.
114+
- **EulerSwapPeriphery** for auxiliary quoting and validations.
115115
- **Ethereum Vault Connector (EVC)** for collateralized vault management.
116116
- **Security-focused design** to prevent vulnerabilities in asset handling.
117117

src/Maglev.sol renamed to src/EulerSwap.sol

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.27;
33

4-
import {EVCUtil} from "evc/utils/EVCUtil.sol";
54
import {IEVC} from "evc/interfaces/IEthereumVaultConnector.sol";
65
import {IEVault, IERC20, IBorrowing, IERC4626, IRiskManager} from "evk/EVault/IEVault.sol";
76
import {IUniswapV2Callee} from "./interfaces/IUniswapV2Callee.sol";
8-
import {IMaglev} from "./interfaces/IMaglev.sol";
7+
import {IEulerSwap} from "./interfaces/IEulerSwap.sol";
8+
import {EVCUtil} from "evc/utils/EVCUtil.sol";
9+
10+
contract EulerSwap is IEulerSwap, EVCUtil {
11+
struct Params {
12+
address evc;
13+
address vault0;
14+
address vault1;
15+
address myAccount;
16+
uint112 debtLimit0;
17+
uint112 debtLimit1;
18+
uint256 fee;
19+
}
20+
21+
struct CurveParams {
22+
uint256 priceX;
23+
uint256 priceY;
24+
uint256 concentrationX;
25+
uint256 concentrationY;
26+
}
927

10-
contract Maglev is IMaglev, EVCUtil {
1128
bytes32 public constant curve = keccak256("EulerSwap v1");
1229

1330
address public immutable vault0;
@@ -30,15 +47,7 @@ contract Maglev is IMaglev, EVCUtil {
3047
uint112 public reserve1;
3148
uint32 public status; // 0 = unactivated, 1 = unlocked, 2 = locked
3249

33-
error Locked();
34-
error Overflow();
35-
error BadFee();
36-
error DifferentEVC();
37-
error AssetsOutOfOrderOrEqual();
38-
error CurveViolation();
39-
40-
event MaglevCreated(address indexed maglev, address indexed asset0, address indexed asset1);
41-
50+
event EulerSwapCreated(address indexed eulerSwap, address indexed asset0, address indexed asset1);
4251
event Swap(
4352
address indexed sender,
4453
uint256 amount0In,
@@ -50,6 +59,13 @@ contract Maglev is IMaglev, EVCUtil {
5059
address indexed to
5160
);
5261

62+
error Locked();
63+
error Overflow();
64+
error BadFee();
65+
error DifferentEVC();
66+
error AssetsOutOfOrderOrEqual();
67+
error CurveViolation();
68+
5369
modifier nonReentrant() {
5470
if (status == 0) activate();
5571
require(status == 1, Locked());
@@ -58,25 +74,8 @@ contract Maglev is IMaglev, EVCUtil {
5874
status = 1;
5975
}
6076

61-
struct Params {
62-
address evc;
63-
address vault0;
64-
address vault1;
65-
address myAccount;
66-
uint112 debtLimit0;
67-
uint112 debtLimit1;
68-
uint256 fee;
69-
}
70-
71-
struct CurveParams {
72-
uint256 priceX;
73-
uint256 priceY;
74-
uint256 concentrationX;
75-
uint256 concentrationY;
76-
}
77-
7877
constructor(Params memory params, CurveParams memory curveParams) EVCUtil(params.evc) {
79-
// Maglev params
78+
// EulerSwap params
8079

8180
require(params.fee < 1e18, BadFee());
8281

@@ -106,38 +105,10 @@ contract Maglev is IMaglev, EVCUtil {
106105
concentrationX = curveParams.concentrationX;
107106
concentrationY = curveParams.concentrationY;
108107

109-
emit MaglevCreated(address(this), asset0Addr, asset1Addr);
110-
}
111-
112-
/// @inheritdoc IMaglev
113-
function activate() public {
114-
require(status != 2, Locked());
115-
status = 1;
116-
117-
IERC20(asset0).approve(vault0, type(uint256).max);
118-
IERC20(asset1).approve(vault1, type(uint256).max);
119-
120-
IEVC(evc).enableCollateral(myAccount, vault0);
121-
IEVC(evc).enableCollateral(myAccount, vault1);
122-
}
123-
124-
/// @dev EulerSwap curve definition
125-
function f(uint256 xt, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c) internal pure returns (uint256) {
126-
return y0 + px * 1e18 / py * (c * (2 * x0 - xt) / 1e18 + (1e18 - c) * x0 / 1e18 * x0 / xt - x0) / 1e18;
127-
}
128-
129-
/// @inheritdoc IMaglev
130-
function verify(uint256 newReserve0, uint256 newReserve1) public view returns (bool) {
131-
if (newReserve0 >= initialReserve0) {
132-
if (newReserve1 >= initialReserve1) return true;
133-
return newReserve0 >= f(newReserve1, priceY, priceX, initialReserve1, initialReserve0, concentrationY);
134-
} else {
135-
if (newReserve1 < initialReserve1) return false;
136-
return newReserve1 >= f(newReserve0, priceX, priceY, initialReserve0, initialReserve1, concentrationX);
137-
}
108+
emit EulerSwapCreated(address(this), asset0Addr, asset1Addr);
138109
}
139110

140-
/// @inheritdoc IMaglev
111+
/// @inheritdoc IEulerSwap
141112
function swap(uint256 amount0Out, uint256 amount1Out, address to, bytes calldata data)
142113
external
143114
callThroughEVC
@@ -188,29 +159,27 @@ contract Maglev is IMaglev, EVCUtil {
188159
return (reserve0, reserve1, status);
189160
}
190161

191-
// Internal utilities
162+
/// @inheritdoc IEulerSwap
163+
function activate() public {
164+
require(status != 2, Locked());
165+
status = 1;
192166

193-
function myDebt(address vault) internal view returns (uint256) {
194-
return IEVault(vault).debtOf(myAccount);
195-
}
167+
IERC20(asset0).approve(vault0, type(uint256).max);
168+
IERC20(asset1).approve(vault1, type(uint256).max);
196169

197-
function myBalance(address vault) internal view returns (uint256) {
198-
uint256 shares = IEVault(vault).balanceOf(myAccount);
199-
return shares == 0 ? 0 : IEVault(vault).convertToAssets(shares);
170+
IEVC(evc).enableCollateral(myAccount, vault0);
171+
IEVC(evc).enableCollateral(myAccount, vault1);
200172
}
201173

202-
function offsetReserve(uint112 reserve, address vault) internal view returns (uint112) {
203-
uint256 offset;
204-
uint256 debt = myDebt(vault);
205-
206-
if (debt != 0) {
207-
offset = reserve > debt ? reserve - debt : 0;
174+
/// @inheritdoc IEulerSwap
175+
function verify(uint256 newReserve0, uint256 newReserve1) public view returns (bool) {
176+
if (newReserve0 >= initialReserve0) {
177+
if (newReserve1 >= initialReserve1) return true;
178+
return newReserve0 >= f(newReserve1, priceY, priceX, initialReserve1, initialReserve0, concentrationY);
208179
} else {
209-
offset = reserve + myBalance(vault);
180+
if (newReserve1 < initialReserve1) return false;
181+
return newReserve1 >= f(newReserve0, priceX, priceY, initialReserve0, initialReserve1, concentrationX);
210182
}
211-
212-
require(offset <= type(uint112).max, Overflow());
213-
return uint112(offset);
214183
}
215184

216185
function withdrawAssets(address vault, uint256 amount, address to) internal {
@@ -243,4 +212,32 @@ contract Maglev is IMaglev, EVCUtil {
243212
}
244213
}
245214
}
215+
216+
function myDebt(address vault) internal view returns (uint256) {
217+
return IEVault(vault).debtOf(myAccount);
218+
}
219+
220+
function myBalance(address vault) internal view returns (uint256) {
221+
uint256 shares = IEVault(vault).balanceOf(myAccount);
222+
return shares == 0 ? 0 : IEVault(vault).convertToAssets(shares);
223+
}
224+
225+
function offsetReserve(uint112 reserve, address vault) internal view returns (uint112) {
226+
uint256 offset;
227+
uint256 debt = myDebt(vault);
228+
229+
if (debt != 0) {
230+
offset = reserve > debt ? reserve - debt : 0;
231+
} else {
232+
offset = reserve + myBalance(vault);
233+
}
234+
235+
require(offset <= type(uint112).max, Overflow());
236+
return uint112(offset);
237+
}
238+
239+
/// @dev EulerSwap curve definition
240+
function f(uint256 xt, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c) internal pure returns (uint256) {
241+
return y0 + px * 1e18 / py * (c * (2 * x0 - xt) / 1e18 + (1e18 - c) * x0 / 1e18 * x0 / xt - x0) / 1e18;
242+
}
246243
}

0 commit comments

Comments
 (0)