Skip to content

Commit 56be616

Browse files
authored
fix(router): allow surplus from swap when routing intents (#38)
* fix(router): allow surplus from swap when routing intents * fmt
1 parent 9da7ddc commit 56be616

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/Router.sol

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,13 @@ contract Router is
389389
zrc20ToTokenName[intentInfo.zrc20]
390390
);
391391

392-
// Validate that swap result is not greater than expected amount
393-
require(wantedAmountWithTip >= settlementInfo.amountWithTipOut, "Swap returned invalid amount");
394-
395392
// Calculate slippage difference and adjust tip accordingly
396-
uint256 slippageAndFeeCost = wantedAmountWithTip - settlementInfo.amountWithTipOut;
393+
// If swap returns more than expected (surplus), slippageAndFeeCost will be 0
394+
// Note: Surplus amounts are currently ignored as they are too small to handle
395+
// This will be addressed in future updates
396+
uint256 slippageAndFeeCost = wantedAmountWithTip > settlementInfo.amountWithTipOut
397+
? wantedAmountWithTip - settlementInfo.amountWithTipOut
398+
: 0;
397399

398400
// Check if tip covers the slippage and fee costs
399401
if (wantedTip > slippageAndFeeCost) {

test/Router.t.sol

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ contract RouterTest is Test {
13921392
// even better with an explicit check of the settlement payload
13931393
}
13941394

1395-
function test_OnCall_InvalidSwapAmountOut() public {
1395+
function test_OnCall_SwapSurplusHandling() public {
13961396
// Setup intent contract
13971397
uint256 sourceChainId = 1;
13981398
uint256 targetChainId = 2;
@@ -1421,7 +1421,7 @@ contract RouterTest is Test {
14211421
// Set modest slippage (5%)
14221422
swapModule.setSlippage(500);
14231423

1424-
// Set a custom amount out for testing that is more than the wanted amount
1424+
// Set a custom amount out for testing that is more than the wanted amount (surplus)
14251425
swapModule.setCustomAmountOut(amount + tip + 1 ether);
14261426

14271427
// Mock setup for IZRC20 withdrawGasFeeWithGasLimit
@@ -1446,10 +1446,11 @@ contract RouterTest is Test {
14461446
senderEVM: sourceIntentContract
14471447
});
14481448

1449-
// Call onCall
1449+
// Call onCall - should succeed even with surplus
14501450
vm.prank(address(gateway));
1451-
vm.expectRevert("Swap returned invalid amount");
14521451
router.onCall(context, address(inputToken), amount + tip, intentPayloadBytes);
1452+
1453+
// Test passes if no revert occurs - surplus is handled gracefully
14531454
}
14541455

14551456
function test_OnCall_UsesCustomGasLimitFromPayload() public {

0 commit comments

Comments
 (0)