This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GMXOracle.sol
316 lines (276 loc) · 10.5 KB
/
GMXOracle.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { ISyntheticReader } from "../interfaces/protocols/gmx/ISyntheticReader.sol";
import { IChainlinkOracle } from "../interfaces/oracles/IChainlinkOracle.sol";
import { Errors } from "../utils/Errors.sol";
import { console } from "forge-std/console.sol";
contract GMXOracle {
/* ==================== STATE VARIABLES ==================== */
// GMX DataStore
address public immutable dataStore;
// GMX Synthetic Reader
ISyntheticReader public immutable syntheticReader;
// Chainlink oracle
IChainlinkOracle public immutable chainlinkOracle;
/* ====================== CONSTANTS ======================== */
uint256 public constant SAFE_MULTIPLIER = 1e18;
/* ====================== CONSTRUCTOR ====================== */
/**
* @param _DataStore Address of GMX DataStore
* @param _syntheticReader Address of GMX Synthetic Reader
* @param _chainlinkOracle Address of Chainlink oracle
*/
constructor(
address _DataStore,
ISyntheticReader _syntheticReader,
IChainlinkOracle _chainlinkOracle
) {
if (_DataStore == address(0)) revert Errors.ZeroAddressNotAllowed();
if (address(_syntheticReader) == address(0)) revert Errors.ZeroAddressNotAllowed();
if (address(_chainlinkOracle) == address(0)) revert Errors.ZeroAddressNotAllowed();
dataStore = _DataStore;
syntheticReader = _syntheticReader;
chainlinkOracle = _chainlinkOracle;
}
/* ===================== VIEW FUNCTIONS ==================== */
/**
* @notice Get amountsOut of either the long or short token based on the amountsIn
* of either long or short token in the market
* @param marketToken LP token address
* @param indexToken Index token address
* @param longToken Long token address
* @param shortToken Short token address
* @param tokenIn TokenIn address
* @param amountIn Amount of tokenIn, expressed in tokenIn's decimals
* @return amountsOut Amount of tokenOut within LP (market) to be received, expressed in tokenOut's decimals
*/
function getAmountsOut(
address marketToken,
address indexToken,
address longToken,
address shortToken,
address tokenIn,
uint256 amountIn
) public view returns (uint256) {
ISyntheticReader.MarketProps memory _market;
_market.marketToken = marketToken;
_market.indexToken = indexToken;
_market.longToken = longToken;
_market.shortToken = shortToken;
ISyntheticReader.PriceProps memory _indexTokenPrice;
_indexTokenPrice.min = _getTokenPriceMinMaxFormatted(indexToken);
_indexTokenPrice.max = _getTokenPriceMinMaxFormatted(indexToken);
ISyntheticReader.PriceProps memory _longTokenPrice;
_longTokenPrice.min = _getTokenPriceMinMaxFormatted(longToken);
_longTokenPrice.max = _getTokenPriceMinMaxFormatted(longToken);
ISyntheticReader.PriceProps memory _shortTokenPrice;
_shortTokenPrice.min = _getTokenPriceMinMaxFormatted(shortToken);
_shortTokenPrice.max = _getTokenPriceMinMaxFormatted(shortToken);
ISyntheticReader.MarketPrices memory _prices;
_prices.indexTokenPrice = _indexTokenPrice;
_prices.longTokenPrice = _longTokenPrice;
_prices.shortTokenPrice = _shortTokenPrice;
address _uiFeeReceiver = address(0);
(uint256 _amountsOut,,) = syntheticReader.getSwapAmountOut(
dataStore,
_market,
_prices,
tokenIn,
amountIn,
_uiFeeReceiver
);
return _amountsOut;
}
/**
* @notice Helper function to calculate amountIn of either long or short token for swapping for
* desired amountsOut of long or short token
* @notice We utilise GMX's getSwapAmountOut() with tokenOut being tokenIn, multiplying
* the amountsOut value by 1.0015x to account for fees and normal chainlink price feed differential
* @param marketToken LP token address
* @param indexToken Index token address
* @param longToken Long token address
* @param shortToken Short token address
* @param tokenOut TokenIn address
* @param amountsOut Amount of tokenIn, expressed in tokenIn's decimals
* @return amountsOut Amount of tokenOut within LP (market) to be received, expressed in tokenOut's decimals
*/
function getAmountsIn(
address marketToken,
address indexToken,
address longToken,
address shortToken,
address tokenOut,
uint256 amountsOut
) public view returns (uint256) {
return getAmountsOut(
marketToken,
indexToken,
longToken,
shortToken,
tokenOut,
amountsOut
) * (1e18 + 15e14) / SAFE_MULTIPLIER;
}
/**
* @notice Get LP (market) token info
* @param marketToken LP token address
* @param indexToken Index token address
* @param longToken Long token address
* @param shortToken Short token address
* @param pnlFactorType P&L Factory type in bytes32 hashed string
* @param maximize Min/max price boolean
* @return (marketTokenPrice, MarketPoolValueInfoProps MarketInfo)
*/
function getMarketTokenInfo(
address marketToken,
address indexToken,
address longToken,
address shortToken,
bytes32 pnlFactorType,
bool maximize
) public view returns (int256, ISyntheticReader.MarketPoolValueInfoProps memory) {
if (address(marketToken) == address(0)) revert Errors.ZeroAddressNotAllowed();
if (address(indexToken) == address(0)) revert Errors.ZeroAddressNotAllowed();
if (address(longToken) == address(0)) revert Errors.ZeroAddressNotAllowed();
if (address(shortToken) == address(0)) revert Errors.ZeroAddressNotAllowed();
ISyntheticReader.MarketProps memory _market;
_market.marketToken = marketToken;
_market.indexToken = indexToken;
_market.longToken = longToken;
_market.shortToken = shortToken;
ISyntheticReader.PriceProps memory _indexTokenPrice;
_indexTokenPrice.min = _getTokenPriceMinMaxFormatted(indexToken);
_indexTokenPrice.max = _getTokenPriceMinMaxFormatted(indexToken);
ISyntheticReader.PriceProps memory _longTokenPrice;
_longTokenPrice.min = _getTokenPriceMinMaxFormatted(longToken);
_longTokenPrice.max = _getTokenPriceMinMaxFormatted(longToken);
ISyntheticReader.PriceProps memory _shortTokenPrice;
_shortTokenPrice.min = _getTokenPriceMinMaxFormatted(shortToken);
_shortTokenPrice.max = _getTokenPriceMinMaxFormatted(shortToken);
return syntheticReader.getMarketTokenPrice(
dataStore,
_market,
_indexTokenPrice,
_longTokenPrice,
_shortTokenPrice,
pnlFactorType,
maximize
);
}
/**
* @notice Get LP (market) token reserves
* @param marketToken LP token address
* @param indexToken Index token address
* @param longToken Long token address
* @param shortToken Short token address
* @return (reserveA, reserveB) Reserve amount of longToken and shortToken respectively
*/
function getLpTokenReserves(
address marketToken,
address indexToken,
address longToken,
address shortToken
) public view returns (uint256, uint256) {
// _pnlFactorType value does not matter in getting token reserves
bytes32 _pnlFactorType = keccak256(abi.encode("MAX_PNL_FACTOR_FOR_DEPOSITS"));
// _maximize value does not matter in getting token reserves
bool _maximize = false;
(, ISyntheticReader.MarketPoolValueInfoProps memory _marketInfo) = getMarketTokenInfo(
marketToken,
indexToken,
longToken,
shortToken,
_pnlFactorType,
_maximize
);
return (
_marketInfo.longTokenAmount,
_marketInfo.shortTokenAmount
);
}
/**
* @notice Get LP (market) token reserves
* @param marketToken LP token address
* @param indexToken Index token address
* @param longToken Long token address
* @param shortToken Short token address
* @param isDeposit Boolean for deposit or withdrawal
* @param maximize Boolean for minimum or maximum price
* @return marketTokenPrice in 1e18
*/
function getLpTokenValue(
address marketToken,
address indexToken,
address longToken,
address shortToken,
bool isDeposit,
bool maximize
) public view returns (uint256) {
bytes32 _pnlFactorType;
if (isDeposit) {
_pnlFactorType = keccak256(abi.encode("MAX_PNL_FACTOR_FOR_DEPOSITS"));
} else {
_pnlFactorType = keccak256(abi.encode("MAX_PNL_FACTOR_FOR_WITHDRAWALS"));
}
(int256 _marketTokenPrice,) = getMarketTokenInfo(
marketToken,
indexToken,
longToken,
shortToken,
_pnlFactorType,
maximize
);
// If LP token value is negative, return 0
if (_marketTokenPrice < 0) {
return 0;
} else {
// Price returned in 1e30, we normalize it to 1e18
return uint256(_marketTokenPrice) / 1e12;
}
}
/**
* @notice Get token A and token B's LP token amount required for a given value
* @param givenValue Given value needed, expressed in 1e30
* @param marketToken LP token address
* @param indexToken Index token address
* @param longToken Long token address
* @param shortToken Short token address
* @param isDeposit Boolean for deposit or withdrawal
* @param maximize Boolean for minimum or maximum price
* @return lpTokenAmount Amount of LP tokens; expressed in 1e18
*/
function getLpTokenAmount(
uint256 givenValue,
address marketToken,
address indexToken,
address longToken,
address shortToken,
bool isDeposit,
bool maximize
) public view returns (uint256) {
uint256 _lpTokenValue = getLpTokenValue(
marketToken,
indexToken,
longToken,
shortToken,
isDeposit,
maximize
);
return givenValue * SAFE_MULTIPLIER / _lpTokenValue;
}
/* ================== INTERNAL FUNCTIONS =================== */
/**
* @notice Get token price formatted for GMX mix/max decimals for 1e30 normalization
* @dev E.g. if token decimals is 18, to normalize to 1e30, we need to return 30-18 = 1e12
* consult() usually returns asset price in 8 decimals, so 30 - tokenDecimals - priceDecimals
* should format the decimals correctly for 1e30
* @param token Token address
* @return tokenPriceMinMaxFormatted
*/
function _getTokenPriceMinMaxFormatted(address token) internal view returns (uint256) {
(int256 _price, uint8 _priceDecimals) = chainlinkOracle.consult(token);
return uint256(_price) * 10 ** (30 - IERC20Metadata(token).decimals() - _priceDecimals);
}
}