This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFraxlendPairDeployer.sol
343 lines (288 loc) · 15.3 KB
/
FraxlendPairDeployer.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// SPDX-License-Identifier: ISC
pragma solidity ^0.8.19;
// ====================================================================
// | ______ _______ |
// | / _____________ __ __ / ____(_____ ____ _____ ________ |
// | / /_ / ___/ __ `| |/_/ / /_ / / __ \/ __ `/ __ \/ ___/ _ \ |
// | / __/ / / / /_/ _> < / __/ / / / / / /_/ / / / / /__/ __/ |
// | /_/ /_/ \__,_/_/|_| /_/ /_/_/ /_/\__,_/_/ /_/\___/\___/ |
// | |
// ====================================================================
// ====================== FraxlendPairDeployer ========================
// ====================================================================
// Frax Finance: https://github.com/FraxFinance
// Primary Author
// Drake Evans: https://github.com/DrakeEvans
// Reviewers
// Dennis: https://github.com/denett
// Sam Kazemian: https://github.com/samkazemian
// Travis Moore: https://github.com/FortisFortuna
// Jack Corddry: https://github.com/corddry
// Rich Gee: https://github.com/zer0blockchain
// ====================================================================
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {SSTORE2} from "@rari-capital/solmate/src/utils/SSTORE2.sol";
import {BytesLib} from "solidity-bytes-utils/contracts/BytesLib.sol";
import {IFraxlendWhitelist} from "./interfaces/IFraxlendWhitelist.sol";
import {IFraxlendPair} from "./interfaces/IFraxlendPair.sol";
import {IFraxlendPairRegistry} from "./interfaces/IFraxlendPairRegistry.sol";
import {SafeERC20} from "./libraries/SafeERC20.sol";
// solhint-disable no-inline-assembly
struct ConstructorParams {
address circuitBreaker;
address comptroller;
address timelock;
address fraxlendWhitelist;
address fraxlendPairRegistry;
}
/// @title FraxlendPairDeployer
/// @author Drake Evans (Frax Finance) https://github.com/drakeevans
/// @notice Deploys and initializes new FraxlendPairs
/// @dev Uses create2 to deploy the pairs, logs an event, and records a list of all deployed pairs
contract FraxlendPairDeployer is Ownable {
using Strings for uint256;
using SafeERC20 for IERC20;
// Storage
address public contractAddress1;
address public contractAddress2;
// Admin contracts
address public circuitBreakerAddress;
address public comptrollerAddress;
address public timelockAddress;
address public fraxlendPairRegistryAddress;
address public fraxlendWhitelistAddress;
// Default swappers
address[] public defaultSwappers;
// Default deposit amount for new pairs
uint256 public defaultDepositAmt;
/// @notice Emits when a new pair is deployed
/// @notice The ```LogDeploy``` event is emitted when a new Pair is deployed
/// @param address_ The address of the pair
/// @param asset The address of the Asset Token contract
/// @param collateral The address of the Collateral Token contract
/// @param name The name of the Pair
/// @param configData The config data of the Pair
/// @param immutables The immutables of the Pair
/// @param customConfigData The custom config data of the Pair
event LogDeploy(
address indexed address_,
address indexed asset,
address indexed collateral,
string name,
bytes configData,
bytes immutables,
bytes customConfigData
);
/// @notice List of the names of all deployed Pairs
address[] public deployedPairsArray;
constructor(ConstructorParams memory _params) Ownable() {
circuitBreakerAddress = _params.circuitBreaker;
comptrollerAddress = _params.comptroller;
timelockAddress = _params.timelock;
fraxlendWhitelistAddress = _params.fraxlendWhitelist;
fraxlendPairRegistryAddress = _params.fraxlendPairRegistry;
}
function version() external pure returns (uint256 _major, uint256 _minor, uint256 _patch) {
return (4, 1, 0);
}
// ============================================================================================
// Functions: View Functions
// ============================================================================================
/// @notice The ```deployedPairsLength``` function returns the length of the deployedPairsArray
/// @return length of array
function deployedPairsLength() external view returns (uint256) {
return deployedPairsArray.length;
}
/// @notice The ```getAllPairAddresses``` function returns all pair addresses in deployedPairsArray
/// @return _deployedPairs memory All deployed pair addresses
function getAllPairAddresses() external view returns (address[] memory _deployedPairs) {
_deployedPairs = deployedPairsArray;
}
function getNextNameSymbol(address _asset, address _collateral)
public
view
returns (string memory _name, string memory _symbol)
{
uint256 _length = IFraxlendPairRegistry(fraxlendPairRegistryAddress).deployedPairsLength();
_name = string(
abi.encodePacked(
"Fraxlend Interest Bearing ",
IERC20(_asset).safeSymbol(),
" (",
IERC20(_collateral).safeName(),
")",
" - ",
(_length + 1).toString()
)
);
_symbol = string(
abi.encodePacked(
"f",
IERC20(_asset).safeSymbol(),
"(",
IERC20(_collateral).safeSymbol(),
")",
"-",
(_length + 1).toString()
)
);
}
// ============================================================================================
// Functions: Setters
// ============================================================================================
/// @notice The ```setCreationCode``` function sets the bytecode for the fraxlendPair
/// @dev splits the data if necessary to accommodate creation code that is slightly larger than 24kb
/// @param _creationCode The creationCode for the Fraxlend Pair
function setCreationCode(bytes calldata _creationCode) external onlyOwner {
bytes memory _firstHalf = BytesLib.slice(_creationCode, 0, 13_000);
contractAddress1 = SSTORE2.write(_firstHalf);
if (_creationCode.length > 13_000) {
bytes memory _secondHalf = BytesLib.slice(_creationCode, 13_000, _creationCode.length - 13_000);
contractAddress2 = SSTORE2.write(_secondHalf);
}
}
/// @notice The ```setDefaultSwappers``` function is used to set default list of approved swappers
/// @param _swappers The list of swappers to set as default allowed
function setDefaultSwappers(address[] memory _swappers) external onlyOwner {
defaultSwappers = _swappers;
}
function setDefaultDepositAmt(uint256 _amount) external onlyOwner {
defaultDepositAmt = _amount;
}
/// @notice The ```SetTimelock``` event is emitted when the timelockAddress is set
/// @param oldAddress The original address
/// @param newAddress The new address
event SetTimelock(address oldAddress, address newAddress);
/// @notice The ```setTimelock``` function sets the timelockAddress
/// @param _newAddress the new time lock address
function setTimelock(address _newAddress) external onlyOwner {
emit SetTimelock(timelockAddress, _newAddress);
timelockAddress = _newAddress;
}
/// @notice The ```SetRegistry``` event is emitted when the fraxlendPairRegistryAddress is set
/// @param oldAddress The old address
/// @param newAddress The new address
event SetRegistry(address oldAddress, address newAddress);
/// @notice The ```setRegistry``` function sets the fraxlendPairRegistryAddress
/// @param _newAddress The new address
function setRegistry(address _newAddress) external onlyOwner {
emit SetRegistry(fraxlendPairRegistryAddress, _newAddress);
fraxlendPairRegistryAddress = _newAddress;
}
/// @notice The ```SetComptroller``` event is emitted when the comptrollerAddress is set
/// @param oldAddress The old address
/// @param newAddress The new address
event SetComptroller(address oldAddress, address newAddress);
/// @notice The ```setComptroller``` function sets the comptrollerAddress
/// @param _newAddress The new address
function setComptroller(address _newAddress) external onlyOwner {
emit SetComptroller(comptrollerAddress, _newAddress);
comptrollerAddress = _newAddress;
}
/// @notice The ```SetWhitelist``` event is emitted when the fraxlendWhitelistAddress is set
/// @param oldAddress The old address
/// @param newAddress The new address
event SetWhitelist(address oldAddress, address newAddress);
/// @notice The ```setWhitelist``` function sets the fraxlendWhitelistAddress
/// @param _newAddress The new address
function setWhitelist(address _newAddress) external onlyOwner {
emit SetWhitelist(fraxlendWhitelistAddress, _newAddress);
fraxlendWhitelistAddress = _newAddress;
}
/// @notice The ```SetCircuitBreaker``` event is emitted when the circuitBreakerAddress is set
/// @param oldAddress The old address
/// @param newAddress The new address
event SetCircuitBreaker(address oldAddress, address newAddress);
/// @notice The ```setCircuitBreaker``` function sets the circuitBreakerAddress
/// @param _newAddress The new address
function setCircuitBreaker(address _newAddress) external onlyOwner {
emit SetCircuitBreaker(circuitBreakerAddress, _newAddress);
circuitBreakerAddress = _newAddress;
}
// ============================================================================================
// Functions: Internal Methods
// ============================================================================================
/// @notice The ```_deploy``` function is an internal function with deploys the pair
/// @param _configData abi.encode(address _asset, address _collateral, address _oracle, uint32 _maxOracleDeviation, address _rateContract, uint64 _fullUtilizationRate, uint256 _maxLTV, uint256 _cleanLiquidationFee, uint256 _dirtyLiquidationFee, uint256 _protocolLiquidationFee)
/// @param _immutables abi.encode(address _circuitBreakerAddress, address _comptrollerAddress, address _timelockAddress)
/// @param _customConfigData abi.encode(string memory _nameOfContract, string memory _symbolOfContract, uint8 _decimalsOfContract)
/// @return _pairAddress The address to which the Pair was deployed
function _deploy(bytes memory _configData, bytes memory _immutables, bytes memory _customConfigData)
private
returns (address _pairAddress)
{
// Get creation code
bytes memory _creationCode = BytesLib.concat(SSTORE2.read(contractAddress1), SSTORE2.read(contractAddress2));
// Get bytecode
bytes memory bytecode = abi.encodePacked(_creationCode, abi.encode(_configData, _immutables, _customConfigData));
// Generate salt using constructor params
bytes32 salt = keccak256(abi.encodePacked(_configData, _immutables, _customConfigData));
/// @solidity memory-safe-assembly
assembly {
_pairAddress := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
if (_pairAddress == address(0)) revert Create2Failed();
deployedPairsArray.push(_pairAddress);
// Set additional values for FraxlendPair
IFraxlendPair _fraxlendPair = IFraxlendPair(_pairAddress);
if (defaultDepositAmt > 0) {
IERC20(_fraxlendPair.asset()).safeTransferFrom(msg.sender, address(this), defaultDepositAmt);
IERC20(_fraxlendPair.asset()).approve(address(_fraxlendPair), defaultDepositAmt);
_fraxlendPair.deposit(defaultDepositAmt, msg.sender);
}
address[] memory _defaultSwappers = defaultSwappers;
for (uint256 i = 0; i < _defaultSwappers.length; i++) {
_fraxlendPair.setSwapper(_defaultSwappers[i], true);
}
return _pairAddress;
}
// ============================================================================================
// Functions: External Deploy Methods
// ============================================================================================
/// @notice The ```deploy``` function allows the deployment of a FraxlendPair with default values
/// @param _configData abi.encode(address _asset, address _collateral, address _oracle, uint32 _maxOracleDeviation, address _rateContract, uint64 _fullUtilizationRate, uint256 _maxLTV, uint256 _cleanLiquidationFee, uint256 _dirtyLiquidationFee, uint256 _protocolLiquidationFee)
/// @return _pairAddress The address to which the Pair was deployed
function deploy(bytes memory _configData) external returns (address _pairAddress) {
if (!IFraxlendWhitelist(fraxlendWhitelistAddress).fraxlendDeployerWhitelist(msg.sender)) {
revert WhitelistedDeployersOnly();
}
(address _asset, address _collateral,,,,,,,) =
abi.decode(_configData, (address, address, address, uint32, address, uint64, uint256, uint256, uint256));
(string memory _name, string memory _symbol) = getNextNameSymbol(_asset, _collateral);
bytes memory _immutables = abi.encode(circuitBreakerAddress, comptrollerAddress, timelockAddress);
bytes memory _customConfigData = abi.encode(_name, _symbol, IERC20(_asset).safeDecimals());
_pairAddress = _deploy(_configData, _immutables, _customConfigData);
IFraxlendPairRegistry(fraxlendPairRegistryAddress).addPair(_pairAddress);
emit LogDeploy(_pairAddress, _asset, _collateral, _name, _configData, _immutables, _customConfigData);
}
// ============================================================================================
// Functions: Admin
// ============================================================================================
/// @notice The ```globalPause``` function calls the pause() function on a given set of pair addresses
/// @dev Ignores reverts when calling pause()
/// @param _addresses Addresses to attempt to pause()
/// @return _updatedAddresses Addresses for which pause() was successful
function globalPause(address[] memory _addresses) external returns (address[] memory _updatedAddresses) {
if (msg.sender != circuitBreakerAddress) revert CircuitBreakerOnly();
address _pairAddress;
uint256 _lengthOfArray = _addresses.length;
_updatedAddresses = new address[](_lengthOfArray);
for (uint256 i = 0; i < _lengthOfArray;) {
_pairAddress = _addresses[i];
try IFraxlendPair(_pairAddress).pause() {
_updatedAddresses[i] = _addresses[i];
} catch {}
unchecked {
i = i + 1;
}
}
}
// ============================================================================================
// Errors
// ============================================================================================
error CircuitBreakerOnly();
error WhitelistedDeployersOnly();
error Create2Failed();
}