-
Notifications
You must be signed in to change notification settings - Fork 51
/
LBFactory.sol
756 lines (647 loc) Β· 28.6 KB
/
LBFactory.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {EnumerableMap} from "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {Ownable2Step, Ownable} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {PairParameterHelper} from "./libraries/PairParameterHelper.sol";
import {Encoded} from "./libraries/math/Encoded.sol";
import {ImmutableClone} from "./libraries/ImmutableClone.sol";
import {PriceHelper} from "./libraries/PriceHelper.sol";
import {SafeCast} from "./libraries/math/SafeCast.sol";
import {Hooks} from "./libraries/Hooks.sol";
import {ILBFactory} from "./interfaces/ILBFactory.sol";
import {ILBPair} from "./interfaces/ILBPair.sol";
import {ILBHooks} from "./interfaces/ILBHooks.sol";
/**
* @title Liquidity Book Factory
* @author Trader Joe
* @notice Contract used to deploy and register new LBPairs.
* Enables setting fee parameters, flashloan fees and LBPair implementation.
* Unless the `isOpen` is `true`, only the owner of the factory can create pairs.
*/
contract LBFactory is Ownable2Step, AccessControl, ILBFactory {
using SafeCast for uint256;
using Encoded for bytes32;
using PairParameterHelper for bytes32;
using EnumerableSet for EnumerableSet.AddressSet;
using EnumerableSet for EnumerableSet.UintSet;
using EnumerableMap for EnumerableMap.UintToUintMap;
bytes32 public constant LB_HOOKS_MANAGER_ROLE = keccak256("LB_HOOKS_MANAGER_ROLE");
uint256 private constant _OFFSET_IS_PRESET_OPEN = 255;
uint256 private constant _MIN_BIN_STEP = 1; // 0.001%
uint256 private constant _MAX_FLASHLOAN_FEE = 0.1e18; // 10%
address private _feeRecipient;
uint256 private _flashLoanFee;
address private _lbPairImplementation;
ILBPair[] private _allLBPairs;
/**
* @dev Mapping from a (tokenA, tokenB, binStep) to a LBPair. The tokens are ordered to save gas, but they can be
* in the reverse order in the actual pair.
* Always query one of the 2 tokens of the pair to assert the order of the 2 tokens
*/
mapping(IERC20 => mapping(IERC20 => mapping(uint256 => LBPairInformation))) private _lbPairsInfo;
EnumerableMap.UintToUintMap private _presets;
EnumerableSet.AddressSet private _quoteAssetWhitelist;
/**
* @dev Mapping from a (tokenA, tokenB) to a set of available bin steps, this is used to keep track of the
* bin steps that are already used for a pair.
* The tokens are ordered to save gas, but they can be in the reverse order in the actual pair.
* Always query one of the 2 tokens of the pair to assert the order of the 2 tokens
*/
mapping(IERC20 => mapping(IERC20 => EnumerableSet.UintSet)) private _availableLBPairBinSteps;
/**
* @notice Constructor
* @param feeRecipient The address of the fee recipient
* @param flashLoanFee The value of the fee for flash loan
*/
constructor(address feeRecipient, address initialOwner, uint256 flashLoanFee) Ownable(initialOwner) {
if (flashLoanFee > _MAX_FLASHLOAN_FEE) revert LBFactory__FlashLoanFeeAboveMax(flashLoanFee, _MAX_FLASHLOAN_FEE);
_setFeeRecipient(feeRecipient);
_flashLoanFee = flashLoanFee;
emit FlashLoanFeeSet(0, flashLoanFee);
}
/**
* @notice Get the minimum bin step a pair can have
* @return minBinStep
*/
function getMinBinStep() external pure override returns (uint256 minBinStep) {
return _MIN_BIN_STEP;
}
/**
* @notice Get the protocol fee recipient
* @return feeRecipient
*/
function getFeeRecipient() external view override returns (address feeRecipient) {
return _feeRecipient;
}
/**
* @notice Get the maximum fee percentage for flashLoans
* @return maxFee
*/
function getMaxFlashLoanFee() external pure override returns (uint256 maxFee) {
return _MAX_FLASHLOAN_FEE;
}
/**
* @notice Get the fee for flash loans, in 1e18
* @return flashLoanFee The fee for flash loans, in 1e18
*/
function getFlashLoanFee() external view override returns (uint256 flashLoanFee) {
return _flashLoanFee;
}
/**
* @notice Get the address of the LBPair implementation
* @return lbPairImplementation The address of the LBPair implementation
*/
function getLBPairImplementation() external view override returns (address lbPairImplementation) {
return _lbPairImplementation;
}
/**
* @notice View function to return the number of LBPairs created
* @return lbPairNumber
*/
function getNumberOfLBPairs() external view override returns (uint256 lbPairNumber) {
return _allLBPairs.length;
}
/**
* @notice View function to return the LBPair created at index `index`
* @param index The index
* @return lbPair The address of the LBPair at index `index`
*/
function getLBPairAtIndex(uint256 index) external view override returns (ILBPair lbPair) {
return _allLBPairs[index];
}
/**
* @notice View function to return the number of quote assets whitelisted
* @return numberOfQuoteAssets The number of quote assets
*/
function getNumberOfQuoteAssets() external view override returns (uint256 numberOfQuoteAssets) {
return _quoteAssetWhitelist.length();
}
/**
* @notice View function to return the quote asset whitelisted at index `index`
* @param index The index
* @return asset The address of the quoteAsset at index `index`
*/
function getQuoteAssetAtIndex(uint256 index) external view override returns (IERC20 asset) {
return IERC20(_quoteAssetWhitelist.at(index));
}
/**
* @notice View function to return whether a token is a quotedAsset (true) or not (false)
* @param token The address of the asset
* @return isQuote Whether the token is a quote asset or not
*/
function isQuoteAsset(IERC20 token) external view override returns (bool isQuote) {
return _quoteAssetWhitelist.contains(address(token));
}
/**
* @notice Returns the LBPairInformation if it exists,
* if not, then the address 0 is returned. The order doesn't matter
* @param tokenA The address of the first token of the pair
* @param tokenB The address of the second token of the pair
* @param binStep The bin step of the LBPair
* @return lbPairInformation The LBPairInformation
*/
function getLBPairInformation(IERC20 tokenA, IERC20 tokenB, uint256 binStep)
external
view
override
returns (LBPairInformation memory lbPairInformation)
{
return _getLBPairInformation(tokenA, tokenB, binStep);
}
/**
* @notice View function to return the different parameters of the preset
* Will revert if the preset doesn't exist
* @param binStep The bin step of the preset
* @return baseFactor The base factor
* @return filterPeriod The filter period of the preset
* @return decayPeriod The decay period of the preset
* @return reductionFactor The reduction factor of the preset
* @return variableFeeControl The variable fee control of the preset
* @return protocolShare The protocol share of the preset
* @return maxVolatilityAccumulator The max volatility accumulator of the preset
* @return isOpen Whether the preset is open or not
*/
function getPreset(uint256 binStep)
external
view
override
returns (
uint256 baseFactor,
uint256 filterPeriod,
uint256 decayPeriod,
uint256 reductionFactor,
uint256 variableFeeControl,
uint256 protocolShare,
uint256 maxVolatilityAccumulator,
bool isOpen
)
{
if (!_presets.contains(binStep)) revert LBFactory__BinStepHasNoPreset(binStep);
bytes32 preset = bytes32(_presets.get(binStep));
baseFactor = preset.getBaseFactor();
filterPeriod = preset.getFilterPeriod();
decayPeriod = preset.getDecayPeriod();
reductionFactor = preset.getReductionFactor();
variableFeeControl = preset.getVariableFeeControl();
protocolShare = preset.getProtocolShare();
maxVolatilityAccumulator = preset.getMaxVolatilityAccumulator();
isOpen = preset.decodeBool(_OFFSET_IS_PRESET_OPEN);
}
/**
* @notice View function to return the list of available binStep with a preset
* @return binStepWithPreset The list of binStep
*/
function getAllBinSteps() external view override returns (uint256[] memory binStepWithPreset) {
return _presets.keys();
}
/**
* @notice View function to return the list of open binSteps
* @return openBinStep The list of open binSteps
*/
function getOpenBinSteps() external view override returns (uint256[] memory openBinStep) {
uint256 length = _presets.length();
if (length > 0) {
openBinStep = new uint256[](length);
uint256 index;
for (uint256 i; i < length; ++i) {
(uint256 binStep, uint256 preset) = _presets.at(i);
if (_isPresetOpen(bytes32(preset))) {
openBinStep[index] = binStep;
index++;
}
}
if (index < length) {
assembly {
mstore(openBinStep, index)
}
}
}
}
/**
* @notice View function to return all the LBPair of a pair of tokens
* @param tokenX The first token of the pair
* @param tokenY The second token of the pair
* @return lbPairsAvailable The list of available LBPairs
*/
function getAllLBPairs(IERC20 tokenX, IERC20 tokenY)
external
view
override
returns (LBPairInformation[] memory lbPairsAvailable)
{
unchecked {
(IERC20 tokenA, IERC20 tokenB) = _sortTokens(tokenX, tokenY);
EnumerableSet.UintSet storage addressSet = _availableLBPairBinSteps[tokenA][tokenB];
uint256 length = addressSet.length();
if (length > 0) {
lbPairsAvailable = new LBPairInformation[](length);
mapping(uint256 => LBPairInformation) storage lbPairsInfo = _lbPairsInfo[tokenA][tokenB];
for (uint256 i = 0; i < length; ++i) {
uint16 binStep = addressSet.at(i).safe16();
lbPairsAvailable[i] = LBPairInformation({
binStep: binStep,
LBPair: lbPairsInfo[binStep].LBPair,
createdByOwner: lbPairsInfo[binStep].createdByOwner,
ignoredForRouting: lbPairsInfo[binStep].ignoredForRouting
});
}
}
}
}
/**
* @notice Set the LBPair implementation address
* @dev Needs to be called by the owner
* @param newLBPairImplementation The address of the implementation
*/
function setLBPairImplementation(address newLBPairImplementation) external override onlyOwner {
if (ILBPair(newLBPairImplementation).getFactory() != this) {
revert LBFactory__LBPairSafetyCheckFailed(newLBPairImplementation);
}
address oldLBPairImplementation = _lbPairImplementation;
if (oldLBPairImplementation == newLBPairImplementation) {
revert LBFactory__SameImplementation(newLBPairImplementation);
}
_lbPairImplementation = newLBPairImplementation;
emit LBPairImplementationSet(oldLBPairImplementation, newLBPairImplementation);
}
/**
* @notice Create a liquidity bin LBPair for tokenX and tokenY
* @param tokenX The address of the first token
* @param tokenY The address of the second token
* @param activeId The active id of the pair
* @param binStep The bin step in basis point, used to calculate log(1 + binStep / 10_000)
* @return pair The address of the newly created LBPair
*/
function createLBPair(IERC20 tokenX, IERC20 tokenY, uint24 activeId, uint16 binStep)
external
override
returns (ILBPair pair)
{
if (!_presets.contains(binStep)) revert LBFactory__BinStepHasNoPreset(binStep);
bytes32 preset = bytes32(_presets.get(binStep));
bool isOwner = msg.sender == owner();
if (!_isPresetOpen(preset) && !isOwner) {
revert LBFactory__PresetIsLockedForUsers(msg.sender, binStep);
}
if (!_quoteAssetWhitelist.contains(address(tokenY))) revert LBFactory__QuoteAssetNotWhitelisted(tokenY);
if (tokenX == tokenY) revert LBFactory__IdenticalAddresses(tokenX);
// safety check, making sure that the price can be calculated
PriceHelper.getPriceFromId(activeId, binStep);
// We sort token for storage efficiency, only one input needs to be stored because they are sorted
(IERC20 tokenA, IERC20 tokenB) = _sortTokens(tokenX, tokenY);
// single check is sufficient
if (address(tokenA) == address(0)) revert LBFactory__AddressZero();
if (address(_lbPairsInfo[tokenA][tokenB][binStep].LBPair) != address(0)) {
revert LBFactory__LBPairAlreadyExists(tokenX, tokenY, binStep);
}
{
address implementation = _lbPairImplementation;
if (implementation == address(0)) revert LBFactory__ImplementationNotSet();
pair = ILBPair(
ImmutableClone.cloneDeterministic(
implementation,
abi.encodePacked(tokenX, tokenY, binStep),
keccak256(abi.encode(tokenA, tokenB, binStep))
)
);
}
_lbPairsInfo[tokenA][tokenB][binStep] =
LBPairInformation({binStep: binStep, LBPair: pair, createdByOwner: isOwner, ignoredForRouting: false});
_allLBPairs.push(pair);
_availableLBPairBinSteps[tokenA][tokenB].add(binStep);
emit LBPairCreated(tokenX, tokenY, binStep, pair, _allLBPairs.length - 1);
pair.initialize(
preset.getBaseFactor(),
preset.getFilterPeriod(),
preset.getDecayPeriod(),
preset.getReductionFactor(),
preset.getVariableFeeControl(),
preset.getProtocolShare(),
preset.getMaxVolatilityAccumulator(),
activeId
);
}
/**
* @notice Function to set whether the pair is ignored or not for routing, it will make the pair unusable by the router
* @dev Needs to be called by the owner
* Reverts if:
* - The pair doesn't exist
* - The ignored state is already in the same state
* @param tokenX The address of the first token of the pair
* @param tokenY The address of the second token of the pair
* @param binStep The bin step in basis point of the pair
* @param ignored Whether to ignore (true) or not (false) the pair for routing
*/
function setLBPairIgnored(IERC20 tokenX, IERC20 tokenY, uint16 binStep, bool ignored) external override onlyOwner {
(IERC20 tokenA, IERC20 tokenB) = _sortTokens(tokenX, tokenY);
LBPairInformation memory pairInformation = _lbPairsInfo[tokenA][tokenB][binStep];
if (address(pairInformation.LBPair) == address(0)) {
revert LBFactory__LBPairDoesNotExist(tokenX, tokenY, binStep);
}
if (pairInformation.ignoredForRouting == ignored) revert LBFactory__LBPairIgnoredIsAlreadyInTheSameState();
_lbPairsInfo[tokenA][tokenB][binStep].ignoredForRouting = ignored;
emit LBPairIgnoredStateChanged(pairInformation.LBPair, ignored);
}
/**
* @notice Sets the preset parameters of a bin step
* @dev Needs to be called by the owner
* Reverts if:
* - The binStep is lower than the minimum bin step
* @param binStep The bin step in basis point, used to calculate the price
* @param baseFactor The base factor, used to calculate the base fee, baseFee = baseFactor * binStep
* @param filterPeriod The period where the accumulator value is untouched, prevent spam
* @param decayPeriod The period where the accumulator value is decayed, by the reduction factor
* @param reductionFactor The reduction factor, used to calculate the reduction of the accumulator
* @param variableFeeControl The variable fee control, used to control the variable fee, can be 0 to disable it
* @param protocolShare The share of the fees received by the protocol
* @param maxVolatilityAccumulator The max value of the volatility accumulator
*/
function setPreset(
uint16 binStep,
uint16 baseFactor,
uint16 filterPeriod,
uint16 decayPeriod,
uint16 reductionFactor,
uint24 variableFeeControl,
uint16 protocolShare,
uint24 maxVolatilityAccumulator,
bool isOpen
) external override onlyOwner {
if (binStep < _MIN_BIN_STEP) revert LBFactory__BinStepTooLow(binStep);
bytes32 preset = bytes32(0).setStaticFeeParameters(
baseFactor,
filterPeriod,
decayPeriod,
reductionFactor,
variableFeeControl,
protocolShare,
maxVolatilityAccumulator
);
if (isOpen) {
preset = preset.setBool(true, _OFFSET_IS_PRESET_OPEN);
}
_presets.set(binStep, uint256(preset));
emit PresetSet(
binStep,
baseFactor,
filterPeriod,
decayPeriod,
reductionFactor,
variableFeeControl,
protocolShare,
maxVolatilityAccumulator
);
emit PresetOpenStateChanged(binStep, isOpen);
}
/**
* @notice Sets if the preset is open or not to be used by users
* @dev Needs to be called by the owner
* Reverts if:
* - The binStep doesn't have a preset
* - The preset is already in the same state
* @param binStep The bin step in basis point, used to calculate the price
* @param isOpen Whether the preset is open or not
*/
function setPresetOpenState(uint16 binStep, bool isOpen) external override onlyOwner {
if (!_presets.contains(binStep)) revert LBFactory__BinStepHasNoPreset(binStep);
bytes32 preset = bytes32(_presets.get(binStep));
if (preset.decodeBool(_OFFSET_IS_PRESET_OPEN) == isOpen) {
revert LBFactory__PresetOpenStateIsAlreadyInTheSameState();
}
_presets.set(binStep, uint256(preset.setBool(isOpen, _OFFSET_IS_PRESET_OPEN)));
emit PresetOpenStateChanged(binStep, isOpen);
}
/**
* @notice Remove the preset linked to a binStep
* @dev Needs to be called by the owner
* Reverts if:
* - The binStep doesn't have a preset
* @param binStep The bin step to remove
*/
function removePreset(uint16 binStep) external override onlyOwner {
if (!_presets.remove(binStep)) revert LBFactory__BinStepHasNoPreset(binStep);
emit PresetRemoved(binStep);
}
/**
* @notice Function to set the fee parameter of a LBPair
* @dev Needs to be called by the owner
* Reverts if:
* - The pair doesn't exist
* @param tokenX The address of the first token
* @param tokenY The address of the second token
* @param binStep The bin step in basis point, used to calculate the price
* @param baseFactor The base factor, used to calculate the base fee, baseFee = baseFactor * binStep
* @param filterPeriod The period where the accumulator value is untouched, prevent spam
* @param decayPeriod The period where the accumulator value is decayed, by the reduction factor
* @param reductionFactor The reduction factor, used to calculate the reduction of the accumulator
* @param variableFeeControl The variable fee control, used to control the variable fee, can be 0 to disable it
* @param protocolShare The share of the fees received by the protocol
* @param maxVolatilityAccumulator The max value of volatility accumulator
*/
function setFeesParametersOnPair(
IERC20 tokenX,
IERC20 tokenY,
uint16 binStep,
uint16 baseFactor,
uint16 filterPeriod,
uint16 decayPeriod,
uint16 reductionFactor,
uint24 variableFeeControl,
uint16 protocolShare,
uint24 maxVolatilityAccumulator
) external override onlyOwner {
ILBPair lbPair = _getLBPairInformation(tokenX, tokenY, binStep).LBPair;
if (address(lbPair) == address(0)) revert LBFactory__LBPairNotCreated(tokenX, tokenY, binStep);
lbPair.setStaticFeeParameters(
baseFactor,
filterPeriod,
decayPeriod,
reductionFactor,
variableFeeControl,
protocolShare,
maxVolatilityAccumulator
);
}
/**
* @notice Function to set the hooks parameters of a pair
* @dev Needs to be called by an address with the LB_HOOKS_MANAGER_ROLE
* Reverts if:
* - The pair doesn't exist
* - The hooks is `address(0)` or the hooks flags are all false
* @param tokenX The address of the first token
* @param tokenY The address of the second token
* @param binStep The bin step in basis point, used to calculate the price
* @param hooksParameters The hooks parameters
* @param onHooksSetData The data to pass to the onHooksSet function
*/
function setLBHooksParametersOnPair(
IERC20 tokenX,
IERC20 tokenY,
uint16 binStep,
bytes32 hooksParameters,
bytes calldata onHooksSetData
) external override onlyRole(LB_HOOKS_MANAGER_ROLE) {
if (Hooks.getHooks(hooksParameters) == address(0) || Hooks.getFlags(hooksParameters) == 0) {
revert LBFactory__InvalidHooksParameters();
}
_setLBHooksParametersOnPair(tokenX, tokenY, binStep, hooksParameters, onHooksSetData);
}
/**
* @notice Function to remove the hooks contract from the pair
* @dev Needs to be called by an address with the LB_HOOKS_MANAGER_ROLE
* Reverts if:
* - The pair doesn't exist
* @param tokenX The address of the first token
* @param tokenY The address of the second token
* @param binStep The bin step in basis point, used to calculate the price
*/
function removeLBHooksOnPair(IERC20 tokenX, IERC20 tokenY, uint16 binStep)
external
override
onlyRole(LB_HOOKS_MANAGER_ROLE)
{
_setLBHooksParametersOnPair(tokenX, tokenY, binStep, 0, new bytes(0));
}
/**
* @notice Function to set the recipient of the fees. This address needs to be able to receive ERC20s
* @dev Needs to be called by the owner
* Reverts if:
* - The feeRecipient is `address(0)`
* - The feeRecipient is the same as the current one
* @param feeRecipient The address of the recipient
*/
function setFeeRecipient(address feeRecipient) external override onlyOwner {
_setFeeRecipient(feeRecipient);
}
/**
* @notice Function to set the flash loan fee
* @dev Needs to be called by the owner
* Reverts if:
* - The flashLoanFee is the same as the current one
* - The flashLoanFee is above the maximum flash loan fee
* @param flashLoanFee The value of the fee for flash loan
*/
function setFlashLoanFee(uint256 flashLoanFee) external override onlyOwner {
uint256 oldFlashLoanFee = _flashLoanFee;
if (oldFlashLoanFee == flashLoanFee) revert LBFactory__SameFlashLoanFee(flashLoanFee);
if (flashLoanFee > _MAX_FLASHLOAN_FEE) revert LBFactory__FlashLoanFeeAboveMax(flashLoanFee, _MAX_FLASHLOAN_FEE);
_flashLoanFee = flashLoanFee;
emit FlashLoanFeeSet(oldFlashLoanFee, flashLoanFee);
}
/**
* @notice Function to add an asset to the whitelist of quote assets
* @dev Needs to be called by the owner
* Reverts if:
* - The quoteAsset is already whitelisted
* @param quoteAsset The quote asset (e.g: NATIVE, USDC...)
*/
function addQuoteAsset(IERC20 quoteAsset) external override onlyOwner {
if (!_quoteAssetWhitelist.add(address(quoteAsset))) {
revert LBFactory__QuoteAssetAlreadyWhitelisted(quoteAsset);
}
emit QuoteAssetAdded(quoteAsset);
}
/**
* @notice Function to remove an asset from the whitelist of quote assets
* @dev Needs to be called by the owner
* Reverts if:
* - The quoteAsset was not whitelisted
* @param quoteAsset The quote asset (e.g: NATIVE, USDC...)
*/
function removeQuoteAsset(IERC20 quoteAsset) external override onlyOwner {
if (!_quoteAssetWhitelist.remove(address(quoteAsset))) revert LBFactory__QuoteAssetNotWhitelisted(quoteAsset);
emit QuoteAssetRemoved(quoteAsset);
}
function _isPresetOpen(bytes32 preset) internal pure returns (bool) {
return preset.decodeBool(_OFFSET_IS_PRESET_OPEN);
}
/**
* @notice Internal function to set the recipient of the fee
* @param feeRecipient The address of the recipient
*/
function _setFeeRecipient(address feeRecipient) internal {
if (feeRecipient == address(0)) revert LBFactory__AddressZero();
address oldFeeRecipient = _feeRecipient;
if (oldFeeRecipient == feeRecipient) revert LBFactory__SameFeeRecipient(_feeRecipient);
_feeRecipient = feeRecipient;
emit FeeRecipientSet(oldFeeRecipient, feeRecipient);
}
/**
* @notice Function to force the decay of the volatility accumulator of a pair
* @dev Needs to be called by the owner
* @param pair The pair to force the decay
*/
function forceDecay(ILBPair pair) external override onlyOwner {
pair.forceDecay();
}
/**
* @notice Returns the LBPairInformation if it exists,
* if not, then the address 0 is returned. The order doesn't matter
* @param tokenA The address of the first token of the pair
* @param tokenB The address of the second token of the pair
* @param binStep The bin step of the LBPair
* @return The LBPairInformation
*/
function _getLBPairInformation(IERC20 tokenA, IERC20 tokenB, uint256 binStep)
private
view
returns (LBPairInformation memory)
{
(tokenA, tokenB) = _sortTokens(tokenA, tokenB);
return _lbPairsInfo[tokenA][tokenB][binStep];
}
/**
* @notice Private view function to sort 2 tokens in ascending order
* @param tokenA The first token
* @param tokenB The second token
* @return The sorted first token
* @return The sorted second token
*/
function _sortTokens(IERC20 tokenA, IERC20 tokenB) private pure returns (IERC20, IERC20) {
if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
return (tokenA, tokenB);
}
/**
* @notice Internal function to set a hooks contract to the pair
* @param tokenX The address of the first token
* @param tokenY The address of the second token
* @param binStep The bin step in basis point, used to calculate the price
* @param hooksParameters The hooks parameters
* @param onHooksSetData The data to pass to the onHooksSet function
*/
function _setLBHooksParametersOnPair(
IERC20 tokenX,
IERC20 tokenY,
uint16 binStep,
bytes32 hooksParameters,
bytes memory onHooksSetData
) internal {
ILBPair lbPair = _getLBPairInformation(tokenX, tokenY, binStep).LBPair;
if (address(lbPair) == address(0)) revert LBFactory__LBPairNotCreated(tokenX, tokenY, binStep);
if (lbPair.getLBHooksParameters() == hooksParameters) revert LBFactory__SameHooksParameters(hooksParameters);
lbPair.setHooksParameters(hooksParameters, onHooksSetData);
}
/**
* @notice Returns whether the caller has the role or not, only the owner has the DEFAULT_ADMIN_ROLE
* @param role The role to check
* @param account The address to check
* @return Whether the account has the role or not
*/
function hasRole(bytes32 role, address account) public view override returns (bool) {
if (role == DEFAULT_ADMIN_ROLE) return account == owner();
return super.hasRole(role, account);
}
/**
* @notice Grants a role to an address, the DEFAULT_ADMIN_ROLE can not be granted
* @param role The role to grant
* @param account The address to grant the role to
* @return Whether the role has been granted or not
*/
function _grantRole(bytes32 role, address account) internal override returns (bool) {
if (role == DEFAULT_ADMIN_ROLE) revert LBFactory__CannotGrantDefaultAdminRole();
return super._grantRole(role, account);
}
}