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
/
PartyAFacetImpl.sol
298 lines (267 loc) · 12 KB
/
PartyAFacetImpl.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
// 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/AccountStorage.sol";
import "../../storages/SymbolStorage.sol";
library PartyAFacetImpl {
using LockedValuesOps for LockedValues;
function sendQuote(
address[] memory partyBsWhiteList,
uint256 symbolId,
PositionType positionType,
OrderType orderType,
uint256 price,
uint256 quantity,
uint256 cva,
uint256 mm,
uint256 lf,
uint256 maxInterestRate,
uint256 deadline,
SingleUpnlAndPriceSig memory upnlSig
) internal returns (uint256 currentId) {
QuoteStorage.Layout storage quoteLayout = QuoteStorage.layout();
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
MAStorage.Layout storage maLayout = MAStorage.layout();
SymbolStorage.Layout storage symbolLayout = SymbolStorage.layout();
require(
quoteLayout.partyAPendingQuotes[msg.sender].length < maLayout.pendingQuotesValidLength,
"PartyAFacet: Number of pending quotes out of range"
);
require(symbolLayout.symbols[symbolId].isValid, "PartyAFacet: Symbol is not valid");
require(deadline >= block.timestamp, "PartyAFacet: Low deadline");
LockedValues memory lockedValues = LockedValues(cva, mm, lf);
uint256 tradingPrice = orderType == OrderType.LIMIT ? price : upnlSig.price;
uint256 notionalValue = (quantity * tradingPrice) / 1e18;
require(
lockedValues.total() <= notionalValue,
"PartyAFacet: Leverage can't be lower than one"
);
require(
lockedValues.lf >=
(symbolLayout.symbols[symbolId].minAcceptablePortionLF * lockedValues.total()) /
1e18,
"PartyAFacet: LF is not enough"
);
require(
lockedValues.total() >= symbolLayout.symbols[symbolId].minAcceptableQuoteValue,
"PartyAFacet: Quote value is low"
);
for (uint8 i = 0; i < partyBsWhiteList.length; i++) {
require(
partyBsWhiteList[i] != msg.sender,
"PartyAFacet: Sender isn't allowed in partyBWhiteList"
);
}
LibMuon.verifyPartyAUpnlAndPrice(upnlSig, msg.sender, symbolId);
int256 availableBalance = LibAccount.partyAAvailableForQuote(upnlSig.upnl, msg.sender);
require(availableBalance > 0, "PartyAFacet: Available balance is lower than zero");
require(
uint256(availableBalance) >=
lockedValues.total() +
((quantity * tradingPrice * symbolLayout.symbols[symbolId].tradingFee) / 1e36),
"PartyAFacet: insufficient available balance"
);
// lock funds the in middle of way
accountLayout.pendingLockedBalances[msg.sender].add(lockedValues);
currentId = ++quoteLayout.lastId;
accountLayout.partyANonces[msg.sender] += 1;
// create quote.
Quote memory quote = Quote({
id: currentId,
partyBsWhiteList: partyBsWhiteList,
symbolId: symbolId,
positionType: positionType,
orderType: orderType,
openedPrice: 0,
requestedOpenPrice: price,
marketPrice: upnlSig.price,
quantity: quantity,
closedAmount: 0,
lockedValues: lockedValues,
initialLockedValues: lockedValues,
maxInterestRate: maxInterestRate,
partyA: msg.sender,
partyB: address(0),
quoteStatus: QuoteStatus.PENDING,
avgClosedPrice: 0,
requestedClosePrice: 0,
parentId: 0,
createTimestamp: block.timestamp,
modifyTimestamp: block.timestamp,
quantityToClose: 0,
deadline: deadline
});
quoteLayout.quoteIdsOf[msg.sender].push(currentId);
quoteLayout.partyAPendingQuotes[msg.sender].push(currentId);
quoteLayout.quotes[currentId] = quote;
LibQuote.receiveTradingFee(currentId);
}
function requestToCancelQuote(uint256 quoteId) internal returns (QuoteStatus result) {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(
quote.quoteStatus == QuoteStatus.PENDING || quote.quoteStatus == QuoteStatus.LOCKED,
"PartyAFacet: Invalid state"
);
accountLayout.partyANonces[quote.partyA] += 1;
if (block.timestamp > quote.deadline) {
result = LibQuote.expireQuote(quoteId);
} else if (quote.quoteStatus == QuoteStatus.PENDING) {
quote.quoteStatus = QuoteStatus.CANCELED;
LibQuote.returnTradingFee(quoteId);
accountLayout.pendingLockedBalances[quote.partyA].subQuote(quote);
LibQuote.removeFromPartyAPendingQuotes(quote);
result = QuoteStatus.CANCELED;
} else {
// Quote is locked
quote.quoteStatus = QuoteStatus.CANCEL_PENDING;
result = QuoteStatus.CANCEL_PENDING;
}
quote.modifyTimestamp = block.timestamp;
}
function requestToClosePosition(
uint256 quoteId,
uint256 closePrice,
uint256 quantityToClose,
OrderType orderType,
uint256 deadline,
SingleUpnlAndPriceSig memory upnlSig
) internal {
SymbolStorage.Layout storage symbolLayout = SymbolStorage.layout();
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.OPENED, "PartyAFacet: Invalid state");
require(deadline >= block.timestamp, "PartyAFacet: Low deadline");
require(
LibQuote.quoteOpenAmount(quote) >= quantityToClose,
"PartyAFacet: Invalid quantityToClose"
);
LibMuon.verifyPartyAUpnlAndPrice(upnlSig, quote.partyA, quote.symbolId);
LibSolvency.isSolventAfterRequestToClosePosition(
quoteId,
closePrice,
quantityToClose,
upnlSig
);
// check that remaining position is not too small
if (LibQuote.quoteOpenAmount(quote) > quantityToClose) {
require(
((LibQuote.quoteOpenAmount(quote) - quantityToClose) * quote.lockedValues.total()) /
LibQuote.quoteOpenAmount(quote) >=
symbolLayout.symbols[quote.symbolId].minAcceptableQuoteValue,
"PartyAFacet: Remaining quote value is low"
);
}
accountLayout.partyANonces[quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.CLOSE_PENDING;
quote.requestedClosePrice = closePrice;
quote.quantityToClose = quantityToClose;
quote.orderType = orderType;
quote.deadline = deadline;
}
function requestToCancelCloseRequest(uint256 quoteId) internal returns (QuoteStatus) {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.CLOSE_PENDING, "PartyAFacet: Invalid state");
if (block.timestamp > quote.deadline) {
LibQuote.expireQuote(quoteId);
return QuoteStatus.OPENED;
} else {
accountLayout.partyANonces[quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.CANCEL_CLOSE_PENDING;
return QuoteStatus.CANCEL_CLOSE_PENDING;
}
}
function forceCancelQuote(uint256 quoteId) internal {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
MAStorage.Layout storage maLayout = MAStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(quote.quoteStatus == QuoteStatus.CANCEL_PENDING, "PartyAFacet: Invalid state");
require(
block.timestamp > quote.modifyTimestamp + maLayout.forceCancelCooldown,
"PartyAFacet: Cooldown not reached"
);
accountLayout.partyANonces[quote.partyA] += 1;
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 forceCancelCloseRequest(uint256 quoteId) internal {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
MAStorage.Layout storage maLayout = MAStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
require(
quote.quoteStatus == QuoteStatus.CANCEL_CLOSE_PENDING,
"PartyAFacet: Invalid state"
);
require(
block.timestamp > quote.modifyTimestamp + maLayout.forceCancelCloseCooldown,
"PartyAFacet: Cooldown not reached"
);
accountLayout.partyANonces[quote.partyA] += 1;
quote.modifyTimestamp = block.timestamp;
quote.quoteStatus = QuoteStatus.OPENED;
quote.requestedClosePrice = 0;
quote.quantityToClose = 0;
}
function forceClosePosition(uint256 quoteId, PairUpnlAndPriceSig memory upnlSig) internal {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
MAStorage.Layout storage maLayout = MAStorage.layout();
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
uint256 filledAmount = quote.quantityToClose;
require(quote.quoteStatus == QuoteStatus.CLOSE_PENDING, "PartyAFacet: Invalid state");
require(
block.timestamp > quote.modifyTimestamp + maLayout.forceCloseCooldown,
"PartyAFacet: Cooldown not reached"
);
require(block.timestamp <= quote.deadline, "PartyBFacet: Quote is expired");
require(
quote.orderType == OrderType.LIMIT,
"PartyBFacet: Quote's order type should be LIMIT"
);
if (quote.positionType == PositionType.LONG) {
require(
upnlSig.price >=
quote.requestedClosePrice +
(quote.requestedClosePrice * maLayout.forceCloseGapRatio) /
1e18,
"PartyAFacet: Requested close price not reached"
);
} else {
require(
upnlSig.price <=
quote.requestedClosePrice -
(quote.requestedClosePrice * maLayout.forceCloseGapRatio) /
1e18,
"PartyAFacet: Requested close price not reached"
);
}
LibMuon.verifyPairUpnlAndPrice(upnlSig, quote.partyB, quote.partyA, quote.symbolId);
LibSolvency.isSolventAfterClosePosition(
quoteId,
filledAmount,
quote.requestedClosePrice,
upnlSig
);
accountLayout.partyANonces[quote.partyA] += 1;
accountLayout.partyBNonces[quote.partyB][quote.partyA] += 1;
LibQuote.closeQuote(quote, filledAmount, quote.requestedClosePrice);
}
}