Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat teahouse reward apy #1076

Merged
merged 15 commits into from
Nov 13, 2023
Merged
58 changes: 55 additions & 3 deletions src/adaptors/teahouse-permissionless/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -164,6 +177,7 @@ async function addLiquidityData(vault: Vault, interval: number): Promise<Vault>
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)
Expand All @@ -176,14 +190,33 @@ async function addLiquidityData(vault: Vault, interval: number): Promise<Vault>
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<number>{
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<Vault> {
const chain = vault.chain
const [blockBefore] = await utils.getBlocksByTime([time], chain)
Expand Down Expand Up @@ -216,6 +249,7 @@ function calculateAPY(price: bn, priceBefore: bn, interval: number): number {

function convertToPool(vault: Vault): Promise<Pool> {
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,
Expand All @@ -224,9 +258,9 @@ function convertToPool(vault: Vault): Promise<Pool> {
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;
Expand Down Expand Up @@ -273,6 +307,7 @@ interface Vault {
name: string;
chain: string;
underlyingTokens: Array<UnderlyingToken>;
rewardTokens: Array<RewardToken>;
isAsset0Main: boolean;
url: string;
vaultMeta: string; //other info
Expand All @@ -283,6 +318,7 @@ interface Vault {
shareSupply?: bn;
shareSupplyBefore?: bn;
apy?: number;
rewardApy?: number;
}

interface UnderlyingToken {
Expand All @@ -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;
Expand Down
Loading