Skip to content

Commit 51ab591

Browse files
authored
ERC4626: Allow overriding underlying assets transfer mechanisms (#5970)
1 parent 4cead2a commit 51ab591

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

.changeset/spotty-plums-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
`ERC4626`: Allow overriding underlying assets transfer mechanisms through new internal virtual functions (`_transferIn` and `_transferOut`).

contracts/token/ERC20/extensions/ERC4626.sol

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ abstract contract ERC4626 is ERC20, IERC4626 {
267267
// Conclusion: we need to do the transfer before we mint so that any reentrancy would happen before the
268268
// assets are transferred and before the shares are minted, which is a valid state.
269269
// slither-disable-next-line reentrancy-no-eth
270-
SafeERC20.safeTransferFrom(IERC20(asset()), caller, address(this), assets);
270+
_transferIn(caller, assets);
271271
_mint(receiver, shares);
272272

273273
emit Deposit(caller, receiver, assets, shares);
@@ -294,11 +294,21 @@ abstract contract ERC4626 is ERC20, IERC4626 {
294294
// Conclusion: we need to do the transfer after the burn so that any reentrancy would happen after the
295295
// shares are burned and after the assets are transferred, which is a valid state.
296296
_burn(owner, shares);
297-
SafeERC20.safeTransfer(IERC20(asset()), receiver, assets);
297+
_transferOut(receiver, assets);
298298

299299
emit Withdraw(caller, receiver, owner, assets, shares);
300300
}
301301

302+
/// @dev Performs a transfer in of underlying assets. The default implementation uses `SafeERC20`. Used by {_deposit}.
303+
function _transferIn(address from, uint256 assets) internal virtual {
304+
SafeERC20.safeTransferFrom(IERC20(asset()), from, address(this), assets);
305+
}
306+
307+
/// @dev Performs a transfer out of underlying assets. The default implementation uses `SafeERC20`. Used by {_withdraw}.
308+
function _transferOut(address to, uint256 assets) internal virtual {
309+
SafeERC20.safeTransfer(IERC20(asset()), to, assets);
310+
}
311+
302312
function _decimalsOffset() internal view virtual returns (uint8) {
303313
return 0;
304314
}

0 commit comments

Comments
 (0)