Skip to content

Commit f9efe92

Browse files
committed
fmt
1 parent 5bff386 commit f9efe92

File tree

2 files changed

+100
-60
lines changed

2 files changed

+100
-60
lines changed

src/MaglevEulerSwap.sol

Lines changed: 98 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ contract MaglevEulerSwap is MaglevBase {
3030
_cy = params.cy;
3131
}
3232

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+
);
4037
}
4138

4239
uint256 private constant roundingCompensation = 1.0000000000001e18;
@@ -48,39 +45,41 @@ contract MaglevEulerSwap is MaglevBase {
4845
override
4946
returns (uint256)
5047
{
51-
int dx;
52-
int dy;
48+
int256 dx;
49+
int256 dy;
5350

5451
if (exactIn) {
55-
if (asset0IsInput) dx = int(amount);
56-
else dy = int(amount);
52+
if (asset0IsInput) dx = int256(amount);
53+
else dy = int256(amount);
5754
} else {
58-
if (asset0IsInput) dy = -int(amount);
59-
else dx = -int(amount);
55+
if (asset0IsInput) dy = -int256(amount);
56+
else dx = -int256(amount);
6057
}
6158

6259
(dx, dy) = simulateSwap(dx, dy, reserve0, reserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy);
6360

64-
uint output;
61+
uint256 output;
6562

6663
if (exactIn) {
67-
if (asset0IsInput) output = uint(-dy);
68-
else output = uint(-dx);
64+
if (asset0IsInput) output = uint256(-dy);
65+
else output = uint256(-dx);
6966
output = output * 1e18 / roundingCompensation;
7067
} else {
71-
if (asset0IsInput) output = uint(dx);
72-
else output = uint(dy);
68+
if (asset0IsInput) output = uint256(dx);
69+
else output = uint256(dy);
7370
output = output * roundingCompensation / 1e18;
7471
}
7572

7673
return output;
7774
}
7875

79-
80-
8176
/////
8277

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+
{
8483
require(xt > 0, "Reserves must be greater than zero");
8584
if (xt <= x0) {
8685
return fx1(xt, px, py, x0, y0, cx, cy);
@@ -89,24 +88,37 @@ contract MaglevEulerSwap is MaglevBase {
8988
}
9089
}
9190

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+
{
9396
require(xt <= x0, "Invalid input coordinate");
9497
return y0 + px * 1e18 / py * (cx * (2 * x0 - xt) / 1e18 + (1e18 - cx) * x0 / 1e18 * x0 / xt - x0) / 1e18;
9598
}
9699

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+
{
98105
require(xt > x0, "Invalid input coordinate");
99106
// 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;
106114
return numerator * 1e18 / denominator;
107115
}
108116

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+
{
110122
require(yt > 0, "Reserves must be greater than zero");
111123
if (yt <= y0) {
112124
return fx1(yt, py, px, y0, x0, cy, cx);
@@ -115,51 +127,84 @@ contract MaglevEulerSwap is MaglevBase {
115127
}
116128
}
117129

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);
122144

123145
if (dx != 0) {
124146
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));
126148
}
127149
if (dy != 0) {
128150
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));
130152
}
131-
dx = xtNew - int(xt);
132-
dy = ytNew - int(yt);
153+
dx = xtNew - int256(xt);
154+
dy = ytNew - int256(yt);
133155

134156
return (dx, dy);
135157
}
136158

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;
139165

140166
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));
142168
} 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));
144170
}
145-
171+
146172
// if distance is > zero, then point is above the curve, and invariant passes
147173
return (delta >= 0);
148174
}
149175

150176
// EIP-7054
151177
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 {
154181
uint256 xx = x;
155182
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;
163208
r = (r + x / r) >> 1;
164209
r = (r + x / r) >> 1;
165210
r = (r + x / r) >> 1;
@@ -168,7 +213,7 @@ contract MaglevEulerSwap is MaglevBase {
168213
r = (r + x / r) >> 1;
169214
r = (r + x / r) >> 1;
170215
uint256 r1 = x / r;
171-
return uint128 (r < r1 ? r : r1);
216+
return uint128(r < r1 ? r : r1);
172217
}
173218
}
174219
}

test/EulerSwap.t.sol

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ contract EulerSwapTest is MaglevTestBase {
1717
super.setUp();
1818

1919
vm.prank(owner);
20-
maglev = new Maglev(_getMaglevBaseParams(), Maglev.EulerSwapParams({
21-
px: 1e18,
22-
py: 1e18,
23-
cx: 0.40e18,
24-
cy: 0.85e18
25-
}));
20+
maglev =
21+
new Maglev(_getMaglevBaseParams(), Maglev.EulerSwapParams({px: 1e18, py: 1e18, cx: 0.4e18, cy: 0.85e18}));
2622

2723
vm.prank(holder);
2824
evc.setAccountOperator(holder, address(maglev), true);
@@ -46,7 +42,6 @@ contract EulerSwapTest is MaglevTestBase {
4642
assertEq(assetTST2.balanceOf(address(this)), amountOut);
4743
}
4844

49-
5045
function test_pathIndependent(uint256 amount, bool dir) public {
5146
amount = bound(amount, 0.1e18, 25e18);
5247

0 commit comments

Comments
 (0)