Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: abstract baseline #55

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/contracts-bedrock/semver-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
"sourceCodeHash": "0x4b806cc85cead74c8df34ab08f4b6c6a95a1a387a335ec8a7cb2de4ea4e1cf41"
},
"src/L2/OptimismSuperchainERC20.sol": {
"initCodeHash": "0xdd16dbc0ccbbac53ec2d4273f06334960a907a9f20b7c40300833227ee31d0de",
"sourceCodeHash": "0x910d43a17800df64dbc104f69ef1f900ca761cec4949c01d1c1126fde5268349"
"initCodeHash": "0x4fd71b5352b78d51d39625b6defa77a75be53067b32f3cba86bd17a46917adf9",
"sourceCodeHash": "0xa684393658f8e0c5cc25fb476cb49b92a366714ea8643db495116a55e068ff68"
},
"src/L2/OptimismSuperchainERC20Beacon.sol": {
"initCodeHash": "0x99ce8095b23c124850d866cbc144fee6cee05dbc6bb5d83acadfe00b90cf42c7",
Expand Down
65 changes: 16 additions & 49 deletions packages/contracts-bedrock/src/L2/OptimismSuperchainERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
pragma solidity 0.8.25;

import { IOptimismSuperchainERC20Extension } from "src/L2/interfaces/IOptimismSuperchainERC20.sol";
import { ERC20 } from "@solady/tokens/ERC20.sol";
import { IL2ToL2CrossDomainMessenger } from "src/L2/interfaces/IL2ToL2CrossDomainMessenger.sol";
import { ISemver } from "src/universal/interfaces/ISemver.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { ZeroAddress } from "src/libraries/errors/CommonErrors.sol";
import { ERC20 } from "@solady/tokens/ERC20.sol";
import { SuperchainERC20 } from "src/L2/SuperchainERC20.sol";
import { Initializable } from "@openzeppelin/contracts-v5/proxy/utils/Initializable.sol";
import { ERC165 } from "@openzeppelin/contracts-v5/utils/introspection/ERC165.sol";

Expand All @@ -20,9 +22,6 @@ error InvalidCrossDomainSender();
/// @notice Thrown when attempting to mint or burn tokens and the function caller is not the StandardBridge.
error OnlyBridge();

/// @notice Thrown when attempting to mint or burn tokens and the account is the zero address.
error ZeroAddress();

/// @custom:proxied true
/// @title OptimismSuperchainERC20
/// @notice OptimismSuperchainERC20 is a standard extension of the base ERC20 token contract that unifies ERC20 token
Expand All @@ -31,10 +30,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, ERC20, ISemver, Initializable, ERC165 {
/// @notice Address of the L2ToL2CrossDomainMessenger Predeploy.
address internal constant MESSENGER = Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER;

contract OptimismSuperchainERC20 is
IOptimismSuperchainERC20Extension,
SuperchainERC20,
ISemver,
Initializable,
ERC165
{
/// @notice Address of the StandardBridge Predeploy.
address internal constant BRIDGE = Predeploys.L2_STANDARD_BRIDGE;

Expand All @@ -57,7 +59,7 @@ contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, ERC20, IS
}

/// @notice Returns the storage for the OptimismSuperchainERC20Metadata.
function _getMetadataStorage() private pure returns (OptimismSuperchainERC20Metadata storage _storage) {
function _getStorage() private pure returns (OptimismSuperchainERC20Metadata storage _storage) {
assembly {
_storage.slot := OPTIMISM_SUPERCHAIN_ERC20_METADATA_SLOT
}
Expand Down Expand Up @@ -92,7 +94,7 @@ contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, ERC20, IS
external
initializer
{
OptimismSuperchainERC20Metadata storage _storage = _getMetadataStorage();
OptimismSuperchainERC20Metadata storage _storage = _getStorage();
_storage.remoteToken = _remoteToken;
_storage.name = _name;
_storage.symbol = _symbol;
Expand Down Expand Up @@ -121,54 +123,19 @@ contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, ERC20, IS
emit Burn(_from, _amount);
}

/// @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 {
if (_to == address(0)) revert ZeroAddress();

_burn(msg.sender, _amount);

bytes memory _message = abi.encodeCall(this.relayERC20, (msg.sender, _to, _amount));
IL2ToL2CrossDomainMessenger(MESSENGER).sendMessage(_chainId, address(this), _message);

emit SendERC20(msg.sender, _to, _amount, _chainId);
}

/// @notice Relays tokens received from another chain.
/// @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 {
if (_to == address(0)) revert ZeroAddress();

if (msg.sender != MESSENGER) revert CallerNotL2ToL2CrossDomainMessenger();

if (IL2ToL2CrossDomainMessenger(MESSENGER).crossDomainMessageSender() != address(this)) {
revert InvalidCrossDomainSender();
}

uint256 source = IL2ToL2CrossDomainMessenger(MESSENGER).crossDomainMessageSource();

_mint(_to, _amount);

emit RelayERC20(_from, _to, _amount, source);
}

/// @notice Returns the address of the corresponding version of this token on the remote chain.
function remoteToken() public view override returns (address) {
return _getMetadataStorage().remoteToken;
return _getStorage().remoteToken;
}

/// @notice Returns the name of the token.
function name() public view virtual override returns (string memory) {
return _getMetadataStorage().name;
return _getStorage().name;
}

/// @notice Returns the symbol of the token.
function symbol() public view virtual override returns (string memory) {
return _getMetadataStorage().symbol;
return _getStorage().symbol;
}

/// @notice Returns the number of decimals used to get its user representation.
Expand All @@ -178,7 +145,7 @@ contract OptimismSuperchainERC20 is IOptimismSuperchainERC20Extension, ERC20, IS
/// no way affects any of the arithmetic of the contract, including
/// {IERC20-balanceOf} and {IERC20-transfer}.
function decimals() public view override returns (uint8) {
return _getMetadataStorage().decimals;
return _getStorage().decimals;
}

/// @notice ERC165 interface check function.
Expand Down
57 changes: 57 additions & 0 deletions packages/contracts-bedrock/src/L2/SuperchainERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import { ISuperchainERC20Extensions } from "src/L2/interfaces/ISuperchainERC20.sol";
import { ERC20 } from "@solady/tokens/ERC20.sol";
import { IL2ToL2CrossDomainMessenger } from "src/L2/interfaces/IL2ToL2CrossDomainMessenger.sol";
import { Predeploys } from "src/libraries/Predeploys.sol";
import { ZeroAddress } from "src/libraries/errors/CommonErrors.sol";

/// @notice Thrown when attempting to relay a message and the function caller (msg.sender) is not
/// L2ToL2CrossDomainMessenger.
error CallerNotL2ToL2CrossDomainMessenger();

/// @notice Thrown when attempting to relay a message and the cross domain message sender is not this SuperchainERC20.
error InvalidCrossDomainSender();

/// @title SuperchainERC20
/// @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.
abstract contract SuperchainERC20 is ISuperchainERC20Extensions, ERC20 {
/// @notice Address of the L2ToL2CrossDomainMessenger Predeploy.
address internal constant MESSENGER = Predeploys.L2_TO_L2_CROSS_DOMAIN_MESSENGER;

/// @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 virtual {
if (_to == address(0)) revert ZeroAddress();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to add the comment and open the discussion around this one


_burn(msg.sender, _amount);

bytes memory _message = abi.encodeCall(this.relayERC20, (msg.sender, _to, _amount));
IL2ToL2CrossDomainMessenger(MESSENGER).sendMessage(_chainId, address(this), _message);

emit SendERC20(msg.sender, _to, _amount, _chainId);
}

/// @notice Relays tokens received from another chain.
/// @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 virtual {
if (msg.sender != MESSENGER) revert CallerNotL2ToL2CrossDomainMessenger();

if (IL2ToL2CrossDomainMessenger(MESSENGER).crossDomainMessageSender() != address(this)) {
revert InvalidCrossDomainSender();
}

uint256 source = IL2ToL2CrossDomainMessenger(MESSENGER).crossDomainMessageSource();

_mint(_to, _amount);

emit RelayERC20(_from, _to, _amount, source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ error NotCustomGasToken();

/// @notice Error for when a transfer via call fails.
error TransferFailed();

/// @notice Thrown when attempting to perform an operation and the account is the zero address.
error ZeroAddress();
0xDiscotech marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 16 additions & 30 deletions packages/contracts-bedrock/test/L2/OptimismSuperchainERC20.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import { BeaconProxy } from "@openzeppelin/contracts-v5/proxy/beacon/BeaconProxy

// Target contract
import {
OptimismSuperchainERC20,
IOptimismSuperchainERC20Extension,
OptimismSuperchainERC20, IOptimismSuperchainERC20Extension, OnlyBridge
} from "src/L2/OptimismSuperchainERC20.sol";

// SuperchainERC20 Errors
import {
ZeroAddress,
CallerNotL2ToL2CrossDomainMessenger,
InvalidCrossDomainSender,
OnlyBridge,
ZeroAddress
InvalidCrossDomainSender
} from "src/L2/OptimismSuperchainERC20.sol";

import { ISuperchainERC20Extensions } from "src/L2/interfaces/ISuperchainERC20.sol";

/// @title OptimismSuperchainERC20Test
Expand Down Expand Up @@ -152,11 +155,11 @@ contract OptimismSuperchainERC20Test is Test {
uint256 _toBalanceBefore = superchainERC20.balanceOf(_to);

// Look for the emit of the `Transfer` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit IERC20.Transfer(ZERO_ADDRESS, _to, _amount);

// Look for the emit of the `Mint` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit IOptimismSuperchainERC20Extension.Mint(_to, _amount);

// Call the `mint` function with the bridge caller
Expand Down Expand Up @@ -205,11 +208,11 @@ contract OptimismSuperchainERC20Test is Test {
uint256 _fromBalanceBefore = superchainERC20.balanceOf(_from);

// Look for the emit of the `Transfer` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit IERC20.Transfer(_from, ZERO_ADDRESS, _amount);

// Look for the emit of the `Burn` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit IOptimismSuperchainERC20Extension.Burn(_from, _amount);

// Call the `burn` function with the bridge caller
Expand Down Expand Up @@ -247,11 +250,11 @@ contract OptimismSuperchainERC20Test is Test {
uint256 _senderBalanceBefore = superchainERC20.balanceOf(_sender);

// Look for the emit of the `Transfer` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit IERC20.Transfer(_sender, ZERO_ADDRESS, _amount);

// Look for the emit of the `SendERC20` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit ISuperchainERC20Extensions.SendERC20(_sender, _to, _amount, _chainId);

// Mock the call over the `sendMessage` function and expect it to be called properly
Expand Down Expand Up @@ -314,23 +317,6 @@ contract OptimismSuperchainERC20Test is Test {
superchainERC20.relayERC20(_crossDomainMessageSender, _to, _amount);
}

/// @notice Tests the `relayERC20` function reverts when the `_to` address is the zero address.
function testFuzz_relayERC20_zeroAddressTo_reverts(uint256 _amount) public {
// Expect the revert with `ZeroAddress` selector
vm.expectRevert(ZeroAddress.selector);

// Mock the call over the `crossDomainMessageSender` function setting the same address as value
vm.mockCall(
MESSENGER,
abi.encodeWithSelector(IL2ToL2CrossDomainMessenger.crossDomainMessageSender.selector),
abi.encode(address(superchainERC20))
);

// Call the `relayERC20` function with the zero address
vm.prank(MESSENGER);
superchainERC20.relayERC20({ _from: ZERO_ADDRESS, _to: ZERO_ADDRESS, _amount: _amount });
}

/// @notice Tests the `relayERC20` mints the proper amount and emits the `RelayERC20` event.
function testFuzz_relayERC20_succeeds(address _from, address _to, uint256 _amount, uint256 _source) public {
vm.assume(_from != ZERO_ADDRESS);
Expand All @@ -355,11 +341,11 @@ contract OptimismSuperchainERC20Test is Test {
uint256 _toBalanceBefore = superchainERC20.balanceOf(_to);

// Look for the emit of the `Transfer` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit IERC20.Transfer(ZERO_ADDRESS, _to, _amount);

// Look for the emit of the `RelayERC20` event
vm.expectEmit(true, true, true, true, address(superchainERC20));
vm.expectEmit(address(superchainERC20));
emit ISuperchainERC20Extensions.RelayERC20(_from, _to, _amount, _source);

// Call the `relayERC20` function with the messenger caller
Expand Down
Loading