# V2 Vaults Calculation $f(t) =$ `Vault.getTokenValue()` at timestamp $t$ $t_0 =$ Timestamp at which vault was deployed or any timestamp before the first position is opened $f(t_0) = 1$ $Year =$ 1 Year in seconds $=31,536,000$ $Shares(u) =$ `VaultToken.balanceOf(u)` where `u` is the address of the user $Price_T(t)$ = Dollar value of the token $T$ at timestamp $t$ ## Vault performance $P$ - Vault performance between 2 timestamps $t_1$ and $t_2$ $$ P = {{f(t_2) \over f(t_1)} - 1} $$ ## Vault Performance Since Inception $P$ - Vault performance at timestamp $t_1$ since inception $$ P = f(t_1) - 1 $$ ## Converting Vault Performance to APR & APY Let $P$ be the vault performance between timestamps $t_1$ and $t_2$ and $P_{apr}$ & $P_{apy}$ be the corresponding APR & APY $N$ - Number of periods required to complete a year $$ N = {Year \over t_2 - t_1} $$ $$ P_{apr} = PN $$ $$ P_{apy} = (1 + P)^N - 1 $$ ## User Balances & Yield Earned Let $v_A(u, t)$ be the total of value held by the user $u$ in vault $A$ at timestamp $t$ in the deposit token $T$, and $V_A(u, t)$ be its corresponding value in Dollars ($) $$ v_A(u, t) = Shares_A(u) * f_A(t) $$ $$ V_A(u, t) = v_A(u, t) * Price_T(t) $$ Let $d_A(u)$ be the sum of all value depositted by the user $u$ to the vault $A$, and $w_A(u)$ be the sum of all value withdrawn by the user $u$ to the vault $A$ Thus yield earned (in deposit token $T$) by the user $u$ in vault $A$ is $$ \lambda_A(u, t) = v_A(u, t) + w_A(u) - d_A(u) $$ Corresponding value in dollar terms $$ \Lambda_A(u, t) = \lambda_A(u, t) * Price_T(t) $$ Following will give the total dollar value held by the user $u$ across all vaults $$ \sum_{i=1}^n V_{A_i}(u, t) $$ $A_1$ to $A_n$ are different vaults ## ROI of the User ROI is a bit tricky to calculate Let $t_1$ be the timestamp in which the user $u$ first deposits to the vault $A$, and user does a deposit or withdraw events in the following timestamps $t_2$, $t_3$,...,$t_n$ Let $Shares_{A, t_a, t_b}(u)$ be the shares held by the user $u$ in Vault $A$ between timestamps $t_a$ and $t_b$ Given that, ROI of the user $u$ in vault $A$ can be calculated by, $$ ROI_A (u) = {\sum_{i=1}^{n-1} ({f(t_{i+1}) \over f(t_i)} - 1) * Shares_{A, t_i, t_i+1}(u) \over \sum_{i=1}^{n-1} Shares_{A, t_i, t_i+1}(u) } $$ In the above equation $f(t_i)$ is the token value of the vault $A$ at timestamp $t_i$ Let $d_{A, t_i}(u)$ be the total value user holds in the vault at timestamp $t_i$ and no deposit/withdraw occurs till timestamp $t_{i+1}$, then total yield earned by the user is $$ Yield_A(u) = \sum_{i=1}^{n-1} ({f(t_{i+1}) \over f(t_i)} - 1) * d_{A, t_i}(u) $$ ## Current Deposit Use `Vault.totalFunds() + Vault.totalQueuedDeposits()` `Vault.totalQueuedDeposits()` is invalid for Basis Trading Vault, so use `Vault.totalFunds()` for Basis Trading Vault From the contracts point of view, the vaults do not have maximum capacity. Instead this will be an off-chain value and when the vaults reach this value in deposits, it'll be paused for deposits. ## Vault Transactions All vaults have events for opening a position (`OpenPosition`) and closing a position (`ClosePosition`). Read the [Natspec Documentation](https://docs.soliditylang.org/en/develop/natspec-format.html) of these events for further details. Further, Call Selling and Put Selling vaults have two additional events. One for adding additional collateral (`AddCollateral`) and one for settling options (`SettleOption`). Again please refer to the event documentations in the contracts repo. Example - [Call Selling Vault](https://github.com/Polynomial-Protocol/earn-contracts-v2/blob/master/contracts/core/CallSellingVault.sol#L897-L946) For the logic behind clubbing the event, please co-ordinate with Gautham and Abhishek. In my opinion, every event with the same strike ID can be clubbed together for Call Selling and Put Selling. Clubbing wouldn't make much sense for Gamma Vault and Basis Trading Vault. ## Vault Positions `Vault.totalFunds()` is the total funds under management. `Vault.usedFunds()` is the total amount of funds being used for trading, managing etc... ### Call Selling / Put Selling `Vault.liveStrikes()` returns an array of strike IDs in which the vault have a position `Vault.positionDatas(strikeId)` returns the position data of the strike ID `strikeId`. It returns the following data: `positionId` - Corresponding Lyra Position ID `amount` - Total amount of short position `collateral` - Total collateral used for the position `premiumCollected` - Total premium collected from this position, and this can be negative if the vault closes a position at a loss but not yet settled ### Gamma Vault `Vault.positionData()` returns the following data `strikeId` - Current Strike ID being traded by the vault `positionId` - Corresponding position ID `optionAmount` - Amount of options held by the vault of `strikeId` `premiumPaid` - Premium paid by the vault to purchase total amount of options `shortAmount` - Amount of short position held by the vault in the underlying asset `totalMargin` - Margin or collateral being used for holding the short position ### Basis Trading `Vault.positionSize()` will return the amount of short position in futures market held by the vault. Also vault will hold equal amount of underlying asset as well. Margin will need to calculated from the Synthetix futures market contract. Will be included in the resolver. --- Delta of the option with specific `strikeId` need to be calculated from the Lyra contracts. This will be included in the resolver. One short position ($1e18 = 1$) will have a delta of $-1$ Total delta of the Gamma Vault can be calculated by the following $$ \alpha * \delta_{option} - \beta $$ where $\alpha =$ `optionAmount`, $\beta =$ `shortAmount` and $\delta_{option} =$ delta of the option