-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLong.sol
More file actions
237 lines (217 loc) · 9.27 KB
/
Long.sol
File metadata and controls
237 lines (217 loc) · 9.27 KB
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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {console} from "forge-std/Test.sol";
import {IERC20} from "../interfaces/IERC20.sol";
import {IExchangeRouter} from "../interfaces/IExchangeRouter.sol";
import {IDataStore} from "../interfaces/IDataStore.sol";
import {IReader} from "../interfaces/IReader.sol";
import {Order} from "../types/Order.sol";
import {Position} from "../types/Position.sol";
import {Market} from "../types/Market.sol";
import {MarketUtils} from "../types/MarketUtils.sol";
import {Price} from "../types/Price.sol";
import {IBaseOrderUtils} from "../types/IBaseOrderUtils.sol";
import {Oracle} from "../lib/Oracle.sol";
import "../Constants.sol";
contract Long {
IERC20 constant weth = IERC20(WETH);
IERC20 constant usdc = IERC20(USDC);
IExchangeRouter constant exchangeRouter = IExchangeRouter(EXCHANGE_ROUTER);
IDataStore constant dataStore = IDataStore(DATA_STORE);
IReader constant reader = IReader(READER);
Oracle immutable oracle;
constructor(address _oracle) {
oracle = Oracle(_oracle);
}
// Task 1 - Receive execution fee refund from GMX
receive() external payable {}
// Task 2 - Create an order to long ETH with WETH collateral
function createLongOrder(
uint256 leverage,
uint256 wethAmount
) external payable returns (bytes32 key) {
uint256 executionFee = 0.1 * 1e18;
weth.transferFrom(msg.sender, address(this), wethAmount);
// Task 2.1 - Send execution fee to the order vault
exchangeRouter.sendWnt{value: executionFee}({
receiver: ORDER_VAULT,
amount: executionFee
});
// Task 2.2 - Send WETH to the order vault
weth.approve(ROUTER, wethAmount);
exchangeRouter.sendTokens({
token: WETH,
receiver: ORDER_VAULT,
amount: wethAmount
});
// Task 2.3 - Create an order
// ethPrice : 1e8 (from oracle)
// wethAmount (in wei) = 1e18
// sizeDeltaUsd => expected to be 1e30
// leverage = unitless (1)
// calculate sizeDeltaUsd
// sizeDeltaUsd = wethAmount * ethPrice * leverage;
// result will have 18 + 8 = 26 decimals
// But sizeDeltaUsd needs 30 decimals. need to scale up by 1e4:
uint256 ethPrice = oracle.getPrice(CHAINLINK_ETH_USD);
uint256 sizeDeltaUsd = wethAmount * ethPrice * leverage * 1e4;
// `acceptablePrice` has 12 decimals (1e12 = 1 USD)
// scale four more up, 101/100 => 1%
uint256 acceptablePrice = ((ethPrice * 1e4) * 101) / 100;
return
exchangeRouter.createOrder(
IBaseOrderUtils.CreateOrderParams({
addresses: IBaseOrderUtils.CreateOrderParamsAddresses({
receiver: address(this),
cancellationReceiver: address(0),
callbackContract: address(0),
uiFeeReceiver: address(0),
market: GM_TOKEN_ETH_WETH_USDC,
initialCollateralToken: WETH,
swapPath: new address[](0)
}),
numbers: IBaseOrderUtils.CreateOrderParamsNumbers({
sizeDeltaUsd: sizeDeltaUsd,
initialCollateralDeltaAmount: 0,
triggerPrice: 0,
acceptablePrice: acceptablePrice,
executionFee: executionFee,
callbackGasLimit: 0,
minOutputAmount: 0,
validFromTime: 0
}),
// 2
orderType: Order.OrderType.MarketIncrease,
decreasePositionSwapType: Order
.DecreasePositionSwapType
.NoSwap,
isLong: true,
shouldUnwrapNativeToken: false,
autoCancel: false,
referralCode: bytes32(uint256(0))
})
);
}
// Task 3 - Get position key
function getPositionKey() public view returns (bytes32 key) {
return
Position.getPositionKey({
account: address(this),
market: GM_TOKEN_ETH_WETH_USDC,
collateralToken: WETH,
isLong: true
});
}
// Task 4 - Get position
function getPosition(
bytes32 key
) public view returns (Position.Props memory) {
return reader.getPositionKey(address(dataStore), key);
}
/* function getPositionPnlUsd(
address dataStore,
Market.Props memory market,
MarketUtils.MarketPrices memory prices,
bytes32 positionKey,
uint256 sizeDeltaUsd
) external view returns (int256, int256, uint256); */
// Task 5 - Get position profit and loss
function getPositionPnlUsd(
bytes32 key,
uint256 ethPrice
) external view returns (int256) {
// get the position(key) => uniqueID of the trade
// fetches all the details of the position
Position.Props memory position = getPosition(key);
// Define the market prices structure
MarketUtils.MarketPrices memory prices = MarketUtils.MarketPrices({
indexTokenPrice: Price.props({
// eth price = 1e8
// weth = 1e18
// your position size or swap amounts are in WETH units (1e18)
// GMX internal scale (1e30) / (Chainlink decimals 1e8 * WETH decimals 1e18)
// Multiply/divide to scale it to 30 decimals because GMX uses high precision internally (30)
min: (((ethPrice * 1e30) / (1e8 * 1e18)) * 99) / 100,
max: (((ethPrice * 1e30) / (1e8 * 1e18)) * 101) / 100
}),
longTokenPrice: Price.props({
min: (((ethPrice * 1e30) / (1e8 * 1e18)) * 99) / 100,
max: (((ethPrice * 1e30) / (1e8 * 1e18)) * 101) / 100
}),
shortTokenPrice: Price.props({
// usdc = 1e6
// GMX internal scale (1e30) / (Chainlink decimals 1e8 * USDC decimals 1e6)
min: (((1 * 1e30) / (1e8 * 1e6)) * 99) / 100,
max: (((1 * 1e30) / (1e8 * 1e6)) * 101) / 100
})
});
(
int256 positionPnlUsd,
int256 uncappedPositionPnlUsd,
uint256 sizeDeltaInTokens
) = reader.getPositionPnlUsd({
dataStore: address(dataStore),
market: Market.Props({
marketToken: GM_TOKEN_ETH_WETH_USDC,
indexToken: WETH,
longToken: WETH,
shortToken: USDC
}),
prices: prices,
positionKey: key,
sizeDeltaUsd: position.numbers.sizeInUsd
});
}
// Task 6 - Create an order to close the long position created by this contract
function createCloseOrder() external payable returns (bytes32 key) {
uint256 executionFee = 0.1 * 1e18;
// Task 6.1 - Get position
Position.Props memory position = getPosition(getPositionKey());
require(position.numbers.sizeInUsd > 0, "position size = 0");
// Task 6.2 - Send execution fee to the order vault
exchangeRouter.sendWnt{value: executionFee}({
receiver: ORDER_VAULT,
amount: executionFee
});
// Task 6.3 - Create an order
// q: what order type to be created? MarketDecrease (4 in enum)
// any extra params to be created?
// withdraw of all collateral
// acceptablePrice at 1% below current price of ETH
// remember ethPrice is 1e8 * 1e4 = 1e12 (scale fro acceptable price)
uint256 ethPrice = oracle.getPrice(CHAINLINK_ETH_USD);
uint256 acceptablePrice = (ethPrice * 1e4 * 99) / 100; // 1% below current price
exchangeRouter.createOrder(
IBaseOrderUtils.CreateOrderParams({
addresses: IBaseOrderUtils.CreateOrderParamsAddresses({
receiver: address(this),
cancellationReceiver: address(0),
callbackContract: address(0),
uiFeeReceiver: address(0),
market: GM_TOKEN_ETH_WETH_USDC,
initialCollateralToken: WETH,
swapPath: new address[](0)
}),
numbers: IBaseOrderUtils.CreateOrderParamsNumbers({
sizeDeltaUsd: position.numbers.sizeInUsd,
initialCollateralDeltaAmount: position
.numbers
.collateralAmount,
triggerPrice: 0,
acceptablePrice: acceptablePrice,
executionFee: executionFee,
callbackGasLimit: 0,
minOutputAmount: 0,
validFromTime: 0
}),
// 4
orderType: Order.OrderType.MarketDecrease,
decreasePositionSwapType: Order.DecreasePositionSwapType.NoSwap,
isLong: true,
shouldUnwrapNativeToken: false,
autoCancel: false,
referralCode: bytes32(uint256(0))
})
);
}
}