Skip to content

Commit

Permalink
optimize allowances
Browse files Browse the repository at this point in the history
  • Loading branch information
1kresh committed Aug 12, 2024
1 parent 668bcd9 commit d2eccb7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/contracts/DefaultCollateralMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {IDefaultCollateralMigrator} from "src/interfaces/IDefaultCollateralMigra
import {IDefaultCollateral} from "@symbiotic/collateral/interfaces/defaultCollateral/IDefaultCollateral.sol";
import {IVault} from "@symbiotic/core/interfaces/vault/IVault.sol";

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract DefaultCollateralMigrator is IDefaultCollateralMigrator {
using SafeERC20 for IERC20;

/**
* @inheritdoc IDefaultCollateralMigrator
*/
Expand All @@ -23,7 +25,9 @@ contract DefaultCollateralMigrator is IDefaultCollateralMigrator {

address asset = IDefaultCollateral(collateral).asset();
amount = IERC20(asset).balanceOf(address(this));
IERC20(asset).approve(vault, amount);
if (IERC20(asset).allowance(address(this), vault) < amount) {
IERC20(asset).forceApprove(vault, type(uint256).max);
}
return IVault(vault).deposit(onBehalfOf, amount);
}
}
9 changes: 9 additions & 0 deletions test/DefaultCollateralMigrator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ contract DefaultCollateralMigratorTest is Test {
);

assertEq(vault.balanceOf(address(this)), amount);
assertEq(
IERC20(collateral.asset()).allowance(address(defaultCollateralMigrator), address(vault)), type(uint256).max
);
}

function test_MigrateFeeOnTransferToken(uint256 amount) public {
Expand Down Expand Up @@ -263,6 +266,12 @@ contract DefaultCollateralMigratorTest is Test {
);

assertEq(vaultFeeOnTransfer.balanceOf(address(this)), amount - 2);
assertEq(
IERC20(feeOnTransferCollateral.asset()).allowance(
address(defaultCollateralMigrator), address(vaultFeeOnTransfer)
),
type(uint256).max
);
}

function _getVault(uint48 epochDuration) internal returns (Vault) {
Expand Down

0 comments on commit d2eccb7

Please sign in to comment.