forked from sherlock-audit/2023-06-symmetrical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlFacet.sol
333 lines (290 loc) · 12.7 KB
/
ControlFacet.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
// SPDX-License-Identifier: SYMM-Core-Business-Source-License-1.1
// This contract is licensed under the SYMM Core Business Source License 1.1
// Copyright (c) 2023 Symmetry Labs AG
// For more information, see https://docs.symm.io/legal-disclaimer/license
pragma solidity >=0.8.18;
import "../../utils/Ownable.sol";
import "../../utils/Accessibility.sol";
import "../../storages/MAStorage.sol";
import "../../storages/MuonStorage.sol";
import "../../storages/GlobalAppStorage.sol";
import "../../storages/SymbolStorage.sol";
import "./IControlEvents.sol";
contract ControlFacet is Accessibility, Ownable, IControlEvents {
// Just For Testnet
function init(address user, address collateral, address feeCollector) external onlyOwner {
MAStorage.Layout storage maLayout = MAStorage.layout();
GlobalAppStorage.Layout storage appLayout = GlobalAppStorage.layout();
appLayout.collateral = collateral;
appLayout.balanceLimitPerUser = 500e18;
appLayout.feeCollector = feeCollector;
maLayout.deallocateCooldown = 300;
maLayout.forceCancelCooldown = 3000000000000000;
maLayout.forceCloseCooldown = 3000000000000000;
maLayout.forceCancelCloseCooldown = 3000000000000000;
maLayout.pendingQuotesValidLength = 15;
maLayout.liquidatorShare = 80e16;
maLayout.liquidationTimeout = 600;
appLayout.hasRole[user][LibAccessibility.DEFAULT_ADMIN_ROLE] = true;
appLayout.hasRole[user][LibAccessibility.SYMBOL_MANAGER_ROLE] = true;
appLayout.hasRole[user][LibAccessibility.MUON_SETTER_ROLE] = true;
appLayout.hasRole[user][LibAccessibility.SETTER_ROLE] = true;
appLayout.hasRole[user][LibAccessibility.PARTY_B_MANAGER_ROLE] = true;
}
function setAdmin(address user) external onlyOwner {
GlobalAppStorage.layout().hasRole[user][LibAccessibility.DEFAULT_ADMIN_ROLE] = true;
emit RoleGranted(LibAccessibility.DEFAULT_ADMIN_ROLE, user);
}
function grantRole(
address user,
bytes32 role
) external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
GlobalAppStorage.layout().hasRole[user][role] = true;
emit RoleGranted(role, user);
}
function revokeRole(
address user,
bytes32 role
) external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
GlobalAppStorage.layout().hasRole[user][role] = false;
emit RoleRevoked(role, user);
}
function registerPartyB(
address partyB
) external onlyRole(LibAccessibility.PARTY_B_MANAGER_ROLE) {
require(
!MAStorage.layout().partyBStatus[partyB],
"ControlFacet: Address is already registered"
);
MAStorage.layout().partyBStatus[partyB] = true;
MAStorage.layout().partyBList.push(partyB);
emit RegisterPartyB(partyB);
}
function setMuonConfig(
uint256 upnlValidTime,
uint256 priceValidTime,
uint256 priceQuantityValidTime
) external onlyRole(LibAccessibility.MUON_SETTER_ROLE) {
emit SetMuonConfig(upnlValidTime, priceValidTime, priceQuantityValidTime);
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
muonLayout.upnlValidTime = upnlValidTime;
muonLayout.priceValidTime = priceValidTime;
muonLayout.priceQuantityValidTime = priceQuantityValidTime;
}
function setMuonIds(
uint256 muonAppId,
address validGateway,
PublicKey memory publicKey
) external onlyRole(LibAccessibility.MUON_SETTER_ROLE) {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
muonLayout.muonAppId = muonAppId;
muonLayout.validGateway = validGateway;
muonLayout.muonPublicKey = publicKey;
emit SetMuonIds(muonAppId, validGateway, publicKey.x, publicKey.parity);
}
function setCollateral(
address collateral
) external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
GlobalAppStorage.layout().collateral = collateral;
emit SetCollateral(collateral);
}
// Symbol State
function addSymbol(
string memory name,
uint256 minAcceptableQuoteValue,
uint256 minAcceptablePortionLF,
uint256 tradingFee
) public onlyRole(LibAccessibility.SYMBOL_MANAGER_ROLE) {
uint256 lastId = ++SymbolStorage.layout().lastId;
Symbol memory symbol = Symbol(
lastId,
name,
true,
minAcceptableQuoteValue,
minAcceptablePortionLF,
tradingFee
);
SymbolStorage.layout().symbols[lastId] = symbol;
emit AddSymbol(lastId, name, minAcceptableQuoteValue, minAcceptablePortionLF, tradingFee);
}
function addSymbols(
Symbol[] memory symbols
) external onlyRole(LibAccessibility.SYMBOL_MANAGER_ROLE) {
for (uint8 i; i < symbols.length; i++) {
addSymbol(
symbols[i].name,
symbols[i].minAcceptableQuoteValue,
symbols[i].minAcceptablePortionLF,
symbols[i].tradingFee
);
}
}
function setSymbolValidationState(
uint256 symbolId,
bool isValid
) external onlyRole(LibAccessibility.SYMBOL_MANAGER_ROLE) {
SymbolStorage.Layout storage symbolLayout = SymbolStorage.layout();
require(symbolId >= 1 && symbolId <= symbolLayout.lastId, "ControlFacet: Invalid id");
emit SetSymbolValidationState(symbolId, symbolLayout.symbols[symbolId].isValid, isValid);
symbolLayout.symbols[symbolId].isValid = isValid;
}
function setSymbolAcceptableValues(
uint256 symbolId,
uint256 minAcceptableQuoteValue,
uint256 minAcceptablePortionLF
) external onlyRole(LibAccessibility.SYMBOL_MANAGER_ROLE) {
SymbolStorage.Layout storage symbolLayout = SymbolStorage.layout();
require(symbolId >= 1 && symbolId <= symbolLayout.lastId, "ControlFacet: Invalid id");
emit SetSymbolAcceptableValues(
symbolId,
symbolLayout.symbols[symbolId].minAcceptableQuoteValue,
symbolLayout.symbols[symbolId].minAcceptablePortionLF,
minAcceptableQuoteValue,
minAcceptablePortionLF
);
symbolLayout.symbols[symbolId].minAcceptableQuoteValue = minAcceptableQuoteValue;
symbolLayout.symbols[symbolId].minAcceptablePortionLF = minAcceptablePortionLF;
}
function setSymbolTradingFee(
uint256 symbolId,
uint256 tradingFee
) external onlyRole(LibAccessibility.SYMBOL_MANAGER_ROLE) {
SymbolStorage.Layout storage symbolLayout = SymbolStorage.layout();
require(symbolId >= 1 && symbolId <= symbolLayout.lastId, "ControlFacet: Invalid id");
emit SetSymbolTradingFee(symbolId, symbolLayout.symbols[symbolId].tradingFee, tradingFee);
symbolLayout.symbols[symbolId].tradingFee = tradingFee;
}
/////////////////////////////////////
// CoolDowns
function setDeallocateCooldown(
uint256 deallocateCooldown
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetDeallocateCooldown(MAStorage.layout().deallocateCooldown, deallocateCooldown);
MAStorage.layout().deallocateCooldown = deallocateCooldown;
}
function setForceCancelCooldown(
uint256 forceCancelCooldown
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetForceCancelCooldown(MAStorage.layout().forceCancelCooldown, forceCancelCooldown);
MAStorage.layout().forceCancelCooldown = forceCancelCooldown;
}
function setForceCloseCooldown(
uint256 forceCloseCooldown
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetForceCloseCooldown(MAStorage.layout().forceCloseCooldown, forceCloseCooldown);
MAStorage.layout().forceCloseCooldown = forceCloseCooldown;
}
function setForceCancelCloseCooldown(
uint256 forceCancelCloseCooldown
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetForceCancelCloseCooldown(
MAStorage.layout().forceCancelCloseCooldown,
forceCancelCloseCooldown
);
MAStorage.layout().forceCancelCloseCooldown = forceCancelCloseCooldown;
}
function setLiquidatorShare(
uint256 liquidatorShare
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetLiquidatorShare(MAStorage.layout().liquidatorShare, liquidatorShare);
MAStorage.layout().liquidatorShare = liquidatorShare;
}
function setForceCloseGapRatio(
uint256 forceCloseGapRatio
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetForceCloseGapRatio(MAStorage.layout().forceCloseGapRatio, forceCloseGapRatio);
MAStorage.layout().forceCloseGapRatio = forceCloseGapRatio;
}
function setPendingQuotesValidLength(
uint256 pendingQuotesValidLength
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetPendingQuotesValidLength(
MAStorage.layout().pendingQuotesValidLength,
pendingQuotesValidLength
);
MAStorage.layout().pendingQuotesValidLength = pendingQuotesValidLength;
}
// Pause State
function setFeeCollector(
address feeCollector
) external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
emit SetFeeCollector(GlobalAppStorage.layout().feeCollector, feeCollector);
GlobalAppStorage.layout().feeCollector = feeCollector;
}
function pauseGlobal() external onlyRole(LibAccessibility.PAUSER_ROLE) {
GlobalAppStorage.layout().globalPaused = true;
emit PauseGlobal();
}
function pauseLiquidation() external onlyRole(LibAccessibility.PAUSER_ROLE) {
GlobalAppStorage.layout().liquidationPaused = true;
emit PauseLiquidation();
}
function pauseAccounting() external onlyRole(LibAccessibility.PAUSER_ROLE) {
GlobalAppStorage.layout().accountingPaused = true;
emit PauseAccounting();
}
function pausePartyAActions() external onlyRole(LibAccessibility.PAUSER_ROLE) {
GlobalAppStorage.layout().partyAActionsPaused = true;
emit PausePartyAActions();
}
function pausePartyBActions() external onlyRole(LibAccessibility.PAUSER_ROLE) {
GlobalAppStorage.layout().partyBActionsPaused = true;
emit PausePartyBActions();
}
function activeEmergencyMode() external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
GlobalAppStorage.layout().emergencyMode = true;
emit ActiveEmergencyMode();
}
function unpauseGlobal() external onlyRole(LibAccessibility.UNPAUSER_ROLE) {
GlobalAppStorage.layout().globalPaused = false;
emit UnpauseGlobal();
}
function unpauseLiquidation() external onlyRole(LibAccessibility.UNPAUSER_ROLE) {
GlobalAppStorage.layout().liquidationPaused = false;
emit UnpauseLiquidation();
}
function unpauseAccounting() external onlyRole(LibAccessibility.UNPAUSER_ROLE) {
GlobalAppStorage.layout().accountingPaused = false;
emit UnpauseAccounting();
}
function unpausePartyAActions() external onlyRole(LibAccessibility.UNPAUSER_ROLE) {
GlobalAppStorage.layout().partyAActionsPaused = false;
emit UnpausePartyAActions();
}
function unpausePartyBActions() external onlyRole(LibAccessibility.UNPAUSER_ROLE) {
GlobalAppStorage.layout().partyBActionsPaused = false;
emit UnpausePartyBActions();
}
function setLiquidationTimeout(
uint256 liquidationTimeout
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetLiquidationTimeout(MAStorage.layout().liquidationTimeout, liquidationTimeout);
MAStorage.layout().liquidationTimeout = liquidationTimeout;
}
function setSuspendedAddress(
address user,
bool isSuspended
) external onlyRole(LibAccessibility.SETTER_ROLE) {
emit SetSuspendedAddress(user, isSuspended);
AccountStorage.layout().suspendedAddresses[user] = isSuspended;
}
function deactiveEmergencyMode() external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
GlobalAppStorage.layout().emergencyMode = false;
emit DeactiveEmergencyMode();
}
function setBalanceLimitPerUser(
uint256 balanceLimitPerUser
) external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
emit SetBalanceLimitPerUser(balanceLimitPerUser);
GlobalAppStorage.layout().balanceLimitPerUser = balanceLimitPerUser;
}
function setPartyBEmergencyStatus(
address[] memory partyBs,
bool status
) external onlyRole(LibAccessibility.DEFAULT_ADMIN_ROLE) {
for (uint8 i; i < partyBs.length; i++) {
GlobalAppStorage.layout().partyBEmergencyStatus[partyBs[i]] = status;
emit SetPartyBEmergencyStatus(partyBs[i], status);
}
}
}