From 4d3e423443d0d0f80861235e5b58866572640114 Mon Sep 17 00:00:00 2001 From: Kimani Kelly Date: Tue, 7 Feb 2023 20:16:36 -0500 Subject: [PATCH] Make ERC20Wrapper.underlying variable private (#4029) Co-authored-by: Kimani Kelly Co-authored-by: Hadrien Croubois --- .changeset/proud-comics-deliver.md | 5 +++++ .../token/ERC20/extensions/ERC20Wrapper.sol | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 .changeset/proud-comics-deliver.md diff --git a/.changeset/proud-comics-deliver.md b/.changeset/proud-comics-deliver.md new file mode 100644 index 000000000..e9c1015f8 --- /dev/null +++ b/.changeset/proud-comics-deliver.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': minor +--- + +`ERC20Wrapper`: Make the `underlying` variable private and add a public accessor. diff --git a/contracts/token/ERC20/extensions/ERC20Wrapper.sol b/contracts/token/ERC20/extensions/ERC20Wrapper.sol index 8b153ffca..ce515693e 100644 --- a/contracts/token/ERC20/extensions/ERC20Wrapper.sol +++ b/contracts/token/ERC20/extensions/ERC20Wrapper.sol @@ -16,28 +16,35 @@ import "../utils/SafeERC20.sol"; * _Available since v4.2._ */ abstract contract ERC20Wrapper is ERC20 { - IERC20 public immutable underlying; + IERC20 private immutable _underlying; constructor(IERC20 underlyingToken) { - underlying = underlyingToken; + _underlying = underlyingToken; } /** * @dev See {ERC20-decimals}. */ function decimals() public view virtual override returns (uint8) { - try IERC20Metadata(address(underlying)).decimals() returns (uint8 value) { + try IERC20Metadata(address(_underlying)).decimals() returns (uint8 value) { return value; } catch { return super.decimals(); } } + /** + * @dev Returns the address of the underlying ERC-20 token that is being wrapped. + */ + function underlying() public view returns (IERC20) { + return _underlying; + } + /** * @dev Allow a user to deposit underlying tokens and mint the corresponding number of wrapped tokens. */ function depositFor(address account, uint256 amount) public virtual returns (bool) { - SafeERC20.safeTransferFrom(underlying, _msgSender(), address(this), amount); + SafeERC20.safeTransferFrom(_underlying, _msgSender(), address(this), amount); _mint(account, amount); return true; } @@ -47,7 +54,7 @@ abstract contract ERC20Wrapper is ERC20 { */ function withdrawTo(address account, uint256 amount) public virtual returns (bool) { _burn(_msgSender(), amount); - SafeERC20.safeTransfer(underlying, account, amount); + SafeERC20.safeTransfer(_underlying, account, amount); return true; } @@ -56,7 +63,7 @@ abstract contract ERC20Wrapper is ERC20 { * function that can be exposed with access control if desired. */ function _recover(address account) internal virtual returns (uint256) { - uint256 value = underlying.balanceOf(address(this)) - totalSupply(); + uint256 value = _underlying.balanceOf(address(this)) - totalSupply(); _mint(account, value); return value; }