-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMooniFactory.sol
72 lines (56 loc) · 2.08 KB
/
MooniFactory.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
66
67
68
69
70
71
72
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.8;
import "./MooniswapPool/libraries/UniERC20.sol";
import "./MooniswapPool/access/Ownable.sol";
import "./MooniswapPool/Mooniswap.sol";
contract MooniFactory is Ownable {
using UniERC20 for IERC20;
event Deployed(
address indexed mooniswap,
address indexed token1,
address indexed token2
);
uint256 public constant MAX_FEE = 0.003e18; // 0.3%
uint256 public fee;
Mooniswap[] public allPools;
mapping(Mooniswap => bool) public isPool;
mapping(IERC20 => mapping(IERC20 => Mooniswap)) public pools;
function getAllPools() external view returns(Mooniswap[] memory) {
return allPools;
}
function setFee(uint256 newFee) external onlyOwner {
require(newFee <= MAX_FEE, "Factory: fee should be <= 0.3%");
fee = newFee;
}
function deploy(IERC20 tokenA, IERC20 tokenB) public returns(Mooniswap pool) {
require(tokenA != tokenB, "Factory: not support same tokens");
require(address(pools[tokenA][tokenB]) == address(0), "Factory: pool already exists");
(IERC20 token1, IERC20 token2) = sortTokens(tokenA, tokenB);
IERC20[] memory tokens = new IERC20[](2);
tokens[0] = token1;
tokens[1] = token2;
string memory symbol1 = token1.uniSymbol();
string memory symbol2 = token2.uniSymbol();
pool = new Mooniswap(
tokens,
string(abi.encodePacked("Mooniswap V1 (", symbol1, "-", symbol2, ")")),
string(abi.encodePacked("MOON-V1-", symbol1, "-", symbol2))
);
pool.transferOwnership(owner());
pools[token1][token2] = pool;
pools[token2][token1] = pool;
allPools.push(pool);
isPool[pool] = true;
emit Deployed(
address(pool),
address(token1),
address(token2)
);
}
function sortTokens(IERC20 tokenA, IERC20 tokenB) public pure returns(IERC20, IERC20) {
if (tokenA < tokenB) {
return (tokenA, tokenB);
}
return (tokenB, tokenA);
}
}