Skip to content

Commit

Permalink
deployment working
Browse files Browse the repository at this point in the history
  • Loading branch information
dan13ram committed Sep 3, 2024
1 parent 493b12c commit fea4880
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 150 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"main": "index.js",
"repository": "https://github.com/yieldnest/yieldnest-cross-chain",
"scripts": {
"build": "yarn install && forge build",
"build": "yarn install && forge install && forge build",
"compile": "forge compile",
"format": "forge fmt --root .",
"test": "forge test",
"test": "forge test -vvv",
"prepare": "husky"
}
}
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ forge-std/=node_modules/forge-std/src/
@layerzerolabs/lz-evm-protocol-v2/contracts/=node_modules/@layerzerolabs/lz-evm-protocol-v2/contracts/
@layerzerolabs/lz-evm-messagelib-v2/contracts/=node_modules/@layerzerolabs/lz-evm-messagelib-v2/contracts/
@layerzerolabs/lz-evm-oapp-v2/contracts-upgradeable/=node_modules/layerzero-v2/oapp/contracts/
@layerzerolabs/lz-evm-oapp-v2/test/=node_modules/layerzero-v2/oapp/test/
@openzeppelin/contracts/=node_modules/@openzeppelin/contracts/
@openzeppelin/contracts-upgradeable/=node_modules/@openzeppelin/contracts-upgradeable/
@solmate/=lib/solmate/src/
Expand Down
5 changes: 2 additions & 3 deletions src/L2YnERC20Upgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ contract L2YnERC20Upgradeable is ERC20Upgradeable, AccessControlUpgradeable, IMi
_disableInitializers();
}

function initialize(string memory _name, string memory _symbol, address _minter) public initializer {
function initialize(string memory _name, string memory _symbol, address _owner) public initializer {
__ERC20_init(_name, _symbol);
__AccessControl_init();

_grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
_grantRole(MINTER_ROLE, _minter);
_grantRole(DEFAULT_ADMIN_ROLE, _owner);
}

function mint(address _to, uint256 _amount) public onlyRole(MINTER_ROLE) {
Expand Down
69 changes: 32 additions & 37 deletions src/factory/MultiChainDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity ^0.8.24;

import {CREATE3} from "solmate/utils/CREATE3.sol";
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {RateLimiter} from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/utils/RateLimiter.sol";
import {IMultiChainDeployer} from "@interfaces/IMultiChainDeployer.sol";
import {L2YnOFTAdapterUpgradeable} from "@adapters/L2YnOFTAdapterUpgradeable.sol";
import {L2YnERC20Upgradeable} from "@adapters/L2YnERC20Upgradeable.sol";
import "forge-std/console.sol";

contract MultiChainDeployer is IMultiChainDeployer {
event ContractCreated(address deployedAddress);
Expand All @@ -23,8 +23,6 @@ contract MultiChainDeployer is IMultiChainDeployer {
modifier containsCaller(bytes32 salt) {
// prevent contract submissions from being stolen from tx.pool by requiring
// that the first 20 bytes of the submitted salt match msg.sender.
console.log(address(bytes20(salt)));
console.log(msg.sender);
require(
(address(bytes20(salt)) == msg.sender) || (bytes20(salt) == bytes20(0)),
"Invalid salt - first 20 bytes of the salt must match calling address."
Expand All @@ -33,18 +31,15 @@ contract MultiChainDeployer is IMultiChainDeployer {
}

/// @inheritdoc IMultiChainDeployer
function deploy(bytes32 salt, bytes calldata creationCode)
function deploy(bytes32 salt, bytes memory initCode)
public
payable
override
containsCaller(salt)
returns (address _deployedContract)
{
// move the initialization code from calldata to memory.
bytes memory initCode = creationCode;

// get target deployment
address targetDeploymentAddress = getDeployed(msg.sender, salt);
address targetDeploymentAddress = CREATE3.getDeployed(salt);

require(
!_deployed[targetDeploymentAddress],
Expand All @@ -65,32 +60,39 @@ contract MultiChainDeployer is IMultiChainDeployer {
}

/// @inheritdoc IMultiChainDeployer
function deployOFTAdapter(
bytes32 salt,
bytes calldata creationCode,
function deployL2YnOFTAdapter(
bytes32 _implSalt,
bytes32 _proxySalt,
address _token,
address _lzEndpoint,
address _owner,
RateLimiter.RateLimitConfig[] calldata _rateLimitConfigs
) public returns (address _deployedContract) {
//deploy the contract with create3
_deployedContract = this.deploy(salt, creationCode);
// initialize the YnOFTAdapter
initializeOFTAdapter(_deployedContract, _rateLimitConfigs);
bytes memory bytecode = type(L2YnOFTAdapterUpgradeable).creationCode;
bytes memory constructorParams = abi.encode(_token, _lzEndpoint);
bytes memory contractCode = abi.encodePacked(bytecode, constructorParams);

address adapterImpl = deploy(_implSalt, contractCode);
_deployedContract = deployProxy(_proxySalt, adapterImpl, _owner);
L2YnOFTAdapterUpgradeable(_deployedContract).initialize(_owner, _rateLimitConfigs);
}

/// @inheritdoc IMultiChainDeployer
function deployYnERC20(bytes32 salt, bytes calldata creationCode, string memory _name, string memory _symbol)
public
returns (address _deployedContract)
{
//deploy the contract with create3
_deployedContract = _deployedContract = this.deploy(salt, creationCode);
// initialize the deployed ERC20
initializeYnERC20Upgradeable(_deployedContract, _name, _symbol);
function deployL2YnERC20(
bytes32 _implSalt,
bytes32 _proxySalt,
string memory _name,
string memory _symbol,
address _owner
) public returns (address _deployedContract) {
address adapterImpl = deploy(_implSalt, type(L2YnERC20Upgradeable).creationCode);
_deployedContract = deployProxy(_proxySalt, adapterImpl, _owner);
L2YnERC20Upgradeable(_deployedContract).initialize(_name, _symbol, _owner);
}

/// @inheritdoc IMultiChainDeployer
function getDeployed(address deployer, bytes32 salt) public view override returns (address deployed) {
function getDeployed(bytes32 salt) public view override returns (address deployed) {
// hash salt with the deployer address to give each deployer its own namespace
salt = keccak256(abi.encodePacked(deployer, salt));
return CREATE3.getDeployed(salt);
}

Expand All @@ -99,17 +101,10 @@ contract MultiChainDeployer is IMultiChainDeployer {
return _deployed[deploymentAddress];
}

/// @inheritdoc IMultiChainDeployer
function initializeOFTAdapter(address _deployedContract, RateLimiter.RateLimitConfig[] calldata _rateLimitConfigs)
public
{
L2YnOFTAdapterUpgradeable(_deployedContract).initialize(msg.sender, _rateLimitConfigs);
}

/// @inheritdoc IMultiChainDeployer
function initializeYnERC20Upgradeable(address _deployedContract, string memory _name, string memory _symbol)
public
{
L2YnERC20Upgradeable(_deployedContract).initialize(_name, _symbol, msg.sender);
function deployProxy(bytes32 salt, address implementation, address controller) internal returns (address proxy) {
bytes memory bytecode = type(TransparentUpgradeableProxy).creationCode;
bytes memory constructorParams = abi.encode(implementation, controller, "");
bytes memory contractCode = abi.encodePacked(bytecode, constructorParams);
proxy = deploy(salt, contractCode);
}
}
48 changes: 21 additions & 27 deletions src/interfaces/IMultiChainDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,46 @@ interface IMultiChainDeployer {

/// @notice Deploys a deployOFTAdapter contract using CREATE3 and initializes in the same call
/// @dev The provided salt is hashed together with msg.sender to generate the final salt
/// @param salt The deployer-specific salt for determining the deployed contract's address
/// @param creationCode The creation code of the contract to deploy
/// @param _implSalt the salt for the oft adapter to be passed to the initializer
/// @param _proxySalt the salt for the oft adapter to be passed to the initializer
/// @param _token the token address for the oft adapter to be passed to the initializer
/// @param _lzEndpoint the lz endpoint for the oft adapter to be passed to the initializer
/// @param _rateLimitConfigs the desired rate limit configs for the oft adapter to be passed to the initializer
/// @return deployed The address of the deployed contract
function deployOFTAdapter(
bytes32 salt,
bytes calldata creationCode,
function deployL2YnOFTAdapter(
bytes32 _implSalt,
bytes32 _proxySalt,
address _token,
address _lzEndpoint,
address _owner,
RateLimiter.RateLimitConfig[] calldata _rateLimitConfigs
) external returns (address deployed);

/// @notice Deploys a deployYnERC20 contract using CREATE3 and initializes in the same call
/// @dev The provided salt is hashed together with msg.sender to generate the final salt
/// @param salt The deployer-specific salt for determining the deployed contract's address
/// @param creationCode The creation code of the contract to deploy
/// @param _implSalt the salt for the oft adapter to be passed to the initializer
/// @param _proxySalt the salt for the oft adapter to be passed to the initializer
/// @param _name the name of the erc20 to be passed to the initializer
/// @param _symbol the symbol of the erc20 to be passed to the initializer
/// @param _owner the owner of the erc20 to be passed to the initializer
/// @return deployed The address of the deployed contract
function deployYnERC20(bytes32 salt, bytes calldata creationCode, string memory _name, string memory _symbol)
external
returns (address deployed);
function deployL2YnERC20(
bytes32 _implSalt,
bytes32 _proxySalt,
string memory _name,
string memory _symbol,
address _owner
) external returns (address deployed);

/// @notice Predicts the address of a deployed contract
/// @dev The provided salt is hashed together with the deployer address to generate the final salt
/// @param deployer The deployer account that will call deploy()
/// @param salt The deployer-specific salt for determining the deployed contract's address
/// @return deployed The address of the contract that will be deployed
function getDeployed(address deployer, bytes32 salt) external view returns (address deployed);
function getDeployed(bytes32 salt) external view returns (address deployed);

/// @dev Determine if a contract has already been deployed by the factory to a
/// given address.
/// @param deploymentAddress address The contract address to check.
/// @return True if the contract has been deployed, false otherwise.
function hasBeenDeployed(address deploymentAddress) external view returns (bool);

/// @notice Initializes a deployed YnOFTAdapter
/// @dev This is intended to be called atomically in the deployOFTAdapter, but can be called seperatly as well
/// @param _deployedContract the address of the deployed contract
/// @param _rateLimitConfigs the desired rate limit configs for the oft adapter
function initializeOFTAdapter(address _deployedContract, RateLimiter.RateLimitConfig[] calldata _rateLimitConfigs)
external;

/// @notice Initializes a deployed YnERC20Upgradeable contract
/// @dev This is intended to be called atomically in the deployYnERC20 call, but can be called seperatly as well
/// @param _deployedContract the address of the deployed contract
/// @param _name the name of the erc20
/// @param _symbol the symbol of the erc20
function initializeYnERC20Upgradeable(address _deployedContract, string memory _name, string memory _symbol)
external;
}
79 changes: 0 additions & 79 deletions test/CrossChainBaseTest.sol

This file was deleted.

Loading

0 comments on commit fea4880

Please sign in to comment.