Skip to content

Commit

Permalink
Add alias helper
Browse files Browse the repository at this point in the history
  • Loading branch information
neokry committed Nov 13, 2023
1 parent 69cf40c commit e63345c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 44 deletions.
2 changes: 1 addition & 1 deletion addresses/84531.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"Treasury": "0xb4cae287ca424171a7493e60af2983cb3b2a5a4a",
"Governor": "0xafb5069b56e66b579fda30551936bfd6c5d3221a",
"MerkleReserveMinter": "0xcb7442f81ebed5d525d2e97f8410751a08a7cd1d",
"MigrationDeployer": "0x7d6e75e6d71b1363c3cfeac82a2c540737c73210"
"MigrationDeployer": "0xf698fc9a4158d50ac7a16a546deee90f2166d7f7"
}
4 changes: 2 additions & 2 deletions deploys/84531.version2_new.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Merkle Reserve Minter: 0xcb7442f81ebed5d525d2e97f8410751a08a7cd1d
Migration Deployer: 0x7d6e75e6d71b1363c3cfeac82a2c540737c73210
Merkle Reserve Minter: 0x411a7476b2a197a3b3d5576040b1111f560a8b57
Migration Deployer: 0x1f94beb2656c02a3c367023ffeabc5c2673e8247
6 changes: 4 additions & 2 deletions script/GetInterfaceIds.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ pragma solidity ^0.8.13;
import "forge-std/Script.sol";
import "forge-std/console2.sol";

import { OPAddressAliasHelper } from "../src/lib/utils/OPAddressAliasHelper.sol";
import { IBaseMetadata } from "../src/token/metadata/interfaces/IBaseMetadata.sol";
import { IPropertyIPFSMetadataRenderer } from "../src/token/metadata/interfaces/IPropertyIPFSMetadataRenderer.sol";

contract GetInterfaceIds is Script {
function run() public view {
console2.logBytes4(type(IBaseMetadata).interfaceId);
console2.logBytes4(type(IPropertyIPFSMetadataRenderer).interfaceId);
console2.logAddress(OPAddressAliasHelper.applyL1ToL2Alias(0x7498e6e471f31e869f038D8DBffbDFdf650c3F95));
//console2.logBytes4(type(IBaseMetadata).interfaceId);
//console2.logBytes4(type(IPropertyIPFSMetadataRenderer).interfaceId);
}
}
35 changes: 18 additions & 17 deletions src/deployers/L2MigrationDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import { MerkleReserveMinter } from "../minters/MerkleReserveMinter.sol";
import { TokenTypesV2 } from "../token/types/TokenTypesV2.sol";
import { Ownable } from "../lib/utils/Ownable.sol";
import { ICrossDomainMessenger } from "./interfaces/ICrossDomainMessenger.sol";
import { OPAddressAliasHelper } from "../lib/utils/OPAddressAliasHelper.sol";

/// @title L2MigrationDeployer
/// @notice A deployer that allows a caller on L1 to deploy and seed a DAO on an OP Stack L2
/// @dev This contract is designed to be called from the OPStack L1CrossDomainMessenger or OptimismPortal
/// @author @neokry
contract L2MigrationDeployer {
/// ///
Expand Down Expand Up @@ -59,16 +61,6 @@ contract L2MigrationDeployer {
/// @notice Mapping of L1 deployer => L2 deployed token
mapping(address => address) public crossDomainDeployerToToken;

/// ///
/// MODIFIERS ///
/// ///

/// @notice Modifier to revert if sender is not cross domain messenger
modifier onlyCrossDomainMessenger() {
if (msg.sender != address(crossDomainMessenger)) revert NOT_CROSS_DOMAIN_MESSENGER();
_;
}

/// ///
/// CONSTRUCTOR ///
/// ///
Expand Down Expand Up @@ -100,7 +92,7 @@ contract L2MigrationDeployer {
IManager.AuctionParams calldata _auctionParams,
IManager.GovParams calldata _govParams,
MerkleReserveMinter.MerkleMinterSettings calldata _minterParams
) external onlyCrossDomainMessenger returns (address token) {
) external returns (address token) {
if (_getTokenFromSender() != address(0)) {
revert DAO_ALREADY_DEPLOYED();
}
Expand Down Expand Up @@ -128,17 +120,23 @@ contract L2MigrationDeployer {
}

///@notice Resets the stored deployment if L1 DAO wants to redeploy
function resetDeployment() external onlyCrossDomainMessenger {
function resetDeployment() external {
_resetTokenDeployer();
}

/// ///
/// HELPER FUNCTIONS ///
/// ///

/// @notice Helper method to get the address alias for simulation purposes
/// @param l1Address The L1 address to apply the alias to
function applyL1ToL2Alias(address l1Address) external pure returns (address) {
return OPAddressAliasHelper.applyL1ToL2Alias(l1Address);
}

///@notice Helper method to pass a call along to the deployed metadata renderer
/// @param _data The names of the properties to add
function callMetadataRenderer(bytes memory _data) external onlyCrossDomainMessenger {
function callMetadataRenderer(bytes memory _data) external {
(, address metadata, , , ) = _getDAOAddressesFromSender();

// Call the metadata renderer
Expand All @@ -151,7 +149,7 @@ contract L2MigrationDeployer {
}

///@notice Helper method to deposit ether from L1 DAO treasury to L2 DAO treasury
function depositToTreasury() external payable onlyCrossDomainMessenger {
function depositToTreasury() external payable {
(, , , address treasury, ) = _getDAOAddressesFromSender();

// Transfer ether to treasury
Expand All @@ -164,7 +162,7 @@ contract L2MigrationDeployer {
}

///@notice Transfers ownership of migrated DAO contracts to treasury
function renounceOwnership() external onlyCrossDomainMessenger {
function renounceOwnership() external {
(address token, , address auction, address treasury, ) = _getDAOAddressesFromSender();

// Transfer ownership of token contract
Expand All @@ -179,8 +177,11 @@ contract L2MigrationDeployer {
/// ///

function _xMsgSender() private view returns (address) {
// Return the xDomain message sender from the Optimism cross domain messenger
return ICrossDomainMessenger(crossDomainMessenger).xDomainMessageSender();
// Return the xDomain message sender
return
msg.sender == crossDomainMessenger
? ICrossDomainMessenger(crossDomainMessenger).xDomainMessageSender()
: OPAddressAliasHelper.undoL1ToL2Alias(msg.sender);
}

function _setTokenDeployer(address token) private returns (address deployer) {
Expand Down
27 changes: 27 additions & 0 deletions src/lib/utils/OPAddressAliasHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

// Source: https://github.com/ethereum-optimism/optimism/blob/96562692558e5c3851899488bcebe51fbe3b7f09/packages/contracts-bedrock/src/vendor/AddressAliasHelper.sol
library OPAddressAliasHelper {
uint160 constant offset = uint160(0x1111000000000000000000000000000000001111);

/// @notice Utility function that converts the address in the L1 that submitted a tx to
/// the inbox to the msg.sender viewed in the L2
/// @param l1Address the address in the L1 that triggered the tx to L2
/// @return l2Address L2 address as viewed in msg.sender
function applyL1ToL2Alias(address l1Address) internal pure returns (address l2Address) {
unchecked {
l2Address = address(uint160(l1Address) + offset);
}
}

/// @notice Utility function that converts the msg.sender viewed in the L2 to the
/// address in the L1 that submitted a tx to the inbox
/// @param l2Address L2 address as viewed in msg.sender
/// @return l1Address the address in the L1 that triggered the tx to L2
function undoL1ToL2Alias(address l2Address) internal pure returns (address l1Address) {
unchecked {
l1Address = address(uint160(l2Address) - offset);
}
}
}
22 changes: 0 additions & 22 deletions test/L2MigrationDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,6 @@ contract L2MigrationDeployerTest is NounsBuilderTest {
assertEq(address(treasury).balance, 0.1 ether);
}

function testRevert_OnlyCrossDomainMessenger() external {
setAltMockFounderParams();

setMockTokenParams();

setMockAuctionParams();

setMockGovParams();

vm.expectRevert(abi.encodeWithSignature("NOT_CROSS_DOMAIN_MESSENGER()"));
deployer.deploy(foundersArr, tokenParams, auctionParams, govParams, minterParams);

vm.expectRevert(abi.encodeWithSignature("NOT_CROSS_DOMAIN_MESSENGER()"));
addMetadataProperties();

vm.expectRevert(abi.encodeWithSignature("NOT_CROSS_DOMAIN_MESSENGER()"));
deployer.renounceOwnership();

vm.expectRevert(abi.encodeWithSignature("NOT_CROSS_DOMAIN_MESSENGER()"));
deployer.resetDeployment();
}

function testRevert_NoDAODeployed() external {
vm.startPrank(address(xDomainMessenger));

Expand Down

0 comments on commit e63345c

Please sign in to comment.