Skip to content

Commit

Permalink
test: l2 factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlagonia committed May 7, 2024
1 parent 7c78c31 commit 4f610dc
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 30 deletions.
18 changes: 9 additions & 9 deletions src/L2Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import {L2Token} from "@zkevm-stb/L2Token.sol";
import {L2Escrow} from "@zkevm-stb/L2Escrow.sol";
import {L2TokenConverter} from "@zkevm-stb/L2TokenConverter.sol";

/// @title Polygon CDK Stake the Bridge L2 Deployer.
/// @title Factory for deployment of L2 Contracts.
contract L2Factory {
address public l2Deployer;

address public l1Deployer;
address public immutable l1Deployer;

address public l2TokenImplementation;
address public immutable polygonZkEVMBridge;

address public l2EscrowImplementation;
address public immutable l2TokenImplementation;

address public l2ConverterImplementation;
address public immutable l2EscrowImplementation;

uint32 internal constant ORIGIN_NETWORK_ID = 0;
address public immutable l2ConverterImplementation;

address public constant polygonZkEVMBridge =
0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe;
uint32 internal constant ORIGIN_NETWORK_ID = 0;

constructor(address _l1Deployer) {
constructor(address _l1Deployer, address _polygonZkEVMBridge) {
l1Deployer = _l1Deployer;
polygonZkEVMBridge = _polygonZkEVMBridge;
l2TokenImplementation = address(new L2Token());
l2EscrowImplementation = address(new L2Escrow());
l2ConverterImplementation = address(new L2TokenConverter());
Expand Down
20 changes: 0 additions & 20 deletions test/L2Deployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ contract L2DeployerTest is Setup {
mockEscrow = deployMockL1Escrow();
}

// Deploy L2 Deployer using the mock bridge
function deployL2Contracts() public override {
l2TokenImpl = new L2Token();

l2EscrowImpl = new L2Escrow();

l2TokenConverterImpl = new L2TokenConverter();

l2Deployer = new L2Deployer(
l2Admin,
address(l1Deployer),
l2RiskManager,
l2EscrowManager,
address(polygonZkEVMBridge),
address(l2TokenImpl),
address(l2EscrowImpl),
address(l2TokenConverterImpl)
);
}

function test_transferAdmin() public {
bytes32 L2_ADMIN = l2Deployer.L2_ADMIN();
bytes32 PENDING_ADMIN = l2Deployer.PENDING_ADMIN();
Expand Down
175 changes: 175 additions & 0 deletions test/L2Factory.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity >=0.8.18;

import {MockBridge} from "./mocks/MockBridge.sol";
import {IPolygonRollupManager, IPolygonRollupContract} from "../src/interfaces/Polygon/IPolygonRollupManager.sol";
import {Setup, console, L2Token, L2Escrow, L2TokenConverter, L2Factory, L2Deployer, IPolygonZkEVMBridge} from "./utils/Setup.sol";

contract L2FactoryTest is Setup {
event RegisteredNewRollup(
uint32 indexed rollupID,
address indexed rollupContract,
address indexed escrowManager
);

function setUp() public virtual override {
polygonZkEVMBridge = IPolygonZkEVMBridge(address(new MockBridge()));
super.setUp();
}

function deployL2Contracts() public override {
l2Factory = new L2Factory(
address(l1Deployer),
address(polygonZkEVMBridge)
);

l2TokenImpl = L2Token(l2Factory.l2TokenImplementation());

l2EscrowImpl = L2Escrow(l2Factory.l2EscrowImplementation());

l2TokenConverterImpl = L2TokenConverter(
l2Factory.l2ConverterImplementation()
);
}

function test_deployL2Deployer() public {
bytes memory data = abi.encode(l2Admin, l2RiskManager, l2EscrowManager);

vm.expectRevert("L2Factory: Not PolygonZkEVMBridge");
l2Factory.onMessageReceived(address(l1Deployer), l1RollupID, data);

vm.expectRevert("L2Factory: Not deployer contract");
vm.prank(address(polygonZkEVMBridge));
l2Factory.onMessageReceived(address(69), l1RollupID, data);

vm.expectRevert("L2Deployer: Not counterpart network");
vm.prank(address(polygonZkEVMBridge));
l2Factory.onMessageReceived(address(l1Deployer), l2RollupID, data);

vm.prank(address(polygonZkEVMBridge));
l2Factory.onMessageReceived(address(l1Deployer), l1RollupID, data);

l2Deployer = L2Deployer(l2Factory.l2Deployer());
assertNeq(address(l2Deployer), address(0));
}

function test_l1AdminDeploysDeployer_notRegistered() public {
uint32 rollupID = 1;
assertEq(l1Deployer.getRollupContract(rollupID), address(0));
assertEq(l2Factory.l2Deployer(), address(0));

address rollupContract = address(
IPolygonRollupManager(polygonZkEVMBridge.polygonRollupManager())
.rollupIDToRollupData(rollupID)
.rollupContract
);
address rollupAdmin = address(
IPolygonRollupManager(polygonZkEVMBridge.polygonRollupManager())
.rollupIDToRollupData(rollupID)
.rollupContract
.admin()
);

vm.expectRevert("!admin");
l1Deployer.deployL2Deployer(
rollupID,
l2Admin,
l2RiskManager,
l2EscrowManager
);

vm.expectRevert("ZERO ADDRESS");
vm.prank(rollupAdmin);
l1Deployer.deployL2Deployer(
rollupID,
address(0),
l2RiskManager,
l2EscrowManager
);

vm.expectEmit(true, true, true, true, address(l1Deployer));
emit RegisteredNewRollup(rollupID, rollupContract, rollupAdmin);
vm.prank(rollupAdmin);
l1Deployer.deployL2Deployer(
rollupID,
l2Admin,
l2RiskManager,
l2EscrowManager
);

l2Deployer = L2Deployer(l2Factory.l2Deployer());
assertEq(l1Deployer.getRollupContract(rollupID), rollupContract);
assertNeq(address(l2Deployer), address(0));
assertEq(l2Deployer.getPositionHolder(l2Deployer.L2_ADMIN()), l2Admin);
assertEq(
l2Deployer.getPositionHolder(l2Deployer.RISK_MANAGER()),
l2RiskManager
);
assertEq(
l2Deployer.getPositionHolder(l2Deployer.ESCROW_MANAGER()),
l2EscrowManager
);
}

function test_l1AdminDeploysDeployer_registered() public {
uint32 rollupID = 1;
assertEq(l1Deployer.getRollupContract(rollupID), address(0));
assertEq(l2Factory.l2Deployer(), address(0));

address rollupContract = address(
IPolygonRollupManager(polygonZkEVMBridge.polygonRollupManager())
.rollupIDToRollupData(rollupID)
.rollupContract
);
address rollupAdmin = address(
IPolygonRollupManager(polygonZkEVMBridge.polygonRollupManager())
.rollupIDToRollupData(rollupID)
.rollupContract
.admin()
);

vm.expectEmit(true, true, true, true, address(l1Deployer));
emit RegisteredNewRollup(rollupID, rollupContract, rollupAdmin);
l1Deployer.registerRollup(rollupID, address(0));

assertEq(l1Deployer.getRollupContract(rollupID), rollupContract);

vm.expectRevert("!admin");
l1Deployer.deployL2Deployer(
rollupID,
l2Admin,
l2RiskManager,
l2EscrowManager
);

vm.expectRevert("ZERO ADDRESS");
vm.prank(rollupAdmin);
l1Deployer.deployL2Deployer(
rollupID,
address(0),
l2RiskManager,
l2EscrowManager
);

vm.prank(rollupAdmin);
l1Deployer.deployL2Deployer(
rollupID,
l2Admin,
l2RiskManager,
l2EscrowManager
);

l2Deployer = L2Deployer(l2Factory.l2Deployer());
assertEq(l1Deployer.getRollupContract(rollupID), rollupContract);
assertNeq(address(l2Deployer), address(0));
assertEq(l2Deployer.getPositionHolder(l2Deployer.L2_ADMIN()), l2Admin);
assertEq(
l2Deployer.getPositionHolder(l2Deployer.RISK_MANAGER()),
l2RiskManager
);
assertEq(
l2Deployer.getPositionHolder(l2Deployer.ESCROW_MANAGER()),
l2EscrowManager
);
}
}
5 changes: 4 additions & 1 deletion test/utils/Setup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ contract Setup is ExtendedTest {
}

function deployL2Contracts() public virtual {
l2Factory = new L2Factory(address(l1Deployer));
l2Factory = new L2Factory(
address(l1Deployer),
address(polygonZkEVMBridge)
);

l2TokenImpl = L2Token(l2Factory.l2TokenImplementation());

Expand Down

0 comments on commit 4f610dc

Please sign in to comment.