From 0923ce87b82563be6e84b5cdf6c25839c7f5d13e Mon Sep 17 00:00:00 2001 From: mesozoic-technology Date: Sun, 24 Aug 2025 15:35:42 -0400 Subject: [PATCH 1/2] added beforeDeposit and afterWithdrawal hooks --- src/tokens/ERC4626.sol | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tokens/ERC4626.sol b/src/tokens/ERC4626.sol index 0a34ac98..5fb25113 100644 --- a/src/tokens/ERC4626.sol +++ b/src/tokens/ERC4626.sol @@ -47,6 +47,8 @@ abstract contract ERC4626 is ERC20 { // Check for rounding error since we round down in previewDeposit. require((shares = previewDeposit(assets)) != 0, "ZERO_SHARES"); + beforeDeposit(assets, shares); + // Need to transfer before minting or ERC777s could reenter. asset.safeTransferFrom(msg.sender, address(this), assets); @@ -60,6 +62,8 @@ abstract contract ERC4626 is ERC20 { function mint(uint256 shares, address receiver) public virtual returns (uint256 assets) { assets = previewMint(shares); // No need to check for rounding error, previewMint rounds up. + beforeDeposit(assets, shares); + // Need to transfer before minting or ERC777s could reenter. asset.safeTransferFrom(msg.sender, address(this), assets); @@ -90,6 +94,8 @@ abstract contract ERC4626 is ERC20 { emit Withdraw(msg.sender, receiver, owner, assets, shares); asset.safeTransfer(receiver, assets); + + afterWithdraw(assets, shares); } function redeem( @@ -113,6 +119,8 @@ abstract contract ERC4626 is ERC20 { emit Withdraw(msg.sender, receiver, owner, assets, shares); asset.safeTransfer(receiver, assets); + + afterWithdraw(assets, shares); } /*////////////////////////////////////////////////////////////// @@ -177,7 +185,12 @@ abstract contract ERC4626 is ERC20 { INTERNAL HOOKS LOGIC //////////////////////////////////////////////////////////////*/ + function beforeDeposit(uint256 assets, uint256 shares) internal virtual {} + function beforeWithdraw(uint256 assets, uint256 shares) internal virtual {} function afterDeposit(uint256 assets, uint256 shares) internal virtual {} + + function afterWithdraw(uint256 assets, uint256 shares) internal virtual {} + } From 1e9380752019252042b001c67bd39fbfaeca43dd Mon Sep 17 00:00:00 2001 From: mesozoic-technology Date: Sun, 24 Aug 2025 15:38:36 -0400 Subject: [PATCH 2/2] ran npm run lint --- src/test/CREATE3.t.sol | 6 ++---- src/tokens/ERC4626.sol | 1 - src/utils/SafeTransferLib.sol | 6 +++--- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/test/CREATE3.t.sol b/src/test/CREATE3.t.sol index 7ee8fc0d..d49fccd9 100644 --- a/src/test/CREATE3.t.sol +++ b/src/test/CREATE3.t.sol @@ -40,10 +40,8 @@ contract CREATE3Test is DSTestPlus { bytes32 salt = keccak256(bytes("A salt!")); Factory factory = new Factory(); - MockERC20 deployed = MockERC20( - factory.deploy(salt) - ); - + MockERC20 deployed = MockERC20(factory.deploy(salt)); + assertEq(address(deployed), CREATE3.getDeployed(salt, address(factory))); assertTrue(address(deployed) != CREATE3.getDeployed(salt)); diff --git a/src/tokens/ERC4626.sol b/src/tokens/ERC4626.sol index 5fb25113..6d7dfd87 100644 --- a/src/tokens/ERC4626.sol +++ b/src/tokens/ERC4626.sol @@ -192,5 +192,4 @@ abstract contract ERC4626 is ERC20 { function afterDeposit(uint256 assets, uint256 shares) internal virtual {} function afterWithdraw(uint256 assets, uint256 shares) internal virtual {} - } diff --git a/src/utils/SafeTransferLib.sol b/src/utils/SafeTransferLib.sol index 7f8236db..5ba42919 100644 --- a/src/utils/SafeTransferLib.sol +++ b/src/utils/SafeTransferLib.sol @@ -53,7 +53,7 @@ library SafeTransferLib { // Set success to whether the call reverted, if not we check it either // returned exactly 1 (can't just be non-zero data), or had no return data and token has code. if and(iszero(and(eq(mload(0), 1), gt(returndatasize(), 31))), success) { - success := iszero(or(iszero(extcodesize(token)), returndatasize())) + success := iszero(or(iszero(extcodesize(token)), returndatasize())) } } @@ -84,7 +84,7 @@ library SafeTransferLib { // Set success to whether the call reverted, if not we check it either // returned exactly 1 (can't just be non-zero data), or had no return data and token has code. if and(iszero(and(eq(mload(0), 1), gt(returndatasize(), 31))), success) { - success := iszero(or(iszero(extcodesize(token)), returndatasize())) + success := iszero(or(iszero(extcodesize(token)), returndatasize())) } } @@ -115,7 +115,7 @@ library SafeTransferLib { // Set success to whether the call reverted, if not we check it either // returned exactly 1 (can't just be non-zero data), or had no return data and token has code. if and(iszero(and(eq(mload(0), 1), gt(returndatasize(), 31))), success) { - success := iszero(or(iszero(extcodesize(token)), returndatasize())) + success := iszero(or(iszero(extcodesize(token)), returndatasize())) } }