-
Couldn't load subscription status.
- Fork 22
Feat: factory #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
16c81b3
MaglevEulerSwapFactory
haythemsellami cdfbfbe
test
haythemsellami ade9485
typo
haythemsellami 40391c5
add tests
haythemsellami 94e072d
add interface
haythemsellami e69004a
feat: allow re-deploying pool with same asset0/asset1/fee
haythemsellami db650f3
clean'
haythemsellami 69245d0
update factory
haythemsellami 9193f59
lint
haythemsellami 577291f
Merge branch 'master' into feat/factory
haythemsellami 651f1e2
factory
haythemsellami baeb174
update config
haythemsellami 283fa1e
Merge branch 'master' into feat/factory
haythemsellami bbd8d80
fix
haythemsellami 9e1ab2d
fix
haythemsellami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // SPDX-License-Identifier: UNLICENSED | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| import {IEulerSwapFactory} from "./interfaces/IEulerSwapFactory.sol"; | ||
| import {IEulerSwap, EulerSwap} from "./EulerSwap.sol"; | ||
| import {Ownable} from "openzeppelin-contracts/access/Ownable.sol"; | ||
|
|
||
| /// @title EulerSwapFactory contract | ||
| /// @custom:security-contact [email protected] | ||
| /// @author Euler Labs (https://www.eulerlabs.com/) | ||
| contract EulerSwapFactory is IEulerSwapFactory, Ownable { | ||
| /// @dev An array to store all pools addresses. | ||
| address[] public allPools; | ||
| /// @dev Mapping to store pool addresses | ||
| mapping(bytes32 poolKey => address pool) public getPool; | ||
|
|
||
| event PoolDeployed( | ||
| address indexed asset0, | ||
| address indexed asset1, | ||
| uint256 indexed feeMultiplier, | ||
| address swapAccount, | ||
| uint256 priceX, | ||
| uint256 priceY, | ||
| uint256 concentrationX, | ||
| uint256 concentrationY, | ||
| address pool | ||
| ); | ||
|
|
||
| error InvalidQuery(); | ||
|
|
||
| constructor() Ownable(msg.sender) {} | ||
|
|
||
| /// @notice Deploy EulerSwap pool. | ||
| function deployPool(DeployParams memory params) external returns (address) { | ||
| EulerSwap pool = new EulerSwap( | ||
| IEulerSwap.Params({ | ||
| vault0: params.vault0, | ||
| vault1: params.vault1, | ||
| myAccount: params.holder, | ||
| debtLimit0: params.debtLimit0, | ||
| debtLimit1: params.debtLimit1, | ||
| fee: params.fee | ||
| }), | ||
| IEulerSwap.CurveParams({ | ||
| priceX: params.priceX, | ||
| priceY: params.priceY, | ||
| concentrationX: params.concentrationX, | ||
| concentrationY: params.concentrationY | ||
| }) | ||
| ); | ||
|
|
||
| address poolAsset0 = pool.asset0(); | ||
| address poolAsset1 = pool.asset1(); | ||
| uint256 feeMultiplier = pool.feeMultiplier(); | ||
|
|
||
| bytes32 poolKey = keccak256( | ||
| abi.encode( | ||
| poolAsset0, | ||
| poolAsset1, | ||
| feeMultiplier, | ||
| params.holder, | ||
| params.priceX, | ||
| params.priceY, | ||
| params.concentrationX, | ||
| params.concentrationY | ||
| ) | ||
| ); | ||
|
|
||
| getPool[poolKey] = address(pool); | ||
| allPools.push(address(pool)); | ||
|
|
||
| emit PoolDeployed( | ||
| poolAsset0, | ||
| poolAsset1, | ||
| feeMultiplier, | ||
| params.holder, | ||
| params.priceX, | ||
| params.priceY, | ||
| params.concentrationX, | ||
| params.concentrationY, | ||
| address(pool) | ||
| ); | ||
|
|
||
| return address(pool); | ||
| } | ||
|
|
||
| /// @notice Get the length of `allPools` array. | ||
| /// @return `allPools` length. | ||
| function allPoolsLength() external view returns (uint256) { | ||
| return allPools.length; | ||
| } | ||
|
|
||
| /// @notice Get a slice of the deployed pools array. | ||
| /// @param _start Start index of the slice. | ||
| /// @param _end End index of the slice. | ||
| /// @return An array containing the slice of the deployed pools. | ||
| function getAllPoolsListSlice(uint256 _start, uint256 _end) external view returns (address[] memory) { | ||
| uint256 length = allPools.length; | ||
| if (_end == type(uint256).max) _end = length; | ||
| if (_end < _start || _end > length) revert InvalidQuery(); | ||
|
|
||
| address[] memory allPoolsList = new address[](_end - _start); | ||
| for (uint256 i; i < _end - _start; ++i) { | ||
| allPoolsList[i] = allPools[_start + i]; | ||
| } | ||
|
|
||
| return allPoolsList; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // SPDX-License-Identifier: UNLICENSED | ||
| pragma solidity >=0.8.0; | ||
|
|
||
| interface IEulerSwapFactory { | ||
| struct DeployParams { | ||
| address vault0; | ||
| address vault1; | ||
| address holder; | ||
| uint256 fee; | ||
| uint256 priceX; | ||
| uint256 priceY; | ||
| uint256 concentrationX; | ||
| uint256 concentrationY; | ||
| uint112 debtLimit0; | ||
| uint112 debtLimit1; | ||
| } | ||
|
|
||
| function deployPool(DeployParams memory params) external returns (address); | ||
|
|
||
| function allPools(uint256 index) external view returns (address); | ||
| function getPool(bytes32 poolKey) external view returns (address); | ||
| function allPoolsLength() external view returns (uint256); | ||
| function getAllPoolsListSlice(uint256 start, uint256 end) external view returns (address[] memory); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
| pragma solidity ^0.8.24; | ||
|
|
||
| import {EulerSwapTestBase, EulerSwap} from "./EulerSwapTestBase.t.sol"; | ||
| import {EulerSwapFactory, IEulerSwapFactory} from "../src/EulerSwapFactory.sol"; | ||
|
|
||
| contract EulerSwapFactoryTest is EulerSwapTestBase { | ||
| EulerSwapFactory public eulerSwapFactory; | ||
|
|
||
| uint256 minFee = 0.0000000000001e18; | ||
|
|
||
| function setUp() public virtual override { | ||
| super.setUp(); | ||
|
|
||
| vm.prank(creator); | ||
| eulerSwapFactory = new EulerSwapFactory(); | ||
| } | ||
|
|
||
| function testDeployPool() public { | ||
| uint256 allPoolsLengthBefore = eulerSwapFactory.allPoolsLength(); | ||
|
|
||
| vm.prank(creator); | ||
| EulerSwap eulerSwap = EulerSwap( | ||
| eulerSwapFactory.deployPool( | ||
| IEulerSwapFactory.DeployParams( | ||
| address(eTST), address(eTST2), holder, 0, 1e18, 1e18, 0.4e18, 0.85e18, 50e18, 50e18 | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| uint256 allPoolsLengthAfter = eulerSwapFactory.allPoolsLength(); | ||
| bytes32 poolKey = keccak256( | ||
| abi.encode( | ||
| eulerSwap.asset0(), | ||
| eulerSwap.asset1(), | ||
| eulerSwap.feeMultiplier(), | ||
| eulerSwap.myAccount(), | ||
| eulerSwap.priceX(), | ||
| eulerSwap.priceY(), | ||
| eulerSwap.concentrationX(), | ||
| eulerSwap.concentrationY() | ||
| ) | ||
| ); | ||
|
|
||
| assertEq(allPoolsLengthAfter - allPoolsLengthBefore, 1); | ||
| assertEq(eulerSwapFactory.getPool(poolKey), address(eulerSwap)); | ||
| assertEq(eulerSwapFactory.getPool(poolKey), address(eulerSwap)); | ||
|
|
||
| address[] memory poolsList = eulerSwapFactory.getAllPoolsListSlice(0, type(uint256).max); | ||
| assertEq(poolsList.length, 1); | ||
| assertEq(poolsList[0], address(eulerSwap)); | ||
| assertEq(eulerSwapFactory.allPools(0), address(eulerSwap)); | ||
| } | ||
|
|
||
| function testInvalidGetAllPoolsListSliceQuery() public { | ||
| vm.expectRevert(EulerSwapFactory.InvalidQuery.selector); | ||
| eulerSwapFactory.getAllPoolsListSlice(1, 0); | ||
| } | ||
|
|
||
| function testDeployWithAssetsOutOfOrderOrEqual() public { | ||
| vm.prank(creator); | ||
| vm.expectRevert(EulerSwap.AssetsOutOfOrderOrEqual.selector); | ||
| eulerSwapFactory.deployPool( | ||
| IEulerSwapFactory.DeployParams( | ||
| address(eTST), address(eTST), holder, 0, 1e18, 1e18, 0.4e18, 0.85e18, 50e18, 50e18 | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function testDeployWithBadFee() public { | ||
| vm.prank(creator); | ||
| vm.expectRevert(EulerSwap.BadFee.selector); | ||
| eulerSwapFactory.deployPool( | ||
| IEulerSwapFactory.DeployParams( | ||
| address(eTST), address(eTST2), holder, 1e18, 1e18, 1e18, 0.4e18, 0.85e18, 50e18, 50e18 | ||
| ) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved them to interface file