Skip to content

Instantly share code, notes, and snippets.

@dexcell
Created April 1, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexcell/8342d0224a68e8b57f24681c12ff6102 to your computer and use it in GitHub Desktop.
Save dexcell/8342d0224a68e8b57f24681c12ff6102 to your computer and use it in GitHub Desktop.
Calculate yVault APY
Number of tokens within an yVault is calculated for the first time by
[number of token outside of yVault] / [factor] = [number of token within yVault]
Then the number inside the yVault is updated by simple multiplication by a multiplication factor:
[old number of token outside of yVault] * [factor] = [new number of token within yVault]
Since old number doesn't change, and only the factor does, it is enough to compare the % difference between two factors over different lengths to get the % change of the investment.
(500 * 10 = 5000) ,500 * (10*10%=11) = 550 [= 500*10%]
_
this [factor] is the same in every contract, [getPricePerFullShare]
Now, we take snapshots at given intervals and call the function getPricePerFullshare.
Let's say each 5 min, 288 times (= 24hs)
then we calculate the daily continuous compounding interest rate like so:
i = ln (n2/n1)/288
So, that is the continuously compounding interest rate for one single day, if we have have more than 288 values (because we're trying to update the apy value every x minutes, we just take the last one (n[-1]) and the 287th last one (n[-288])
and do the same
i = ln(n[-1]/n[-288])/288
From that we can calculate the APY using the formula
EAR = e**(i*n) - 1
where n = 365 days in the year, and i from our previous calculation
And convert to %
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment