Skip to content

Commit 6a95354

Browse files
committed
add working fuzz test
1 parent cd64cb2 commit 6a95354

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/CurveLib.sol

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ library CurveLib {
113113
}
114114

115115
function binarySearch(
116-
uint256 y,
117-
uint256 px,
118-
uint256 py,
119-
uint256 x0,
120-
uint256 y0,
121-
uint256 c,
116+
IEulerSwap.Params memory p,
117+
uint256 newReserve1,
118+
// uint256 y,
119+
// uint256 px,
120+
// uint256 py,
121+
// uint256 x0,
122+
// uint256 y0,
123+
// uint256 c,
122124
uint256 xMin,
123125
uint256 xMax
124126
) internal pure returns (uint256) {
@@ -127,14 +129,14 @@ library CurveLib {
127129
}
128130
while (xMin < xMax) {
129131
uint256 xMid = (xMin + xMax) / 2;
130-
uint256 fxMid = f(xMid, px, py, x0, y0, c);
131-
if (y >= fxMid) {
132+
uint256 fxMid = f(xMid, p.priceX, p.priceY, p.equilibriumReserve0, p.equilibriumReserve1, p.concentrationX);
133+
if (newReserve1 >= fxMid) {
132134
xMax = xMid;
133135
} else {
134136
xMin = xMid + 1;
135137
}
136138
}
137-
if (y < f(xMin, px, py, x0, y0, c)) {
139+
if (newReserve1 < f(xMin, p.priceX, p.priceY, p.equilibriumReserve0, p.equilibriumReserve1, p.concentrationX)) {
138140
xMin += 1;
139141
}
140142
return xMin;

test/CurveLib.t.sol

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity ^0.8.24;
44
import "forge-std/Test.sol";
55
import "forge-std/console.sol";
66
import {EulerSwapTestBase, EulerSwap, TestERC20} from "./EulerSwapTestBase.t.sol";
7+
import {IEulerSwap} from "../src/interfaces/IEulerSwap.sol";
78
import {CurveLib} from "../src/CurveLib.sol";
89

910
contract CurveLibTest is EulerSwapTestBase {
@@ -15,9 +16,8 @@ contract CurveLibTest is EulerSwapTestBase {
1516

1617
function test_fuzzfInverse(uint256 x, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 cx, uint256 cy)
1718
public
18-
pure
19+
view
1920
{
20-
2121
// Params
2222
px = 1e18;
2323
py = bound(py, 1, 1e36);
@@ -32,6 +32,21 @@ contract CurveLibTest is EulerSwapTestBase {
3232
console.log("cx", cx);
3333
console.log("cy", cy);
3434

35+
IEulerSwap.Params memory p = IEulerSwap.Params({
36+
vault0: address(0),
37+
vault1: address(0),
38+
eulerAccount: address(0),
39+
equilibriumReserve0: uint112(x0),
40+
equilibriumReserve1: uint112(y0),
41+
priceX: px,
42+
priceY: py,
43+
concentrationX: cx,
44+
concentrationY: cy,
45+
fee: 0,
46+
protocolFee: 0,
47+
protocolFeeRecipient: address(0)
48+
});
49+
3550
// Note without -2 in the max bound, f() sometimes fails when x gets too close to centre.
3651
// Note small x values lead to large y-values, which causes problems for both f() and fInverse(), so we cap it here
3752
x = bound(x, 1e2 - 3, x0 - 3);
@@ -41,7 +56,7 @@ contract CurveLibTest is EulerSwapTestBase {
4156
uint256 xCalc = CurveLib.fInverse(y, px, py, x0, y0, cx);
4257
console.log("xCalc", xCalc);
4358
uint256 yCalc = CurveLib.f(xCalc, px, py, x0, y0, cx);
44-
uint256 xBin = binarySearch(y, px, py, x0, y0, cx, 1, x0);
59+
uint256 xBin = CurveLib.binarySearch(p, y, 1, x0);
4560
uint256 yBin = CurveLib.f(xBin, px, py, x0, y0, cx);
4661
console.log("x ", x);
4762
console.log("xCalc", xCalc);
@@ -51,9 +66,8 @@ contract CurveLibTest is EulerSwapTestBase {
5166
console.log("yBin ", yBin);
5267

5368
if (x < type(uint112).max && y < type(uint112).max) {
54-
assert(CurveLib.verify(xCalc, y, x0, y0, px, py, cx, cy));
69+
assert(CurveLib.verify(p, xCalc, y));
5570
assert(int256(xCalc) - int256(xBin) <= 3 || int256(yCalc) - int256(yBin) <= 3); // suspect this is 2 wei error in fInverse() + 1 wei error in f()
5671
}
5772
}
58-
59-
}
73+
}

0 commit comments

Comments
 (0)