-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSymbolService.sol
183 lines (165 loc) · 6.98 KB
/
SymbolService.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.7.4;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/EnumerableSet.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "../interface/ILiquidityPool.sol";
contract SymbolService is Ownable {
using Address for address;
using SafeMath for uint256;
using EnumerableSet for EnumerableSet.UintSet;
using EnumerableSet for EnumerableSet.AddressSet;
struct PerpetualUID {
address liquidityPool;
uint256 perpetualIndex;
}
mapping(uint256 => PerpetualUID) internal _perpetualUIDs;
mapping(bytes32 => EnumerableSet.UintSet) internal _perpetualSymbols;
uint256 internal _nextSymbol;
uint256 internal _reservedSymbolCount;
EnumerableSet.AddressSet internal _whitelistedFactories;
event AllocateSymbol(address liquidityPool, uint256 perpetualIndex, uint256 symbol);
event AddWhitelistedFactory(address factory);
event RemoveWhitelistedFactory(address factory);
constructor(uint256 reservedSymbolCount) Ownable() {
_nextSymbol = reservedSymbolCount;
_reservedSymbolCount = reservedSymbolCount;
}
/**
* @notice Check if the factory is whitelisted
* @param factory The address of the factory
* @return bool True if the factory is whitelisted
*/
function isWhitelistedFactory(address factory) public view returns (bool) {
return _whitelistedFactories.contains(factory);
}
/**
* @notice Add the factory to the whitelist. Can only called by owner
* @param factory The address of the factory
*/
function addWhitelistedFactory(address factory) public onlyOwner {
require(factory.isContract(), "factory must be a contract");
require(!isWhitelistedFactory(factory), "factory already exists");
_whitelistedFactories.add(factory);
emit AddWhitelistedFactory(factory);
}
/**
* @notice Remove the factory from the whitelist. Can only called by owner
* @param factory The address of the factory
*/
function removeWhitelistedFactory(address factory) public onlyOwner {
require(isWhitelistedFactory(factory), "factory not found");
_whitelistedFactories.remove(factory);
emit RemoveWhitelistedFactory(factory);
}
modifier onlyWhitelisted(address liquidityPool) {
require(Address.isContract(liquidityPool), "must called by contract");
(, , address[7] memory addresses, , ) =
ILiquidityPool(liquidityPool).getLiquidityPoolInfo();
require(_whitelistedFactories.contains(addresses[0]), "wrong factory");
_;
}
/**
* @notice Get the unique id(liquidity pool + perpetual index) of the perpetual by the symbol
* @param symbol The symbol
* @return liquidityPool The address of the liquidity pool
* @return perpetualIndex The index of the perpetual in the liquidity pool
*/
function getPerpetualUID(uint256 symbol)
public
view
returns (address liquidityPool, uint256 perpetualIndex)
{
PerpetualUID storage perpetualUID = _perpetualUIDs[symbol];
require(perpetualUID.liquidityPool != address(0), "symbol not found");
liquidityPool = perpetualUID.liquidityPool;
perpetualIndex = perpetualUID.perpetualIndex;
}
/**
* @notice Get the symbols of the perpetual by the unique id(liquidity pool + perpetual index)
* @param liquidityPool The address of the liquidity pool
* @param perpetualIndex The index of the perpetual in the liquidity pool
* @return symbols The symbols of the perpetual
*/
function getSymbols(address liquidityPool, uint256 perpetualIndex)
public
view
returns (uint256[] memory symbols)
{
bytes32 key = _getPerpetualKey(liquidityPool, perpetualIndex);
uint256 length = _perpetualSymbols[key].length();
if (length == 0) {
return symbols;
}
symbols = new uint256[](length);
for (uint256 i = 0; i < length; i++) {
symbols[i] = _perpetualSymbols[key].at(i);
}
}
/**
* @notice Allocate the perpetual an unreserved symbol The perpetual must have no symbol before.
* Can only called by whitelisted factory
* @param liquidityPool The address of the liquidity pool
* @param perpetualIndex The index of the perpetual in the liquidity pool
* @return symbol The symbol allocated
*/
function allocateSymbol(address liquidityPool, uint256 perpetualIndex)
public
onlyWhitelisted(msg.sender)
returns (uint256 symbol)
{
bytes32 key = _getPerpetualKey(liquidityPool, perpetualIndex);
require(_perpetualSymbols[key].length() == 0, "perpetual already exists");
symbol = _nextSymbol;
require(symbol < type(uint256).max, "not enough symbol");
_perpetualUIDs[symbol] = PerpetualUID({
liquidityPool: liquidityPool,
perpetualIndex: perpetualIndex
});
_perpetualSymbols[key].add(symbol);
_nextSymbol = _nextSymbol.add(1);
emit AllocateSymbol(liquidityPool, perpetualIndex, symbol);
}
/**
* @notice Assign perpetual a reserved symbol. The perpetual must have unreserved symbol
* and not have reserved symbol before. Can only called by owner
* @param liquidityPool The address of the liquidity pool
* @param perpetualIndex The index of the perpetual in the liquidity pool
* @param symbol The symbol assigned
*/
function assignReservedSymbol(
address liquidityPool,
uint256 perpetualIndex,
uint256 symbol
) public onlyOwner onlyWhitelisted(liquidityPool) {
require(symbol < _reservedSymbolCount, "symbol exceeds reserved symbol count");
require(_perpetualUIDs[symbol].liquidityPool == address(0), "symbol already exists");
bytes32 key = _getPerpetualKey(liquidityPool, perpetualIndex);
require(
_perpetualSymbols[key].length() == 1 &&
_perpetualSymbols[key].at(0) >= _reservedSymbolCount,
"perpetual must have normal symbol and mustn't have reversed symbol"
);
_perpetualUIDs[symbol] = PerpetualUID({
liquidityPool: liquidityPool,
perpetualIndex: perpetualIndex
});
_perpetualSymbols[key].add(symbol);
emit AllocateSymbol(liquidityPool, perpetualIndex, symbol);
}
/**
* @dev Get the key of the perpetual
* @param liquidityPool The address of the liquidity pool which the perpetual belongs to
* @param perpetualIndex The index of the perpetual in the liquidity pool
* @return bytes32 The key of the perpetual
*/
function _getPerpetualKey(address liquidityPool, uint256 perpetualIndex)
internal
pure
returns (bytes32)
{
return keccak256(abi.encodePacked(liquidityPool, perpetualIndex));
}
}