1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity ^ 0.8.24 ;
3
+
4
+ import "forge-std/Test.sol " ;
5
+ import "forge-std/console.sol " ;
6
+ import {EulerSwapTestBase, EulerSwap, TestERC20} from "./EulerSwapTestBase.t.sol " ;
7
+ import {CurveLib} from "../src/CurveLib.sol " ;
8
+
9
+ contract CurveLibTest is EulerSwapTestBase {
10
+ EulerSwap public eulerSwap;
11
+
12
+ function setUp () public virtual override {
13
+ super .setUp ();
14
+ }
15
+
16
+ function test_fuzzfInverse (uint256 x , uint256 px , uint256 py , uint256 x0 , uint256 y0 , uint256 cx , uint256 cy )
17
+ public
18
+ pure
19
+ {
20
+
21
+ // Params
22
+ px = 1e18 ;
23
+ py = bound (py, 1 , 1e36 );
24
+ x0 = bound (x0, 1e2 , 1e28 );
25
+ y0 = bound (y0, 0 , 1e28 );
26
+ cx = bound (cx, 1 , 1e18 );
27
+ cy = bound (cy, 1 , 1e18 );
28
+ console.log ("px " , px);
29
+ console.log ("py " , py);
30
+ console.log ("x0 " , x0);
31
+ console.log ("y0 " , y0);
32
+ console.log ("cx " , cx);
33
+ console.log ("cy " , cy);
34
+
35
+ // Note without -2 in the max bound, f() sometimes fails when x gets too close to centre.
36
+ // Note small x values lead to large y-values, which causes problems for both f() and fInverse(), so we cap it here
37
+ x = bound (x, 1e2 - 3 , x0 - 3 );
38
+
39
+ uint256 y = CurveLib.f (x, px, py, x0, y0, cx);
40
+ console.log ("y " , y);
41
+ uint256 xCalc = CurveLib.fInverse (y, px, py, x0, y0, cx);
42
+ console.log ("xCalc " , xCalc);
43
+ uint256 yCalc = CurveLib.f (xCalc, px, py, x0, y0, cx);
44
+ uint256 xBin = binarySearch (y, px, py, x0, y0, cx, 1 , x0);
45
+ uint256 yBin = CurveLib.f (xBin, px, py, x0, y0, cx);
46
+ console.log ("x " , x);
47
+ console.log ("xCalc " , xCalc);
48
+ console.log ("xBin " , xBin);
49
+ console.log ("y " , y);
50
+ console.log ("yCalc " , yCalc);
51
+ console.log ("yBin " , yBin);
52
+
53
+ if (x < type (uint112 ).max && y < type (uint112 ).max) {
54
+ assert (CurveLib.verify (xCalc, y, x0, y0, px, py, cx, cy));
55
+ assert (int256 (xCalc) - int256 (xBin) <= 3 || int256 (yCalc) - int256 (yBin) <= 3 ); // suspect this is 2 wei error in fInverse() + 1 wei error in f()
56
+ }
57
+ }
58
+
59
+ }
0 commit comments