This repository has been archived by the owner on Dec 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrderFacet.sol
141 lines (131 loc) · 5.81 KB
/
OrderFacet.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/math/SafeCast.sol";
import "../interfaces/IOrder.sol";
import "../interfaces/IAccount.sol";
import "../process/AssetsProcess.sol";
import "../process/OrderProcess.sol";
import "../process/CancelOrderProcess.sol";
import "../process/GasProcess.sol";
import "../storage/RoleAccessControl.sol";
contract OrderFacet is IOrder, ReentrancyGuard {
using SafeCast for uint256;
using Account for Account.Props;
using Order for Order.Props;
function createOrderRequest(PlaceOrderParams calldata params) external payable override nonReentrant {
address account = msg.sender;
if (params.posSide == Order.PositionSide.INCREASE && !params.isCrossMargin) {
require(!params.isNativeToken || msg.value == params.orderMargin, "Deposit native token amount error!");
AssetsProcess.depositToVault(
AssetsProcess.DepositParams(
account,
params.marginToken,
params.orderMargin,
AssetsProcess.DepositFrom.ORDER,
params.isNativeToken
)
);
}
Account.Props storage accountProps = Account.loadOrCreate(account);
OrderProcess.createOrderRequest(accountProps, params, true);
}
function batchCreateOrderRequest(PlaceOrderParams[] calldata params) external payable override nonReentrant {
address account = msg.sender;
Account.Props storage accountProps = Account.loadOrCreate(account);
uint256 totalExecutionFee;
AppConfig.ChainConfig memory chainConfig = AppConfig.getChainConfig();
bool isCrossMargin = params[0].isCrossMargin;
for (uint256 i; i < params.length; i++) {
if (params[i].posSide == Order.PositionSide.INCREASE) {
revert Errors.OnlyDecreaseOrderSupported();
}
if (isCrossMargin != params[i].isCrossMargin) {
revert Errors.MarginModeError();
}
GasProcess.validateExecutionFeeLimit(params[i].executionFee, chainConfig.placeDecreaseOrderGasFeeLimit);
OrderProcess.createOrderRequest(accountProps, params[i], false);
totalExecutionFee += params[i].executionFee;
}
require(msg.value == totalExecutionFee, "Batch place order with execution fee error!");
AssetsProcess.depositToVault(
AssetsProcess.DepositParams(
accountProps.owner,
chainConfig.wrapperToken,
totalExecutionFee,
isCrossMargin ? AssetsProcess.DepositFrom.MANUAL : AssetsProcess.DepositFrom.ORDER,
true
)
);
}
function executeOrder(uint256 orderId, OracleProcess.OracleParam[] calldata oracles) external override {
uint256 startGas = gasleft();
RoleAccessControl.checkRole(RoleAccessControl.ROLE_KEEPER);
Order.OrderInfo memory order = Order.get(orderId);
if (order.account == address(0)) {
revert Errors.OrderNotExists(orderId);
}
OracleProcess.setOraclePrice(oracles);
OrderProcess.executeOrder(orderId, order);
OracleProcess.clearOraclePrice();
GasProcess.processExecutionFee(
GasProcess.PayExecutionFeeParams(
order.isExecutionFeeFromTradeVault
? IVault(address(this)).getTradeVaultAddress()
: IVault(address(this)).getPortfolioVaultAddress(),
order.executionFee,
startGas,
msg.sender,
order.account
)
);
}
function cancelOrder(uint256 orderId, bytes32 reasonCode) external override {
uint256 startGas = gasleft();
Order.OrderInfo memory order = Order.get(orderId);
if (order.account == address(0)) {
revert Errors.OrderNotExists(orderId);
}
bool isKeeper = RoleAccessControl.hasRole(RoleAccessControl.ROLE_KEEPER);
if (!isKeeper && order.account != msg.sender) {
revert Errors.OrderNotExists(orderId);
}
CancelOrderProcess.cancelOrder(orderId, order, reasonCode);
if (isKeeper) {
GasProcess.processExecutionFee(
GasProcess.PayExecutionFeeParams(
order.isExecutionFeeFromTradeVault
? IVault(address(this)).getTradeVaultAddress()
: IVault(address(this)).getPortfolioVaultAddress(),
order.executionFee,
startGas,
msg.sender,
order.account
)
);
} else {
VaultProcess.transferOut(
order.isExecutionFeeFromTradeVault
? IVault(address(this)).getTradeVaultAddress()
: IVault(address(this)).getPortfolioVaultAddress(),
AppConfig.getChainConfig().wrapperToken,
address(this),
order.executionFee
);
VaultProcess.withdrawEther(order.account, order.executionFee);
}
}
receive() external payable {}
function getAccountOrders(address account) external view override returns (AccountOrder[] memory result) {
Account.Props storage accountProps = Account.load(account);
if (accountProps.isExists()) {
uint256[] memory orders = accountProps.getOrders();
result = new AccountOrder[](orders.length);
Order.Props storage orderStorage = Order.load();
for (uint256 i; i < orders.length; i++) {
result[i].orderId = orders[i];
result[i].orderInfo = orderStorage.get(orders[i]);
}
}
}
}