Skip to content

Commit

Permalink
Merge branch 'dev' into test/refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hexshire committed Jul 8, 2024
2 parents 9409fb6 + 990a88d commit 54a75a9
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 39 deletions.
38 changes: 38 additions & 0 deletions script/mainnet/DeployBase.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Script} from 'forge-std/Script.sol';
import {console} from 'forge-std/Test.sol';
import {IL1OpUSDCFactory} from 'interfaces/IL1OpUSDCFactory.sol';
import {USDC_IMPLEMENTATION_CREATION_CODE} from 'script/utils/USDCImplementationCreationCode.sol';
import {USDCInitTxs} from 'src/contracts/utils/USDCInitTxs.sol';

contract DeployBase is Script {
address public constant L1_MESSENGER = 0x866E82a600A1414e583f7F13623F1aC5d58b0Afa;
uint32 public constant MIN_GAS_LIMIT_DEPLOY = 9_000_000;
IL1OpUSDCFactory public immutable L1_FACTORY = IL1OpUSDCFactory(vm.envAddress('L1_FACTORY_MAINNET'));
address public deployer = vm.rememberKey(vm.envUint('MAINNET_DEPLOYER_PK'));

function run() public {
vm.startBroadcast(deployer);
// Deploy the L2 contracts
bytes[] memory _usdcInitTxs = new bytes[](3);
_usdcInitTxs[0] = USDCInitTxs.INITIALIZEV2;
_usdcInitTxs[1] = USDCInitTxs.INITIALIZEV2_1;
_usdcInitTxs[2] = USDCInitTxs.INITIALIZEV2_2;

IL1OpUSDCFactory.L2Deployments memory _l2Deployments = IL1OpUSDCFactory.L2Deployments({
l2AdapterOwner: deployer,
usdcImplementationInitCode: USDC_IMPLEMENTATION_CREATION_CODE,
usdcInitTxs: _usdcInitTxs,
minGasLimitDeploy: MIN_GAS_LIMIT_DEPLOY
});
(address _l1Adapter, address _l2Factory, address _l2Adapter) =
L1_FACTORY.deploy(L1_MESSENGER, deployer, _l2Deployments);
vm.stopBroadcast();

console.log('L1 Adapter:', _l1Adapter);
console.log('L2 Factory:', _l2Factory);
console.log('L2 Adapter:', _l2Adapter);
}
}
2 changes: 1 addition & 1 deletion script/mainnet/DeployOptimism.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {USDCInitTxs} from 'src/contracts/utils/USDCInitTxs.sol';

contract DeployOptimism is Script {
address public constant L1_MESSENGER = 0x25ace71c97B33Cc4729CF772ae268934F7ab5fA1;
uint32 public constant MIN_GAS_LIMIT_DEPLOY = 6_000_000;
uint32 public constant MIN_GAS_LIMIT_DEPLOY = 9_000_000;
IL1OpUSDCFactory public immutable L1_FACTORY = IL1OpUSDCFactory(vm.envAddress('L1_FACTORY_MAINNET'));
address public deployer = vm.rememberKey(vm.envUint('MAINNET_DEPLOYER_PK'));

Expand Down
4 changes: 1 addition & 3 deletions script/mainnet/L1FactoryDeployAndSetup.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import {IL1OpUSDCFactory} from 'interfaces/IL1OpUSDCFactory.sol';

contract L1FactoryDeployAndSetup is Script {
address public constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address public constant L1_MESSENGER_OP = 0x25ace71c97B33Cc4729CF772ae268934F7ab5fA1;
address public constant L1_MESSENGER_BASE = 0x866E82a600A1414e583f7F13623F1aC5d58b0Afa;
address public deployer = vm.rememberKey(vm.envUint('MAINNET_DEPLOYER_PK'));

function run() public {
vm.startBroadcast(deployer);

console.log('Deploying L1OpUSDCFactory ...');
IL1OpUSDCFactory _l1Factory = new L1OpUSDCFactory(USDC);
console.log('L1OpUSDCFactory deployed at:', address(_l1Factory));
/// NOTE: Hardcode the address on `L1_FACTORY_MAINNET` inside the `.env` file

vm.stopBroadcast();
}
Expand Down
2 changes: 1 addition & 1 deletion script/sepolia/DeployOptimism.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {USDCInitTxs} from 'src/contracts/utils/USDCInitTxs.sol';

contract DeployOptimism is Script {
address public constant L1_MESSENGER = 0x58Cc85b8D04EA49cC6DBd3CbFFd00B4B8D6cb3ef;
uint32 public constant MIN_GAS_LIMIT_DEPLOY = 8_000_000;
uint32 public constant MIN_GAS_LIMIT_DEPLOY = 9_000_000;
IL1OpUSDCFactory public immutable L1_FACTORY = IL1OpUSDCFactory(vm.envAddress('L1_FACTORY_SEPOLIA'));

address public deployer = vm.rememberKey(vm.envUint('SEPOLIA_DEPLOYER_PK'));
Expand Down
3 changes: 1 addition & 2 deletions script/sepolia/L1FactoryDeployAndSetup.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {IL1OpUSDCFactory} from 'interfaces/IL1OpUSDCFactory.sol';

contract L1FactoryDeployAndSetup is Script {
address public constant USDC = 0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238;
address public constant L1_MESSENGER_OP = 0x58Cc85b8D04EA49cC6DBd3CbFFd00B4B8D6cb3ef;
address public constant L1_MESSENGER_BASE = 0xC34855F4De64F1840e5686e64278da901e261f20;
address public deployer = vm.rememberKey(vm.envUint('SEPOLIA_DEPLOYER_PK'));

function run() public {
Expand All @@ -18,6 +16,7 @@ contract L1FactoryDeployAndSetup is Script {
console.log('Deploying L1OpUSDCFactory ...');
IL1OpUSDCFactory _l1Factory = new L1OpUSDCFactory(USDC);
console.log('L1OpUSDCFactory deployed at:', address(_l1Factory));
/// NOTE: Hardcode the address on `L1_FACTORY_SEPOLIA` inside the `.env` file

vm.stopBroadcast();
}
Expand Down
9 changes: 5 additions & 4 deletions src/contracts/L1OpUSDCBridgeAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract L1OpUSDCBridgeAdapter is IL1OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
/**
* @notice Modifier to check if the sender is the linked adapter through the messenger
*/
modifier checkSender() {
modifier onlyLinkedAdapter() {
if (msg.sender != MESSENGER || ICrossDomainMessenger(MESSENGER).xDomainMessageSender() != LINKED_ADAPTER) {
revert IOpUSDCBridgeAdapter_InvalidSender();
}
Expand Down Expand Up @@ -93,7 +93,7 @@ contract L1OpUSDCBridgeAdapter is IL1OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
* @param _amount The amount of USDC tokens that will be burned
* @dev Only callable by a whitelisted messenger during its migration process
*/
function setBurnAmount(uint256 _amount) external checkSender {
function setBurnAmount(uint256 _amount) external onlyLinkedAdapter {
if (messengerStatus != Status.Upgrading) revert IOpUSDCBridgeAdapter_NotUpgrading();

burnAmount = _amount;
Expand Down Expand Up @@ -230,7 +230,8 @@ contract L1OpUSDCBridgeAdapter is IL1OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
if (block.timestamp > _deadline) revert IOpUSDCBridgeAdapter_MessageExpired();

// Hash the message
bytes32 _messageHash = keccak256(abi.encode(address(this), block.chainid, _to, _amount, userNonce[_signer]++));
bytes32 _messageHash =
keccak256(abi.encode(address(this), block.chainid, _to, _amount, _deadline, userNonce[_signer]++));

_checkSignature(_signer, _messageHash, _signature);

Expand All @@ -251,7 +252,7 @@ contract L1OpUSDCBridgeAdapter is IL1OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
* @param _user The user to mint the bridged representation for
* @param _amount The amount of tokens to mint
*/
function receiveMessage(address _user, uint256 _amount) external override checkSender {
function receiveMessage(address _user, uint256 _amount) external override onlyLinkedAdapter {
// Transfer the tokens to the user
IUSDC(USDC).safeTransfer(_user, _amount);
emit MessageReceived(_user, _amount, MESSENGER);
Expand Down
13 changes: 7 additions & 6 deletions src/contracts/L2OpUSDCBridgeAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract L2OpUSDCBridgeAdapter is IL2OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
/**
* @notice Modifier to check if the sender is the linked adapter through the messenger
*/
modifier checkSender() {
modifier onlyLinkedAdapter() {
if (msg.sender != MESSENGER || ICrossDomainMessenger(MESSENGER).xDomainMessageSender() != LINKED_ADAPTER) {
revert IOpUSDCBridgeAdapter_InvalidSender();
}
Expand Down Expand Up @@ -74,7 +74,7 @@ contract L2OpUSDCBridgeAdapter is IL2OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
* @param _roleCaller The address that will be allowed to transfer the USDC roles
* @param _setBurnAmountMinGasLimit Minimum gas limit that the setBurnAmount message can be executed on L1
*/
function receiveMigrateToNative(address _roleCaller, uint32 _setBurnAmountMinGasLimit) external checkSender {
function receiveMigrateToNative(address _roleCaller, uint32 _setBurnAmountMinGasLimit) external onlyLinkedAdapter {
isMessagingDisabled = true;
roleCaller = _roleCaller;

Expand Down Expand Up @@ -109,7 +109,7 @@ contract L2OpUSDCBridgeAdapter is IL2OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
/**
* @notice Receive the stop messaging message from the linked adapter and stop outgoing messages
*/
function receiveStopMessaging() external checkSender {
function receiveStopMessaging() external onlyLinkedAdapter {
isMessagingDisabled = true;

emit MessagingStopped(MESSENGER);
Expand All @@ -118,7 +118,7 @@ contract L2OpUSDCBridgeAdapter is IL2OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
/**
* @notice Resume messaging after it was stopped
*/
function receiveResumeMessaging() external checkSender {
function receiveResumeMessaging() external onlyLinkedAdapter {
// NOTE: This is safe because this message can only be received when messaging is not deprecated on the L1 messenger
isMessagingDisabled = false;

Expand Down Expand Up @@ -176,7 +176,8 @@ contract L2OpUSDCBridgeAdapter is IL2OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
if (block.timestamp > _deadline) revert IOpUSDCBridgeAdapter_MessageExpired();

// Hash the message
bytes32 _messageHash = keccak256(abi.encode(address(this), block.chainid, _to, _amount, userNonce[_signer]++));
bytes32 _messageHash =
keccak256(abi.encode(address(this), block.chainid, _to, _amount, _deadline, userNonce[_signer]++));

_checkSignature(_signer, _messageHash, _signature);

Expand All @@ -199,7 +200,7 @@ contract L2OpUSDCBridgeAdapter is IL2OpUSDCBridgeAdapter, OpUSDCBridgeAdapter {
* @param _user The user to mint the bridged representation for
* @param _amount The amount of tokens to mint
*/
function receiveMessage(address _user, uint256 _amount) external override checkSender {
function receiveMessage(address _user, uint256 _amount) external override onlyLinkedAdapter {
// Mint the tokens to the user
IUSDC(USDC).mint(_user, _amount);
emit MessageReceived(_user, _amount, MESSENGER);
Expand Down
5 changes: 3 additions & 2 deletions src/contracts/L2OpUSDCFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {IUSDC} from 'interfaces/external/IUSDC.sol';
* L2 contracts have the same address on different L2s when triggered by different owners.
*/
contract L2OpUSDCFactory is IL2OpUSDCFactory {
address internal constant _L2_MESSENGER = 0x4200000000000000000000000000000000000007;

/**
* @notice Deploys the USDC implementation, proxy, and L2 adapter contracts all at once, and then initializes the USDC
* @param _l1Adapter The address of the L1 adapter contract
Expand Down Expand Up @@ -43,8 +45,7 @@ contract L2OpUSDCFactory is IL2OpUSDCFactory {
emit USDCProxyDeployed(_usdcProxy);

// Deploy L2 Adapter
address _l2Messenger = 0x4200000000000000000000000000000000000007;
bytes memory _l2AdapterCArgs = abi.encode(_usdcProxy, _l2Messenger, _l1Adapter, _l2AdapterOwner);
bytes memory _l2AdapterCArgs = abi.encode(_usdcProxy, _L2_MESSENGER, _l1Adapter, _l2AdapterOwner);
bytes memory _l2AdapterInitCode = bytes.concat(type(L2OpUSDCBridgeAdapter).creationCode, _l2AdapterCArgs);
(address _l2Adapter) = _deployCreate(_l2AdapterInitCode);
emit L2AdapterDeployed(_l2Adapter);
Expand Down
11 changes: 6 additions & 5 deletions test/integration/L1OpUSDCBridgeAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ contract Integration_Bridging is IntegrationBase {

vm.prank(_signerAd);
MAINNET_USDC.approve(address(l1Adapter), _amount);

uint256 _nonce = vm.getNonce(_signerAd);
bytes memory _signature = _generateSignature(_signerAd, _amount, _nonce, _signerAd, _signerPk, address(l1Adapter));
uint256 _deadline = block.timestamp + 1 days;
uint256 _nonce = vm.getNonce(_signerAd);
bytes memory _signature =
_generateSignature(_signerAd, _amount, _deadline, _nonce, _signerAd, _signerPk, address(l1Adapter));

// Different address can execute the message
vm.prank(_user);
Expand Down Expand Up @@ -132,12 +132,13 @@ contract Integration_Bridging is IntegrationBase {

vm.prank(_signerAd);
MAINNET_USDC.approve(address(l1Adapter), _amount);
uint256 _deadline = block.timestamp + 1 days;

uint256 _nonce = vm.getNonce(_signerAd);

// Changing to `to` param to _user but we call it with _signerAd
bytes memory _signature = _generateSignature(_user, _amount, _nonce, _signerAd, _signerPk, address(l1Adapter));
uint256 _deadline = block.timestamp + 1 days;
bytes memory _signature =
_generateSignature(_user, _amount, _deadline, _nonce, _signerAd, _signerPk, address(l1Adapter));

// Different address can execute the message
vm.startPrank(_user);
Expand Down
12 changes: 6 additions & 6 deletions test/integration/L2OpUSDCBridgeAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ contract Integration_Bridging is IntegrationBase {

vm.prank(_signerAd);
bridgedUSDC.approve(address(l2Adapter), _amount);

uint256 _nonce = vm.getNonce(_signerAd);
bytes memory _signature = _generateSignature(_signerAd, _amount, _nonce, _signerAd, _signerPk, address(l2Adapter));
uint256 _deadline = block.timestamp + 1 days;
uint256 _nonce = vm.getNonce(_signerAd);
bytes memory _signature =
_generateSignature(_signerAd, _amount, _deadline, _nonce, _signerAd, _signerPk, address(l2Adapter));

// Different address can execute the message
vm.prank(_user);
Expand Down Expand Up @@ -144,12 +144,12 @@ contract Integration_Bridging is IntegrationBase {

vm.prank(_signerAd);
bridgedUSDC.approve(address(l2Adapter), _amount);

uint256 _deadline = block.timestamp + 1 days;
uint256 _nonce = vm.getNonce(_signerAd);

// Changing to `to` param to _user but we call it with _signerAd
bytes memory _signature = _generateSignature(_user, _amount, _nonce, _signerAd, _signerPk, address(l2Adapter));
uint256 _deadline = block.timestamp + 1 days;
bytes memory _signature =
_generateSignature(_user, _amount, _deadline, _nonce, _signerAd, _signerPk, address(l2Adapter));

// Different address can execute the message
vm.startPrank(_user);
Expand Down
12 changes: 8 additions & 4 deletions test/unit/L1OpUSDCBridgeAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,8 @@ contract L1OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
(address _notSignerAd, uint256 _notSignerPk) = makeAddrAndKey('notSigner');
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _notSignerAd, _notSignerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _notSignerAd, _notSignerPk, address(adapter));

// Execute
vm.prank(_user);
Expand All @@ -803,7 +804,8 @@ contract L1OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.assume(_deadline > 0);
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _signerAd, _signerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _signerAd, _signerPk, address(adapter));

vm.mockCall(
_usdc,
Expand Down Expand Up @@ -834,7 +836,8 @@ contract L1OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.assume(_deadline > 0);
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _signerAd, _signerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _signerAd, _signerPk, address(adapter));

_mockAndExpect(
_usdc,
Expand Down Expand Up @@ -864,7 +867,8 @@ contract L1OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.assume(_deadline > 0);
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _signerAd, _signerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _signerAd, _signerPk, address(adapter));

vm.mockCall(
_usdc,
Expand Down
12 changes: 8 additions & 4 deletions test/unit/L2OpUSDCBridgeAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ contract L2OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
(address _notSignerAd, uint256 _notSignerPk) = makeAddrAndKey('notSigner');
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _notSignerAd, _notSignerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _notSignerAd, _notSignerPk, address(adapter));

// Execute
vm.prank(_user);
Expand All @@ -448,7 +449,8 @@ contract L2OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.assume(_deadline > 0);
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _signerAd, _signerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _signerAd, _signerPk, address(adapter));
vm.mockCall(
_usdc,
abi.encodeWithSignature('transferFrom(address,address,uint256)', _user, address(adapter), _amount),
Expand Down Expand Up @@ -479,7 +481,8 @@ contract L2OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.assume(_deadline > 0);
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _signerAd, _signerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _signerAd, _signerPk, address(adapter));
_mockAndExpect(
_usdc,
abi.encodeWithSignature('transferFrom(address,address,uint256)', _signerAd, address(adapter), _amount),
Expand Down Expand Up @@ -509,7 +512,8 @@ contract L2OpUSDCBridgeAdapter_Unit_SendMessageWithSignature is Base {
vm.assume(_deadline > 0);
vm.warp(_deadline - 1);
uint256 _nonce = adapter.userNonce(_signerAd);
bytes memory _signature = _generateSignature(_to, _amount, _nonce, _signerAd, _signerPk, address(adapter));
bytes memory _signature =
_generateSignature(_to, _amount, _deadline, _nonce, _signerAd, _signerPk, address(adapter));
vm.mockCall(
_usdc,
abi.encodeWithSignature('transferFrom(address,address,uint256)', _user, address(adapter), _amount),
Expand Down
4 changes: 3 additions & 1 deletion test/utils/Helpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ contract Helpers is Test {
function _generateSignature(
address _to,
uint256 _amount,
uint256 _deadline,
uint256 _nonce,
address _signerAd,
uint256 _signerPk,
address _adapter
) internal returns (bytes memory _signature) {
vm.startPrank(_signerAd);
bytes32 _digest = keccak256(abi.encode(_adapter, block.chainid, _to, _amount, _nonce)).toEthSignedMessageHash();
bytes32 _digest =
keccak256(abi.encode(_adapter, block.chainid, _to, _amount, _deadline, _nonce)).toEthSignedMessageHash();
(uint8 v, bytes32 r, bytes32 s) = vm.sign(_signerPk, _digest);
_signature = abi.encodePacked(r, s, v);
vm.stopPrank();
Expand Down

0 comments on commit 54a75a9

Please sign in to comment.