Skip to content

Commit f38db55

Browse files
committed
fix tests, handle error condition
1 parent d20ca38 commit f38db55

File tree

7 files changed

+15
-13
lines changed

7 files changed

+15
-13
lines changed

src/EulerSwap.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ contract EulerSwap is IEulerSwap, EVCUtil {
292292
}
293293

294294
/// @dev EulerSwap curve definition
295-
/// Pre-conditions: x <= x0, 1 <= {px,py} <= 1e36, {x0,y0} <= type(uint112).max, c <= 1e18
295+
/// Pre-conditions: 0 < x <= x0, 1 <= {px,py} <= 1e36, {x0,y0} <= type(uint112).max, c <= 1e18
296296
function f(uint256 x, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c) internal pure returns (uint256) {
297297
unchecked {
298298
uint256 v = Math.mulDiv(px * (x0 - x), c * x + (1e18 - c) * x0, x * 1e18, Math.Rounding.Ceil);

src/EulerSwapPeriphery.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
1515
error SwapLimitExceeded();
1616
error AmountOutLessThanMin();
1717
error AmountInMoreThanMax();
18+
error Overflow();
1819

1920
/// @inheritdoc IEulerSwapPeriphery
2021
function swapExactIn(address eulerSwap, address tokenIn, address tokenOut, uint256 amountIn, uint256 amountOutMin)
@@ -230,9 +231,6 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
230231

231232
//////////////////////////////
232233

233-
234-
// Exact version starts here.
235-
// Strategy is to re-calculate everything using f() and fInverse() and see where things break.
236234
function search(
237235
IEulerSwap eulerSwap,
238236
uint112 reserve0,
@@ -280,6 +278,7 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
280278
// exact out
281279
if (asset0IsInput) {
282280
// swap Y out and X in
281+
require(reserve1 > amount, SwapLimitExceeded());
283282
yNew = reserve1 - amount;
284283
if (yNew < y0) {
285284
// remain on g()
@@ -291,6 +290,7 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
291290
output = xNew > reserve0 ? xNew - reserve0 : 0;
292291
} else {
293292
// swap X out and Y in
293+
require(reserve0 > amount, SwapLimitExceeded());
294294
xNew = reserve0 - amount;
295295
if (xNew < x0) {
296296
// remain on f()
@@ -305,11 +305,11 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
305305
}
306306

307307
/// @dev EulerSwap curve definition
308-
/// Pre-conditions: x <= x0, 1 <= {px,py} <= 1e36, {x0,y0} <= type(uint112).max, c <= 1e18
308+
/// Pre-conditions: 0 < x <= x0, 1 <= {px,py} <= 1e36, {x0,y0} <= type(uint112).max, c <= 1e18
309309
function f(uint256 x, uint256 px, uint256 py, uint256 x0, uint256 y0, uint256 c) internal pure returns (uint256) {
310310
unchecked {
311311
uint256 v = Math.mulDiv(px * (x0 - x), c * x + (1e18 - c) * x0, x * 1e18, Math.Rounding.Ceil);
312-
require(v <= type(uint248).max, "HELP");
312+
require(v <= type(uint248).max, Overflow());
313313
return y0 + (v + (py - 1)) / py;
314314
}
315315
}

test/AltDecimals.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ contract AltDecimals is EulerSwapTestBase {
2222
assetTST.transfer(address(eulerSwap), amount);
2323

2424
{
25-
uint256 qPlus = q + 1;
25+
uint256 qPlus = q + MAX_QUOTE_ERROR + 1;
2626
vm.expectRevert();
2727
eulerSwap.swap(0, qPlus, address(this), "");
2828
}
@@ -62,7 +62,7 @@ contract AltDecimals is EulerSwapTestBase {
6262
assetTST.transfer(address(eulerSwap), amount);
6363

6464
{
65-
uint256 qPlus = q + 1;
65+
uint256 qPlus = q + MAX_QUOTE_ERROR + 1;
6666
vm.expectRevert();
6767
eulerSwap.swap(0, qPlus, address(this), "");
6868
}

test/EulerSwapTest.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ contract EulerSwapTest is EulerSwapTestBase {
159159
t1.transfer(address(eulerSwap), amount);
160160

161161
{
162-
uint256 qPlus = q + 1;
162+
uint256 qPlus = q + MAX_QUOTE_ERROR + 1;
163163
vm.expectRevert();
164164
if (dir) eulerSwap.swap(0, qPlus, address(this), "");
165165
else eulerSwap.swap(qPlus, 0, address(this), "");

test/EulerSwapTestBase.t.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {HookMiner} from "v4-periphery/src/utils/HookMiner.sol";
1212
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
1313

1414
contract EulerSwapTestBase is EVaultTestBase {
15+
uint256 public constant MAX_QUOTE_ERROR = 4;
16+
1517
address public depositor = makeAddr("depositor");
1618
address public creator = makeAddr("creator");
1719
address public holder = makeAddr("holder");

test/Fees.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ contract FeesTest is EulerSwapTestBase {
4141
// Pulling out one extra reverts...
4242

4343
vm.expectRevert(EulerSwap.CurveViolation.selector);
44-
eulerSwap.swap(0, amountOut + 1, address(this), "");
44+
eulerSwap.swap(0, amountOut + MAX_QUOTE_ERROR + 1, address(this), "");
4545

4646
// Just right:
4747

@@ -92,7 +92,7 @@ contract FeesTest is EulerSwapTestBase {
9292
// Pulling out one extra reverts...
9393

9494
vm.expectRevert(EulerSwap.CurveViolation.selector);
95-
eulerSwap.swap(0, amountOut + 1, address(this), "");
95+
eulerSwap.swap(0, amountOut + MAX_QUOTE_ERROR + 1, address(this), "");
9696

9797
// Just right:
9898

test/PreserveNav.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ contract PreserveNav is EulerSwapTestBase {
4747
t1.transfer(address(eulerSwap), amount1);
4848

4949
{
50-
uint256 qPlus = q + 1;
50+
uint256 qPlus = q + MAX_QUOTE_ERROR + 1;
5151
vm.expectRevert();
5252
if (dir1) eulerSwap.swap(0, qPlus, address(this), "");
5353
else eulerSwap.swap(qPlus, 0, address(this), "");
@@ -71,7 +71,7 @@ contract PreserveNav is EulerSwapTestBase {
7171
t1.transfer(address(eulerSwap), amount2);
7272

7373
{
74-
uint256 qPlus = q + 1;
74+
uint256 qPlus = q + MAX_QUOTE_ERROR + 1;
7575
vm.expectRevert();
7676
if (dir2) eulerSwap.swap(0, qPlus, address(this), "");
7777
else eulerSwap.swap(qPlus, 0, address(this), "");

0 commit comments

Comments
 (0)