/
TradingExtension.sol
282 lines (251 loc) · 8.49 KB
/
TradingExtension.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
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/IPairsContract.sol";
import "./utils/TradingLibrary.sol";
import "./interfaces/IReferrals.sol";
import "./interfaces/IPosition.sol";
contract TradingExtension is Ownable{
uint constant private DIVISION_CONSTANT = 1e10; // 100%
address public trading;
uint256 public validSignatureTimer;
bool public chainlinkEnabled;
mapping(address => bool) private isNode;
mapping(address => uint) public minPositionSize;
mapping(address => bool) public allowedMargin;
bool public paused;
IPairsContract private pairsContract;
IReferrals private referrals;
IPosition private position;
uint public maxGasPrice = 1000000000000; // 1000 gwei
constructor(
address _trading,
address _pairsContract,
address _ref,
address _position
)
{
trading = _trading;
pairsContract = IPairsContract(_pairsContract);
referrals = IReferrals(_ref);
position = IPosition(_position);
}
/**
* @notice returns the minimum position size per collateral asset
* @param _asset address of the asset
*/
function minPos(
address _asset
) external view returns(uint) {
return minPositionSize[_asset];
}
/**
* @notice closePosition helper
* @dev only callable by trading contract
* @param _id id of the position NFT
* @param _price current asset price
* @param _percent close percentage
* @return _trade returns the trade struct from NFT contract
* @return _positionSize size of the position
* @return _payout amount of payout to the trader after closing
*/
function _closePosition(
uint _id,
uint _price,
uint _percent
) external onlyProtocol returns (IPosition.Trade memory _trade, uint256 _positionSize, int256 _payout) {
_trade = position.trades(_id);
(_positionSize, _payout) = TradingLibrary.pnl(_trade.direction, _price, _trade.price, _trade.margin, _trade.leverage, _trade.accInterest);
unchecked {
if (_trade.direction) {
modifyLongOi(_trade.asset, _trade.tigAsset, false, (_trade.margin*_trade.leverage/1e18)*_percent/DIVISION_CONSTANT);
} else {
modifyShortOi(_trade.asset, _trade.tigAsset, false, (_trade.margin*_trade.leverage/1e18)*_percent/DIVISION_CONSTANT);
}
}
}
/**
* @notice limitClose helper
* @dev only callable by trading contract
* @param _id id of the position NFT
* @param _tp true if long, else short
* @param _priceData price data object came from the price oracle
* @param _signature to verify the oracle
* @return _limitPrice price of sl or tp returned from positions contract
* @return _tigAsset address of the position collateral asset
*/
function _limitClose(
uint _id,
bool _tp,
PriceData calldata _priceData,
bytes calldata _signature
) external view returns(uint _limitPrice, address _tigAsset) {
_checkGas();
IPosition.Trade memory _trade = position.trades(_id);
_tigAsset = _trade.tigAsset;
getVerifiedPrice(_trade.asset, _priceData, _signature, 0);
uint256 _price = _priceData.price;
if (_trade.orderType != 0) revert("4"); //IsLimit
if (_tp) {
if (_trade.tpPrice == 0) revert("7"); //LimitNotSet
if (_trade.direction) {
if (_trade.tpPrice > _price) revert("6"); //LimitNotMet
} else {
if (_trade.tpPrice < _price) revert("6"); //LimitNotMet
}
_limitPrice = _trade.tpPrice;
} else {
if (_trade.slPrice == 0) revert("7"); //LimitNotSet
if (_trade.direction) {
if (_trade.slPrice < _price) revert("6"); //LimitNotMet
} else {
if (_trade.slPrice > _price) revert("6"); //LimitNotMet
}
_limitPrice = _trade.slPrice;
}
}
function _checkGas() public view {
if (tx.gasprice > maxGasPrice) revert("1"); //GasTooHigh
}
function modifyShortOi(
uint _asset,
address _tigAsset,
bool _onOpen,
uint _size
) public onlyProtocol {
pairsContract.modifyShortOi(_asset, _tigAsset, _onOpen, _size);
}
function modifyLongOi(
uint _asset,
address _tigAsset,
bool _onOpen,
uint _size
) public onlyProtocol {
pairsContract.modifyLongOi(_asset, _tigAsset, _onOpen, _size);
}
function setMaxGasPrice(uint _maxGasPrice) external onlyOwner {
maxGasPrice = _maxGasPrice;
}
function getRef(
address _trader
) external view returns(address) {
return referrals.getReferral(referrals.getReferred(_trader));
}
/**
* @notice verifies the signed price and returns it
* @param _asset id of position asset
* @param _priceData price data object came from the price oracle
* @param _signature to verify the oracle
* @param _withSpreadIsLong 0, 1, or 2 - to specify if we need the price returned to be after spread
* @return _price price after verification and with spread if _withSpreadIsLong is 1 or 2
* @return _spread spread after verification
*/
function getVerifiedPrice(
uint _asset,
PriceData calldata _priceData,
bytes calldata _signature,
uint _withSpreadIsLong
)
public view
returns(uint256 _price, uint256 _spread)
{
TradingLibrary.verifyPrice(
validSignatureTimer,
_asset,
chainlinkEnabled,
pairsContract.idToAsset(_asset).chainlinkFeed,
_priceData,
_signature,
isNode
);
_price = _priceData.price;
_spread = _priceData.spread;
if(_withSpreadIsLong == 1)
_price += _price * _spread / DIVISION_CONSTANT;
else if(_withSpreadIsLong == 2)
_price -= _price * _spread / DIVISION_CONSTANT;
}
function _setReferral(
bytes32 _referral,
address _trader
) external onlyProtocol {
if (_referral != bytes32(0)) {
if (referrals.getReferral(_referral) != address(0)) {
if (referrals.getReferred(_trader) == bytes32(0)) {
referrals.setReferred(_trader, _referral);
}
}
}
}
/**
* @dev validates the inputs of trades
* @param _asset asset id
* @param _tigAsset margin asset
* @param _margin margin
* @param _leverage leverage
*/
function validateTrade(uint _asset, address _tigAsset, uint _margin, uint _leverage) external view {
unchecked {
IPairsContract.Asset memory asset = pairsContract.idToAsset(_asset);
if (!allowedMargin[_tigAsset]) revert("!margin");
if (paused) revert("paused");
if (!pairsContract.allowedAsset(_asset)) revert("!allowed");
if (_leverage < asset.minLeverage || _leverage > asset.maxLeverage) revert("!lev");
if (_margin*_leverage/1e18 < minPositionSize[_tigAsset]) revert("!size");
}
}
function setValidSignatureTimer(
uint _validSignatureTimer
)
external
onlyOwner
{
validSignatureTimer = _validSignatureTimer;
}
function setChainlinkEnabled(bool _bool) external onlyOwner {
chainlinkEnabled = _bool;
}
/**
* @dev whitelists a node
* @param _node node address
* @param _bool bool
*/
function setNode(address _node, bool _bool) external onlyOwner {
isNode[_node] = _bool;
}
/**
* @dev Allows a tigAsset to be used
* @param _tigAsset tigAsset
* @param _bool bool
*/
function setAllowedMargin(
address _tigAsset,
bool _bool
)
external
onlyOwner
{
allowedMargin[_tigAsset] = _bool;
}
/**
* @dev changes the minimum position size
* @param _tigAsset tigAsset
* @param _min minimum position size 18 decimals
*/
function setMinPositionSize(
address _tigAsset,
uint _min
)
external
onlyOwner
{
minPositionSize[_tigAsset] = _min;
}
function setPaused(bool _paused) external onlyOwner {
paused = _paused;
}
modifier onlyProtocol {
require(msg.sender == trading, "!protocol");
_;
}
}