@@ -30,13 +30,10 @@ contract MaglevEulerSwap is MaglevBase {
30
30
_cy = params.cy;
31
31
}
32
32
33
- function verify (uint256 , uint256 , uint256 newReserve0 , uint256 newReserve1 )
34
- internal
35
- view
36
- virtual
37
- override
38
- {
39
- require (verifyCurve (newReserve0, newReserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy), KNotSatisfied ());
33
+ function verify (uint256 , uint256 , uint256 newReserve0 , uint256 newReserve1 ) internal view virtual override {
34
+ require (
35
+ verifyCurve (newReserve0, newReserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy), KNotSatisfied ()
36
+ );
40
37
}
41
38
42
39
uint256 private constant roundingCompensation = 1.0000000000001e18 ;
@@ -48,39 +45,41 @@ contract MaglevEulerSwap is MaglevBase {
48
45
override
49
46
returns (uint256 )
50
47
{
51
- int dx;
52
- int dy;
48
+ int256 dx;
49
+ int256 dy;
53
50
54
51
if (exactIn) {
55
- if (asset0IsInput) dx = int (amount);
56
- else dy = int (amount);
52
+ if (asset0IsInput) dx = int256 (amount);
53
+ else dy = int256 (amount);
57
54
} else {
58
- if (asset0IsInput) dy = - int (amount);
59
- else dx = - int (amount);
55
+ if (asset0IsInput) dy = - int256 (amount);
56
+ else dx = - int256 (amount);
60
57
}
61
58
62
59
(dx, dy) = simulateSwap (dx, dy, reserve0, reserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy);
63
60
64
- uint output;
61
+ uint256 output;
65
62
66
63
if (exactIn) {
67
- if (asset0IsInput) output = uint (- dy);
68
- else output = uint (- dx);
64
+ if (asset0IsInput) output = uint256 (- dy);
65
+ else output = uint256 (- dx);
69
66
output = output * 1e18 / roundingCompensation;
70
67
} else {
71
- if (asset0IsInput) output = uint (dx);
72
- else output = uint (dy);
68
+ if (asset0IsInput) output = uint256 (dx);
69
+ else output = uint256 (dy);
73
70
output = output * roundingCompensation / 1e18 ;
74
71
}
75
72
76
73
return output;
77
74
}
78
75
79
-
80
-
81
76
/////
82
77
83
- function fx (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (uint ){
78
+ function fx (uint256 xt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 cy )
79
+ internal
80
+ pure
81
+ returns (uint256 )
82
+ {
84
83
require (xt > 0 , "Reserves must be greater than zero " );
85
84
if (xt <= x0) {
86
85
return fx1 (xt, px, py, x0, y0, cx, cy);
@@ -89,24 +88,37 @@ contract MaglevEulerSwap is MaglevBase {
89
88
}
90
89
}
91
90
92
- function fx1 (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint ) internal pure returns (uint ){
91
+ function fx1 (uint256 xt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 )
92
+ internal
93
+ pure
94
+ returns (uint256 )
95
+ {
93
96
require (xt <= x0, "Invalid input coordinate " );
94
97
return y0 + px * 1e18 / py * (cx * (2 * x0 - xt) / 1e18 + (1e18 - cx) * x0 / 1e18 * x0 / xt - x0) / 1e18 ;
95
98
}
96
99
97
- function fx2 (uint xt , uint px , uint py , uint x0 , uint y0 , uint , uint cy ) internal pure returns (uint ){
100
+ function fx2 (uint256 xt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 , uint256 cy )
101
+ internal
102
+ pure
103
+ returns (uint256 )
104
+ {
98
105
require (xt > x0, "Invalid input coordinate " );
99
106
// intermediate values for solving quadratic equation
100
- uint a = cy;
101
- int b = (int (px) * 1e18 / int (py)) * (int (xt) - int (x0)) / 1e18 + int (y0) * (1e18 - 2 * int (cy)) / 1e18 ;
102
- int c = (int (cy) - 1e18 ) * int (y0)** 2 / 1e18 / 1e18 ;
103
- uint discriminant = uint (int (uint (b** 2 )) - 4 * int (a) * int (c));
104
- uint numerator = uint (- b + int (uint (sqrt (discriminant))));
105
- uint denominator = 2 * a;
107
+ uint256 a = cy;
108
+ int256 b = (int256 (px) * 1e18 / int256 (py)) * (int256 (xt) - int256 (x0)) / 1e18
109
+ + int256 (y0) * (1e18 - 2 * int256 (cy)) / 1e18 ;
110
+ int256 c = (int256 (cy) - 1e18 ) * int256 (y0) ** 2 / 1e18 / 1e18 ;
111
+ uint256 discriminant = uint256 (int256 (uint256 (b ** 2 )) - 4 * int256 (a) * int256 (c));
112
+ uint256 numerator = uint256 (- b + int256 (uint256 (sqrt (discriminant))));
113
+ uint256 denominator = 2 * a;
106
114
return numerator * 1e18 / denominator;
107
115
}
108
116
109
- function fy (uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (uint ){
117
+ function fy (uint256 yt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 cy )
118
+ internal
119
+ pure
120
+ returns (uint256 )
121
+ {
110
122
require (yt > 0 , "Reserves must be greater than zero " );
111
123
if (yt <= y0) {
112
124
return fx1 (yt, py, px, y0, x0, cy, cx);
@@ -115,51 +127,84 @@ contract MaglevEulerSwap is MaglevBase {
115
127
}
116
128
}
117
129
118
-
119
- function simulateSwap (int dx , int dy , uint xt , uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (int , int ) {
120
- int xtNew = int (xt);
121
- int ytNew = int (yt);
130
+ function simulateSwap (
131
+ int256 dx ,
132
+ int256 dy ,
133
+ uint256 xt ,
134
+ uint256 yt ,
135
+ uint256 px ,
136
+ uint256 py ,
137
+ uint256 x0 ,
138
+ uint256 y0 ,
139
+ uint256 cx ,
140
+ uint256 cy
141
+ ) internal pure returns (int256 , int256 ) {
142
+ int256 xtNew = int256 (xt);
143
+ int256 ytNew = int256 (yt);
122
144
123
145
if (dx != 0 ) {
124
146
xtNew += dx;
125
- ytNew = int (fx (uint (xtNew), px, py, x0, y0, cx, cy));
147
+ ytNew = int256 (fx (uint256 (xtNew), px, py, x0, y0, cx, cy));
126
148
}
127
149
if (dy != 0 ) {
128
150
ytNew += dy;
129
- xtNew = int (fy (uint (ytNew), px, py, x0, y0, cx, cy));
151
+ xtNew = int256 (fy (uint256 (ytNew), px, py, x0, y0, cx, cy));
130
152
}
131
- dx = xtNew - int (xt);
132
- dy = ytNew - int (yt);
153
+ dx = xtNew - int256 (xt);
154
+ dy = ytNew - int256 (yt);
133
155
134
156
return (dx, dy);
135
157
}
136
158
137
- function verifyCurve (uint xt , uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (bool ){
138
- int delta = 0 ;
159
+ function verifyCurve (uint256 xt , uint256 yt , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 cy )
160
+ internal
161
+ pure
162
+ returns (bool )
163
+ {
164
+ int256 delta = 0 ;
139
165
140
166
if (xt >= x0) {
141
- delta = int (xt) - int (fy (yt, px, py, x0, y0, cx, cy));
167
+ delta = int256 (xt) - int256 (fy (yt, px, py, x0, y0, cx, cy));
142
168
} else {
143
- delta = int (yt) - int (fx (xt, px, py, x0, y0, cx, cy));
169
+ delta = int256 (yt) - int256 (fx (xt, px, py, x0, y0, cx, cy));
144
170
}
145
-
171
+
146
172
// if distance is > zero, then point is above the curve, and invariant passes
147
173
return (delta >= 0 );
148
174
}
149
175
150
176
// EIP-7054
151
177
function sqrt (uint256 x ) internal pure returns (uint128 ) {
152
- if (x == 0 ) return 0 ;
153
- else {
178
+ if (x == 0 ) {
179
+ return 0 ;
180
+ } else {
154
181
uint256 xx = x;
155
182
uint256 r = 1 ;
156
- if (xx >= 0x100000000000000000000000000000000 ) { xx >>= 128 ; r <<= 64 ; }
157
- if (xx >= 0x10000000000000000 ) { xx >>= 64 ; r <<= 32 ; }
158
- if (xx >= 0x100000000 ) { xx >>= 32 ; r <<= 16 ; }
159
- if (xx >= 0x10000 ) { xx >>= 16 ; r <<= 8 ; }
160
- if (xx >= 0x100 ) { xx >>= 8 ; r <<= 4 ; }
161
- if (xx >= 0x10 ) { xx >>= 4 ; r <<= 2 ; }
162
- if (xx >= 0x8 ) { r <<= 1 ; }
183
+ if (xx >= 0x100000000000000000000000000000000 ) {
184
+ xx >>= 128 ;
185
+ r <<= 64 ;
186
+ }
187
+ if (xx >= 0x10000000000000000 ) {
188
+ xx >>= 64 ;
189
+ r <<= 32 ;
190
+ }
191
+ if (xx >= 0x100000000 ) {
192
+ xx >>= 32 ;
193
+ r <<= 16 ;
194
+ }
195
+ if (xx >= 0x10000 ) {
196
+ xx >>= 16 ;
197
+ r <<= 8 ;
198
+ }
199
+ if (xx >= 0x100 ) {
200
+ xx >>= 8 ;
201
+ r <<= 4 ;
202
+ }
203
+ if (xx >= 0x10 ) {
204
+ xx >>= 4 ;
205
+ r <<= 2 ;
206
+ }
207
+ if (xx >= 0x8 ) r <<= 1 ;
163
208
r = (r + x / r) >> 1 ;
164
209
r = (r + x / r) >> 1 ;
165
210
r = (r + x / r) >> 1 ;
@@ -168,7 +213,7 @@ contract MaglevEulerSwap is MaglevBase {
168
213
r = (r + x / r) >> 1 ;
169
214
r = (r + x / r) >> 1 ;
170
215
uint256 r1 = x / r;
171
- return uint128 (r < r1 ? r : r1);
216
+ return uint128 (r < r1 ? r : r1);
172
217
}
173
218
}
174
219
}
0 commit comments