Calculating APY for cross chain uniswap v2 clones

Is there a simple way to calculate APYs & TVLs for various uniswap v2 clones across chains?. Been looking at how Sushiswap do it but it’s entangled in their front end. Very messy. In general a few utility functions for common platform types would be very useful. Any tips on how to do this?

There’s no simple way unless it’s offered from the DEX directly, see if they have API endpoints for this sort of data. Otherwise you will have to analyze the code.

Accurate APYs/TVLs, etc. will depend on tokenomics, so I think it’d be hard to find or develop utilities for this sort of thing that work in general. And clones may adjust their calculation methods.

This is an example of calculating a token’s APR for one of their pools using the contract data in combination with using SpookySwap (a fork of Uniswap) with the intent of matching the token’s website’s APR stat.

// SDK
const { Fetcher, Route, Token, ChainId } = require('@ac32/spookyswap-sdk');

// Utility functions
async function getCoinInUsd(response) {
  let coinBalance = response.coinBalance;
  coinBalance = ethers.utils.formatEther(coinBalance);

  let usdBalance = response.usdBalance;
  usdBalance = ethers.utils.formatUnits(usdBalance, '6');

  const coinPrice = (usdBalance / coinBalance).toString();

  return coinPrice;
}

async function getTokenPriceInCoin(coinAddress, tokenAddress, symbol) {
  const coinToken = new Token(ChainId, coinAddress, 18, 'WFTM'); 
  const token = new Token(ChainId, tokenAddress, 18, symbol); 

  const coinToToken = await Fetcher.fetchPairData(coinToken, token, provider);
  const tokenPriceInCoin = new Route([coinToToken], token);

  return tokenPriceInCoin.midPrice.toFixed(4);
}

async function getTokenPriceInDollars(tokenPriceInCoin, coinPrice) {
  const tokenPriceInDollars = (tokenPriceInCoin * coinPrice).toFixed(2);

  return tokenPriceInDollars;
}

// Get prices
const coinPrice = await getCoinInUsd(response);
const priceOfTokenInCoin = await getTokenPriceInCoin(ADDRESSES.Coin, ADDRESSES.Token, response);
const priceOfTokenInUsd = await getTokenPriceInDollars(priceOfTokenInCoin, coinPrice);


// APR - lastHistory is read from contract, tvl is from another calculation
const lastHistory = response.lastHistory;
const lastRewardsReceived = lastHistory[1];

const epochRewardsPerShare = lastRewardsReceived / 1e18;

const amountOfRewardsPerDay = epochRewardsPerShare * Number(priceOfTokenInUsd) * 4;

let apr = ((amountOfRewardsPerDay * 100) / tvl) * 365;
apr = parseDecimal(apr, 2);
1 Like