-
Notifications
You must be signed in to change notification settings - Fork 31
/
Order.sol
407 lines (356 loc) · 14.9 KB
/
Order.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "../chain/Chain.sol";
// @title Order
// @dev Struct for orders
library Order {
using Order for Props;
enum OrderType {
// @dev MarketSwap: swap token A to token B at the current market price
// the order will be cancelled if the minOutputAmount cannot be fulfilled
MarketSwap,
// @dev LimitSwap: swap token A to token B if the minOutputAmount can be fulfilled
LimitSwap,
// @dev MarketIncrease: increase position at the current market price
// the order will be cancelled if the position cannot be increased at the acceptablePrice
MarketIncrease,
// @dev LimitIncrease: increase position if the triggerPrice is reached and the acceptablePrice can be fulfilled
LimitIncrease,
// @dev MarketDecrease: decrease position at the current market price
// the order will be cancelled if the position cannot be decreased at the acceptablePrice
MarketDecrease,
// @dev LimitDecrease: decrease position if the triggerPrice is reached and the acceptablePrice can be fulfilled
LimitDecrease,
// @dev StopLossDecrease: decrease position if the triggerPrice is reached and the acceptablePrice can be fulfilled
StopLossDecrease,
// @dev Liquidation: allows liquidation of positions if the criteria for liquidation are met
Liquidation
}
// to help further differentiate orders
enum SecondaryOrderType {
None,
Adl
}
enum DecreasePositionSwapType {
NoSwap,
SwapPnlTokenToCollateralToken,
SwapCollateralTokenToPnlToken
}
// @dev there is a limit on the number of fields a struct can have when being passed
// or returned as a memory variable which can cause "Stack too deep" errors
// use sub-structs to avoid this issue
// @param addresses address values
// @param numbers number values
// @param flags boolean values
struct Props {
Addresses addresses;
Numbers numbers;
Flags flags;
}
// @param account the account of the order
// @param receiver the receiver for any token transfers
// this field is meant to allow the output of an order to be
// received by an address that is different from the creator of the
// order whether this is for swaps or whether the account is the owner
// of a position
// for funding fees and claimable collateral, the funds are still
// credited to the owner of the position indicated by order.account
// @param callbackContract the contract to call for callbacks
// @param uiFeeReceiver the ui fee receiver
// @param market the trading market
// @param initialCollateralToken for increase orders, initialCollateralToken
// is the token sent in by the user, the token will be swapped through the
// specified swapPath, before being deposited into the position as collateral
// for decrease orders, initialCollateralToken is the collateral token of the position
// withdrawn collateral from the decrease of the position will be swapped
// through the specified swapPath
// for swaps, initialCollateralToken is the initial token sent for the swap
// @param swapPath an array of market addresses to swap through
struct Addresses {
address account;
address receiver;
address callbackContract;
address uiFeeReceiver;
address market;
address initialCollateralToken;
address[] swapPath;
}
// @param sizeDeltaUsd the requested change in position size
// @param initialCollateralDeltaAmount for increase orders, initialCollateralDeltaAmount
// is the amount of the initialCollateralToken sent in by the user
// for decrease orders, initialCollateralDeltaAmount is the amount of the position's
// collateralToken to withdraw
// for swaps, initialCollateralDeltaAmount is the amount of initialCollateralToken sent
// in for the swap
// @param orderType the order type
// @param triggerPrice the trigger price for non-market orders
// @param acceptablePrice the acceptable execution price for increase / decrease orders
// @param executionFee the execution fee for keepers
// @param callbackGasLimit the gas limit for the callbackContract
// @param minOutputAmount the minimum output amount for decrease orders and swaps
// note that for decrease orders, multiple tokens could be received, for this reason, the
// minOutputAmount value is treated as a USD value for validation in decrease orders
// @param updatedAtBlock the block at which the order was last updated
struct Numbers {
OrderType orderType;
DecreasePositionSwapType decreasePositionSwapType;
uint256 sizeDeltaUsd;
uint256 initialCollateralDeltaAmount;
uint256 triggerPrice;
uint256 acceptablePrice;
uint256 executionFee;
uint256 callbackGasLimit;
uint256 minOutputAmount;
uint256 updatedAtBlock;
}
// @param isLong whether the order is for a long or short
// @param shouldUnwrapNativeToken whether to unwrap native tokens before
// transferring to the user
// @param isFrozen whether the order is frozen
struct Flags {
bool isLong;
bool shouldUnwrapNativeToken;
bool isFrozen;
}
// @dev the order account
// @param props Props
// @return the order account
function account(Props memory props) internal pure returns (address) {
return props.addresses.account;
}
// @dev set the order account
// @param props Props
// @param value the value to set to
function setAccount(Props memory props, address value) internal pure {
props.addresses.account = value;
}
// @dev the order receiver
// @param props Props
// @return the order receiver
function receiver(Props memory props) internal pure returns (address) {
return props.addresses.receiver;
}
// @dev set the order receiver
// @param props Props
// @param value the value to set to
function setReceiver(Props memory props, address value) internal pure {
props.addresses.receiver = value;
}
// @dev the order callbackContract
// @param props Props
// @return the order callbackContract
function callbackContract(Props memory props) internal pure returns (address) {
return props.addresses.callbackContract;
}
// @dev set the order callbackContract
// @param props Props
// @param value the value to set to
function setCallbackContract(Props memory props, address value) internal pure {
props.addresses.callbackContract = value;
}
// @dev the order market
// @param props Props
// @return the order market
function market(Props memory props) internal pure returns (address) {
return props.addresses.market;
}
// @dev set the order market
// @param props Props
// @param value the value to set to
function setMarket(Props memory props, address value) internal pure {
props.addresses.market = value;
}
// @dev the order initialCollateralToken
// @param props Props
// @return the order initialCollateralToken
function initialCollateralToken(Props memory props) internal pure returns (address) {
return props.addresses.initialCollateralToken;
}
// @dev set the order initialCollateralToken
// @param props Props
// @param value the value to set to
function setInitialCollateralToken(Props memory props, address value) internal pure {
props.addresses.initialCollateralToken = value;
}
// @dev the order uiFeeReceiver
// @param props Props
// @return the order uiFeeReceiver
function uiFeeReceiver(Props memory props) internal pure returns (address) {
return props.addresses.uiFeeReceiver;
}
// @dev set the order uiFeeReceiver
// @param props Props
// @param value the value to set to
function setUiFeeReceiver(Props memory props, address value) internal pure {
props.addresses.uiFeeReceiver = value;
}
// @dev the order swapPath
// @param props Props
// @return the order swapPath
function swapPath(Props memory props) internal pure returns (address[] memory) {
return props.addresses.swapPath;
}
// @dev set the order swapPath
// @param props Props
// @param value the value to set to
function setSwapPath(Props memory props, address[] memory value) internal pure {
props.addresses.swapPath = value;
}
// @dev the order type
// @param props Props
// @return the order type
function orderType(Props memory props) internal pure returns (OrderType) {
return props.numbers.orderType;
}
// @dev set the order type
// @param props Props
// @param value the value to set to
function setOrderType(Props memory props, OrderType value) internal pure {
props.numbers.orderType = value;
}
function decreasePositionSwapType(Props memory props) internal pure returns (DecreasePositionSwapType) {
return props.numbers.decreasePositionSwapType;
}
function setDecreasePositionSwapType(Props memory props, DecreasePositionSwapType value) internal pure {
props.numbers.decreasePositionSwapType = value;
}
// @dev the order sizeDeltaUsd
// @param props Props
// @return the order sizeDeltaUsd
function sizeDeltaUsd(Props memory props) internal pure returns (uint256) {
return props.numbers.sizeDeltaUsd;
}
// @dev set the order sizeDeltaUsd
// @param props Props
// @param value the value to set to
function setSizeDeltaUsd(Props memory props, uint256 value) internal pure {
props.numbers.sizeDeltaUsd = value;
}
// @dev the order initialCollateralDeltaAmount
// @param props Props
// @return the order initialCollateralDeltaAmount
function initialCollateralDeltaAmount(Props memory props) internal pure returns (uint256) {
return props.numbers.initialCollateralDeltaAmount;
}
// @dev set the order initialCollateralDeltaAmount
// @param props Props
// @param value the value to set to
function setInitialCollateralDeltaAmount(Props memory props, uint256 value) internal pure {
props.numbers.initialCollateralDeltaAmount = value;
}
// @dev the order triggerPrice
// @param props Props
// @return the order triggerPrice
function triggerPrice(Props memory props) internal pure returns (uint256) {
return props.numbers.triggerPrice;
}
// @dev set the order triggerPrice
// @param props Props
// @param value the value to set to
function setTriggerPrice(Props memory props, uint256 value) internal pure {
props.numbers.triggerPrice = value;
}
// @dev the order acceptablePrice
// @param props Props
// @return the order acceptablePrice
function acceptablePrice(Props memory props) internal pure returns (uint256) {
return props.numbers.acceptablePrice;
}
// @dev set the order acceptablePrice
// @param props Props
// @param value the value to set to
function setAcceptablePrice(Props memory props, uint256 value) internal pure {
props.numbers.acceptablePrice = value;
}
// @dev set the order executionFee
// @param props Props
// @param value the value to set to
function setExecutionFee(Props memory props, uint256 value) internal pure {
props.numbers.executionFee = value;
}
// @dev the order executionFee
// @param props Props
// @return the order executionFee
function executionFee(Props memory props) internal pure returns (uint256) {
return props.numbers.executionFee;
}
// @dev the order callbackGasLimit
// @param props Props
// @return the order callbackGasLimit
function callbackGasLimit(Props memory props) internal pure returns (uint256) {
return props.numbers.callbackGasLimit;
}
// @dev set the order callbackGasLimit
// @param props Props
// @param value the value to set to
function setCallbackGasLimit(Props memory props, uint256 value) internal pure {
props.numbers.callbackGasLimit = value;
}
// @dev the order minOutputAmount
// @param props Props
// @return the order minOutputAmount
function minOutputAmount(Props memory props) internal pure returns (uint256) {
return props.numbers.minOutputAmount;
}
// @dev set the order minOutputAmount
// @param props Props
// @param value the value to set to
function setMinOutputAmount(Props memory props, uint256 value) internal pure {
props.numbers.minOutputAmount = value;
}
// @dev the order updatedAtBlock
// @param props Props
// @return the order updatedAtBlock
function updatedAtBlock(Props memory props) internal pure returns (uint256) {
return props.numbers.updatedAtBlock;
}
// @dev set the order updatedAtBlock
// @param props Props
// @param value the value to set to
function setUpdatedAtBlock(Props memory props, uint256 value) internal pure {
props.numbers.updatedAtBlock = value;
}
// @dev whether the order is for a long or short
// @param props Props
// @return whether the order is for a long or short
function isLong(Props memory props) internal pure returns (bool) {
return props.flags.isLong;
}
// @dev set whether the order is for a long or short
// @param props Props
// @param value the value to set to
function setIsLong(Props memory props, bool value) internal pure {
props.flags.isLong = value;
}
// @dev whether to unwrap the native token before transfers to the user
// @param props Props
// @return whether to unwrap the native token before transfers to the user
function shouldUnwrapNativeToken(Props memory props) internal pure returns (bool) {
return props.flags.shouldUnwrapNativeToken;
}
// @dev set whether the native token should be unwrapped before being
// transferred to the receiver
// @param props Props
// @param value the value to set to
function setShouldUnwrapNativeToken(Props memory props, bool value) internal pure {
props.flags.shouldUnwrapNativeToken = value;
}
// @dev whether the order is frozen
// @param props Props
// @return whether the order is frozen
function isFrozen(Props memory props) internal pure returns (bool) {
return props.flags.isFrozen;
}
// @dev set whether the order is frozen
// transferred to the receiver
// @param props Props
// @param value the value to set to
function setIsFrozen(Props memory props, bool value) internal pure {
props.flags.isFrozen = value;
}
// @dev set the order.updatedAtBlock to the current block number
// @param props Props
function touch(Props memory props) internal view {
props.setUpdatedAtBlock(Chain.currentBlockNumber());
}
}