Skip to content

Commit ca01cee

Browse files
committed
add error handling for zero swap amount in GenericSwap
1 parent 0eaf592 commit ca01cee

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

contracts/GenericSwap.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ contract GenericSwap is IGenericSwap, TokenCollector, EIP712 {
6666
) private returns (uint256 returnAmount) {
6767
if (_swapData.expiry < block.timestamp) revert ExpiredOrder();
6868
if (_swapData.recipient == address(0)) revert ZeroAddress();
69+
if (_swapData.takerTokenAmount == 0) revert SwapWithZeroAmount();
6970

7071
address _inputToken = _swapData.takerToken;
7172
address _outputToken = _swapData.makerToken;

contracts/interfaces/IGenericSwap.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ interface IGenericSwap {
5454
/// @dev This error is used to ensure that a valid address is provided.
5555
error ZeroAddress();
5656

57+
/// @notice Error to be thrown when a swap amount is zero.
58+
/// @dev This error is used to ensure that a valid, nonzero swap amount is provided.
59+
error SwapWithZeroAmount();
60+
5761
/// @notice Executes a swap using provided swap data and taker token permit.
5862
/// @param swapData The swap data containing details of the swap.
5963
/// @param takerTokenPermit The permit for spending taker's tokens.

snapshots/GenericSwap.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"executeSwap(): testGenericSwapWithUniswap": "248116",
3-
"executeSwap(): testLeaveOneWeiWithMultipleUsers(the first deposit)": "248116",
4-
"executeSwap(): testLeaveOneWeiWithMultipleUsers(the second deposit)": "204470",
5-
"executeSwap(): testSwapWithETHInput": "97797",
6-
"executeSwap(): testSwapWithETHOutput": "127948",
7-
"executeSwap(): testSwapWithLessOutputButWithinTolerance": "157404",
8-
"executeSwapWithSig(): testGenericSwapRelayed": "279447"
2+
"executeSwap(): testGenericSwapWithUniswap": "248141",
3+
"executeSwap(): testLeaveOneWeiWithMultipleUsers(the first deposit)": "248141",
4+
"executeSwap(): testLeaveOneWeiWithMultipleUsers(the second deposit)": "204495",
5+
"executeSwap(): testSwapWithETHInput": "97813",
6+
"executeSwap(): testSwapWithETHOutput": "127973",
7+
"executeSwap(): testSwapWithLessOutputButWithinTolerance": "157429",
8+
"executeSwapWithSig(): testGenericSwapRelayed": "279472"
99
}

test/forkMainnet/GenericSwap.t.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ contract GenericSwapTest is Test, Tokens, BalanceUtil, Permit2Helper, SigHelper
321321
vm.stopPrank();
322322
}
323323

324+
function testCannotSwapWithZeroAmount() public {
325+
GenericSwapData memory gsData = defaultGSData;
326+
gsData.takerTokenAmount = 0;
327+
328+
vm.startPrank(taker);
329+
vm.expectRevert(IGenericSwap.SwapWithZeroAmount.selector);
330+
genericSwap.executeSwap(gsData, defaultTakerPermit);
331+
vm.stopPrank();
332+
}
333+
324334
function testGenericSwapRelayed() public {
325335
Snapshot memory takerTakerToken = BalanceSnapshot.take({ owner: taker, token: defaultGSData.takerToken });
326336
Snapshot memory takerMakerToken = BalanceSnapshot.take({ owner: taker, token: defaultGSData.makerToken });

0 commit comments

Comments
 (0)