Skip to content

Commit 83d9026

Browse files
committed
validate input token against output tokens
if one of them match input token it must expect output amount to be 0 to avoid roundtrip of that portion
1 parent 5354994 commit 83d9026

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

contracts/BasketSwap.sol

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import "./BasketHelper.sol";
88
* @dev A contract to perform a basket swap of one input currency (ERC20 or native)
99
* into five predefined output ERC20 tokens using Uniswap V2 or V3.
1010
* The input token is now passed as a parameter to each function.
11+
*
12+
* @notice When the input token matches one of the output tokens, users MUST:
13+
* - Set the swap amount for that token position to 0
14+
* - Exclude that amount from the totalAmountIn
15+
* - This ensures the matching token portion remains in the user's wallet
16+
* - Exception: Native ETH can be swapped to WETH (ETH->WETH is allowed)
1117
*/
1218
contract BasketSwap is Ownable, ReentrancyGuard {
1319

@@ -33,6 +39,8 @@ contract BasketSwap is Ownable, ReentrancyGuard {
3339
}
3440

3541
function _validateAndPrepareInput(
42+
address _inputToken,
43+
address[5] memory _outputTokens,
3644
uint256 _totalAmountIn,
3745
uint256[5] memory _amountsToSwapForOutputs,
3846
uint256[5] memory _minAmountsOut
@@ -43,9 +51,22 @@ contract BasketSwap is Ownable, ReentrancyGuard {
4351
uint256 calculatedTotalAmountIn = 0;
4452

4553
for (uint256 i = 0; i < 5; i++) {
54+
// Check if input token matches this output token
55+
// Skip this check for native ETH (address(0)) since ETH->WETH is valid
56+
if (_inputToken != address(0) && _inputToken == _outputTokens[i]) {
57+
require(
58+
_amountsToSwapForOutputs[i] == 0,
59+
"MUST_SET_AMOUNT_TO_0"
60+
);
61+
}
62+
4663
calculatedTotalAmountIn += _amountsToSwapForOutputs[i];
64+
4765
if (_amountsToSwapForOutputs[i] == 0) {
48-
require(_minAmountsOut[i] == 0, "Min output must be 0 for 0 input");
66+
require(
67+
_minAmountsOut[i] == 0,
68+
"MUST_SET_AMOUNT_TO_0"
69+
);
4970
}
5071
}
5172

@@ -69,6 +90,8 @@ contract BasketSwap is Ownable, ReentrancyGuard {
6990
nonReentrant
7091
{
7192
_validateAndPrepareInput(
93+
_inputToken,
94+
outputTokens,
7295
_totalAmountIn,
7396
_amountsToSwapForOutputs,
7497
_minAmountsOut
@@ -155,6 +178,8 @@ contract BasketSwap is Ownable, ReentrancyGuard {
155178
nonReentrant
156179
{
157180
_validateAndPrepareInput(
181+
_inputToken,
182+
outputTokens,
158183
_totalAmountIn,
159184
_amountsToSwapForOutputs,
160185
_minAmountsOut
@@ -215,6 +240,8 @@ contract BasketSwap is Ownable, ReentrancyGuard {
215240
{
216241
require(msg.value == _totalAmountIn, "Native value sent does not match totalAmountIn");
217242
_validateAndPrepareInput(
243+
address(0),
244+
outputTokens,
218245
_totalAmountIn,
219246
_amountsToSwapForOutputs,
220247
_minAmountsOut
@@ -264,6 +291,8 @@ contract BasketSwap is Ownable, ReentrancyGuard {
264291
nonReentrant
265292
{
266293
_validateAndPrepareInput(
294+
_inputToken,
295+
outputTokens,
267296
_totalAmountIn,
268297
_amountsToSwapForOutputs,
269298
_minAmountsOut
@@ -324,6 +353,8 @@ contract BasketSwap is Ownable, ReentrancyGuard {
324353
{
325354
require(msg.value == _totalAmountIn, "Native value sent does not match totalAmountIn");
326355
_validateAndPrepareInput(
356+
address(0),
357+
outputTokens,
327358
_totalAmountIn,
328359
_amountsToSwapForOutputs,
329360
_minAmountsOut

0 commit comments

Comments
 (0)