@@ -60,6 +60,10 @@ contract Router is
60
60
// Default gas limit for withdraw operations
61
61
uint256 private constant DEFAULT_WITHDRAW_GAS_LIMIT = 400000 ;
62
62
63
+ // Min and max gas limits for withdraw operations
64
+ uint256 private constant MIN_WITHDRAW_GAS_LIMIT = 100000 ;
65
+ uint256 private constant MAX_WITHDRAW_GAS_LIMIT = 10000000 ;
66
+
63
67
// Current gas limit for withdraw operations (can be modified by admin)
64
68
uint256 public withdrawGasLimit;
65
69
@@ -198,6 +202,10 @@ contract Router is
198
202
pure
199
203
returns (uint256 )
200
204
{
205
+ // Input validation
206
+ require (decimalsIn <= 30 , "Source decimals too high " );
207
+ require (decimalsOut <= 30 , "Destination decimals too high " );
208
+
201
209
// If decimals are the same, no conversion needed
202
210
if (decimalsIn == decimalsOut) {
203
211
return amountIn;
@@ -206,6 +214,10 @@ contract Router is
206
214
// If destination has more decimals, multiply
207
215
if (decimalsOut > decimalsIn) {
208
216
uint256 scalingFactor = 10 ** (decimalsOut - decimalsIn);
217
+
218
+ // Check for potential overflow before multiplication
219
+ require (amountIn == 0 || (type (uint256 ).max / amountIn) >= scalingFactor, "Decimal conversion overflow " );
220
+
209
221
return amountIn * scalingFactor;
210
222
}
211
223
@@ -364,6 +376,7 @@ contract Router is
364
376
settlementInfo.tipAfterSwap = wantedTip;
365
377
} else {
366
378
// Approve swap module to spend tokens
379
+ IERC20 (intentInfo.zrc20).approve (swapModule, 0 );
367
380
IERC20 (intentInfo.zrc20).approve (swapModule, intentInfo.amountWithTip);
368
381
369
382
// Perform swap through swap module
@@ -415,6 +428,7 @@ contract Router is
415
428
bytes memory settlementPayload
416
429
) internal {
417
430
// Transfer tokens to the target Intent contract
431
+ IERC20 (zrc20).approve (intentContract, 0 );
418
432
IERC20 (zrc20).approve (intentContract, amount);
419
433
420
434
// Create a MessageContext
@@ -458,7 +472,9 @@ contract Router is
458
472
});
459
473
460
474
// Approve gateway to spend tokens
475
+ IERC20 (targetZRC20).approve (gateway, 0 );
461
476
IERC20 (targetZRC20).approve (gateway, amount);
477
+ IERC20 (gasZRC20).approve (gateway, 0 );
462
478
IERC20 (gasZRC20).approve (gateway, gasFee);
463
479
464
480
// Call gateway to withdraw and call intent contract
@@ -675,7 +691,8 @@ contract Router is
675
691
* @param newGasLimit The new gas limit to set
676
692
*/
677
693
function setWithdrawGasLimit (uint256 newGasLimit ) public onlyRole (DEFAULT_ADMIN_ROLE) {
678
- require (newGasLimit > 0 , "Gas limit cannot be zero " );
694
+ require (newGasLimit >= MIN_WITHDRAW_GAS_LIMIT, "Gas limit below minimum " );
695
+ require (newGasLimit <= MAX_WITHDRAW_GAS_LIMIT, "Gas limit above maximum " );
679
696
emit WithdrawGasLimitUpdated (withdrawGasLimit, newGasLimit);
680
697
withdrawGasLimit = newGasLimit;
681
698
}
@@ -686,7 +703,8 @@ contract Router is
686
703
* @param gasLimit The gas limit to set
687
704
*/
688
705
function setChainWithdrawGasLimit (uint256 chainId , uint256 gasLimit ) public onlyRole (DEFAULT_ADMIN_ROLE) {
689
- require (gasLimit > 0 , "Gas limit cannot be zero " );
706
+ require (gasLimit >= MIN_WITHDRAW_GAS_LIMIT, "Gas limit below minimum " );
707
+ require (gasLimit <= MAX_WITHDRAW_GAS_LIMIT, "Gas limit above maximum " );
690
708
chainWithdrawGasLimits[chainId] = gasLimit;
691
709
emit ChainWithdrawGasLimitSet (chainId, gasLimit);
692
710
}
0 commit comments