Skip to content

Instantly share code, notes, and snippets.

@ajb413
Last active November 28, 2023 06:11
Show Gist options
  • Save ajb413/a6f89486ec5485746cd5eac1e10e4fc2 to your computer and use it in GitHub Desktop.
Save ajb413/a6f89486ec5485746cd5eac1e10e4fc2 to your computer and use it in GitHub Desktop.
How to calculate the Net APY that is displayed for users on https://app.compound.finance/

Net APY Calculation

  1. Convert all supplied and borrowed asset amounts to a single asset (like USD or ETH).
  2. Calculate the sum of (suppliedAmount * supplyApyAsDecimal - borrowedAmount * borrowApyAsDecimal) for all underlying assets.
  3. If the calculated sum from the previous step is >0 then Net APY = 100 * (sum / totalSuppliedValue). If the calculation from the previous step is <0 then Net APY = 100 * (sum / totalBorrowedValue). If the calculation from the previous step is 0 then Net APY = 0.

Example

Net APY:

  • -7.29%

Supplied Assets:

  • 0.6 ETH ($260.20) @ 1.11% APY

Borrowed Assets:

  • 75 BAT ($26.56) @ 5.57% APY
  • 100.02 USDT ($100.02) @ 10.64 APY%

Calculation:

(260.20 * 0.0111 - 0 * 0) + (0 * 0 - 26.56 * 0.0557) + (0 * 0 - 100.02 * 0.1064) = -9.2333

100 * (-9.2333 / (26.56 + 100.02)) = -7.294438299889398

-7.29%
@notesbytom
Copy link

For a market, how are you retrieving the suppliedAmount for an address? I can get my current cToken balance and exchange for underlying amount from each cToken contract, but not as easy to determine the original amount supplied? I was thinking a hard way might be to look at transaction history of the address against the cToken contract.

@ajb413
Copy link
Author

ajb413 commented Jan 20, 2021

If the account transfers out cTokens, then their net APY reduces, because their account is entitled to fewer underlying tokens. So using the cToken balance is valid.

@notesbytom
Copy link

notesbytom commented Jan 20, 2021

Okay I see, it's just a snapshot rate for an account. Has nothing to do with historical rate or historical balance at all! Thanks :-)

@crypto-perry
Copy link

this formula for the net APY depends on the value of the funds you put in... that should not be the case because the netAPY should only depend on the supply APY, borrow APY and loan to value ratio.... but definitely not on the account value...

@johnmosesman
Copy link

As of now on Compound on Rinkeby network, the divisor seems to be the sum of the assets you've supplied, i.e., their definition seems to be "Net APY compared to what you invested".

This gets the correct value for me:

Net APY = 100 * (sum / totalSupplied)

Don't need the conditional check on if the sum is positive or negative.

Example from my rinkeby wallet (using DAI and USDC so no currency conversion step here):

sum = (14.53*0.0691 + 59.99*0 - 34.24*0.1369)/(14.53+59.99) = -0.0494
net apy = sum * 100 = -4.94%

image

@CherryDT
Copy link

Hm, something is not adding up. I did an example calculation and got a very different value than shown on the website, perhaps it's because the website somehow includes COMP while you don't? Not sure how, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment