|
| 1 | +// SPDX-License-Identifier: Apache 2 |
| 2 | +pragma solidity >=0.8.8 <0.9.0; |
| 3 | + |
| 4 | +import {OwnableUpgradeable} from |
| 5 | + "openzeppelin-contracts-upgradeable/contracts/access/OwnableUpgradeable.sol"; |
| 6 | +import {ERC20Upgradeable} from |
| 7 | + "openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol"; |
| 8 | +import {ERC20BurnableUpgradeable} from |
| 9 | + "openzeppelin-contracts-upgradeable/contracts/token/ERC20/extensions/ERC20BurnableUpgradeable.sol"; |
| 10 | +import {UUPSUpgradeable} from |
| 11 | + "openzeppelin-contracts-upgradeable/contracts/proxy/utils/UUPSUpgradeable.sol"; |
| 12 | +import {INttToken} from "@wormhole-foundation/native_token_transfer/interfaces/INttToken.sol"; |
| 13 | + |
| 14 | +/// @title WstEthL2Token |
| 15 | +/// @notice A UUPS upgradeable token with access controlled minting and burning. |
| 16 | +contract WstEthL2Token is |
| 17 | + INttToken, |
| 18 | + UUPSUpgradeable, |
| 19 | + ERC20Upgradeable, |
| 20 | + ERC20BurnableUpgradeable, |
| 21 | + OwnableUpgradeable |
| 22 | +{ |
| 23 | + // =============== Storage ============================================================== |
| 24 | + |
| 25 | + struct MinterStorage { |
| 26 | + address _minter; |
| 27 | + } |
| 28 | + |
| 29 | + bytes32 private constant MINTER_SLOT = bytes32(uint256(keccak256("wsteth.minter")) - 1); |
| 30 | + |
| 31 | + // =============== Storage Getters/Setters ============================================== |
| 32 | + |
| 33 | + function _getMinterStorage() internal pure returns (MinterStorage storage $) { |
| 34 | + uint256 slot = uint256(MINTER_SLOT); |
| 35 | + assembly ("memory-safe") { |
| 36 | + $.slot := slot |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// @notice A function to set the new minter for the tokens. |
| 41 | + /// @param newMinter The address to add as both a minter and burner. |
| 42 | + function setMinter(address newMinter) external onlyOwner { |
| 43 | + if (newMinter == address(0)) { |
| 44 | + revert InvalidMinterZeroAddress(); |
| 45 | + } |
| 46 | + address previousMinter = _getMinterStorage()._minter; |
| 47 | + _getMinterStorage()._minter = newMinter; |
| 48 | + emit NewMinter(previousMinter, newMinter); |
| 49 | + } |
| 50 | + |
| 51 | + /// @dev Returns the address of the current minter. |
| 52 | + function minter() public view returns (address) { |
| 53 | + MinterStorage storage $ = _getMinterStorage(); |
| 54 | + return $._minter; |
| 55 | + } |
| 56 | + |
| 57 | + /// @dev Throws if called by any account other than the minter. |
| 58 | + modifier onlyMinter() { |
| 59 | + if (minter() != _msgSender()) { |
| 60 | + revert CallerNotMinter(_msgSender()); |
| 61 | + } |
| 62 | + _; |
| 63 | + } |
| 64 | + |
| 65 | + /// @dev An error thrown when a method is not implemented. |
| 66 | + error UnimplementedMethod(); |
| 67 | + |
| 68 | + /// @custom:oz-upgrades-unsafe-allow constructor |
| 69 | + constructor() { |
| 70 | + _disableInitializers(); |
| 71 | + } |
| 72 | + |
| 73 | + /// @notice A one-time configuration method meant to be called immediately upon the deployment of `WstEthL2Token`. It sets |
| 74 | + /// up the token's name, symbol, and owner |
| 75 | + function initialize( |
| 76 | + string memory _name, |
| 77 | + string memory _symbol, |
| 78 | + address _owner |
| 79 | + ) external initializer { |
| 80 | + // OpenZeppelin upgradeable contracts documentation says: |
| 81 | + // |
| 82 | + // "Use with multiple inheritance requires special care. Initializer |
| 83 | + // functions are not linearized by the compiler like constructors. |
| 84 | + // Because of this, each __{ContractName}_init function embeds the |
| 85 | + // linearized calls to all parent initializers. As a consequence, |
| 86 | + // calling two of these init functions can potentially initialize the |
| 87 | + // same contract twice." |
| 88 | + // |
| 89 | + // Note that ERC20 extensions do not linearize calls to ERC20Upgradeable |
| 90 | + // initializer so we call all extension initializers individually. |
| 91 | + __ERC20_init(_name, _symbol); |
| 92 | + __Ownable_init(_owner); |
| 93 | + |
| 94 | + // These initializers don't do anything, so we won't call them |
| 95 | + // __ERC20Burnable_init(); |
| 96 | + // __UUPSUpgradeable_init(); |
| 97 | + } |
| 98 | + |
| 99 | + /// @notice A function that will burn tokens held by the `msg.sender`. |
| 100 | + /// @param _value The amount of tokens to be burned. |
| 101 | + function burn(uint256 _value) public override(INttToken, ERC20BurnableUpgradeable) onlyMinter { |
| 102 | + ERC20BurnableUpgradeable.burn(_value); |
| 103 | + } |
| 104 | + |
| 105 | + /// @notice This method is not implemented and should not be called. |
| 106 | + function burnFrom(address, uint256) public pure override { |
| 107 | + revert UnimplementedMethod(); |
| 108 | + } |
| 109 | + |
| 110 | + /// @notice A function that mints new tokens to a specific account. |
| 111 | + /// @param _account The address where new tokens will be minted. |
| 112 | + /// @param _amount The amount of new tokens that will be minted. |
| 113 | + function mint(address _account, uint256 _amount) external onlyMinter { |
| 114 | + _mint(_account, _amount); |
| 115 | + } |
| 116 | + |
| 117 | + function _authorizeUpgrade(address /* newImplementation */ ) internal view override onlyOwner {} |
| 118 | +} |
0 commit comments