draft-ERC20Bridgeable.sol 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.4.0-rc.0) (token/ERC20/extensions/draft-ERC20Bridgeable.sol)
  3. pragma solidity ^0.8.20;
  4. import {ERC20} from "../ERC20.sol";
  5. import {ERC165, IERC165} from "../../../utils/introspection/ERC165.sol";
  6. import {IERC7802} from "../../../interfaces/draft-IERC7802.sol";
  7. /**
  8. * @dev ERC20 extension that implements the standard token interface according to
  9. * https://eips.ethereum.org/EIPS/eip-7802[ERC-7802].
  10. */
  11. abstract contract ERC20Bridgeable is ERC20, ERC165, IERC7802 {
  12. /// @dev Modifier to restrict access to the token bridge.
  13. modifier onlyTokenBridge() {
  14. // Token bridge should never be impersonated using a relayer/forwarder. Using msg.sender is preferable to
  15. // _msgSender() for security reasons.
  16. _checkTokenBridge(msg.sender);
  17. _;
  18. }
  19. /// @inheritdoc ERC165
  20. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  21. return interfaceId == type(IERC7802).interfaceId || super.supportsInterface(interfaceId);
  22. }
  23. /**
  24. * @dev See {IERC7802-crosschainMint}. Emits a {IERC7802-CrosschainMint} event.
  25. */
  26. function crosschainMint(address to, uint256 value) public virtual override onlyTokenBridge {
  27. _mint(to, value);
  28. emit CrosschainMint(to, value, _msgSender());
  29. }
  30. /**
  31. * @dev See {IERC7802-crosschainBurn}. Emits a {IERC7802-CrosschainBurn} event.
  32. */
  33. function crosschainBurn(address from, uint256 value) public virtual override onlyTokenBridge {
  34. _burn(from, value);
  35. emit CrosschainBurn(from, value, _msgSender());
  36. }
  37. /**
  38. * @dev Checks if the caller is a trusted token bridge. MUST revert otherwise.
  39. *
  40. * Developers should implement this function using an access control mechanism that allows
  41. * customizing the list of allowed senders. Consider using {AccessControl} or {AccessManaged}.
  42. */
  43. function _checkTokenBridge(address caller) internal virtual;
  44. }