@@ -12,6 +12,8 @@ contract MaglevEulerSwap is MaglevBase {
12
12
uint256 public _cy;
13
13
14
14
error KNotSatisfied ();
15
+ error ReservesZero ();
16
+ error InvalidInputCoordinate ();
15
17
16
18
struct EulerSwapParams {
17
19
uint256 px;
@@ -51,7 +53,7 @@ contract MaglevEulerSwap is MaglevBase {
51
53
view
52
54
virtual
53
55
override
54
- returns (uint256 )
56
+ returns (uint256 output )
55
57
{
56
58
int256 dx;
57
59
int256 dy;
@@ -64,9 +66,22 @@ contract MaglevEulerSwap is MaglevBase {
64
66
else dx = - int256 (amount);
65
67
}
66
68
67
- (dx, dy) = simulateSwap (dx, dy, reserve0, reserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy);
68
-
69
- uint256 output;
69
+ {
70
+ int256 reserve0New = int256 (uint256 (reserve0));
71
+ int256 reserve1New = int256 (uint256 (reserve1));
72
+
73
+ if (dx != 0 ) {
74
+ reserve0New += dx;
75
+ reserve1New = int256 (fx (uint256 (reserve0New), _px, _py, initialReserve0, initialReserve1, _cx, _cy));
76
+ }
77
+ if (dy != 0 ) {
78
+ reserve1New += dy;
79
+ reserve0New = int256 (fy (uint256 (reserve1New), _px, _py, initialReserve0, initialReserve1, _cx, _cy));
80
+ }
81
+
82
+ dx = reserve0New - int256 (uint256 (reserve0));
83
+ dy = reserve1New - int256 (uint256 (reserve1));
84
+ }
70
85
71
86
if (exactIn) {
72
87
if (asset0IsInput) output = uint256 (- dy);
@@ -77,8 +92,6 @@ contract MaglevEulerSwap is MaglevBase {
77
92
else output = uint256 (dy);
78
93
output = output * roundingCompensation / 1e18 ;
79
94
}
80
-
81
- return output;
82
95
}
83
96
84
97
/////
@@ -88,20 +101,33 @@ contract MaglevEulerSwap is MaglevBase {
88
101
pure
89
102
returns (uint256 )
90
103
{
91
- require (xt > 0 , " Reserves must be greater than zero " );
104
+ require (xt > 0 , ReservesZero () );
92
105
if (xt <= x0) {
93
106
return fx1 (xt, px, py, x0, y0, cx, cy);
94
107
} else {
95
108
return fx2 (xt, px, py, x0, y0, cx, cy);
96
109
}
97
110
}
98
111
112
+ function fy (uint256 yt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 cy )
113
+ internal
114
+ pure
115
+ returns (uint256 )
116
+ {
117
+ require (yt > 0 , ReservesZero ());
118
+ if (yt <= y0) {
119
+ return fx1 (yt, py, px, y0, x0, cy, cx);
120
+ } else {
121
+ return fx2 (yt, py, px, y0, x0, cy, cx);
122
+ }
123
+ }
124
+
99
125
function fx1 (uint256 xt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 )
100
126
internal
101
127
pure
102
128
returns (uint256 )
103
129
{
104
- require (xt <= x0, " Invalid input coordinate " );
130
+ require (xt <= x0, InvalidInputCoordinate () );
105
131
return y0 + px * 1e18 / py * (cx * (2 * x0 - xt) / 1e18 + (1e18 - cx) * x0 / 1e18 * x0 / xt - x0) / 1e18 ;
106
132
}
107
133
@@ -110,7 +136,7 @@ contract MaglevEulerSwap is MaglevBase {
110
136
pure
111
137
returns (uint256 )
112
138
{
113
- require (xt > x0, " Invalid input coordinate " );
139
+ require (xt > x0, InvalidInputCoordinate () );
114
140
// intermediate values for solving quadratic equation
115
141
uint256 a = cy;
116
142
int256 b = (int256 (px) * 1e18 / int256 (py)) * (int256 (xt) - int256 (x0)) / 1e18
@@ -121,46 +147,4 @@ contract MaglevEulerSwap is MaglevBase {
121
147
uint256 denominator = 2 * a;
122
148
return numerator * 1e18 / denominator;
123
149
}
124
-
125
- function fy (uint256 yt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 cy )
126
- internal
127
- pure
128
- returns (uint256 )
129
- {
130
- require (yt > 0 , "Reserves must be greater than zero " );
131
- if (yt <= y0) {
132
- return fx1 (yt, py, px, y0, x0, cy, cx);
133
- } else {
134
- return fx2 (yt, py, px, y0, x0, cy, cx);
135
- }
136
- }
137
-
138
- function simulateSwap (
139
- int256 dx ,
140
- int256 dy ,
141
- uint256 xt ,
142
- uint256 yt ,
143
- uint256 px ,
144
- uint256 py ,
145
- uint256 x0 ,
146
- uint256 y0 ,
147
- uint256 cx ,
148
- uint256 cy
149
- ) internal pure returns (int256 , int256 ) {
150
- int256 xtNew = int256 (xt);
151
- int256 ytNew = int256 (yt);
152
-
153
- if (dx != 0 ) {
154
- xtNew += dx;
155
- ytNew = int256 (fx (uint256 (xtNew), px, py, x0, y0, cx, cy));
156
- }
157
- if (dy != 0 ) {
158
- ytNew += dy;
159
- xtNew = int256 (fy (uint256 (ytNew), px, py, x0, y0, cx, cy));
160
- }
161
- dx = xtNew - int256 (xt);
162
- dy = ytNew - int256 (yt);
163
-
164
- return (dx, dy);
165
- }
166
150
}
0 commit comments