@@ -8,6 +8,23 @@ import {IEulerSwap} from "./interfaces/IEulerSwap.sol";
8
8
import {EVCUtil} from "evc/utils/EVCUtil.sol " ;
9
9
10
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
+ }
27
+
11
28
bytes32 public constant curve = keccak256 ("EulerSwap v1 " );
12
29
13
30
address public immutable vault0;
@@ -30,15 +47,7 @@ contract EulerSwap is IEulerSwap, EVCUtil {
30
47
uint112 public reserve1;
31
48
uint32 public status; // 0 = unactivated, 1 = unlocked, 2 = locked
32
49
33
- error Locked ();
34
- error Overflow ();
35
- error BadFee ();
36
- error DifferentEVC ();
37
- error AssetsOutOfOrderOrEqual ();
38
- error CurveViolation ();
39
-
40
50
event EulerSwapCreated (address indexed eulerSwap , address indexed asset0 , address indexed asset1 );
41
-
42
51
event Swap (
43
52
address indexed sender ,
44
53
uint256 amount0In ,
@@ -50,6 +59,13 @@ contract EulerSwap is IEulerSwap, EVCUtil {
50
59
address indexed to
51
60
);
52
61
62
+ error Locked ();
63
+ error Overflow ();
64
+ error BadFee ();
65
+ error DifferentEVC ();
66
+ error AssetsOutOfOrderOrEqual ();
67
+ error CurveViolation ();
68
+
53
69
modifier nonReentrant () {
54
70
if (status == 0 ) activate ();
55
71
require (status == 1 , Locked ());
@@ -58,23 +74,6 @@ contract EulerSwap is IEulerSwap, EVCUtil {
58
74
status = 1 ;
59
75
}
60
76
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
-
78
77
constructor (Params memory params , CurveParams memory curveParams ) EVCUtil (params.evc) {
79
78
// EulerSwap params
80
79
@@ -109,34 +108,6 @@ contract EulerSwap is IEulerSwap, EVCUtil {
109
108
emit EulerSwapCreated (address (this ), asset0Addr, asset1Addr);
110
109
}
111
110
112
- /// @inheritdoc IEulerSwap
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 IEulerSwap
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
- }
138
- }
139
-
140
111
/// @inheritdoc IEulerSwap
141
112
function swap (uint256 amount0Out , uint256 amount1Out , address to , bytes calldata data )
142
113
external
@@ -188,29 +159,27 @@ contract EulerSwap is IEulerSwap, EVCUtil {
188
159
return (reserve0, reserve1, status);
189
160
}
190
161
191
- // Internal utilities
162
+ /// @inheritdoc IEulerSwap
163
+ function activate () public {
164
+ require (status != 2 , Locked ());
165
+ status = 1 ;
192
166
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);
196
169
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);
200
172
}
201
173
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);
208
179
} else {
209
- offset = reserve + myBalance (vault);
180
+ if (newReserve1 < initialReserve1) return false ;
181
+ return newReserve1 >= f (newReserve0, priceX, priceY, initialReserve0, initialReserve1, concentrationX);
210
182
}
211
-
212
- require (offset <= type (uint112 ).max, Overflow ());
213
- return uint112 (offset);
214
183
}
215
184
216
185
function withdrawAssets (address vault , uint256 amount , address to ) internal {
@@ -243,4 +212,32 @@ contract EulerSwap is IEulerSwap, EVCUtil {
243
212
}
244
213
}
245
214
}
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
+ }
246
243
}
0 commit comments