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 pathPartyBFacet.sol
239 lines (228 loc) · 9.15 KB
/
PartyBFacet.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
// 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 "./PartyBFacetImpl.sol";
import "../../utils/Accessibility.sol";
import "../../utils/Pausable.sol";
import "./IPartyBEvents.sol";
import "../../storages/MuonStorage.sol";
import "../Account/IAccountEvents.sol";
import "../Account/AccountFacetImpl.sol";
contract PartyBFacet is Accessibility, Pausable, IPartyBEvents, IAccountEvents {
using LockedValuesOps for LockedValues;
function lockQuote(
uint256 quoteId,
SingleUpnlSig memory upnlSig
) external whenNotPartyBActionsPaused onlyPartyB notLiquidated(quoteId) {
PartyBFacetImpl.lockQuote(quoteId, upnlSig, true);
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
emit LockQuote(quote.partyB, quoteId, quote.quoteStatus);
}
function allocateAndLockQuote(
uint256 quoteId,
uint256 amount,
SingleUpnlSig memory upnlSig
) external whenNotPartyBActionsPaused onlyPartyB notLiquidated(quoteId) {
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
AccountFacetImpl.allocateForPartyB(amount, quote.partyA, false);
PartyBFacetImpl.lockQuote(quoteId, upnlSig, true);
emit AllocateForPartyB(msg.sender, quote.partyA, amount);
emit LockQuote(quote.partyB, quoteId, quote.quoteStatus);
}
function lockAndOpenQuote(
uint256 quoteId,
uint256 filledAmount,
uint256 openedPrice,
SingleUpnlSig memory upnlSig,
PairUpnlAndPriceSig memory pairUpnlSig
) external whenNotPartyBActionsPaused onlyPartyB notLiquidated(quoteId) {
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
PartyBFacetImpl.lockQuote(quoteId, upnlSig, false);
emit LockQuote(quote.partyB, quoteId, quote.quoteStatus);
uint256 newId = PartyBFacetImpl.openPosition(quoteId, filledAmount, openedPrice, pairUpnlSig);
emit OpenPosition(
quoteId,
quote.partyA,
quote.partyB,
filledAmount,
openedPrice,
QuoteStatus.OPENED
);
if (newId != 0) {
Quote storage newQuote = QuoteStorage.layout().quotes[newId];
if (newQuote.quoteStatus == QuoteStatus.PENDING) {
emit SendQuote(
newQuote.partyA,
newQuote.id,
newQuote.partyBsWhiteList,
newQuote.symbolId,
newQuote.positionType,
newQuote.orderType,
newQuote.requestedOpenPrice,
newQuote.marketPrice,
newQuote.quantity,
newQuote.lockedValues.cva,
newQuote.lockedValues.mm,
newQuote.lockedValues.lf,
newQuote.maxInterestRate,
newQuote.deadline,
newQuote.quoteStatus
);
} else if (newQuote.quoteStatus == QuoteStatus.CANCELED) {
emit AcceptCancelRequest(newQuote.id, QuoteStatus.CANCELED);
}
}
}
function allocateAndLockAndOpenQuote(
uint256 quoteId,
uint256 allocateAmount,
uint256 filledAmount,
uint256 openedPrice,
SingleUpnlSig memory upnlSig,
PairUpnlAndPriceSig memory pairUpnlSig
) external whenNotPartyBActionsPaused onlyPartyB notLiquidated(quoteId) {
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
AccountFacetImpl.allocateForPartyB(allocateAmount, quote.partyA, false);
PartyBFacetImpl.lockQuote(quoteId, upnlSig, false);
emit AllocateForPartyB(msg.sender, quote.partyA, allocateAmount);
emit LockQuote(quote.partyB, quoteId, quote.quoteStatus);
uint256 newId = PartyBFacetImpl.openPosition(quoteId, filledAmount, openedPrice, pairUpnlSig);
emit OpenPosition(
quoteId,
quote.partyA,
quote.partyB,
filledAmount,
openedPrice,
QuoteStatus.OPENED
);
if (newId != 0) {
Quote storage newQuote = QuoteStorage.layout().quotes[newId];
if (newQuote.quoteStatus == QuoteStatus.PENDING) {
emit SendQuote(
newQuote.partyA,
newQuote.id,
newQuote.partyBsWhiteList,
newQuote.symbolId,
newQuote.positionType,
newQuote.orderType,
newQuote.requestedOpenPrice,
newQuote.marketPrice,
newQuote.quantity,
newQuote.lockedValues.cva,
newQuote.lockedValues.mm,
newQuote.lockedValues.lf,
newQuote.maxInterestRate,
newQuote.deadline,
newQuote.quoteStatus
);
} else if (newQuote.quoteStatus == QuoteStatus.CANCELED) {
emit AcceptCancelRequest(newQuote.id, QuoteStatus.CANCELED);
}
}
}
function unlockQuote(
uint256 quoteId
) external whenNotPartyBActionsPaused onlyPartyBOfQuote(quoteId) notLiquidated(quoteId) {
QuoteStatus res = PartyBFacetImpl.unlockQuote(quoteId);
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
if (res == QuoteStatus.EXPIRED) {
emit ExpireQuote(res, quoteId);
} else if (res == QuoteStatus.PENDING) {
emit UnlockQuote(quote.partyB, quoteId, QuoteStatus.PENDING);
}
}
function acceptCancelRequest(
uint256 quoteId
) external whenNotPartyBActionsPaused onlyPartyBOfQuote(quoteId) notLiquidated(quoteId) {
PartyBFacetImpl.acceptCancelRequest(quoteId);
emit AcceptCancelRequest(quoteId, QuoteStatus.CANCELED);
}
function openPosition(
uint256 quoteId,
uint256 filledAmount,
uint256 openedPrice,
PairUpnlAndPriceSig memory upnlSig
) external whenNotPartyBActionsPaused onlyPartyBOfQuote(quoteId) notLiquidated(quoteId) {
uint256 newId = PartyBFacetImpl.openPosition(quoteId, filledAmount, openedPrice, upnlSig);
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
emit OpenPosition(
quoteId,
quote.partyA,
quote.partyB,
filledAmount,
openedPrice,
QuoteStatus.OPENED
);
if (newId != 0) {
Quote storage newQuote = QuoteStorage.layout().quotes[newId];
if (newQuote.quoteStatus == QuoteStatus.PENDING) {
emit SendQuote(
newQuote.partyA,
newQuote.id,
newQuote.partyBsWhiteList,
newQuote.symbolId,
newQuote.positionType,
newQuote.orderType,
newQuote.requestedOpenPrice,
newQuote.marketPrice,
newQuote.quantity,
newQuote.lockedValues.cva,
newQuote.lockedValues.mm,
newQuote.lockedValues.lf,
newQuote.maxInterestRate,
newQuote.deadline,
newQuote.quoteStatus
);
} else if (newQuote.quoteStatus == QuoteStatus.CANCELED) {
emit AcceptCancelRequest(newQuote.id, QuoteStatus.CANCELED);
}
}
}
function fillCloseRequest(
uint256 quoteId,
uint256 filledAmount,
uint256 closedPrice,
PairUpnlAndPriceSig memory upnlSig
) external whenNotPartyBActionsPaused onlyPartyBOfQuote(quoteId) notLiquidated(quoteId) {
PartyBFacetImpl.fillCloseRequest(quoteId, filledAmount, closedPrice, upnlSig);
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
emit FillCloseRequest(
quoteId,
quote.partyA,
quote.partyB,
filledAmount,
closedPrice,
quote.quoteStatus
);
}
function acceptCancelCloseRequest(
uint256 quoteId
) external whenNotPartyBActionsPaused onlyPartyBOfQuote(quoteId) notLiquidated(quoteId) {
PartyBFacetImpl.acceptCancelCloseRequest(quoteId);
emit AcceptCancelCloseRequest(quoteId, QuoteStatus.OPENED);
}
function emergencyClosePosition(
uint256 quoteId,
PairUpnlAndPriceSig memory upnlSig
)
external
whenNotPartyBActionsPaused
onlyPartyBOfQuote(quoteId)
whenEmergencyMode(msg.sender)
notLiquidated(quoteId)
{
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
uint256 filledAmount = LibQuote.quoteOpenAmount(quote);
PartyBFacetImpl.emergencyClosePosition(quoteId, upnlSig);
emit EmergencyClosePosition(
quoteId,
quote.partyA,
quote.partyB,
filledAmount,
upnlSig.price,
quote.quoteStatus
);
}
}