forked from timeless-fi/yield-daddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ERC4626Factory.sol
65 lines (55 loc) · 3.09 KB
/
ERC4626Factory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.13;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ERC4626} from "solmate/mixins/ERC4626.sol";
import {Bytes32AddressLib} from "solmate/utils/Bytes32AddressLib.sol";
/// @title ERC4626Factory
/// @author zefram.eth
/// @notice Abstract base contract for deploying ERC4626 wrappers
/// @dev Uses CREATE2 deterministic deployment, so there can only be a single
/// vault for each asset.
abstract contract ERC4626Factory {
/// -----------------------------------------------------------------------
/// Library usage
/// -----------------------------------------------------------------------
using Bytes32AddressLib for bytes32;
/// -----------------------------------------------------------------------
/// Events
/// -----------------------------------------------------------------------
/// @notice Emitted when a new ERC4626 vault has been created
/// @param asset The base asset used by the vault
/// @param vault The vault that was created
event CreateERC4626(ERC20 indexed asset, ERC4626 vault);
/// -----------------------------------------------------------------------
/// External functions
/// -----------------------------------------------------------------------
/// @notice Creates an ERC4626 vault for an asset
/// @dev Uses CREATE2 deterministic deployment, so there can only be a single
/// vault for each asset. Will revert if a vault has already been deployed for the asset.
/// @param asset The base asset used by the vault
/// @return vault The vault that was created
function createERC4626(ERC20 asset) external virtual returns (ERC4626 vault);
/// @notice Computes the address of the ERC4626 vault corresponding to an asset. Returns
/// a valid result regardless of whether the vault has already been deployed.
/// @param asset The base asset used by the vault
/// @return vault The vault corresponding to the asset
function computeERC4626Address(ERC20 asset) external view virtual returns (ERC4626 vault);
/// -----------------------------------------------------------------------
/// Internal functions
/// -----------------------------------------------------------------------
/// @notice Computes the address of a contract deployed by this factory using CREATE2, given
/// the bytecode hash of the contract. Can also be used to predict addresses of contracts yet to
/// be deployed.
/// @dev Always uses bytes32(0) as the salt
/// @param bytecodeHash The keccak256 hash of the creation code of the contract being deployed concatenated
/// with the ABI-encoded constructor arguments.
/// @return The address of the deployed contract
function _computeCreate2Address(bytes32 bytecodeHash) internal view virtual returns (address) {
return keccak256(abi.encodePacked(bytes1(0xFF), address(this), bytes32(0), bytecodeHash))
// Prefix:
// Creator:
// Salt:
// Bytecode hash:
.fromLast20Bytes(); // Convert the CREATE2 hash into an address.
}
}