forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 1
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
test: add superchain erc20 factory tests #25
Merged
agusduha
merged 2 commits into
sc/superchain-erc20-factory
from
test/superchain-erc20-factory-tests
Aug 22, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also assert for name symbol and remote token |
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you remove the
Predeploys...
way of calling it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to replicate the way they use the initialized predeploys across the different test suites