vToken APIs

There are three ways to query vToken exchange price:

  1. Use the Bifrost Runtime API, which is the most up-to-date and accurate method.
  2. Call EVM contract on Moonbeam.
  3. API for Frontend (with more vToken related storages)

Bifrost Runtime API (Get vToken Exchange Rate)

<mark style="color:purple;">https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fbifrost-polkadot-rpc.dwellir.com#/runtime</mark>

vtokenMintingRuntimeApi

  1. getCurrencyAmountByVCurrencyAmount
  2. getVCurrencyAmountByCurrencyAmount

<figure><img src="https://757947912-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJFtVsA5N3spWTWKvFvv0%2Fuploads%2F0sY8BSE5I5OVjYhuHxbF%2Fimage.png?alt=media&#x26;token=b3e053da-3de8-438d-bca5-b9eaf18809f0" alt=""><figcaption></figcaption></figure>

As the showcase above, token2:0 is DOT and vtoken2:0 is vDOT on Bifrost Polkadot, getCurrencyAmountByVCurrencyAmount is to input vDOT amount (with decimals) to query the exchange price in DOT.

{% hint style="info" %} Check the Token indexs via <mark style="color:purple;">here</mark>. {% endhint %}

Bifrost Runtime API (Get vToken Price from Stable Swap)

Bifrost Runtime API (Get vToken Price from Stable Swap)

_calcOutGivenIn/_calcInGivenOut is the method we use to query vToken prices from the Stable Pool, querying the price from DOT/vDOT stable pool as shown in the example below:

export const postStablePrice = ({
  currentStablePool,
  amount,
  sourceToken,
  type,
}) => {
  const amountIn = bn(amount).multipliedBy(getDecimalsWithZero(sourceToken));
  const { poolBalances, precision, futureA } = currentStablePool;

  const isToken = currentStablePool?.token0 === sourceToken;
  const rateToken = isToken ? currentStablePool.rate0 : currentStablePool.rate1;
  const rateVToken = isToken
    ? currentStablePool.rate1
    : currentStablePool.rate0;

  if (type === 'amountIn') {
    const price = _calcOutGivenIn(
      bn(futureA),
      [bn(poolBalances[0]), bn(poolBalances[1])],
      isToken ? 0 : 1,
      isToken ? 1 : 0,
      amountIn.multipliedBy(rateToken[1]).div(rateToken[0]),
    );

    return price
      .multipliedBy(rateVToken[0])
      .div(rateVToken[1])
      .multipliedBy(0.997)
      .div(precision)
      .toString();
  } else {
    const price = _calcInGivenOut(
      bn(futureA),
      [bn(poolBalances[0]), bn(poolBalances[1])],
      isToken ? 0 : 1,
      isToken ? 1 : 0,
      amountIn.multipliedBy(rateVToken[1]).div(rateVToken[0]),
    );

    return price
      .multipliedBy(rateToken[0])
      .div(rateToken[1])
      .div(0.997)
      .div(precision)
      .toString();
  }
};

Some parameter you need to get from Bifrost storage as explained below:

You can find poolBalances and futureA from stableAsset.pool:

<figure><img src="https://757947912-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJFtVsA5N3spWTWKvFvv0%2Fuploads%2FThmdybzBCOkuLI9RW0FN%2Fimage.png?alt=media&#x26;token=98de3ac6-51bd-4f9c-b545-d2cc7c0b7dec" alt=""><figcaption></figcaption></figure>