forked from sherlock-audit/2023-06-symmetrical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewFacet.sol
434 lines (381 loc) · 15.1 KB
/
ViewFacet.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
// 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 "../libraries/LibLockedValues.sol";
import "../libraries/LibQuote.sol";
import "../libraries/LibMuon.sol";
import "../storages/AccountStorage.sol";
import "../storages/MAStorage.sol";
import "../storages/QuoteStorage.sol";
import "../storages/GlobalAppStorage.sol";
import "../storages/SymbolStorage.sol";
import "../storages/MuonStorage.sol";
import "../libraries/LibLockedValues.sol";
contract ViewFacet {
using LockedValuesOps for LockedValues;
// Account
function balanceOf(address user) external view returns (uint256) {
return AccountStorage.layout().balances[user];
}
function partyAStats(
address partyA
)
external
view
returns (
bool,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256,
uint256
)
{
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
MAStorage.Layout storage maLayout = MAStorage.layout();
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
return (
maLayout.liquidationStatus[partyA],
accountLayout.allocatedBalances[partyA],
accountLayout.lockedBalances[partyA].cva,
accountLayout.lockedBalances[partyA].mm,
accountLayout.lockedBalances[partyA].lf,
accountLayout.lockedBalances[partyA].total(),
accountLayout.pendingLockedBalances[partyA].cva,
accountLayout.pendingLockedBalances[partyA].mm,
accountLayout.pendingLockedBalances[partyA].lf,
accountLayout.pendingLockedBalances[partyA].total(),
quoteLayout.partyAPositionsCount[partyA],
quoteLayout.partyAPendingQuotes[partyA].length,
accountLayout.partyANonces[partyA],
quoteLayout.quoteIdsOf[partyA].length
);
}
function balanceInfoOfPartyA(
address partyA
)
external
view
returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256)
{
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
return (
accountLayout.allocatedBalances[partyA],
accountLayout.lockedBalances[partyA].cva,
accountLayout.lockedBalances[partyA].mm,
accountLayout.lockedBalances[partyA].lf,
accountLayout.lockedBalances[partyA].total(),
accountLayout.pendingLockedBalances[partyA].cva,
accountLayout.pendingLockedBalances[partyA].mm,
accountLayout.pendingLockedBalances[partyA].lf,
accountLayout.pendingLockedBalances[partyA].total()
);
}
function balanceInfoOfPartyB(
address partyB,
address partyA
)
external
view
returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256)
{
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
return (
accountLayout.partyBAllocatedBalances[partyB][partyA],
accountLayout.partyBLockedBalances[partyB][partyA].cva,
accountLayout.partyBLockedBalances[partyB][partyA].mm,
accountLayout.partyBLockedBalances[partyB][partyA].lf,
accountLayout.partyBLockedBalances[partyB][partyA].total(),
accountLayout.partyBPendingLockedBalances[partyB][partyA].cva,
accountLayout.partyBPendingLockedBalances[partyB][partyA].mm,
accountLayout.partyBPendingLockedBalances[partyB][partyA].lf,
accountLayout.partyBPendingLockedBalances[partyB][partyA].total()
);
}
function allocatedBalanceOfPartyA(address partyA) external view returns (uint256) {
return AccountStorage.layout().allocatedBalances[partyA];
}
function allocatedBalanceOfPartyB(
address partyB,
address partyA
) external view returns (uint256) {
return AccountStorage.layout().partyBAllocatedBalances[partyB][partyA];
}
function withdrawCooldownOf(address user) external view returns (uint256) {
return AccountStorage.layout().withdrawCooldown[user];
}
function nonceOfPartyA(address partyA) external view returns (uint256) {
return AccountStorage.layout().partyANonces[partyA];
}
function nonceOfPartyB(address partyB, address partyA) external view returns (uint256) {
return AccountStorage.layout().partyBNonces[partyB][partyA];
}
function isSuspended(address user) external view returns (bool) {
return AccountStorage.layout().suspendedAddresses[user];
}
///////////////////////////////////////////
// Symbols
function getSymbol(uint256 symbolId) external view returns (Symbol memory) {
return SymbolStorage.layout().symbols[symbolId];
}
function getSymbols(uint256 start, uint256 size) external view returns (Symbol[] memory) {
SymbolStorage.Layout storage symbolLayout = SymbolStorage.layout();
if (symbolLayout.lastId < start + size) {
size = symbolLayout.lastId - start;
}
Symbol[] memory symbols = new Symbol[](size);
for (uint256 i = start; i < start + size; i++) {
symbols[i - start] = symbolLayout.symbols[i + 1];
}
return symbols;
}
function symbolsByQuoteId(uint256[] memory quoteIds) external view returns (Symbol[] memory) {
Symbol[] memory symbols = new Symbol[](quoteIds.length);
for (uint256 i = 0; i < quoteIds.length; i++) {
symbols[i] = SymbolStorage.layout().symbols[
QuoteStorage.layout().quotes[quoteIds[i]].symbolId
];
}
return symbols;
}
function symbolNameByQuoteId(
uint256[] memory quoteIds
) external view returns (string[] memory) {
string[] memory symbols = new string[](quoteIds.length);
for (uint256 i = 0; i < quoteIds.length; i++) {
symbols[i] = SymbolStorage
.layout()
.symbols[QuoteStorage.layout().quotes[quoteIds[i]].symbolId]
.name;
}
return symbols;
}
function symbolNameById(uint256[] memory symbolIds) external view returns (string[] memory) {
string[] memory symbols = new string[](symbolIds.length);
for (uint256 i = 0; i < symbolIds.length; i++) {
symbols[i] = SymbolStorage.layout().symbols[symbolIds[i]].name;
}
return symbols;
}
////////////////////////////////////
// Quotes
function getQuote(uint256 quoteId) external view returns (Quote memory) {
return QuoteStorage.layout().quotes[quoteId];
}
function getQuotesByParent(
uint256 quoteId,
uint256 size
) external view returns (Quote[] memory) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
Quote[] memory quotes = new Quote[](size);
Quote memory quote = quoteLayout.quotes[quoteId];
quotes[0] = quote;
for (uint256 i = 1; i < size; i++) {
if (quote.parentId == 0) {
break;
}
quote = quoteLayout.quotes[quote.parentId];
quotes[i] = quote;
}
return quotes;
}
function quoteIdsOf(
address partyA,
uint256 start,
uint256 size
) external view returns (uint256[] memory) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
if (quoteLayout.quoteIdsOf[partyA].length < start + size) {
size = quoteLayout.quoteIdsOf[partyA].length - start;
}
uint256[] memory quoteIds = new uint256[](size);
for (uint256 i = start; i < start + size; i++) {
quoteIds[i - start] = quoteLayout.quoteIdsOf[partyA][i];
}
return quoteIds;
}
function getQuotes(
address partyA,
uint256 start,
uint256 size
) external view returns (Quote[] memory) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
if (quoteLayout.quoteIdsOf[partyA].length < start + size) {
size = quoteLayout.quoteIdsOf[partyA].length - start;
}
Quote[] memory quotes = new Quote[](size);
for (uint256 i = start; i < start + size; i++) {
quotes[i - start] = quoteLayout.quotes[quoteLayout.quoteIdsOf[partyA][i]];
}
return quotes;
}
function quotesLength(address user) external view returns (uint256) {
return QuoteStorage.layout().quoteIdsOf[user].length;
}
function partyAPositionsCount(address partyA) external view returns (uint256) {
return QuoteStorage.layout().partyAPositionsCount[partyA];
}
function getPartyAOpenPositions(
address partyA,
uint256 start,
uint256 size
) external view returns (Quote[] memory) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
if (quoteLayout.partyAOpenPositions[partyA].length < start + size) {
size = quoteLayout.partyAOpenPositions[partyA].length - start;
}
Quote[] memory quotes = new Quote[](size);
for (uint256 i = start; i < start + size; i++) {
quotes[i - start] = quoteLayout.quotes[quoteLayout.partyAOpenPositions[partyA][i]];
}
return quotes;
}
function getPartyBOpenPositions(
address partyB,
address partyA,
uint256 start,
uint256 size
) external view returns (Quote[] memory) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
if (quoteLayout.partyBOpenPositions[partyB][partyA].length < start + size) {
size = quoteLayout.partyBOpenPositions[partyB][partyA].length - start;
}
Quote[] memory quotes = new Quote[](size);
for (uint256 i = start; i < start + size; i++) {
quotes[i - start] = quoteLayout.quotes[
quoteLayout.partyBOpenPositions[partyB][partyA][i]
];
}
return quotes;
}
function partyBPositionsCount(address partyB, address partyA) external view returns (uint256) {
return QuoteStorage.layout().partyBPositionsCount[partyB][partyA];
}
function getPartyAPendingQuotes(address partyA) external view returns (uint256[] memory) {
return QuoteStorage.layout().partyAPendingQuotes[partyA];
}
function getPartyBPendingQuotes(
address partyB,
address partyA
) external view returns (uint256[] memory) {
return QuoteStorage.layout().partyBPendingQuotes[partyB][partyA];
}
/////////////////////////////////////
// Role
function hasRole(address user, bytes32 role) external view returns (bool) {
return GlobalAppStorage.layout().hasRole[user][role];
}
function getRoleHash(string memory str) external pure returns (bytes32) {
return keccak256(abi.encodePacked(str));
}
//////////////////////////////////////
// MA
function getCollateral() external view returns (address) {
return GlobalAppStorage.layout().collateral;
}
function getFeeCollector() external view returns (address) {
return GlobalAppStorage.layout().feeCollector;
}
function isPartyALiquidated(address partyA) external view returns (bool) {
return MAStorage.layout().liquidationStatus[partyA];
}
function isPartyBLiquidated(address partyB, address partyA) external view returns (bool) {
return MAStorage.layout().partyBLiquidationStatus[partyB][partyA];
}
function isPartyB(address user) external view returns (bool) {
return MAStorage.layout().partyBStatus[user];
}
function pendingQuotesValidLength() external view returns (uint256) {
return MAStorage.layout().pendingQuotesValidLength;
}
function forceCloseGapRatio() external view returns (uint256) {
return MAStorage.layout().forceCloseGapRatio;
}
function liquidatorShare() external view returns (uint256) {
return MAStorage.layout().liquidatorShare;
}
function liquidationTimeout() external view returns (uint256) {
return MAStorage.layout().liquidationTimeout;
}
function partyALiquidationTimestamp(address partyA) external view returns (uint256) {
return MAStorage.layout().liquidationTimestamp[partyA];
}
function partyBLiquidationTimestamp(
address partyB,
address partyA
) external view returns (uint256) {
return MAStorage.layout().partyBLiquidationTimestamp[partyB][partyA];
}
function coolDownsOfMA() external view returns (uint256, uint256, uint256, uint256) {
return (
MAStorage.layout().deallocateCooldown,
MAStorage.layout().forceCancelCooldown,
MAStorage.layout().forceCancelCloseCooldown,
MAStorage.layout().forceCloseCooldown
);
}
///////////////////////////////////////////
function getMuonConfig()
external
view
returns (uint256 upnlValidTime, uint256 priceValidTime, uint256 priceQuantityValidTime)
{
upnlValidTime = MuonStorage.layout().upnlValidTime;
priceValidTime = MuonStorage.layout().priceValidTime;
priceQuantityValidTime = MuonStorage.layout().priceQuantityValidTime;
}
function getMuonIds()
external
view
returns (uint256 muonAppId, PublicKey memory muonPublicKey, address validGateway)
{
muonAppId = MuonStorage.layout().muonAppId;
muonPublicKey = MuonStorage.layout().muonPublicKey;
validGateway = MuonStorage.layout().validGateway;
}
function pauseState()
external
view
returns (
bool globalPaused,
bool liquidationPaused,
bool accountingPaused,
bool partyBActionsPaused,
bool partyAActionsPaused,
bool emergencyMode
)
{
GlobalAppStorage.Layout storage appLayout = GlobalAppStorage.layout();
return (
appLayout.globalPaused,
appLayout.liquidationPaused,
appLayout.accountingPaused,
appLayout.partyBActionsPaused,
appLayout.partyAActionsPaused,
appLayout.emergencyMode
);
}
function getPartyBEmergencyStatus(address partyB) external view returns (bool isEmergency) {
return GlobalAppStorage.layout().partyBEmergencyStatus[partyB];
}
function getBalanceLimitPerUser() external view returns (uint256) {
return GlobalAppStorage.layout().balanceLimitPerUser;
}
function verifyMuonTSSAndGateway(
bytes32 hash,
SchnorrSign memory sign,
bytes memory gatewaySignature
) external view {
LibMuon.verifyTSSAndGateway(hash, sign, gatewaySignature);
}
}