Skip to content

Commit 5cab028

Browse files
committed
fixed latex problem, format
1 parent ba5dacb commit 5cab028

File tree

1 file changed

+77
-36
lines changed

1 file changed

+77
-36
lines changed

defi/GMX/Share.md

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GMX share
22

3-
- GMX项目分享视频回放
3+
- GMX 项目分享视频回放
44
- YouTube
55
- 1p <https://www.youtube.com/watch?v=_KyDkEu0sYs>
66
- 2p <https://www.youtube.com/watch?v=7wTPIUw6iGo>
@@ -133,16 +133,19 @@ $$
133133

134134
$$
135135
\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}}\\
139139
\end{align*}
140140
$$
141141

142142
对于 short 部分:
143143

144144
$$
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*}
146149
$$
147150

148151
对于 long 部分:
@@ -168,11 +171,12 @@ function getAum(bool maximise) public view returns (uint256)
168171
第七步:拿到该token的小数位数
169172
第八步:如果该token是稳定币,则aum+=poolAmount*Price
170173
第九步:如果该token不是稳定币,则统计该token的价值时,
171-
还需要考虑系统的在该token上的未实现盈亏,分别考虑多头和空头部分
174+
还需要考虑系统的在该token上的未实现盈亏,分别考虑多头和空头部分
172175
第十步:需要首先拿到该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
176180
第十二步:考虑多头部分,aum+=guranteedUsd[token],即将用户从LP这里借的钱也算作LP的资产
177181
第十三步:拿到系统预留下的token数量,reserveAmount
178182
第十四步:从poolAmount中扣除reserveAmount后,在乘以price得到该token的净价值
@@ -217,7 +221,7 @@ $$
217221
\begin{align*}
218222
Price_{avg} & = \frac{ Price \times Size}{Size+\Delta}\\
219223
\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
221225
\end{align*}
222226
$$
223227

@@ -230,23 +234,30 @@ $$
230234
$$
231235
\begin{align*}
232236
\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}
237241
\end{align*}
238242
$$
239243

240244
然后分别更新仓位的 entry 和 size,time 等:
241245

242246
$$
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*}
244252
$$
245253

246254
最后是更新仓位的 reserveAmount:
247255

248256
$$
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*}
250261
$$
251262

252263
更新完 Position 的仓位数据后,对于做多,还需要更新如下账本:
@@ -279,7 +290,11 @@ $$
279290
然后是计算 margin 的手续费:
280291

281292
$$
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*}
283298
$$
284299

285300
平仓盈亏计算:
@@ -306,9 +321,9 @@ $$
306321

307322
$$
308323
\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} \\
312327
\end{align*}
313328
$$
314329

@@ -317,26 +332,37 @@ $$
317332
然后是扣除手续费,如果 USDout 大于手续费,则直接从 USDout 里面扣除,如果 USDout 小于手续费,则从保证金里扣除
318333

319334
$$
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}
321339
$$
322340

323341
然后是更新头寸和 entryFoundingRate,
324342

325343
$$
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}\\
327347
\end{align*}
328348
$$
329349

330350
对于做多,此时还需要更新 guranteedUSD 表
331351

332352
$$
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*}
334357
$$
335358

336359
在给用户转账 USDout 前,需要折算 USDout 等价的 token,并将其更新到 PoolAmount 账本中
337360

338361
$$
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*}
340366
$$
341367

342368
最后把折算好的 token 数量打给用户即可。
@@ -370,14 +396,21 @@ MethodID: 0x1d4e3740
370396
它的计算逻辑是:
371397

372398
- 从 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
374407

375-
3.非稳定币:UNILINK
376-
对于 UNILINK,它的基本计算逻辑与 WETHWBTC 的逻辑一致,只是在返回的 price 上要额外乘以一个系数:正负 0.07%
408+
3.非稳定币:UNILINK
409+
对于 UNILINK,它的基本计算逻辑与 WETHWBTC 的逻辑一致,只是在返回的 price 上要额外乘以一个系数:正负 0.07%
377410

378-
$$
379-
maximise ? price * 1.0007 : price * 0.9993
380-
$$
411+
```ts
412+
maximise ? price * 1.0007 : price * 0.9993;
413+
```
381414

382415
项目方设定了一个机器人,让机器人每两个块,调用一次 setCompactedPrices,来更新对应的 token 的价格,只包括 WBTC,WETH,UNI,LINK 的价格,不包括稳定币的价格. 如果两次更新价格的时间间隔超过了 300 秒,则不使用 fastPriceFeed,让其直接返回 chainlink 的价格。
383416

@@ -386,11 +419,19 @@ MethodID: 0x1d4e3740
386419
GMX 里面设计了很多种 token,GMXGLP,exGMX 等。其质押的核心逻辑还是 masterchef 方式。在多种 token 的交互中,主要的设计思路是质押,托管,兑现(vesting)
387420

388421
<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+
390425
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
395426

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

Comments
 (0)