@@ -5,18 +5,29 @@ import {console} from "forge-std/Test.sol";
5
5
import {MaglevBase} from "./MaglevBase.sol " ;
6
6
7
7
contract MaglevEulerSwap is MaglevBase {
8
+ uint256 public _px;
9
+ uint256 public _py;
10
+ uint256 public _cx;
11
+ uint256 public _cy;
8
12
9
13
error KNotSatisfied ();
10
14
11
15
struct EulerSwapParams {
12
- uint256 junk;
16
+ uint256 px;
17
+ uint256 py;
18
+ uint256 cx;
19
+ uint256 cy;
13
20
}
14
21
15
22
constructor (BaseParams memory baseParams , EulerSwapParams memory params ) MaglevBase (baseParams) {
16
23
setEulerSwapParams (params);
17
24
}
18
25
19
26
function setEulerSwapParams (EulerSwapParams memory params ) public onlyOwner {
27
+ _px = params.px;
28
+ _py = params.py;
29
+ _cx = params.cx;
30
+ _cy = params.cy;
20
31
}
21
32
22
33
function verify (uint256 , uint256 , uint256 newReserve0 , uint256 newReserve1 )
@@ -25,33 +36,51 @@ contract MaglevEulerSwap is MaglevBase {
25
36
virtual
26
37
override
27
38
{
28
- uint256 px = 1e18 ;
29
- uint256 py = 1e18 ;
30
- uint256 cx = 0.40e18 ;
31
- uint256 cy = 0.85e18 ;
32
-
33
- //require(_verify(49e18, 51e18, px, py, 50e18, 50e18, cx, cy), KNotSatisfied());
34
-
35
- console.log ("QQ " , newReserve0, newReserve1);
36
- console.log ("ZZ " , initialReserve0, initialReserve1);
37
- require (_verify (newReserve0, newReserve1, px, py, initialReserve0, initialReserve1, cx, cy), KNotSatisfied ());
39
+ require (verifyCurve (newReserve0, newReserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy), KNotSatisfied ());
38
40
}
39
41
40
- function computeQuote (uint256 , bool , bool )
42
+ uint256 private constant roundingCompensation = 1.0000000000001e18 ;
43
+
44
+ function computeQuote (uint256 amount , bool exactIn , bool asset0IsInput )
41
45
internal
42
46
view
43
47
virtual
44
48
override
45
49
returns (uint256 )
46
50
{
47
- return 0 ;
51
+ int dx;
52
+ int dy;
53
+
54
+ if (exactIn) {
55
+ if (asset0IsInput) dx = int (amount);
56
+ else dy = int (amount);
57
+ } else {
58
+ if (asset0IsInput) dy = - int (amount);
59
+ else dx = - int (amount);
60
+ }
61
+
62
+ (dx, dy) = simulateSwap (dx, dy, reserve0, reserve1, _px, _py, initialReserve0, initialReserve1, _cx, _cy);
63
+
64
+ uint output;
65
+
66
+ if (exactIn) {
67
+ if (asset0IsInput) output = uint (- dy);
68
+ else output = uint (- dx);
69
+ output = output * 1e18 / roundingCompensation;
70
+ } else {
71
+ if (asset0IsInput) output = uint (dx);
72
+ else output = uint (dy);
73
+ output = output * roundingCompensation / 1e18 ;
74
+ }
75
+
76
+ return output;
48
77
}
49
78
50
79
51
80
52
81
/////
53
82
54
- function fx (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) public pure returns (uint ){
83
+ function fx (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (uint ){
55
84
require (xt > 0 , "Reserves must be greater than zero " );
56
85
if (xt <= x0) {
57
86
return fx1 (xt, px, py, x0, y0, cx, cy);
@@ -60,12 +89,12 @@ contract MaglevEulerSwap is MaglevBase {
60
89
}
61
90
}
62
91
63
- function fx1 (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) public pure returns (uint ){
92
+ function fx1 (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint ) internal pure returns (uint ){
64
93
require (xt <= x0, "Invalid input coordinate " );
65
94
return y0 + px * 1e18 / py * (cx * (2 * x0 - xt) / 1e18 + (1e18 - cx) * x0 / 1e18 * x0 / xt - x0) / 1e18 ;
66
95
}
67
96
68
- function fx2 (uint xt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) public pure returns (uint ){
97
+ function fx2 (uint xt , uint px , uint py , uint x0 , uint y0 , uint , uint cy ) internal pure returns (uint ){
69
98
require (xt > x0, "Invalid input coordinate " );
70
99
// intermediate values for solving quadratic equation
71
100
uint a = cy;
@@ -77,7 +106,7 @@ contract MaglevEulerSwap is MaglevBase {
77
106
return numerator * 1e18 / denominator;
78
107
}
79
108
80
- function fy (uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) public pure returns (uint ){
109
+ function fy (uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (uint ){
81
110
require (yt > 0 , "Reserves must be greater than zero " );
82
111
if (yt <= y0) {
83
112
return fx1 (yt, py, px, y0, x0, cy, cx);
@@ -87,7 +116,7 @@ contract MaglevEulerSwap is MaglevBase {
87
116
}
88
117
89
118
90
- function swap (int dx , int dy , uint xt , uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) public pure returns (int , int ) {
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 ) {
91
120
int xtNew = int (xt);
92
121
int ytNew = int (yt);
93
122
@@ -102,33 +131,24 @@ contract MaglevEulerSwap is MaglevBase {
102
131
dx = xtNew - int (xt);
103
132
dy = ytNew - int (yt);
104
133
105
- // // check invariant
106
- // let invariantPassed = invariantCheck(xtNew, ytNew, parameters);
107
-
108
134
return (dx, dy);
109
135
}
110
136
111
- function _verify (uint xt , uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) public pure returns (bool ){
112
- bool passed = false ;
137
+ function verifyCurve (uint xt , uint yt , uint px , uint py , uint x0 , uint y0 , uint cx , uint cy ) internal pure returns (bool ){
113
138
int delta = 0 ;
114
- if (xt >= x0) {
139
+
140
+ if (xt >= x0) {
115
141
delta = int (xt) - int (fy (yt, px, py, x0, y0, cx, cy));
116
- console.log ("xt: " , int (xt));
117
- console.log ("fy: " , int (fy (yt, px, py, x0, y0, cx, cy)));
118
142
} else {
119
143
delta = int (yt) - int (fx (xt, px, py, x0, y0, cx, cy));
120
- console.log ("yt: " , int (yt));
121
- console.log ("fx: " , int (fx (xt, px, py, x0, y0, cx, cy)));
122
144
}
123
145
124
- if (delta >= 0 ) {
125
- // if distance is > zero, then point is above the curve, and invariant passes
126
- passed = true ;
127
- }
128
- return passed;
146
+ // if distance is > zero, then point is above the curve, and invariant passes
147
+ return (delta >= 0 );
129
148
}
130
149
131
- function sqrt (uint256 x ) public pure returns (uint128 ) {
150
+ // EIP-7054
151
+ function sqrt (uint256 x ) internal pure returns (uint128 ) {
132
152
if (x == 0 ) return 0 ;
133
153
else {
134
154
uint256 xx = x;
0 commit comments