-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathUlyssesFactory.sol
166 lines (132 loc) · 4.93 KB
/
UlyssesFactory.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Ownable} from "solady/auth/Ownable.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {UlyssesPool} from "../UlyssesPool.sol";
import {UlyssesToken} from "../UlyssesToken.sol";
import {IUlyssesFactory} from "../interfaces/IUlyssesFactory.sol";
/// @title Ulysses Pool Deployer
library UlyssesPoolDeployer {
/**
* @notice Deploys a new Ulysses pool.
* @param id The id of the Ulysses pool.
* @param asset The asset of the Ulysses pool.
* @param name The name of the Ulysses pool.
* @param symbol The symbol of the Ulysses pool.
* @param owner The owner of the Ulysses pool.
* @param factory The factory of the Ulysses pool.
*/
function deployPool(
uint256 id,
address asset,
string calldata name,
string calldata symbol,
address owner,
address factory
) public returns (UlyssesPool) {
return new UlyssesPool(id, asset, name, symbol, owner, factory);
}
}
/// @title Factory of new Ulysses instances
contract UlyssesFactory is Ownable, IUlyssesFactory {
using SafeTransferLib for ERC20;
using FixedPointMathLib for uint256;
error ParameterLengthError();
error InvalidPoolId();
error InvalidAsset();
///@notice next poolId
uint256 public poolId = 1;
///@notice next tokenId
uint256 public tokenId = 1;
///@notice Mapping that holds all the Ulysses pools
mapping(uint256 => UlyssesPool) public pools;
///@notice Mapping that holds all the Ulysses tokens
mapping(uint256 => UlyssesToken) public tokens;
constructor(address _owner) {
require(_owner != address(0), "Owner cannot be 0");
_initializeOwner(_owner);
}
function renounceOwnership() public payable override onlyOwner {
revert("Cannot renounce ownership");
}
/*//////////////////////////////////////////////////////////////
NEW LP LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IUlyssesFactory
function createPool(ERC20 asset, address owner) external returns (uint256) {
return _createPool(asset, owner);
}
/**
* @notice Private function that holds the logic for creating a new Ulysses pool.
* @param asset represents the asset that we want to create a Ulysses pool for.
* @return _poolId id of the pool that was created.
*/
function _createPool(ERC20 asset, address owner) private returns (uint256 _poolId) {
if (address(asset) == address(0)) revert InvalidAsset();
_poolId = ++poolId;
pools[_poolId] =
UlyssesPoolDeployer.deployPool(_poolId, address(asset), "Ulysses Pool", "ULP", owner, address(this));
}
/// @inheritdoc IUlyssesFactory
function createPools(ERC20[] calldata assets, uint8[][] calldata weights, address owner)
external
returns (uint256[] memory poolIds)
{
uint256 length = assets.length;
if (length != weights.length) revert ParameterLengthError();
for (uint256 i = 0; i < length;) {
poolIds[i] = _createPool(assets[i], address(this));
unchecked {
++i;
}
}
for (uint256 i = 0; i < length;) {
if (length != weights[i].length) revert ParameterLengthError();
for (uint256 j = 0; j < length;) {
if (j != i && weights[i][j] > 0) pools[poolIds[i]].addNewBandwidth(poolIds[j], weights[i][j]);
unchecked {
++j;
}
}
unchecked {
++i;
}
}
for (uint256 i = 0; i < length;) {
pools[poolIds[i]].transferOwnership(owner);
unchecked {
++i;
}
}
}
/*//////////////////////////////////////////////////////////////
NEW TOKEN LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IUlyssesFactory
function createToken(uint256[] calldata poolIds, uint256[] calldata weights, address owner)
external
returns (uint256 _tokenId)
{
_tokenId = ++tokenId;
uint256 length = poolIds.length;
address[] memory destinations = new address[](length);
for (uint256 i = 0; i < length;) {
address destination = address(pools[poolIds[i]]);
if (destination == address(0)) revert InvalidPoolId();
destinations[i] = destination;
unchecked {
++i;
}
}
tokens[_tokenId] = new UlyssesToken(
_tokenId,
destinations,
weights,
"Ulysses Token",
"ULT",
owner
);
}
}