diff --git a/src/adaptors/teahouse-permissionless/utils.ts b/src/adaptors/teahouse-permissionless/utils.ts index 69ab276895..9313b1331a 100644 --- a/src/adaptors/teahouse-permissionless/utils.ts +++ b/src/adaptors/teahouse-permissionless/utils.ts @@ -51,13 +51,26 @@ function mergeToVault(info: any, stat: any): Vault { const url = generateVaultURL(chain, address) const shareDecimals = new bn(contract?.decimals || 0) const tokens = [] + let rewardTokens = [] if (stat.latestInfo?.token0) tokens.push(getTokenData("0", info, stat)) if (stat.latestInfo?.token1) tokens.push(getTokenData("1", info, stat)) + if (info.rewardTokens){ + rewardTokens=info.rewardTokens.map((el)=>({ + address:el.address, + chain:chain, + decimals:el.decimals, + rewardBook:el.rewardBook, + symbol:el.symbol, + type:el.type + })) + }else { + rewardTokens=[] + } const data = { address, chain, name, vaultMeta, underlyingTokens: tokens, url, - isAsset0Main: info.isAsset0Main, shareDecimals + isAsset0Main: info.isAsset0Main, shareDecimals,rewardTokens:rewardTokens } return data } @@ -164,6 +177,7 @@ async function addLiquidityData(vault: Vault, interval: number): Promise const chain = vault.chain const tokenName = vault.isAsset0Main ? "token0" : "token1" const token = getUnderlyingToken(tokenName, vault) + const rewardTokens = vault.rewardTokens const { tvl, shareSupply } = await getLiquidityData(vault) @@ -176,14 +190,33 @@ async function addLiquidityData(vault: Vault, interval: number): Promise const tokenKey = token.address.toLowerCase() const tokenDecimals = new bn(10).pow(token.decimals) const tvlUsd = new bn(tvl || 0).multipliedBy(prices[tokenKey]).div(tokenDecimals) + const rewardApy = await getRewardTokenApy(chain,rewardTokens,tvlUsd) vault.tvlUsd = tvlUsd vault.tvl = new bn(tvl || 0) vault.tvlBefore = new bn(tvlBefore || 0) vault.shareSupply = new bn(shareSupply || 0) vault.shareSupplyBefore = new bn(shareSupplyBefore || 0) + vault.rewardApy = rewardApy return vault } +async function getRewardTokenApy(chain:string,rewardTokens:RewardToken[],tvlUsd:bn):Promise{ + if (rewardTokens.length===0) return 0 + const addresses= rewardTokens.map((el)=>el.address.toLowerCase()) + const prices = (await utils.getPrices(addresses, chain)).pricesByAddress + const totalApr = rewardTokens.reduce((acc,el)=>{ + const rewardBook = el.rewardBook + const reward = new bn(rewardBook.totalReward) + const totalRewardUsd = reward.multipliedBy(prices[el.address.toLowerCase()]).div(new bn(10).pow(el.decimals)) + const apy = totalRewardUsd + .div(Number(rewardBook.endTime)-Number(rewardBook.startTime)) + .multipliedBy(365*24*60*60) + .div(tvlUsd).multipliedBy(100) + return acc.plus(apy) + },new bn(0)) + return totalApr.toNumber() +} + async function updateBeforeLiquidityData(vault: Vault, time: number): Promise { const chain = vault.chain const [blockBefore] = await utils.getBlocksByTime([time], chain) @@ -216,6 +249,7 @@ function calculateAPY(price: bn, priceBefore: bn, interval: number): number { function convertToPool(vault: Vault): Promise { const tokens = vault.underlyingTokens.map((el) => el.address) + const rewardTokens = vault.rewardTokens.map((el) => el.address) const pool = { pool: `${vault.address}-${vault.chain}`, chain: vault.chain, @@ -224,9 +258,9 @@ function convertToPool(vault: Vault): Promise { project: 'teahouse-permissionless', tvlUsd: vault.tvlUsd.toNumber(), apyBase: vault.apy, - apyReward: 0, + apyReward: vault.rewardApy, underlyingTokens: tokens, - rewardTokens: [], + rewardTokens: rewardTokens, poolMeta: vault.vaultMeta, } return pool; @@ -273,6 +307,7 @@ interface Vault { name: string; chain: string; underlyingTokens: Array; + rewardTokens: Array; isAsset0Main: boolean; url: string; vaultMeta: string; //other info @@ -283,6 +318,7 @@ interface Vault { shareSupply?: bn; shareSupplyBefore?: bn; apy?: number; + rewardApy?: number; } interface UnderlyingToken { @@ -294,6 +330,22 @@ interface UnderlyingToken { shareTokenApr: number; } +interface RewardBook { + address: string; + endTime: string; + startTime: string; + totalReward: string; +} + +interface RewardToken { + address: string; + chain: string; + decimals: number; + rewardBook: RewardBook; + symbol: string; + type: string; +} + interface Performance { tvlUsd: number; apy: number;