Skip to content

Commit

Permalink
feat: make superchain ERC20 simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
agusduha committed Aug 29, 2024
1 parent 9bb16f9 commit 7649a19
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 199 deletions.
8 changes: 2 additions & 6 deletions packages/contracts-bedrock/semver-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@
"sourceCodeHash": "0x972564d2e2fab111cd431f3c78d00c7b0f0ae422fa5e9a8bf5b202cdaef89bf9"
},
"src/L2/OptimismSuperchainERC20.sol": {
"initCodeHash": "0x7a4ee6f99611f6ac222d724fc4f07eb898287d6c30df0320764c497945410384",
"sourceCodeHash": "0x3952dab835654ced243caff7fb7bd1001b306f2986373309a67d41dbf34975cc"
"initCodeHash": "0x24ac38990bc313df5e5fb1a6bbc777c6b77f9e2aefb75a5a0ca603f5d2cf4faa",
"sourceCodeHash": "0x12efb55402b5cdfe1ea032d65e33c101aaf73c9958ec7d3bd8ddefd9609a3877"
},
"src/L2/SequencerFeeVault.sol": {
"initCodeHash": "0xb94145f571e92ee615c6fe903b6568e8aac5fe760b6b65148ffc45d2fb0f5433",
"sourceCodeHash": "0x8f2a54104e5e7105ba03ba37e3ef9b6684a447245f0e0b787ba4cca12957b97c"
},
"src/L2/SuperchainERC20.sol": {
"initCodeHash": "0xad01acef5a73978df39c7c6e8ff8fa74446e324a10a670ba18637884a96da4f9",
"sourceCodeHash": "0x8c4416cd46d2865c8d7fc758d6e88bbfef73e0349a864eac2af2b1b89cc5591f"
},
"src/L2/SuperchainWETH.sol": {
"initCodeHash": "0x599e948350c70d699f8a8be945abffd126097de97fade056d29767128320fe75",
"sourceCodeHash": "0x3df29ee1321418914d88ce303b521bf8267ef234b919870b26639d08d7f806bd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,29 +115,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_symbol",
"type": "string"
},
{
"internalType": "uint8",
"name": "_decimals",
"type": "uint8"
}
],
"name": "initialize",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down
96 changes: 17 additions & 79 deletions packages/contracts-bedrock/snapshots/abi/SuperchainERC20.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
[
{
"inputs": [],
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_symbol",
"type": "string"
},
{
"internalType": "uint8",
"name": "_decimals",
"type": "uint8"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
Expand Down Expand Up @@ -97,29 +113,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_name",
"type": "string"
},
{
"internalType": "string",
"name": "_symbol",
"type": "string"
},
{
"internalType": "uint8",
"name": "_decimals",
"type": "uint8"
}
],
"name": "initialize",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "name",
Expand Down Expand Up @@ -241,25 +234,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes4",
"name": "_interfaceId",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
Expand Down Expand Up @@ -339,19 +313,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "version",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"anonymous": false,
"inputs": [
Expand All @@ -377,19 +338,6 @@
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint64",
"name": "version",
"type": "uint64"
}
],
"name": "Initialized",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -507,21 +455,11 @@
"name": "InvalidCrossDomainSender",
"type": "error"
},
{
"inputs": [],
"name": "InvalidInitialization",
"type": "error"
},
{
"inputs": [],
"name": "InvalidPermit",
"type": "error"
},
{
"inputs": [],
"name": "NotInitializing",
"type": "error"
},
{
"inputs": [],
"name": "PermitExpired",
Expand Down
15 changes: 12 additions & 3 deletions packages/contracts-bedrock/src/L2/OptimismSuperchainERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pragma solidity 0.8.25;
import { IOptimismSuperchainERC20Extension } from "src/L2/IOptimismSuperchainERC20.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { SuperchainERC20 } from "src/L2/SuperchainERC20.sol";
import { ISemver } from "src/universal/ISemver.sol";
import { Initializable } from "@openzeppelin/contracts-v5/proxy/utils/Initializable.sol";
import { ERC165 } from "@openzeppelin/contracts-v5/utils/introspection/ERC165.sol";

/// @notice Thrown when attempting to mint or burn tokens and the function caller is not the StandardBridge.
error OnlyBridge();
Expand All @@ -19,7 +22,13 @@ error ZeroAddress();
/// token, turning it fungible and interoperable across the superchain. Likewise, it also enables the inverse
/// conversion path.
/// Moreover, it builds on top of the L2ToL2CrossDomainMessenger for both replay protection and domain binding.
contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, SuperchainERC20 {
contract OptimismSuperchainERC20 is
IOptimismSuperchainERC20Extension,
SuperchainERC20,
ISemver,
Initializable,
ERC165
{
/// @notice Address of the StandardBridge Predeploy.
address internal constant BRIDGE = Predeploys.L2_STANDARD_BRIDGE;

Expand Down Expand Up @@ -55,7 +64,7 @@ contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, Superchai
}

/// @notice Constructs the OptimismSuperchainERC20 contract.
constructor() {
constructor() SuperchainERC20("", "", 18) {
_disableInitializers();
}

Expand All @@ -73,7 +82,7 @@ contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, Superchai
external
initializer
{
super.initialize(_name, _symbol, _decimals);
_setMetadataStorage(_name, _symbol, _decimals);

OptimismSuperchainERC20Metadata storage _storage = _getStorage();
_storage.remoteToken = _remoteToken;
Expand Down
43 changes: 15 additions & 28 deletions packages/contracts-bedrock/src/L2/SuperchainERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ pragma solidity 0.8.25;
import { ISuperchainERC20Extensions } from "src/L2/ISuperchainERC20.sol";
import { ERC20 } from "@solady/tokens/ERC20.sol";
import { IL2ToL2CrossDomainMessenger } from "src/L2/IL2ToL2CrossDomainMessenger.sol";
import { ISemver } from "src/universal/ISemver.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { Initializable } from "@openzeppelin/contracts-v5/proxy/utils/Initializable.sol";
import { ERC165 } from "@openzeppelin/contracts-v5/utils/introspection/ERC165.sol";

/// @notice Thrown when attempting to relay a message and the function caller (msg.sender) is not
/// L2ToL2CrossDomainMessenger.
Expand All @@ -23,7 +20,7 @@ error ZeroAddress();
/// @notice SuperchainERC20 is a standard extension of the base ERC20 token contract that unifies ERC20 token
/// bridging to make it fungible across the Superchain. It builds on top of the L2ToL2CrossDomainMessenger for
/// both replay protection and domain binding.
contract SuperchainERC20 is ISuperchainERC20Extensions, ERC20, ISemver, Initializable, ERC165 {
contract SuperchainERC20 is ISuperchainERC20Extensions, ERC20 {
/// @notice Address of the L2ToL2CrossDomainMessenger Predeploy.
address internal constant MESSENGER = Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER;

Expand All @@ -50,33 +47,30 @@ contract SuperchainERC20 is ISuperchainERC20Extensions, ERC20, ISemver, Initiali
}
}

/// @notice Semantic version.
/// @custom:semver "1.0.0-beta.1"
function version() public pure virtual returns (string memory) {
return "1.0.0-beta.1";
/// @notice Sets the storage for the SuperchainERC20Metadata.
/// @param _name Name of the token.
/// @param _symbol Symbol of the token.
/// @param _decimals Decimals of the token.
function _setMetadataStorage(string memory _name, string memory _symbol, uint8 _decimals) internal {
SuperchainERC20Metadata storage _storage = _getMetadataStorage();
_storage.name = _name;
_storage.symbol = _symbol;
_storage.decimals = _decimals;
}

/// @notice Constructs the SuperchainERC20 contract.
constructor() {
_disableInitializers();
}

/// @notice Initializes the contract.
/// @param _name ERC20 name.
/// @param _symbol ERC20 symbol.
/// @param _decimals ERC20 decimals.
function initialize(string memory _name, string memory _symbol, uint8 _decimals) public initializer {
SuperchainERC20Metadata storage _storage = _getMetadataStorage();
_storage.name = _name;
_storage.symbol = _symbol;
_storage.decimals = _decimals;
constructor(string memory _name, string memory _symbol, uint8 _decimals) {
_setMetadataStorage(_name, _symbol, _decimals);
}

/// @notice Sends tokens to some target address on another chain.
/// @param _to Address to send tokens to.
/// @param _amount Amount of tokens to send.
/// @param _chainId Chain ID of the destination chain.
function sendERC20(address _to, uint256 _amount, uint256 _chainId) external {
function sendERC20(address _to, uint256 _amount, uint256 _chainId) external virtual {
if (_to == address(0)) revert ZeroAddress();

_burn(msg.sender, _amount);
Expand All @@ -91,7 +85,7 @@ contract SuperchainERC20 is ISuperchainERC20Extensions, ERC20, ISemver, Initiali
/// @param _from Address of the msg.sender of sendERC20 on the source chain.
/// @param _to Address to relay tokens to.
/// @param _amount Amount of tokens to relay.
function relayERC20(address _from, address _to, uint256 _amount) external {
function relayERC20(address _from, address _to, uint256 _amount) external virtual {
if (_to == address(0)) revert ZeroAddress();

if (msg.sender != MESSENGER) revert CallerNotL2ToL2CrossDomainMessenger();
Expand Down Expand Up @@ -123,14 +117,7 @@ contract SuperchainERC20 is ISuperchainERC20Extensions, ERC20, ISemver, Initiali
/// NOTE: This information is only used for _display_ purposes: it in
/// no way affects any of the arithmetic of the contract, including
/// {IERC20-balanceOf} and {IERC20-transfer}.
function decimals() public view override returns (uint8) {
function decimals() public view virtual override returns (uint8) {
return _getMetadataStorage().decimals;
}

/// @notice ERC165 interface check function.
/// @param _interfaceId Interface ID to check.
/// @return Whether or not the interface is supported by this contract.
function supportsInterface(bytes4 _interfaceId) public view virtual override returns (bool) {
return _interfaceId == type(ISuperchainERC20Extensions).interfaceId || super.supportsInterface(_interfaceId);
}
}
Loading

0 comments on commit 7649a19

Please sign in to comment.