Indexing onchain data

In order to execute the calculations outlined above, accessing onchain data is essential. Our method of obtaining a vault's share price depends on the type of vault, as specified below.


ERC-4626 vaults

In the case of ERC-4626 vaults, we can call the totalSupply and totalAssets smart contract functions to calculate share price.

totalSupply = total shares

totalAssets = total shares value

Non ERC-4626 vaults

If a vault is not ERC-4626 compliant, proxies for the above functions can usually be found in the following functions:

  • mint

  • deposit

  • burn

  • redeem

You can see an example of this in the code snippets below.

function deposit(uint256 _amount, address _receiver) 
    external 
    nonReentrant 
    isNotPastMaturity 
    whenNotPaused 
    approvedLender(_receiver) 
    returns (uint256 _sharesReceived)
{
    _addInterest();
    VaultAccount memory _totalAsset = totalAsset;
    _sharesReceived = _totalAsset.toShares(_amount, false);
    _deposit(_totalAsset, _amount.toUint128(), _sharesReceived, _receiver);
}
function toShares(
    VaultAccount memory total,
    uint256 amount,
    bool roundUp
) internal pure returns (uint256 shares) {
    if (total.amount == 0) {
        shares = amount;
    } else {
        shares = (amount * total.shares) / total.amount;
        if (roundUp && (shares * total.amount) / total.shares < amount) {
            shares = shares + 1;
        }
    }
}

The functions provided are typical examples and not an exhaustive list; some vaults may require individual solutions.

Liquid Staking Tokens (LSTs and LRTs)

To calculate APY for LSTs (e.g., Lido) and other vaults that periodically update their earnings (e.g., Yearn vaults), we use the totalSupply and getEthValue(totalSupply)functions.

totalSupply = total shares

getEthValue(totalSupply)= total shares value

Rebasing supply vaults

Vaults that gradually increase the user's LP token balance, such as those on Aave, are known as rebasing supply vaults. We calculate the share price for rebasing supply vaults with the totalSupply and scaledTotalSupply functions.

scaledTotalSupply = total shares

totalSupply = total shares value

Staking mechanism vaults

Vaults that do not compound automatically (e.g., StakeWise) are known as staking mechanism vaults. These vaults do not compound interest automatically, instead making users periodically harvest and redeposit it to maximize yield. Calculating returns in such scenarios can be complex, as the total shares:value ratio does not reflect share price accurately.

Instead, we can ascertain the actual share price with the following formulas:

total principal value=sETH2.totalSupply()total rewards value=rETH2.totalSupply()TVL=total principal value+total rewards valueshare price=1+rETH2.rewardPerToken()totalSupply=TVLshare price\text{total principal value} = \text{sETH2.totalSupply()} \\ \text{total rewards value} = \text{rETH2.totalSupply()} \\ \text{TVL} = \text{total principal value} + \text{total rewards value} \\ \text{share price} = 1 + \text{rETH2.rewardPerToken()} \\ \text{totalSupply} = \frac{\text{TVL}}{\text{share price}}

Real World Assets (RWAs)

RWAs on vaults.fyi are ERC-20 tokens that reflect investments in offchain financial assets. To assess the yield of RWAs we use:

  • The totalSupply function to calculate total shares.

  • The RWAOracle.price function to fetch share price.

  • The StablecoinOracle.price function to fetch the underlying asset's (i.e, a stablecoin) price.

These functions are used in the following formulas to calculate yield:

total shares=totalSupply()share asset price=RWAOracle.price()underlying asset price=StablecoinOracle.price()total shares in USD=total shares×share asset pricetotal shares in asset=total shares in USDunderlying asset pricetotal shares value=total shares in asset\begin{align*} \text{total shares} &= \text{totalSupply()} \\ \text{share asset price} &= \text{RWAOracle.price()} \\ \text{underlying asset price} &= \text{StablecoinOracle.price()} \\ \text{total shares in USD} &= \text{total shares} \times \text{share asset price} \\ \text{total shares in asset} &= \frac{\text{total shares in USD}}{\text{underlying asset price}} \\ \text{total shares value} &= \text{total shares in asset} \end{align*}

Last updated