Skip to content

Commit

Permalink
Morpho instance boilerplate codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
cashd committed Apr 26, 2024
1 parent daa84ff commit 57e06d8
Show file tree
Hide file tree
Showing 16 changed files with 1,130 additions and 0 deletions.
45 changes: 45 additions & 0 deletions contracts/src/deployers/morpho/MorphoHyperdriveCoreDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IHyperdriveCoreDeployer } from "../../interfaces/IHyperdriveCoreDeployer.sol";
import { MorphoHyperdrive } from "../../instances/morpho/MorphoHyperdrive.sol";

/// @author DELV
/// @title MorphoHyperdriveCoreDeployer
/// @notice The core deployer for the MorphoHyperdrive implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoHyperdriveCoreDeployer is IHyperdriveCoreDeployer {
/// @notice Deploys a Hyperdrive instance with the given parameters.
/// @param _config The configuration of the Hyperdrive pool.
/// @param _target0 The target0 address.
/// @param _target1 The target1 address.
/// @param _target2 The target2 address.
/// @param _target3 The target3 address.
/// @param _target4 The target4 address.
/// @param _salt The create2 salt used in the deployment.
/// @return The address of the newly deployed MorphoHyperdrive instance.
function deploy(
IHyperdrive.PoolConfig memory _config,
bytes memory, // unused _extraData,
address _target0,
address _target1,
address _target2,
address _target3,
address _target4,
bytes32 _salt
) external returns (address) {
return (
address(
// NOTE: We hash the sender with the salt to prevent the
// front-running of deployments.
new MorphoHyperdrive{
salt: keccak256(abi.encode(msg.sender, _salt))
}(_config, _target0, _target1, _target2, _target3, _target4)
)
);
}
}
135 changes: 135 additions & 0 deletions contracts/src/deployers/morpho/MorphoHyperdriveDeployerCoordinator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { ERC20 } from "openzeppelin/token/ERC20/ERC20.sol";
import { SafeERC20 } from "openzeppelin/token/ERC20/utils/SafeERC20.sol";
import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IMorphoHyperdrive } from "../../interfaces/IMorphoHyperdrive.sol";
import { IHyperdriveDeployerCoordinator } from "../../interfaces/IHyperdriveDeployerCoordinator.sol";
import { ONE } from "../../libraries/FixedPointMath.sol";
import { HyperdriveDeployerCoordinator } from "../HyperdriveDeployerCoordinator.sol";

/// @author DELV
/// @title MorphoHyperdriveDeployerCoordinator
/// @notice The deployer coordinator for the MorphoHyperdrive
/// implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoHyperdriveDeployerCoordinator is HyperdriveDeployerCoordinator {
using SafeERC20 for ERC20;

/// @notice The deployer coordinator's name.
string public constant override name =
"MorphoHyperdriveDeployerCoordinator";

/// @notice Instantiates the deployer coordinator.
/// @param _factory The factory that this deployer will be registered with.
/// @param _coreDeployer The core deployer.
/// @param _target0Deployer The target0 deployer.
/// @param _target1Deployer The target1 deployer.
/// @param _target2Deployer The target2 deployer.
/// @param _target3Deployer The target3 deployer.
/// @param _target4Deployer The target4 deployer.
constructor(
address _factory,
address _coreDeployer,
address _target0Deployer,
address _target1Deployer,
address _target2Deployer,
address _target3Deployer,
address _target4Deployer
)
HyperdriveDeployerCoordinator(
_factory,
_coreDeployer,
_target0Deployer,
_target1Deployer,
_target2Deployer,
_target3Deployer,
_target4Deployer
)
{}

/// @dev Prepares the coordinator for initialization by drawing funds from
/// the LP, if necessary.
/// @param _hyperdrive The Hyperdrive instance that is being initialized.
/// @param _lp The LP that is initializing the pool.
/// @param _contribution The amount of capital to supply. The units of this
/// quantity are either base or vault shares, depending on the value
/// of `_options.asBase`.
/// @param _options The options that configure how the initialization is
/// settled.
/// @return value The value that should be sent in the initialize transaction.
function _prepareInitialize(
IHyperdrive _hyperdrive,
address _lp,
uint256 _contribution,
IHyperdrive.Options memory _options
) internal override returns (uint256 value) {
// If base is the deposit asset, the initialization will be paid in the
// base token.
address token;
if (_options.asBase) {
token = _hyperdrive.baseToken();
}
// Otherwise, the initialization will be paid in vault shares.
else {
token = _hyperdrive.vaultSharesToken();
}

// ****************************************************************
// FIXME: Implement this for new instances. ERC20 example provided.
// Take custody of the contribution and approve Hyperdrive to pull the
// tokens.
ERC20(token).safeTransferFrom(_lp, address(this), _contribution);
ERC20(token).forceApprove(address(_hyperdrive), _contribution);
// ****************************************************************

return value;
}

/// @dev Prevents the contract from receiving ether.
function _checkMessageValue() internal view override {
if (msg.value != 0) {
revert IHyperdriveDeployerCoordinator.NotPayable();
}
}

/// @notice Checks the pool configuration to ensure that it is valid.
/// @param _deployConfig The deploy configuration of the Hyperdrive pool.
function _checkPoolConfig(
IHyperdrive.PoolDeployConfig memory _deployConfig
) internal view override {
// Perform the default checks.
super._checkPoolConfig(_deployConfig);

// Ensure that the minimum share reserves are equal to 1e15. This value
// has been tested to prevent arithmetic overflows in the
// `_updateLiquidity` function when the share reserves are as high as
// 200 million.
if (_deployConfig.minimumShareReserves != 1e15) {
revert IHyperdriveDeployerCoordinator.InvalidMinimumShareReserves();
}

// Ensure that the minimum transaction amount are equal to 1e15. This
// value has been tested to prevent precision issues.
if (_deployConfig.minimumTransactionAmount != 1e15) {
revert IHyperdriveDeployerCoordinator
.InvalidMinimumTransactionAmount();
}
}

/// @dev Gets the initial vault share price of the Hyperdrive pool.
/// @return The initial vault share price of the Hyperdrive pool.
function _getInitialVaultSharePrice(
IHyperdrive.PoolDeployConfig memory, // unused _deployConfig
bytes memory // unused _extraData
) internal pure override returns (uint256) {
// ****************************************************************
// FIXME: Implement this for new instances.
return ONE;
// ****************************************************************
}
}
34 changes: 34 additions & 0 deletions contracts/src/deployers/morpho/MorphoTarget0Deployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { MorphoTarget0 } from "../../instances/morpho/MorphoTarget0.sol";
import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IHyperdriveTargetDeployer } from "../../interfaces/IHyperdriveTargetDeployer.sol";

/// @author DELV
/// @title MorphoTarget0Deployer
/// @notice The target0 deployer for the MorphoHyperdrive implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoTarget0Deployer is IHyperdriveTargetDeployer {
/// @notice Deploys a target0 instance with the given parameters.
/// @param _config The configuration of the Hyperdrive pool.
/// @param _salt The create2 salt used in the deployment.
/// @return The address of the newly deployed MorphoTarget0 instance.
function deploy(
IHyperdrive.PoolConfig memory _config,
bytes memory, // unused _extraData
bytes32 _salt
) external returns (address) {
return
address(
// NOTE: We hash the sender with the salt to prevent the
// front-running of deployments.
new MorphoTarget0{
salt: keccak256(abi.encode(msg.sender, _salt))
}(_config)
);
}
}
34 changes: 34 additions & 0 deletions contracts/src/deployers/morpho/MorphoTarget1Deployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { MorphoTarget1 } from "../../instances/morpho/MorphoTarget1.sol";
import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IHyperdriveTargetDeployer } from "../../interfaces/IHyperdriveTargetDeployer.sol";

/// @author DELV
/// @title MorphoTarget1Deployer
/// @notice The target1 deployer for the MorphoHyperdrive implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoTarget1Deployer is IHyperdriveTargetDeployer {
/// @notice Deploys a target1 instance with the given parameters.
/// @param _config The configuration of the Hyperdrive pool.
/// @param _salt The create2 salt used in the deployment.
/// @return The address of the newly deployed MorphoTarget1 instance.
function deploy(
IHyperdrive.PoolConfig memory _config,
bytes memory, // unused _extraData
bytes32 _salt
) external returns (address) {
return
address(
// NOTE: We hash the sender with the salt to prevent the
// front-running of deployments.
new MorphoTarget1{
salt: keccak256(abi.encode(msg.sender, _salt))
}(_config)
);
}
}
34 changes: 34 additions & 0 deletions contracts/src/deployers/morpho/MorphoTarget2Deployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { MorphoTarget2 } from "../../instances/morpho/MorphoTarget2.sol";
import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IHyperdriveTargetDeployer } from "../../interfaces/IHyperdriveTargetDeployer.sol";

/// @author DELV
/// @title MorphoTarget2Deployer
/// @notice The target2 deployer for the MorphoHyperdrive implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoTarget2Deployer is IHyperdriveTargetDeployer {
/// @notice Deploys a target2 instance with the given parameters.
/// @param _config The configuration of the Hyperdrive pool.
/// @param _salt The create2 salt used in the deployment.
/// @return The address of the newly deployed MorphoTarget2 instance.
function deploy(
IHyperdrive.PoolConfig memory _config,
bytes memory, // unused _extraData
bytes32 _salt
) external returns (address) {
return
address(
// NOTE: We hash the sender with the salt to prevent the
// front-running of deployments.
new MorphoTarget2{
salt: keccak256(abi.encode(msg.sender, _salt))
}(_config)
);
}
}
34 changes: 34 additions & 0 deletions contracts/src/deployers/morpho/MorphoTarget3Deployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { MorphoTarget3 } from "../../instances/morpho/MorphoTarget3.sol";
import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IHyperdriveTargetDeployer } from "../../interfaces/IHyperdriveTargetDeployer.sol";

/// @author DELV
/// @title MorphoTarget3Deployer
/// @notice The target3 deployer for the MorphoHyperdrive implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoTarget3Deployer is IHyperdriveTargetDeployer {
/// @notice Deploys a target3 instance with the given parameters.
/// @param _config The configuration of the Hyperdrive pool.
/// @param _salt The create2 salt used in the deployment.
/// @return The address of the newly deployed MorphoTarget3 instance.
function deploy(
IHyperdrive.PoolConfig memory _config,
bytes memory, // unused _extraData
bytes32 _salt
) external returns (address) {
return
address(
// NOTE: We hash the sender with the salt to prevent the
// front-running of deployments.
new MorphoTarget3{
salt: keccak256(abi.encode(msg.sender, _salt))
}(_config)
);
}
}
34 changes: 34 additions & 0 deletions contracts/src/deployers/morpho/MorphoTarget4Deployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;

import { MorphoTarget4 } from "../../instances/morpho/MorphoTarget4.sol";
import { IMorpho } from "../../interfaces/IMorpho.sol";
import { IHyperdrive } from "../../interfaces/IHyperdrive.sol";
import { IHyperdriveTargetDeployer } from "../../interfaces/IHyperdriveTargetDeployer.sol";

/// @author DELV
/// @title MorphoTarget4Deployer
/// @notice The target4 deployer for the MorphoHyperdrive implementation.
/// @custom:disclaimer The language used in this code is for coding convenience
/// only, and is not intended to, and does not, have any
/// particular legal or regulatory significance.
contract MorphoTarget4Deployer is IHyperdriveTargetDeployer {
/// @notice Deploys a target4 instance with the given parameters.
/// @param _config The configuration of the Hyperdrive pool.
/// @param _salt The create2 salt used in the deployment.
/// @return The address of the newly deployed MorphoTarget4 instance.
function deploy(
IHyperdrive.PoolConfig memory _config,
bytes memory, // unused _extraData
bytes32 _salt
) external returns (address) {
return
address(
// NOTE: We hash the sender with the salt to prevent the
// front-running of deployments.
new MorphoTarget4{
salt: keccak256(abi.encode(msg.sender, _salt))
}(_config)
);
}
}
Loading

0 comments on commit 57e06d8

Please sign in to comment.