-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPoolCreator.sol
249 lines (230 loc) · 9.52 KB
/
PoolCreator.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.7.4;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/Initializable.sol";
import "@openzeppelin/contracts/proxy/TransparentUpgradeableProxy.sol";
import "@openzeppelin/contracts/proxy/ProxyAdmin.sol";
import "../interface/IGovernor.sol";
import "../interface/ILiquidityPool.sol";
import "../interface/IProxyAdmin.sol";
import "./Tracer.sol";
import "./VersionControl.sol";
import "./Variables.sol";
import "./AccessControl.sol";
contract PoolCreator is Initializable, Tracer, VersionControl, Variables, AccessControl {
using AddressUpgradeable for address;
IProxyAdmin public upgradeAdmin;
event CreateLiquidityPool(
bytes32 versionKey,
address indexed liquidityPool,
address indexed governor,
address indexed operator,
address shareToken, // downward compatibility for offline infrastructure
address collateral,
uint256 collateralDecimals,
bytes initData
);
event UpgradeLiquidityPool(
bytes32 vaersionKey,
address indexed liquidityPool,
address indexed governor
);
function initialize(
address symbolService,
address globalVault,
int256 globalVaultFeeRate
) external initializer {
__Ownable_init();
__Variables_init(symbolService, globalVault, globalVaultFeeRate);
upgradeAdmin = IProxyAdmin(address(new ProxyAdmin()));
}
/**
* @notice Create a liquidity pool with the latest vesion.
* The sender will be the operator of pool.
*
* @param collateral he collateral address of the liquidity pool.
* @param collateralDecimals The collateral's decimals of the liquidity pool.
* @param nonce A random nonce to calculate the address of deployed contracts.
* @param initData A bytes array contains data to initialize new created liquidity pool.
* @return liquidityPool The address of the created liquidity pool.
*/
function createLiquidityPool(
address collateral,
uint256 collateralDecimals,
int256 nonce,
bytes calldata initData
) external returns (address liquidityPool, address governor) {
(liquidityPool, governor) = _createLiquidityPoolWith(
getLatestVersion(),
collateral,
collateralDecimals,
nonce,
initData
);
}
/**
* @notice Create a liquidity pool with the specific version. The operator will be the sender.
*
* @param versionKey The key of the version to create.
* @param collateral The collateral address of the liquidity pool.
* @param collateralDecimals The collateral's decimals of the liquidity pool.
* @param nonce A random nonce to calculate the address of deployed contracts.
* @param initData A bytes array contains data to initialize new created liquidity pool.
* @return liquidityPool The address of the created liquidity pool.
* @return governor The address of the created governor.
*/
function createLiquidityPoolWith(
bytes32 versionKey,
address collateral,
uint256 collateralDecimals,
int256 nonce,
bytes memory initData
) external returns (address liquidityPool, address governor) {
(liquidityPool, governor) = _createLiquidityPoolWith(
versionKey,
collateral,
collateralDecimals,
nonce,
initData
);
}
/**
* @notice Upgrade a liquidity pool and governor pair then call a patch function on the upgraded contract (optional).
* This method checks the sender and forwards the request to ProxyAdmin to do upgrading.
*
* @param targetVersionKey The key of version to be upgrade up. The target version must be compatiable with
* current version.
* @param dataForLiquidityPool The patch calldata for upgraded liquidity pool.
* @param dataForGovernor The patch calldata of upgraded governor.
*/
function upgradeToAndCall(
bytes32 targetVersionKey,
bytes memory dataForLiquidityPool,
bytes memory dataForGovernor
) external {
(
address liquidityPool,
address governor,
address liquidityPoolTemplate,
address governorTemplate
) = _getUpgradeContext(targetVersionKey);
if (dataForLiquidityPool.length > 0) {
upgradeAdmin.upgradeAndCall(liquidityPool, liquidityPoolTemplate, dataForLiquidityPool);
} else {
upgradeAdmin.upgrade(liquidityPool, liquidityPoolTemplate);
}
if (dataForGovernor.length > 0) {
upgradeAdmin.upgradeAndCall(governor, governorTemplate, dataForGovernor);
} else {
upgradeAdmin.upgrade(governor, governorTemplate);
}
_updateDeployedInstances(targetVersionKey, liquidityPool, governor);
emit UpgradeLiquidityPool(targetVersionKey, liquidityPool, governor);
}
/**
* @dev Create a liquidity pool with the specific version. The operator will be the sender.
*
* @param versionKey The address of version
* @param collateral The collateral address of the liquidity pool.
* @param collateralDecimals The collateral's decimals of the liquidity pool.
* @param nonce A random nonce to calculate the address of deployed contracts.
* @param initData A bytes array contains data to initialize new created liquidity pool.
* @return liquidityPool The address of the created liquidity pool.
* @return governor The address of the created governor.
*/
function _createLiquidityPoolWith(
bytes32 versionKey,
address collateral,
uint256 collateralDecimals,
int256 nonce,
bytes memory initData
) internal returns (address liquidityPool, address governor) {
require(isVersionKeyValid(versionKey), "invalid version");
// initialize
address operator = msg.sender;
(address liquidityPoolTemplate, address governorTemplate, ) = getVersion(versionKey);
bytes32 salt = keccak256(abi.encode(versionKey, collateral, initData, nonce));
liquidityPool = _createUpgradeableProxy(liquidityPoolTemplate, salt);
governor = _createUpgradeableProxy(governorTemplate, salt);
ILiquidityPool(liquidityPool).initialize(
operator,
collateral,
collateralDecimals,
governor,
initData
);
IGovernor(governor).initialize(
"MCDEX Share Token",
"STK",
liquidityPool,
liquidityPool,
getMCBToken(),
address(this)
);
// register pool to tracer
_registerLiquidityPool(liquidityPool, operator);
_updateDeployedInstances(versionKey, liquidityPool, governor);
// [EVENT UPDATE]
emit CreateLiquidityPool(
versionKey,
liquidityPool,
governor,
operator,
governor,
collateral,
collateralDecimals,
initData
);
}
/**
* @dev Create an upgradeable proxy contract of the implementation of liquidity pool.
*
* @param implementation The address of the implementation.
* @param salt The random number for create2.
* @return instance The address of the created upgradeable proxy contract.
*/
function _createUpgradeableProxy(address implementation, bytes32 salt)
internal
returns (address instance)
{
require(implementation.isContract(), "implementation must be contract");
bytes memory deploymentData =
abi.encodePacked(
type(TransparentUpgradeableProxy).creationCode,
abi.encode(implementation, address(upgradeAdmin), "")
);
assembly {
instance := create2(0x0, add(0x20, deploymentData), mload(deploymentData), salt)
}
require(instance != address(0), "create2 call failed");
}
/**
* @dev Validate sender:
* - the trnasaction must be sent from a governor.
* - the sender governor and its liquidity pool must be already registered.
* - the target version must be compatiable with the current version.
*/
function _getUpgradeContext(bytes32 targetVersionKey)
internal
view
returns (
address liquidityPool,
address governor,
address liquidityPoolTemplate,
address governorTemplate
)
{
governor = _msgSender();
require(governor.isContract(), "sender must be a contract");
liquidityPool = IGovernor(governor).getTarget();
require(isLiquidityPool(liquidityPool), "sender is not the governor of a registered pool");
bytes32 deployedAddressHash = _getVersionHash(liquidityPool, governor);
bytes32 baseVersionKey = _deployedVersions[deployedAddressHash];
require(
isVersionCompatible(targetVersionKey, baseVersionKey),
"the target version is not compatible"
);
(liquidityPoolTemplate, governorTemplate, ) = getVersion(targetVersionKey);
}
}