|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import {Ownable} from "openzeppelin-contracts/access/Ownable.sol"; |
| 5 | +import {MaglevEulerSwap as Maglev, MaglevBase} from "./MaglevEulerSwap.sol"; |
| 6 | + |
| 7 | +/// @title MaglevEulerSwapRegistry contract |
| 8 | +/// @custom:security-contact [email protected] |
| 9 | +/// @author Euler Labs (https://www.eulerlabs.com/) |
| 10 | +contract MaglevEulerSwapFactory is Ownable { |
| 11 | + event PoolDeployed(address indexed asset0, address indexed asset1, uint256 indexed feeMultiplier, address pool); |
| 12 | + |
| 13 | + error InvalidQuery(); |
| 14 | + error PoolAlreadyDeployed(); |
| 15 | + |
| 16 | + /// @dev EVC address. |
| 17 | + address public immutable evc; |
| 18 | + /// @dev An array to store all pools addresses. |
| 19 | + address[] public allPools; |
| 20 | + /// @dev Mapping from asset0/asset1/fee => pool address. |
| 21 | + mapping(address => mapping(address => mapping(uint256 => address))) public getPool; |
| 22 | + |
| 23 | + constructor(address evcAddr) Ownable(msg.sender) { |
| 24 | + evc = evcAddr; |
| 25 | + } |
| 26 | + |
| 27 | + /// @notice Deploy EulerSwap pool. |
| 28 | + function deployPool( |
| 29 | + address vault0, |
| 30 | + address vault1, |
| 31 | + address holder, |
| 32 | + uint112 debtLimit0, |
| 33 | + uint112 debtLimit1, |
| 34 | + uint256 fee, |
| 35 | + uint256 priceX, |
| 36 | + uint256 priceY, |
| 37 | + uint256 concentrationX, |
| 38 | + uint256 concentrationY |
| 39 | + ) external onlyOwner returns (address) { |
| 40 | + Maglev pool = new Maglev( |
| 41 | + MaglevBase.BaseParams({ |
| 42 | + evc: address(evc), |
| 43 | + vault0: vault0, |
| 44 | + vault1: vault1, |
| 45 | + myAccount: holder, |
| 46 | + debtLimit0: debtLimit0, |
| 47 | + debtLimit1: debtLimit1, |
| 48 | + fee: fee |
| 49 | + }), |
| 50 | + Maglev.EulerSwapParams({ |
| 51 | + priceX: priceX, |
| 52 | + priceY: priceY, |
| 53 | + concentrationX: concentrationX, |
| 54 | + concentrationY: concentrationY |
| 55 | + }) |
| 56 | + ); |
| 57 | + |
| 58 | + address poolAsset0 = pool.asset0(); |
| 59 | + address poolAsset1 = pool.asset1(); |
| 60 | + uint256 feeMultiplier = pool.feeMultiplier(); |
| 61 | + |
| 62 | + require(getPool[poolAsset0][poolAsset1][feeMultiplier] == address(0), PoolAlreadyDeployed()); |
| 63 | + |
| 64 | + getPool[poolAsset0][poolAsset1][feeMultiplier] = address(pool); |
| 65 | + // populate mapping in the reverse direction, deliberate choice to avoid the cost of comparing addresses |
| 66 | + getPool[poolAsset1][poolAsset0][feeMultiplier] = address(pool); |
| 67 | + |
| 68 | + allPools.push(address(pool)); |
| 69 | + |
| 70 | + emit PoolDeployed(poolAsset0, poolAsset1, feeMultiplier, address(pool)); |
| 71 | + |
| 72 | + return address(pool); |
| 73 | + } |
| 74 | + |
| 75 | + /// @notice Get the length of `allPools` array. |
| 76 | + /// @return `allPools` length. |
| 77 | + function allPoolsLength() external view returns (uint256) { |
| 78 | + return allPools.length; |
| 79 | + } |
| 80 | + |
| 81 | + /// @notice Get a slice of the registered pools array. |
| 82 | + /// @param _start Start index of the slice. |
| 83 | + /// @param _end End index of the slice. |
| 84 | + /// @return An array containing the slice of the registered pools. |
| 85 | + function getAllPoolsListSlice(uint256 _start, uint256 _end) external view returns (address[] memory) { |
| 86 | + uint256 length = allPools.length; |
| 87 | + if (_end == type(uint256).max) _end = length; |
| 88 | + if (_end < _start || _end > length) revert InvalidQuery(); |
| 89 | + |
| 90 | + address[] memory allPoolsList = new address[](_end - _start); |
| 91 | + for (uint256 i; i < _end - _start; ++i) { |
| 92 | + allPoolsList[i] = allPools[_start + i]; |
| 93 | + } |
| 94 | + |
| 95 | + return allPoolsList; |
| 96 | + } |
| 97 | +} |
0 commit comments