Skip to content

Commit 3a50c37

Browse files
committed
cleanup
1 parent 8d10376 commit 3a50c37

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

src/MaglevEulerSwap.sol

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ contract MaglevEulerSwap is MaglevBase {
1212
uint256 public _cy;
1313

1414
error KNotSatisfied();
15+
error ReservesZero();
16+
error InvalidInputCoordinate();
1517

1618
struct EulerSwapParams {
1719
uint256 px;
@@ -51,7 +53,7 @@ contract MaglevEulerSwap is MaglevBase {
5153
view
5254
virtual
5355
override
54-
returns (uint256)
56+
returns (uint256 output)
5557
{
5658
int256 dx;
5759
int256 dy;
@@ -64,9 +66,22 @@ contract MaglevEulerSwap is MaglevBase {
6466
else dx = -int256(amount);
6567
}
6668

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+
}
7085

7186
if (exactIn) {
7287
if (asset0IsInput) output = uint256(-dy);
@@ -77,8 +92,6 @@ contract MaglevEulerSwap is MaglevBase {
7792
else output = uint256(dy);
7893
output = output * roundingCompensation / 1e18;
7994
}
80-
81-
return output;
8295
}
8396

8497
/////
@@ -88,20 +101,33 @@ contract MaglevEulerSwap is MaglevBase {
88101
pure
89102
returns (uint256)
90103
{
91-
require(xt > 0, "Reserves must be greater than zero");
104+
require(xt > 0, ReservesZero());
92105
if (xt <= x0) {
93106
return fx1(xt, px, py, x0, y0, cx, cy);
94107
} else {
95108
return fx2(xt, px, py, x0, y0, cx, cy);
96109
}
97110
}
98111

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+
99125
function fx1(uint256 xt, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 cx, uint256)
100126
internal
101127
pure
102128
returns (uint256)
103129
{
104-
require(xt <= x0, "Invalid input coordinate");
130+
require(xt <= x0, InvalidInputCoordinate());
105131
return y0 + px * 1e18 / py * (cx * (2 * x0 - xt) / 1e18 + (1e18 - cx) * x0 / 1e18 * x0 / xt - x0) / 1e18;
106132
}
107133

@@ -110,7 +136,7 @@ contract MaglevEulerSwap is MaglevBase {
110136
pure
111137
returns (uint256)
112138
{
113-
require(xt > x0, "Invalid input coordinate");
139+
require(xt > x0, InvalidInputCoordinate());
114140
// intermediate values for solving quadratic equation
115141
uint256 a = cy;
116142
int256 b = (int256(px) * 1e18 / int256(py)) * (int256(xt) - int256(x0)) / 1e18
@@ -121,46 +147,4 @@ contract MaglevEulerSwap is MaglevBase {
121147
uint256 denominator = 2 * a;
122148
return numerator * 1e18 / denominator;
123149
}
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-
}
166150
}

test/EulerSwap.t.sol

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,23 @@ contract EulerSwapTest is MaglevTestBase {
3030
maglev.setDebtLimit(50e18, 50e18);
3131
}
3232

33-
function test_basicSwap() public monotonicHolderNAV {
33+
function test_basicSwap_exactIn() public monotonicHolderNAV {
3434
uint256 amountIn = 1e18;
3535
uint256 amountOut = maglev.quoteExactInput(address(assetTST), address(assetTST2), amountIn);
36+
assertApproxEqAbs(amountOut, 0.9974e18, 0.0001e18);
37+
38+
assetTST.mint(address(this), amountIn);
39+
40+
assetTST.transfer(address(maglev), amountIn);
41+
maglev.swap(0, amountOut, address(this), "");
42+
43+
assertEq(assetTST2.balanceOf(address(this)), amountOut);
44+
}
45+
46+
function test_basicSwap_exactOut() public monotonicHolderNAV {
47+
uint256 amountOut = 1e18;
48+
uint256 amountIn = maglev.quoteExactOutput(address(assetTST), address(assetTST2), amountOut);
49+
assertApproxEqAbs(amountIn, 1.0025e18, 0.0001e18);
3650

3751
assetTST.mint(address(this), amountIn);
3852

0 commit comments

Comments
 (0)