-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDexManagerFacet.sol
89 lines (75 loc) · 2.71 KB
/
DexManagerFacet.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "../Libraries/LibStorage.sol";
import "../Libraries/LibDiamond.sol";
/**
* @title Optics Router Facet
* @author Li.Finance (https://li.finance)
* @notice Facet contract for managing approved DEXs to be used in swaps.
*/
contract DexManagerFacet {
LibStorage internal s;
/// @notice Register the address of a DEX contract to be approved for swapping.
/// @param _dex The address of the DEX contract to be approved.
function addDex(address _dex) external {
LibDiamond.enforceIsContractOwner();
if (s.dexWhitelist[_dex] == true) {
return;
}
s.dexWhitelist[_dex] = true;
s.dexs.push(_dex);
}
/// @notice Batch register the addresss of DEX contracts to be approved for swapping.
/// @param _dexs The addresses of the DEX contracts to be approved.
function batchAddDex(address[] calldata _dexs) external {
LibDiamond.enforceIsContractOwner();
for (uint256 i; i < _dexs.length; i++) {
if (s.dexWhitelist[_dexs[i]] == true) {
continue;
}
s.dexWhitelist[_dexs[i]] = true;
s.dexs.push(_dexs[i]);
}
}
/// @notice Unregister the address of a DEX contract approved for swapping.
/// @param _dex The address of the DEX contract to be unregistered.
function removeDex(address _dex) external {
LibDiamond.enforceIsContractOwner();
if (s.dexWhitelist[_dex] == false) {
return;
}
s.dexWhitelist[_dex] = false;
for (uint256 i; i < s.dexs.length; i++) {
if (s.dexs[i] == _dex) {
_removeDex(i);
return;
}
}
}
/// @notice Batch unregister the addresses of DEX contracts approved for swapping.
/// @param _dexs The addresses of the DEX contracts to be unregistered.
function batchRemoveDex(address[] calldata _dexs) external {
LibDiamond.enforceIsContractOwner();
for (uint256 i; i < _dexs.length; i++) {
if (s.dexWhitelist[_dexs[i]] == false) {
continue;
}
s.dexWhitelist[_dexs[i]] = false;
for (uint256 j; j < s.dexs.length; j++) {
if (s.dexs[j] == _dexs[i]) {
_removeDex(j);
return;
}
}
}
}
function approvedDexs() external view returns (address[] memory) {
return s.dexs;
}
function _removeDex(uint256 index) private {
// Move the last element into the place to delete
s.dexs[index] = s.dexs[s.dexs.length - 1];
// Remove the last element
s.dexs.pop();
}
}