# Arrakis IL calculation
## Goal
Calculate IL of the pool and display it as a projected APR based on the 24h of past performance.
IL is calculated between 2 points: $t_0$ and $t_{-1}$
Where $t_0$ is current time, and $t_{-1}$ is 24 hours before.
## Challenges
The pool is constantly rebalanced.
Users add/remove liquidity all the time.
Token prices change every block.
## Proposal
1. Get current `token0` + `token1` amounts in Arrakis vault at block number $t_0$.
2. Get current total supply of Arrakis LP tokens at block number $t_0$
3. Calculate block number for $t_{-1}$
4. Get `token0` + `token1` amounts in Arrakis vault at block number $t_{-1}$
5. Get total supply of Arrakis LP tokens at block number $t_{-1}$
6. Calculate the USD value of current ($t_0$) `token0` + `token1` based on current prices
7. Calculate the USD value of past ($t_{-1}$) `token0` + `token1` based on *current prices* (not past prices)
8. Calculate average USD value of 1 Arrakis LP token at block number $t_0$
9. Calculate average USD value of 1 Arrakis LP token at block number $t_{-1}$
10. Calculate the change in prices from $t_{-1}$ to $t_0$
11. Project APR to 365 days
## Example:
- $t_0$ current block 83601585 (Optimism)
- $t_{-1}$ past block 83374720 (Optimism)
- pool wstETH/USDT 0.05% `0x95d7d146ae40d4822c2750276b54b6eed530d374`
1. `token0` = 85096456178279655213, `token1` = 247177751051
2. LP = 19548736484492746
3. $t_{-1}$ = 83374720
4. `token0` = 47667622993098469466, `token1` = 148488773140
5. LP = 11424262734326879
6. Total locked at $t_0$: $413 181.54
- `token0` 85.096456178279655213 * 1947.724872 = $165744.48
- `token1` 247177.751051 * 1.001049089111929728 = $247437.06
7. Total locked at $t_{-1}$: $243 643.10
- `token0` 47.667622993098469466 * 1991.844335 = $94946.48
- `token1` 148488.773140 * 1.001399804400975856 = $148696.62
8. LP price at $t_0$: $413181.54 / 19548736484492746 = $0.00000000002113597164
9. LP price at $t_{-1}$: $243643.10 / 11424262734326879 = $0.00000000002132681168
10. 24h change = 0.99105163758823981888 (**-0.89%**)
- $0.00000000002113597164 / $0.00000000002132681168
11. IL APR: **-326.61%**
## Subgraph links
`https://thegraph.com/hosted-service/subgraph/tnkrxyz/arrakis-finance-optimism`
```graphql
{
vault(id:"0xe9cf1b50420532f3197fc0211a4bbef67c6761f9", block: {number: 83601585})
{
id
name
symbol
inputTokenBalance
_token0 {
symbol
decimals
lastPriceUSD
}
_token1 {
symbol
decimals
lastPriceUSD
}
_token0Amount
_token1Amount
}
}
```