Skip to content

Commit

Permalink
fix: increase allowance when close to max (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Picodes authored May 30, 2023
1 parent f046564 commit c2ed861
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions contracts/BaseRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,7 @@ abstract contract BaseRouter is Initializable {
// Approve transfer to the `uniswapV3Router`
// Since this router is supposed to be a trusted contract, we can leave the allowance to the token
address uniRouter = address(uniswapV3Router);
uint256 currentAllowance = IERC20(inToken).allowance(address(this), uniRouter);
if (currentAllowance < amount)
IERC20(inToken).safeIncreaseAllowance(uniRouter, type(uint256).max - currentAllowance);
_changeAllowance(IERC20(inToken), uniRouter, type(uint256).max);
amountOut = IUniswapV3Router(uniRouter).exactInput(
ExactInputParams(path, address(this), block.timestamp, amount, minAmountOut)
);
Expand Down Expand Up @@ -573,7 +571,9 @@ abstract contract BaseRouter is Initializable {
/// @param amount Amount allowed
function _changeAllowance(IERC20 token, address spender, uint256 amount) internal {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < amount) {
// In case `currentAllowance < type(uint256).max / 2` and we want to increase it:
// Do nothing (to handle tokens that need reapprovals to 0 and save gas)
if (currentAllowance < amount && currentAllowance < type(uint256).max / 2) {
token.safeIncreaseAllowance(spender, amount - currentAllowance);
} else if (currentAllowance > amount) {
token.safeDecreaseAllowance(spender, currentAllowance - amount);
Expand Down

0 comments on commit c2ed861

Please sign in to comment.