Overview

All V2 vaults share a common base type [VaultBaseV1](<https://github.com/sol-farm/tulipv2-sdk/blob/68f6d150fcd7f6b06e737ceb727eb8e60e7a4595/vaults/src/accounts/vault_base.rs#L95-L174>), which implements the [TokenizedShares](<https://github.com/sol-farm/tulipv2-sdk/blob/68f6d150fcd7f6b06e737ceb727eb8e60e7a4595/common/src/traits/vault.rs#L6>) trait, and contains a method [exchange_rate](<https://github.com/sol-farm/tulipv2-sdk/blob/68f6d150fcd7f6b06e737ceb727eb8e60e7a4595/vaults/src/accounts/vault_base.rs#L296-L303>) that allows you to calculate the amount of underlying assets backing a single vault shares token.

If the return type of this function (f64) you may use the [decimal](<https://github.com/sol-farm/tulipv2-sdk/blob/68f6d150fcd7f6b06e737ceb727eb8e60e7a4595/common/src/math/decimal.rs>) library, or other similar crates to helper with calculating the exchange rate in a suitable return value. The reference implementation of the exchange_rate function is below.

fn exchange_rate(&mut self, mint: &spl_token::state::Mint) -> f64 {
        // sync shares first
        self.sync_shares(mint);
        let total_deposited_balance =
            spl_token::amount_to_ui_amount(self.total_deposited_balance, mint.decimals);
        let total_shares = spl_token::amount_to_ui_amount(self.total_shares, mint.decimals);
        total_deposited_balance / total_shares
    }

If implementing the exchange rate function manually, make sure to update the cached token mint supply values in the VaultBaseV1 type before calculation, otherwise you may have out-of-date input values.

VaultBaseV1 Access

The VaultBaseV1 type is fixed at 560-bytes in length, and occupies the first 560 bytes of the data buffer for all vault types. Note that because anchor uses an account discriminator, you will need to access the the VaultBaseV1 bytes starting at an offset of 8.

There are two methods of accessing the VaultBaseV1 type:

  1. The first and cheapest option is to avoid deserializing implementing vault types (ie: do not deserialize the MultiDepositOptimizerV1 type), and directly deserialize the data buffer into a VaultBaseV1 object.

  2. Deserialize the implementing vault type (ie: deserialize the MultiDepositOptimizerV1 type), and then accessing the TokenizedSharesTrait .