From 2d1ce226cb38fb3f074b55d9c88ce14ec341c6db Mon Sep 17 00:00:00 2001 From: viktorking7 <140458814+viktorking7@users.noreply.github.com> Date: Tue, 11 Nov 2025 14:33:48 +0100 Subject: [PATCH] Update ERC4626.sol --- contracts/token/ERC20/extensions/ERC4626.sol | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/contracts/token/ERC20/extensions/ERC4626.sol b/contracts/token/ERC20/extensions/ERC4626.sol index 93a84862b78..50f065dfce3 100644 --- a/contracts/token/ERC20/extensions/ERC4626.sol +++ b/contracts/token/ERC20/extensions/ERC4626.sol @@ -295,8 +295,17 @@ abstract contract ERC4626 is ERC20, IERC4626 { // shares are burned and after the assets are transferred, which is a valid state. _burn(owner, shares); _transferOut(receiver, assets); + uint256 withdrawnAssets = assets; + if (totalSupply() == 0) { + uint256 remainingAssets = IERC20(asset()).balanceOf(address(this)); + if (remainingAssets > 0) { + // Sweep residual assets so a donation or rounding remainder does not strand funds in the vault. + _transferOut(receiver, remainingAssets); + withdrawnAssets += remainingAssets; + } + } - emit Withdraw(caller, receiver, owner, assets, shares); + emit Withdraw(caller, receiver, owner, withdrawnAssets, shares); } /// @dev Performs a transfer in of underlying assets. The default implementation uses `SafeERC20`. Used by {_deposit}.