This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPartyBFacetImpl.sol
323 lines (294 loc) · 14.3 KB
/
PartyBFacetImpl.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
// 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/LibMuon.sol";
import "../../libraries/LibAccount.sol";
import "../../libraries/LibSolvency.sol";
import "../../libraries/LibQuote.sol";
import "../../storages/MAStorage.sol";
import "../../storages/QuoteStorage.sol";
import "../../storages/MuonStorage.sol";
import "../../storages/GlobalAppStorage.sol";
import "../../storages/AccountStorage.sol";
import "../../storages/SymbolStorage.sol";
library PartyBFacetImpl {
using LockedValuesOps for LockedValues;
function lockQuote(uint256 quoteId, SingleUpnlSig memory upnlSig, bool increaseNonce) internal {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = quoteLayout.quotes[quoteId];
LibMuon.verifyPartyBUpnl(upnlSig, msg.sender, quote.partyA);
checkPartyBValidationToLockQuote(quoteId, upnlSig.upnl);
if (increaseNonce) {
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
}
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.LOCKED;
quote.partyB = msg.sender;
// lock funds for partyB
accountLayout.partyBPendingLockedBalances[msg.sender][quote.partyA].addQuote(quote);
quoteLayout.partyBPendingQuotes[msg.sender][quote.partyA].push(quote.id);
}
function unlockQuote(uint256 quoteId) internal returns (QuoteStatus) {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.LOCKED, "PartyBFacet: Invalid state");
if (block.timestamp > quote.deadline) {
QuoteStatus result = LibQuote.expireQuote(quoteId);
return result;
} else {
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.PENDING;
accountLayout.partyBPendingLockedBalances[quote.partyB][quote.partyA].subQuote(quote);
LibQuote.removeFromPartyBPendingQuotes(quote);
quote.partyB = address(0);
return QuoteStatus.PENDING;
}
}
function acceptCancelRequest(uint256 quoteId) internal {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.CANCEL_PENDING, "PartyBFacet: Invalid state");
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.CANCELED;
accountLayout.pendingLockedBalances[quote.partyA].subQuote(quote);
accountLayout.partyBPendingLockedBalances[quote.partyB][quote.partyA].subQuote(quote);
// send trading Fee back to partyA
LibQuote.returnTradingFee(quoteId);
LibQuote.removeFromPendingQuotes(quote);
}
function checkPartyBValidationToLockQuote(uint256 quoteId, int256 upnl) internal view {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
MAStorage.Layout storage maLayout = MAStorage.layout();
Quote storage quote = quoteLayout.quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.PENDING, "PartyBFacet: Invalid state");
require(block.timestamp <= quote.deadline, "PartyBFacet: Quote is expired");
require(quoteId <= quoteLayout.lastId, "PartyBFacet: Invalid quoteId");
int256 availableBalance = LibAccount.partyBAvailableForQuote(
upnl,
msg.sender,
quote.partyA
);
require(availableBalance >= 0, "PartyBFacet: Available balance is lower than zero");
require(
uint256(availableBalance) >= quote.lockedValues.total(),
"PartyBFacet: insufficient available balance"
);
require(
!maLayout.partyBLiquidationStatus[msg.sender][quote.partyA],
"PartyBFacet: PartyB isn't solvent"
);
bool isValidPartyB;
if (quote.partyBsWhiteList.length == 0) {
require(msg.sender != quote.partyA, "PartyBFacet: PartyA can't be partyB too");
isValidPartyB = true;
} else {
for (uint8 index = 0; index < quote.partyBsWhiteList.length; index++) {
if (msg.sender == quote.partyBsWhiteList[index]) {
isValidPartyB = true;
break;
}
}
}
require(isValidPartyB, "PartyBFacet: Sender isn't whitelisted");
}
function openPosition(
uint256 quoteId,
uint256 filledAmount,
uint256 openedPrice,
PairUpnlAndPriceSig memory upnlSig
) internal returns (uint256 currentId) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = quoteLayout.quotes[quoteId];
require(
quote.quoteStatus == QuoteStatus.LOCKED ||
quote.quoteStatus == QuoteStatus.CANCEL_PENDING,
"PartyBFacet: Invalid state"
);
require(block.timestamp <= quote.deadline, "PartyBFacet: Quote is expired");
if (quote.orderType == OrderType.LIMIT) {
require(
quote.quantity >= filledAmount && filledAmount > 0,
"PartyBFacet: Invalid filledAmount"
);
} else {
require(quote.quantity == filledAmount, "PartyBFacet: Invalid filledAmount");
}
if (quote.positionType == PositionType.LONG) {
require(
openedPrice <= quote.requestedOpenPrice,
"PartyBFacet: Opened price isn't valid"
);
} else {
require(
openedPrice >= quote.requestedOpenPrice,
"PartyBFacet: Opened price isn't valid"
);
}
LibMuon.verifyPairUpnlAndPrice(upnlSig, quote.partyB, quote.partyA, quote.symbolId);
quote.openedPrice = openedPrice;
LibSolvency.isSolventAfterOpenPosition(quoteId, filledAmount, upnlSig);
accountLayout.partyANonces[quote.partyA] += 1;
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
LibQuote.removeFromPendingQuotes(quote);
if (quote.quantity == filledAmount) {
accountLayout.pendingLockedBalances[quote.partyA].subQuote(quote);
accountLayout.partyBPendingLockedBalances[quote.partyB][quote.partyA].subQuote(quote);
if (quote.orderType == OrderType.LIMIT) {
quote.lockedValues.mul(openedPrice).div(quote.requestedOpenPrice);
}
accountLayout.lockedBalances[quote.partyA].addQuote(quote);
accountLayout.partyBLockedBalances[quote.partyB][quote.partyA].addQuote(quote);
}
// partially fill
else {
currentId = ++quoteLayout.lastId;
QuoteStatus newStatus;
if (quote.quoteStatus == QuoteStatus.CANCEL_PENDING) {
newStatus = QuoteStatus.CANCELED;
} else {
newStatus = QuoteStatus.PENDING;
quoteLayout.partyAPendingQuotes[quote.partyA].push(currentId);
}
LockedValues memory filledLockedValues = LockedValues(
(quote.lockedValues.cva * filledAmount) / quote.quantity,
(quote.lockedValues.mm * filledAmount) / quote.quantity,
(quote.lockedValues.lf * filledAmount) / quote.quantity
);
LockedValues memory appliedFilledLockedValues = filledLockedValues;
appliedFilledLockedValues = appliedFilledLockedValues.mulMem(openedPrice);
appliedFilledLockedValues = appliedFilledLockedValues.divMem(quote.requestedOpenPrice);
// check that opened position is not minor position
require(
appliedFilledLockedValues.total() >=
SymbolStorage.layout().symbols[quote.symbolId].minAcceptableQuoteValue,
"PartyBFacet: Quote value is low"
);
// check that new pending position is not minor position
require(
(quote.lockedValues.total() - filledLockedValues.total()) >=
SymbolStorage.layout().symbols[quote.symbolId].minAcceptableQuoteValue,
"PartyBFacet: Quote value is low"
);
Quote memory q = Quote({
id: currentId,
partyBsWhiteList: quote.partyBsWhiteList,
symbolId: quote.symbolId,
positionType: quote.positionType,
orderType: quote.orderType,
openedPrice: 0,
requestedOpenPrice: quote.requestedOpenPrice,
marketPrice: quote.marketPrice,
quantity: quote.quantity - filledAmount,
closedAmount: 0,
lockedValues: LockedValues(0, 0, 0),
initialLockedValues: LockedValues(0, 0, 0),
maxInterestRate: quote.maxInterestRate,
partyA: quote.partyA,
partyB: address(0),
quoteStatus: newStatus,
avgClosedPrice: 0,
requestedClosePrice: 0,
parentId: quote.id,
createTimestamp: quote.createTimestamp,
modifyTimestamp: block.timestamp,
quantityToClose: 0,
deadline: quote.deadline
});
quoteLayout.quoteIdsOf[quote.partyA].push(currentId);
quoteLayout.quotes[currentId] = q;
Quote storage newQuote = quoteLayout.quotes[currentId];
if (newStatus == QuoteStatus.CANCELED) {
// send trading Fee back to partyA
LibQuote.returnTradingFee(currentId);
// part of quote has been filled and part of it has been canceled
accountLayout.pendingLockedBalances[quote.partyA].subQuote(quote);
accountLayout.partyBPendingLockedBalances[quote.partyB][quote.partyA].subQuote(
quote
);
} else {
accountLayout.pendingLockedBalances[quote.partyA].sub(filledLockedValues);
accountLayout.partyBPendingLockedBalances[quote.partyB][quote.partyA].sub(
filledLockedValues
);
}
newQuote.lockedValues = quote.lockedValues.sub(filledLockedValues);
newQuote.initialLockedValues = newQuote.lockedValues;
quote.quantity = filledAmount;
quote.lockedValues = appliedFilledLockedValues;
// lock with amount of filledAmount
accountLayout.lockedBalances[quote.partyA].addQuote(quote);
accountLayout.partyBLockedBalances[quote.partyB][quote.partyA].addQuote(quote);
}
quote.quoteStatus = QuoteStatus.OPENED;
LibQuote.addToOpenPositions(quoteId);
}
function fillCloseRequest(
uint256 quoteId,
uint256 filledAmount,
uint256 closedPrice,
PairUpnlAndPriceSig memory upnlSig
) internal {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(
quote.quoteStatus == QuoteStatus.CLOSE_PENDING ||
quote.quoteStatus == QuoteStatus.CANCEL_CLOSE_PENDING,
"PartyBFacet: Invalid state"
);
require(block.timestamp <= quote.deadline, "PartyBFacet: Quote is expired");
if (quote.positionType == PositionType.LONG) {
require(
closedPrice >= quote.requestedClosePrice,
"PartyBFacet: Closed price isn't valid"
);
} else {
require(
closedPrice <= quote.requestedClosePrice,
"PartyBFacet: Closed price isn't valid"
);
}
if (quote.orderType == OrderType.LIMIT) {
require(quote.quantityToClose >= filledAmount, "PartyBFacet: Invalid filledAmount");
} else {
require(quote.quantityToClose == filledAmount, "PartyBFacet: Invalid filledAmount");
}
LibMuon.verifyPairUpnlAndPrice(upnlSig, quote.partyB, quote.partyA, quote.symbolId);
LibSolvency.isSolventAfterClosePosition(quoteId, filledAmount, closedPrice, upnlSig);
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
accountLayout.partyANonces[quote.partyA] += 1;
LibQuote.closeQuote(quote, filledAmount, closedPrice);
}
function acceptCancelCloseRequest(uint256 quoteId) internal {
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(
quote.quoteStatus == QuoteStatus.CANCEL_CLOSE_PENDING,
"PartyBFacet: Invalid state"
);
AccountStorage.layout().partyBNonces[quote.partyB][quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.OPENED;
quote.requestedClosePrice = 0;
quote.quantityToClose = 0;
}
function emergencyClosePosition(uint256 quoteId, PairUpnlAndPriceSig memory upnlSig) internal {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.OPENED, "PartyBFacet: Invalid state");
LibMuon.verifyPairUpnlAndPrice(upnlSig, quote.partyB, quote.partyA, quote.symbolId);
uint256 filledAmount = LibQuote.quoteOpenAmount(quote);
quote.quantityToClose = filledAmount;
quote.requestedClosePrice = upnlSig.price;
LibSolvency.isSolventAfterClosePosition(quoteId, filledAmount, upnlSig.price, upnlSig);
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
accountLayout.partyANonces[quote.partyA] += 1;
LibQuote.closeQuote(quote, filledAmount, upnlSig.price);
}
}