1
1
# GMX share
2
2
3
- - GMX项目分享视频回放
3
+ - GMX 项目分享视频回放
4
4
- YouTube
5
5
- 1p < https://www.youtube.com/watch?v=_KyDkEu0sYs >
6
6
- 2p < https://www.youtube.com/watch?v=7wTPIUw6iGo >
133
133
134
134
$$
135
135
\begin{align*}
136
- AUM_{NonStableToken} & = PoolAmount\times Price+ P\&L_{long}+ P\&L_{short}\\
137
- P\&L_{long} & = GuranteedUSD- ReserveAmount\times Price\\
138
- P\&L_{short} &= \pm Size_{globalShort}\times \frac{\left | Price-avgPrice_{globalShort} \right | }{avgPrice_{globalShort}}\\
136
+ AUM_{NonStableToken} & = PoolAmount \times Price + { P\&L_{long}} + { P\&L_{short} }\\
137
+ P\&L_{long} & = GuranteedUSD - ReserveAmount \times Price\\
138
+ P\&L_{short} & = \pm Size_{globalShort}\times \frac{\left | Price-avgPrice_{globalShort} \right | }{avgPrice_{globalShort}}\\
139
139
\end{align*}
140
140
$$
141
141
142
142
对于 short 部分:
143
143
144
144
$$
145
- \begin{align*}Price>avgPrice_{globalShort},User 亏损,LP 盈利,P\&L_{short}>0\\Price<avgPrice_{globalShort},User 盈利,LP 亏损,P\&L_{short}<0\\\end{align*}
145
+ \begin{align*}
146
+ Price > avgPrice_{globalShort}, {User亏损}, {LP盈利}, {P\&L_{short}}>0\\
147
+ Price < avgPrice_{globalShort}, {User盈利}, {LP亏损}, {P\&L_{short}}<0
148
+ \end{align*}
146
149
$$
147
150
148
151
对于 long 部分:
@@ -168,11 +171,12 @@ function getAum(bool maximise) public view returns (uint256)
168
171
第七步:拿到该token的小数位数
169
172
第八步:如果该token是稳定币,则aum+=poolAmount*Price
170
173
第九步:如果该token不是稳定币,则统计该token的价值时,
171
- 还需要考虑系统的在该token上的未实现盈亏,分别考虑多头和空头部分
174
+ 还需要考虑系统的在该token上的未实现盈亏,分别考虑多头和空头部分
172
175
第十步:需要首先拿到该token的globalShortSize,即vault在该token上的全局空仓头寸。
173
- 第十一步:如果该空仓头寸大于0,则拿到该空仓头寸的平均价格avgPrice,计算delta=size*|price-avgPrice|/avgPrice
174
- 如果当前价格比avgPrice价格高,则用户亏钱,LP赚钱,则aum+=delta
175
- 如果当前价格比avgPrice价格低,则用户赚钱,LP亏钱,则aum-=delta
176
+ 第十一步:如果该空仓头寸大于0,则拿到该空仓头寸的平均价格avgPrice,
177
+ 计算 delta = size * abs(price-avgPrice) / avgPrice
178
+ 如果当前价格比avgPrice价格高,则用户亏钱,LP赚钱,则 aum+=delta
179
+ 如果当前价格比avgPrice价格低,则用户赚钱,LP亏钱,则 aum-=delta
176
180
第十二步:考虑多头部分,aum+=guranteedUsd[token],即将用户从LP这里借的钱也算作LP的资产
177
181
第十三步:拿到系统预留下的token数量,reserveAmount
178
182
第十四步:从poolAmount中扣除reserveAmount后,在乘以price得到该token的净价值
217
221
\begin{align* }
218
222
Price_{avg} & = \frac{ Price \times Size}{Size+ \Delta}\\
219
223
\Delta & = Size^ {before} \times \frac{\left | Price - Price_{avg}^ {before} \right | }{Price_{avg}^ {before}}\\
220
- Size& = Size^ {before}+ \delta Size
224
+ Size & = Size^ {before} + \delta Size
221
225
\end{align* }
222
226
$$
223
227
230
234
$$
231
235
\begin{align* }
232
236
\delta token & = tokenBalance^ {after}- tokenBalance^ {before}\\
233
- collateral& = collateral+ \delta token \times Price_{min}- fee_{margin}\\
234
- fee_{margin}& = fee_{position}+ fee_{funding}\\
235
- fee_{position}& = \delta Size\times 0.1 \% \\
236
- fee_{funding}& = Size^ {before}\times\frac{FundingRate_{acc}- FundingRate_{entry}}{1000000 }
237
+ collateral & = collateral+ \delta token \times Price_{min}- fee_{margin}\\
238
+ fee_{margin} & = fee_{position}+ fee_{funding}\\
239
+ fee_{position} & = \delta Size\times 0.1 \% \\
240
+ fee_{funding} & = Size^ {before}\times\frac{FundingRate_{acc}- FundingRate_{entry}}{1000000 }
237
241
\end{align* }
238
242
$$
239
243
240
244
然后分别更新仓位的 entry 和 size,time 等:
241
245
242
246
$$
243
- \begin{align* }Size & = Size^ {before}+ \delta Size\\entryFundingRate&= FundingRate_{acc}\\lastIncreasedTime&= now\end{align* }
247
+ \begin{align* }
248
+ Size & = Size^ {before}+ \delta Size\\
249
+ entryFundingRate & = FundingRate_{acc}\\
250
+ lastIncreasedTime & = now
251
+ \end{align* }
244
252
$$
245
253
246
254
最后是更新仓位的 reserveAmount:
247
255
248
256
$$
249
- \begin{align* }\delta reserve & = \frac{\delta Size}{Price_{min}}\\reserve & = reserve^ {before}+ \delta reserve \end{align* }
257
+ \begin{align* }
258
+ \delta reserve & = \frac{\delta Size}{Price_{min}}\\
259
+ reserve & = reserve^ {before} + {\delta reserve}
260
+ \end{align* }
250
261
$$
251
262
252
263
更新完 Position 的仓位数据后,对于做多,还需要更新如下账本:
279
290
然后是计算 margin 的手续费:
280
291
281
292
$$
282
- \begin{align* }fee_{margin}&= fee_{position}+ fee_{funding}\\fee_{position}&= \delta Size\times 0.1 \% \\fee_{funding}&= Size^ {before}\times\frac{FundingRate_{acc}- FundingRate_{entry}}{1000000 } \end{align* }
293
+ \begin{align* }
294
+ fee_{margin} & = fee_{position}+ fee_{funding}\\
295
+ fee_{position} & = \delta Size\times 0.1 \% \\
296
+ fee_{funding} & = Size^ {before} \times \frac{FundingRate_{acc}- FundingRate_{entry}}{1000000 }
297
+ \end{align* }
283
298
$$
284
299
285
300
平仓盈亏计算:
306
321
307
322
$$
308
323
\begin{align* }
309
- USD ^ {out}&= \delta coll & \text{ 用户亏损: } price< price_{avg}^ {before} \\
310
- coll&= coll^ {before}- \Delta \times \frac{\delta Size}{Size^ {before}} - \delta coll& \text{ 用户亏损: } price< price_{avg}^ {before} \\
311
- realisedPnL&= realisedPnL^ {before}- \Delta \times \frac{\delta Size}{Size^ {before}} & \text{ 用户亏损: } price< price_{avg}^ {before} \\
324
+ USD ^ {out} &= \delta coll & \text{ 用户亏损: } price< price_{avg}^ {before} \\
325
+ coll &= coll^ {before}- \Delta \times \frac{\delta Size}{Size^ {before}} - \delta coll& \text{ 用户亏损: } price< price_{avg}^ {before} \\
326
+ realisedPnL &= realisedPnL^ {before}- \Delta \times \frac{\delta Size}{Size^ {before}} & \text{ 用户亏损: } price< price_{avg}^ {before} \\
312
327
\end{align* }
313
328
$$
314
329
317
332
然后是扣除手续费,如果 USDout 大于手续费,则直接从 USDout 里面扣除,如果 USDout 小于手续费,则从保证金里扣除
318
333
319
334
$$
320
- \begin{cases}USD ^ {out}&= USD ^ {out}- fee & \text{ if: } USD ^ {out}> fee \\coll&= coll- fee & \text{ if: } USD ^ {out}\le fee \\\end{cases}
335
+ \begin{cases}
336
+ USD ^ {out}&= USD ^ {out}- fee & \text{ if: } USD ^ {out}> fee \\
337
+ coll & = coll- fee & \text{ if: } USD ^ {out}\le fee \\
338
+ \end{cases}
321
339
$$
322
340
323
341
然后是更新头寸和 entryFoundingRate,
324
342
325
343
$$
326
- \begin{align* }Size & = Size^ {before}- \delta Size\\FoundingRate_{entry} & = FoundingRate_{acc}\\
344
+ \begin{align* }
345
+ Size & = Size^ {before}- \delta Size\\
346
+ FoundingRate_{entry} & = FoundingRate_{acc}\\
327
347
\end{align* }
328
348
$$
329
349
330
350
对于做多,此时还需要更新 guranteedUSD 表
331
351
332
352
$$
333
- \begin{align* }guranteedUSD_{token}^ {after} & = guranteedUSD_{token}^ {before}+ \Delta\\\Delta & = coll^ {before}- coll^ {after}- \delta Size\end{align* }
353
+ \begin{align* }
354
+ guranteedUSD_{token}^ {after} & = guranteedUSD_{token}^ {before}+ \Delta\\
355
+ \Delta & = coll^ {before}- coll^ {after} - {\delta Size}
356
+ \end{align* }
334
357
$$
335
358
336
359
在给用户转账 USDout 前,需要折算 USDout 等价的 token,并将其更新到 PoolAmount 账本中
337
360
338
361
$$
339
- \begin{align* }poolAmount_{token} & = poolAmount_{token}^ {before}- \Delta\\\Delta &= \frac{USD ^ {out}}{price_{max}} \end{align* }
362
+ \begin{align* }
363
+ poolAmount_{token} & = poolAmount_{token}^ {before}- \Delta\\
364
+ \Delta &= \frac{USD ^ {out}}{price_{max}}
365
+ \end{align* }
340
366
$$
341
367
342
368
最后把折算好的 token 数量打给用户即可。
@@ -370,14 +396,21 @@ MethodID: 0x1d4e3740
370
396
它的计算逻辑是:
371
397
372
398
- 从 chainlink 里拿到 WETH 的价格 refPrice
373
- - 把 refPrice 传到 fastPriceFeed 合约里,进行如下计算: - 计算 minPrice = refPrice _ (1 - 2.5 % ) - 计算 maxPrice = refPrice _ (1 + 2.5 % ) - 从 fastPriceFeed 里拿到最新 updated 的价格:fastPrice - 如果 fastPrice 在[minPrice, maxPrice]之间,则返回 fastPrice - 如果 fastPrice < minPrice, maximise ? refPrice : minPrice - 如果 fastPrice > maxPrice, maximise ? maxPrice : refPrice
399
+ - 把 refPrice 传到 fastPriceFeed 合约里,进行如下计算
400
+
401
+ - 计算 ` minPrice = refPrice _ (1 - 2.5%)`
402
+ - 计算 ` maxPrice = refPrice _ (1 + 2.5%)`
403
+ - 从 fastPriceFeed 里拿到最新 updated 的价格:fastPrice
404
+ - 如果 fastPrice 在[minPrice, maxPrice]之间,则返回 fastPrice
405
+ - 如果 fastPrice < minPrice, maximise ? refPrice : minPrice
406
+ - 如果 fastPrice > maxPrice, maximise ? maxPrice : refPrice
374
407
375
- 3. 非稳定币:UNI ,LINK
376
- 对于 UNI 和 LINK ,它的基本计算逻辑与 WETH ,WBTC 的逻辑一致,只是在返回的 price 上要额外乘以一个系数:正负 0.07 %
408
+ 3. 非稳定币:UNI ,LINK
409
+ 对于 UNI 和 LINK ,它的基本计算逻辑与 WETH ,WBTC 的逻辑一致,只是在返回的 price 上要额外乘以一个系数:正负 0.07 %
377
410
378
- $$
379
- maximise ? price * 1.0007 : price * 0.9993
380
- $$
411
+ ` ` ` ts
412
+ maximise ? price * 1.0007 : price * 0.9993;
413
+ ` ` `
381
414
382
415
项目方设定了一个机器人,让机器人每两个块,调用一次 setCompactedPrices,来更新对应的 token 的价格,只包括 WBTC ,WETH ,UNI ,LINK 的价格,不包括稳定币的价格. 如果两次更新价格的时间间隔超过了 300 秒,则不使用 fastPriceFeed,让其直接返回 chainlink 的价格。
383
416
@@ -386,11 +419,19 @@ MethodID: 0x1d4e3740
386
419
在 GMX 里面设计了很多种 token,GMX ,GLP ,exGMX 等。其质押的核心逻辑还是 masterchef 方式。在多种 token 的交互中,主要的设计思路是质押,托管,兑现(vesting)
387
420
388
421
< center>< img src= " https://github.com/Dapp-Learning-DAO/Dapp-Learning-Arsenal/blob/main/images/defi/GMX/2022-09-03-11-44-54.png?raw=true" / >< / center>
389
- 质押逻辑:
422
+
423
+ ### 质押逻辑
424
+
390
425
GMX related:
391
- 1. 首先质押GMX ,得到sGMX,同时得到奖励代币esGMX
392
- 2. 拿sGMX质押到Bonus池子里,得到sbGMX,同时得到奖励代币bnGMX
393
- 3. 拿得到的sbGMX和奖励的bnGMX分别质押到手续费池子里,得到sbfGMX,获得奖励代币WETH ,即手续费分成
394
- 4. 拿奖励代币esGMX配上sbfGMX代币,到GMX 兑换池中兑换成GMX
395
426
396
- GLP related: 1. 用 ETH 购买 GLP ,得到 GLP 2. 用 GLP 质押到手续费池子中,得到 fGLP,同时获得手续费奖励 WETH 3. 用得到的 fGLP 质押到 GLP 池子中,得到 fsGLP,同时获得奖励代币 esGMX 4. 拿奖励代币 esGMX 配上 fsGLP,到 GMX 兑换池中兑换成 GMX
427
+ 1. 首先质押GMX ,得到sGMX,同时得到奖励代币esGMX
428
+ 2. 拿sGMX质押到Bonus池子里,得到sbGMX,同时得到奖励代币bnGMX
429
+ 3. 拿得到的sbGMX和奖励的bnGMX分别质押到手续费池子里,得到sbfGMX,获得奖励代币WETH ,即手续费分成
430
+ 4. 拿奖励代币esGMX配上sbfGMX代币,到GMX 兑换池中兑换成GMX
431
+
432
+ GLP related:
433
+
434
+ 1. 用 ETH 购买 GLP ,得到 GLP
435
+ 2. 用 GLP 质押到手续费池子中,得到 fGLP,同时获得手续费奖励 WETH
436
+ 3. 用得到的 fGLP 质押到 GLP 池子中,得到 fsGLP,同时获得奖励代币 esGMX
437
+ 4. 拿奖励代币 esGMX 配上 fsGLP,到 GMX 兑换池中兑换成 GMX
0 commit comments