Skip to content

Commit 54c1af6

Browse files
committed
correct quoting for exact output swaps when point is above curve (chainsecurity finding 5.4.3)
1 parent 93d836b commit 54c1af6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/EulerSwapPeriphery.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ contract EulerSwapPeriphery is IEulerSwapPeriphery {
189189
if (asset0IsInput) output = uint256(-dy);
190190
else output = uint256(-dx);
191191
} else {
192-
if (asset0IsInput) output = uint256(dx);
193-
else output = uint256(dy);
192+
if (asset0IsInput) output = dx >= 0 ? uint256(dx) : 0;
193+
else output = dy >= 0 ? uint256(dy) : 0;
194194
}
195195
}
196196

test/Limits.t.sol

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,35 @@ contract LimitsTest is EulerSwapTestBase {
115115
vm.expectRevert(EulerSwap.AmountTooBig.selector);
116116
eulerSwap.swap(0, type(uint256).max, address(this), "");
117117
}
118+
119+
function test_quoteWhenAboveCurve() public {
120+
// Donate 100 and 100 to the pool, raising the reserves above the curve
121+
assetTST.mint(depositor, 100e18);
122+
assetTST2.mint(depositor, 100e18);
123+
vm.prank(depositor);
124+
assetTST.transfer(address(eulerSwap), 10e18);
125+
vm.prank(depositor);
126+
assetTST2.transfer(address(eulerSwap), 10e18);
127+
eulerSwap.swap(0, 0, address(this), "");
128+
129+
uint256 amount;
130+
131+
// Exact output quotes: Costs nothing to perform this swap (in theory the quote could
132+
// be negative, but this is not supported by the interface)
133+
134+
amount = periphery.quoteExactOutput(address(eulerSwap), address(assetTST), address(assetTST2), 1e18);
135+
assertEq(amount, 0);
136+
137+
amount = periphery.quoteExactOutput(address(eulerSwap), address(assetTST2), address(assetTST), 1e18);
138+
assertEq(amount, 0);
139+
140+
// Exact input quotes: The additional extractable value is provided as swap output, even
141+
// with tiny quotes such as 1 wei.
142+
143+
amount = periphery.quoteExactInput(address(eulerSwap), address(assetTST), address(assetTST2), 1);
144+
assertApproxEqAbs(amount, 19.8e18, 0.1e18);
145+
146+
amount = periphery.quoteExactInput(address(eulerSwap), address(assetTST2), address(assetTST), 1);
147+
assertApproxEqAbs(amount, 19.8e18, 0.1e18);
148+
}
118149
}

0 commit comments

Comments
 (0)