/
PoolFactory.sol
202 lines (172 loc) · 4.61 KB
/
PoolFactory.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// SPDX-License-Identifier: WISE
pragma solidity =0.8.17;
import "./ILiquidInit.sol";
import "./LiquidPool.sol";
import "./LiquidRouter.sol";
/**
* @dev LiquidFactory: Factory is responsible
* for deploying new LiquidPools.
*/
contract PoolFactory {
// Instance for router reference
LiquidRouter immutable liquidRouter;
// Liquid router that manages pools
address public routerAddress;
// Contract that all pools are cloned from
address public defaultPoolTarget;
// Address to manage protocol
address public multisigAddress;
// Simple useful counter
uint256 public poolCount;
/**
* @dev Revert if msg.sender if not multisig
*/
modifier onlyMultisig() {
require(
msg.sender == multisigAddress,
"AccessControl: NOT_MULTISIG"
);
_;
}
/**
* @dev Creates default pool target and sets router
*/
constructor(
address _chainLinkETH
) {
multisigAddress = msg.sender;
liquidRouter = new LiquidRouter(
address(this),
_chainLinkETH
);
routerAddress = address(
liquidRouter
);
defaultPoolTarget = address(
new LiquidPool()
);
}
event PoolCreated(
address indexed pool,
address indexed token
);
/**
* @dev Change the default target contract.
* Only mutlisig address can do this.
*/
function updateDefaultPoolTarget(
address _newDefaultTarget
)
external
onlyMultisig
{
defaultPoolTarget = _newDefaultTarget;
}
/**
* @dev Creates a copy of bytecode of defaultPoolTarget with CREATE2 opcode
* also calls initialise to set state variables
*/
function createLiquidPool(
address _poolTokenAddress,
address _chainLinkFeedAddress,
uint256 _multiplicationFactor,
uint256 _maxCollateralFactor,
address[] memory _nftAddresses,
string memory _tokenName,
string memory _tokenSymbol
)
external
onlyMultisig
returns (address poolAddress)
{
poolAddress = _generatePool(
_poolTokenAddress
);
ILiquidInit(poolAddress).initialise(
_poolTokenAddress,
_chainLinkFeedAddress,
_multiplicationFactor,
_maxCollateralFactor,
_nftAddresses,
_tokenName,
_tokenSymbol
);
liquidRouter.addLiquidPool(
poolAddress
);
emit PoolCreated(
poolAddress,
_poolTokenAddress
);
}
/**
* @dev Deploys a pool with bytecode of defaultPoolTarget with CREATE2 opcode
*/
function _generatePool(
address _poolAddress
)
internal
returns (address poolAddress)
{
bytes32 salt = keccak256(
abi.encodePacked(
poolCount++,
_poolAddress
)
);
bytes20 targetBytes = bytes20(
defaultPoolTarget
);
assembly {
let clone := mload(0x40)
mstore(
clone,
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000
)
mstore(
add(clone, 0x14),
targetBytes
)
mstore(
add(clone, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
poolAddress := create2(
0,
clone,
0x37,
salt
)
}
}
/**
* @dev Pre-compute what address a future pool will exist at.
*/
function predictPoolAddress(
uint256 _index,
address _pool,
address _factory,
address _implementation
)
external
pure
returns (address predicted)
{
bytes32 salt = keccak256(
abi.encodePacked(
_index,
_pool
)
);
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, _implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf3ff00000000000000000000000000000000)
mstore(add(ptr, 0x38), shl(0x60, _factory))
mstore(add(ptr, 0x4c), salt)
mstore(add(ptr, 0x6c), keccak256(ptr, 0x37))
predicted := keccak256(add(ptr, 0x37), 0x55)
}
}
}