Skip to content

Commit

Permalink
fix: add generic factory interface (#14)
Browse files Browse the repository at this point in the history
* test: add L2 standard bridge interop unit tests

* fix: add tests natspec

* fix: add generic factory interface
  • Loading branch information
agusduha authored Aug 9, 2024
1 parent de677a2 commit 593efad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
12 changes: 12 additions & 0 deletions packages/contracts-bedrock/src/L2/IOptimismERC20Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/// @title IOptimismERC20Factory
/// @notice Generic interface for IOptimismMintableERC20Factory and ISuperchainERC20Factory. Used to
/// determine if a ERC20 contract is deployed by a factory.
interface IOptimismERC20Factory {
/// @notice Checks if a ERC20 token is deployed by the factory.
/// @param _token The address of the ERC20 token to check the deployment.
/// @return _remoteToken The address of the remote token if it is deployed or `address(0)` if not.
function deployments(address _token) external view returns (address _remoteToken);
}
15 changes: 3 additions & 12 deletions packages/contracts-bedrock/src/L2/L2StandardBridgeInterop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Predeploys } from "src/libraries/Predeploys.sol";
import { L2StandardBridge } from "src/L2/L2StandardBridge.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { IOptimismERC20Factory } from "src/L2/IOptimismERC20Factory.sol";

/// @notice Thrown when the decimals of the tokens are not the same.
error InvalidDecimals();
Expand All @@ -18,16 +19,6 @@ error InvalidSuperchainAddress();
/// @notice Thrown when the remote addresses of the tokens are not the same.
error InvalidTokenPair();

// TODO: Use OptimismMintableERC20Factory contract instead of interface
interface IOptimismMintableERC20Factory {
function deployments(address) external view returns (address);
}

// TODO: Move to a separate file
interface ISuperchainERC20Factory {
function deployments(address) external view returns (address);
}

// TODO: Use an existing interface with `mint` and `burn`?
interface MintableAndBurnable is IERC20 {
function mint(address, uint256) external;
Expand Down Expand Up @@ -82,12 +73,12 @@ contract L2StandardBridgeInterop is L2StandardBridge {
function _validateFactories(address _legacyAddr, address _superAddr) internal view {
// 2. Valid legacy check
address _legacyRemoteToken =
IOptimismMintableERC20Factory(Predeploys.OPTIMISM_MINTABLE_ERC20_FACTORY).deployments(_legacyAddr);
IOptimismERC20Factory(Predeploys.OPTIMISM_MINTABLE_ERC20_FACTORY).deployments(_legacyAddr);
if (_legacyRemoteToken == address(0)) revert InvalidLegacyAddress();

// 3. Valid SuperchainERC20 check
address _superRemoteToken =
ISuperchainERC20Factory(Predeploys.OPTIMISM_SUPERCHAIN_ERC20_FACTORY).deployments(_superAddr);
IOptimismERC20Factory(Predeploys.OPTIMISM_SUPERCHAIN_ERC20_FACTORY).deployments(_superAddr);
if (_superRemoteToken == address(0)) revert InvalidSuperchainAddress();

// 4. Same remote address check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
InvalidLegacyAddress,
InvalidSuperchainAddress,
InvalidTokenPair,
IOptimismMintableERC20Factory,
IOptimismERC20Factory,
MintableAndBurnable
} from "src/L2/L2StandardBridgeInterop.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
Expand Down Expand Up @@ -47,9 +47,7 @@ contract L2StandardBridgeInterop_Test is Bridge_Initializer {
/// @notice Mock factory deployment
function _mockDeployments(address _factory, address _token, address _deployed) internal {
_mockAndExpect(
_factory,
abi.encodeWithSelector(IOptimismMintableERC20Factory.deployments.selector, _token),
abi.encode(_deployed)
_factory, abi.encodeWithSelector(IOptimismERC20Factory.deployments.selector, _token), abi.encode(_deployed)
);
}
}
Expand Down

0 comments on commit 593efad

Please sign in to comment.