Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,13 @@ contract Router is
zrc20ToTokenName[intentInfo.zrc20]
);

// Validate that swap result is not greater than expected amount
require(wantedAmountWithTip >= settlementInfo.amountWithTipOut, "Swap returned invalid amount");

// Calculate slippage difference and adjust tip accordingly
uint256 slippageAndFeeCost = wantedAmountWithTip - settlementInfo.amountWithTipOut;
// If swap returns more than expected (surplus), slippageAndFeeCost will be 0
// Note: Surplus amounts are currently ignored as they are too small to handle
// This will be addressed in future updates
uint256 slippageAndFeeCost = wantedAmountWithTip > settlementInfo.amountWithTipOut
? wantedAmountWithTip - settlementInfo.amountWithTipOut
: 0;

// Check if tip covers the slippage and fee costs
if (wantedTip > slippageAndFeeCost) {
Expand Down
9 changes: 5 additions & 4 deletions test/Router.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ contract RouterTest is Test {
// even better with an explicit check of the settlement payload
}

function test_OnCall_InvalidSwapAmountOut() public {
function test_OnCall_SwapSurplusHandling() public {
// Setup intent contract
uint256 sourceChainId = 1;
uint256 targetChainId = 2;
Expand Down Expand Up @@ -1421,7 +1421,7 @@ contract RouterTest is Test {
// Set modest slippage (5%)
swapModule.setSlippage(500);

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

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

// Call onCall
// Call onCall - should succeed even with surplus
vm.prank(address(gateway));
vm.expectRevert("Swap returned invalid amount");
router.onCall(context, address(inputToken), amount + tip, intentPayloadBytes);

// Test passes if no revert occurs - surplus is handled gracefully
}

function test_OnCall_UsesCustomGasLimitFromPayload() public {
Expand Down
Loading