forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add superchain erc20 factory tests (#25)
* test: add superchain erc20 factory tests * test: add erc20 asserts
- Loading branch information
Showing
8 changed files
with
176 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
GasBenchMark_L1CrossDomainMessenger:test_sendMessage_benchmark_0() (gas: 369356) | ||
GasBenchMark_L1CrossDomainMessenger:test_sendMessage_benchmark_1() (gas: 2967496) | ||
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_0() (gas: 564483) | ||
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_1() (gas: 4076526) | ||
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_0() (gas: 564489) | ||
GasBenchMark_L1StandardBridge_Deposit:test_depositERC20_benchmark_1() (gas: 4076532) | ||
GasBenchMark_L1StandardBridge_Deposit:test_depositETH_benchmark_0() (gas: 466947) | ||
GasBenchMark_L1StandardBridge_Deposit:test_depositETH_benchmark_1() (gas: 3512629) | ||
GasBenchMark_L1StandardBridge_Finalize:test_finalizeETHWithdrawal_benchmark() (gas: 72624) | ||
GasBenchMark_L2OutputOracle:test_proposeL2Output_benchmark() (gas: 92973) | ||
GasBenchMark_L2OutputOracle:test_proposeL2Output_benchmark() (gas: 92970) | ||
GasBenchMark_OptimismPortal:test_depositTransaction_benchmark() (gas: 68433) | ||
GasBenchMark_OptimismPortal:test_depositTransaction_benchmark_1() (gas: 68903) | ||
GasBenchMark_OptimismPortal:test_proveWithdrawalTransaction_benchmark() (gas: 155618) | ||
GasBenchMark_OptimismPortal:test_proveWithdrawalTransaction_benchmark() (gas: 155615) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/contracts-bedrock/test/L2/OptimismSuperchainERC20Factory.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.25; | ||
|
||
// Testing utilities | ||
import { Test } from "forge-std/Test.sol"; | ||
import { EIP1967Helper } from "test/mocks/EIP1967Helper.sol"; | ||
|
||
// Libraries | ||
import { Predeploys } from "src/libraries/Predeploys.sol"; | ||
import { CREATE3, Bytes32AddressLib } from "@rari-capital/solmate/src/utils/CREATE3.sol"; | ||
import { IBeacon } from "@openzeppelin/contracts-v5/proxy/beacon/IBeacon.sol"; | ||
|
||
// Target contract | ||
import { OptimismSuperchainERC20Factory, OptimismSuperchainERC20 } from "src/L2/OptimismSuperchainERC20Factory.sol"; | ||
|
||
/// @title OptimismSuperchainERC20FactoryTest | ||
/// @notice Contract for testing the OptimismSuperchainERC20Factory contract. | ||
contract OptimismSuperchainERC20FactoryTest is Test { | ||
using Bytes32AddressLib for bytes32; | ||
|
||
OptimismSuperchainERC20 public superchainERC20Impl; | ||
OptimismSuperchainERC20Factory public superchainERC20Factory; | ||
|
||
/// @notice Sets up the test suite. | ||
function setUp() public { | ||
superchainERC20Impl = new OptimismSuperchainERC20(); | ||
|
||
// Deploy the OptimismSuperchainERC20Beacon contract | ||
_deployBeacon(); | ||
|
||
superchainERC20Factory = new OptimismSuperchainERC20Factory(); | ||
} | ||
|
||
/// @notice Deploy the OptimismSuperchainERC20Beacon predeploy contract | ||
function _deployBeacon() internal { | ||
// Deploy the OptimismSuperchainERC20Beacon implementation | ||
address _addr = Predeploys.OPTIMISM_SUPERCHAIN_ERC20_BEACON; | ||
address _impl = Predeploys.predeployToCodeNamespace(_addr); | ||
vm.etch(_impl, vm.getDeployedCode("OptimismSuperchainERC20Beacon.sol:OptimismSuperchainERC20Beacon")); | ||
|
||
// Deploy the ERC1967Proxy contract at the Predeploy | ||
bytes memory code = vm.getDeployedCode("universal/Proxy.sol:Proxy"); | ||
vm.etch(_addr, code); | ||
EIP1967Helper.setAdmin(_addr, Predeploys.PROXY_ADMIN); | ||
EIP1967Helper.setImplementation(_addr, _impl); | ||
|
||
// Mock implementation address | ||
vm.mockCall( | ||
_impl, abi.encodeWithSelector(IBeacon.implementation.selector), abi.encode(address(superchainERC20Impl)) | ||
); | ||
} | ||
|
||
/// @notice Test that calling `deploy` with valid parameters succeeds. | ||
function test_deploy_succeeds( | ||
address _caller, | ||
address _remoteToken, | ||
string memory _name, | ||
string memory _symbol, | ||
uint8 _decimals | ||
) | ||
public | ||
{ | ||
// Arrange | ||
bytes32 salt = keccak256(abi.encode(_remoteToken, _name, _symbol, _decimals)); | ||
address deployment = _calculateTokenAddress(salt, address(superchainERC20Factory)); | ||
|
||
vm.expectEmit(true, true, true, true); | ||
emit OptimismSuperchainERC20Factory.OptimismSuperchainERC20Created(deployment, _remoteToken, _caller); | ||
|
||
// Act | ||
vm.prank(_caller); | ||
address addr = superchainERC20Factory.deploy(_remoteToken, _name, _symbol, _decimals); | ||
|
||
// Assert | ||
assertTrue(addr == deployment); | ||
assertTrue(OptimismSuperchainERC20(deployment).decimals() == _decimals); | ||
assertTrue(OptimismSuperchainERC20(deployment).remoteToken() == _remoteToken); | ||
assertEq(OptimismSuperchainERC20(deployment).name(), _name); | ||
assertEq(OptimismSuperchainERC20(deployment).symbol(), _symbol); | ||
assertEq(superchainERC20Factory.deployments(deployment), _remoteToken); | ||
} | ||
|
||
/// @notice Test that calling `deploy` with the same parameters twice reverts. | ||
function test_deploy_sameTwice_reverts( | ||
address _caller, | ||
address _remoteToken, | ||
string memory _name, | ||
string memory _symbol, | ||
uint8 _decimals | ||
) | ||
external | ||
{ | ||
// Arrange | ||
vm.prank(_caller); | ||
superchainERC20Factory.deploy(_remoteToken, _name, _symbol, _decimals); | ||
|
||
vm.expectRevert(bytes("DEPLOYMENT_FAILED")); | ||
|
||
// Act | ||
vm.prank(_caller); | ||
superchainERC20Factory.deploy(_remoteToken, _name, _symbol, _decimals); | ||
} | ||
|
||
/// @notice Precalculates the address of the token contract using CREATE3. | ||
function _calculateTokenAddress(bytes32 _salt, address _deployer) internal pure returns (address) { | ||
address proxy = | ||
keccak256(abi.encodePacked(bytes1(0xFF), _deployer, _salt, CREATE3.PROXY_BYTECODE_HASH)).fromLast20Bytes(); | ||
|
||
return keccak256(abi.encodePacked(hex"d694", proxy, hex"01")).fromLast20Bytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters