|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {IPoolManager, PoolKey, Currency, TickMath} from "v4-core/src/PoolManager.sol"; |
| 5 | +import {Ownable} from "@openzeppelin/access/Ownable.sol"; |
| 6 | +import {ERC20} from "@openzeppelin/token/ERC20/ERC20.sol"; |
| 7 | +import {ITokenFactory} from "src/interfaces/ITokenFactory.sol"; |
| 8 | +import {IGovernanceFactory} from "src/interfaces/IGovernanceFactory.sol"; |
| 9 | +import {IHookFactory, IHook} from "src/interfaces/IHookFactory.sol"; |
| 10 | +import {IMigrator} from "src/interfaces/IMigrator.sol"; |
| 11 | + |
| 12 | +enum ModuleState { |
| 13 | + NotWhitelisted, |
| 14 | + TokenFactory, |
| 15 | + GovernanceFactory, |
| 16 | + HookFactory, |
| 17 | + Migrator |
| 18 | +} |
| 19 | + |
| 20 | +error WrongModuleState(); |
| 21 | + |
| 22 | +error WrongInitialSupply(); |
| 23 | + |
| 24 | +error ArrayLengthsMismatch(); |
| 25 | + |
| 26 | +struct TokenData { |
| 27 | + PoolKey poolKey; |
| 28 | + address timelock; |
| 29 | + address governance; |
| 30 | + IMigrator migrator; |
| 31 | + address[] recipients; |
| 32 | + uint256[] amounts; |
| 33 | +} |
| 34 | + |
| 35 | +event Create(address asset, PoolKey poolKey, address hook); |
| 36 | + |
| 37 | +event Migrate(address asset, address pool); |
| 38 | + |
| 39 | +event SetModuleState(address module, ModuleState state); |
| 40 | + |
| 41 | +contract Airlock is Ownable { |
| 42 | + IPoolManager public immutable poolManager; |
| 43 | + |
| 44 | + mapping(address => ModuleState) public getModuleState; |
| 45 | + mapping(address token => TokenData) public getTokenData; |
| 46 | + |
| 47 | + receive() external payable {} |
| 48 | + |
| 49 | + constructor(IPoolManager poolManager_) Ownable(msg.sender) { |
| 50 | + poolManager = poolManager_; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * TODO: |
| 55 | + * - Creating a token should incur fees (platform and frontend fees) |
| 56 | + * |
| 57 | + * @param tokenFactory Address of the factory contract deploying the ERC20 token |
| 58 | + * @param governanceFactory Address of the factory contract deploying the governance |
| 59 | + * @param hookFactory Address of the factory contract deploying the Uniswap v4 hook |
| 60 | + */ |
| 61 | + function create( |
| 62 | + string memory name, |
| 63 | + string memory symbol, |
| 64 | + uint256 initialSupply, |
| 65 | + uint256 numTokensToSell, |
| 66 | + PoolKey memory poolKey, |
| 67 | + address owner, |
| 68 | + address[] memory recipients, |
| 69 | + uint256[] memory amounts, |
| 70 | + ITokenFactory tokenFactory, |
| 71 | + bytes memory tokenData, |
| 72 | + IGovernanceFactory governanceFactory, |
| 73 | + bytes memory governanceData, |
| 74 | + IHookFactory hookFactory, |
| 75 | + bytes memory hookData, |
| 76 | + IMigrator migrator, |
| 77 | + bytes32 salt |
| 78 | + ) external returns (address, address, address) { |
| 79 | + require(getModuleState[address(tokenFactory)] == ModuleState.TokenFactory, WrongModuleState()); |
| 80 | + require(getModuleState[address(governanceFactory)] == ModuleState.GovernanceFactory, WrongModuleState()); |
| 81 | + require(getModuleState[address(hookFactory)] == ModuleState.HookFactory, WrongModuleState()); |
| 82 | + require(getModuleState[address(migrator)] == ModuleState.Migrator, WrongModuleState()); |
| 83 | + |
| 84 | + require(recipients.length == amounts.length, ArrayLengthsMismatch()); |
| 85 | + |
| 86 | + uint256 totalToMint = numTokensToSell; |
| 87 | + for (uint256 i; i < amounts.length; i++) { |
| 88 | + totalToMint += amounts[i]; |
| 89 | + } |
| 90 | + require(totalToMint == initialSupply, WrongInitialSupply()); |
| 91 | + |
| 92 | + address token = tokenFactory.create(name, symbol, initialSupply, address(this), address(this), tokenData, salt); |
| 93 | + address hook = hookFactory.create(poolManager, numTokensToSell, hookData, salt); |
| 94 | + |
| 95 | + ERC20(token).transfer(hook, numTokensToSell); |
| 96 | + |
| 97 | + // TODO: I don't think we need to pass the salt here, create2 is not needed anyway. |
| 98 | + (address governance, address timelock) = governanceFactory.create(name, token, governanceData); |
| 99 | + Ownable(token).transferOwnership(timelock); |
| 100 | + |
| 101 | + getTokenData[token] = TokenData({ |
| 102 | + governance: governance, |
| 103 | + recipients: recipients, |
| 104 | + amounts: amounts, |
| 105 | + migrator: migrator, |
| 106 | + timelock: timelock, |
| 107 | + poolKey: poolKey |
| 108 | + }); |
| 109 | + |
| 110 | + // TODO: Do we really have to initialize the pool at the right price? |
| 111 | + poolManager.initialize(poolKey, TickMath.getSqrtPriceAtTick(0), new bytes(0)); |
| 112 | + |
| 113 | + emit Create(token, poolKey, hook); |
| 114 | + |
| 115 | + return (token, governance, hook); |
| 116 | + } |
| 117 | + |
| 118 | + function migrate(address asset) external { |
| 119 | + TokenData memory tokenData = getTokenData[asset]; |
| 120 | + |
| 121 | + uint256 length = tokenData.recipients.length; |
| 122 | + for (uint256 i; i < length; i++) { |
| 123 | + ERC20(asset).transfer(tokenData.recipients[i], tokenData.amounts[i]); |
| 124 | + } |
| 125 | + |
| 126 | + (uint256 amount0, uint256 amount1) = IHook(address(tokenData.poolKey.hooks)).migrate(); |
| 127 | + |
| 128 | + address currency0 = Currency.unwrap(tokenData.poolKey.currency0); |
| 129 | + address currency1 = Currency.unwrap(tokenData.poolKey.currency1); |
| 130 | + |
| 131 | + if (currency0 != address(0)) ERC20(currency0).transfer(address(tokenData.migrator), amount0); |
| 132 | + ERC20(currency1).transfer(address(tokenData.migrator), amount1); |
| 133 | + |
| 134 | + (address pool,) = tokenData.migrator.migrate{value: currency0 == address(0) ? amount0 : 0}( |
| 135 | + currency0, currency1, amount0, amount1, tokenData.timelock, new bytes(0) |
| 136 | + ); |
| 137 | + |
| 138 | + emit Migrate(asset, pool); |
| 139 | + } |
| 140 | + |
| 141 | + // TODO: Maybe we should accept arrays here to batch update states? |
| 142 | + function setModuleState(address module, ModuleState state) external onlyOwner { |
| 143 | + getModuleState[module] = state; |
| 144 | + emit SetModuleState(module, state); |
| 145 | + } |
| 146 | +} |
0 commit comments