Skip to content

Commit

Permalink
Feat teahouse reward apy (#1076)
Browse files Browse the repository at this point in the history
* feat: add teahouse yield adapter

* remove borrow fields

* feat: calculate on-chain APY

* add abi

* rename pool name

* remove the borrowing fields

* feat: update the apy base

* feat: Add the calculation for the reward token.

---------

Co-authored-by: Wade <[email protected]>
  • Loading branch information
daiwanwei and Wade authored Nov 13, 2023
1 parent 7ea5bc1 commit d5341e3
Showing 1 changed file with 55 additions and 3 deletions.
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

0 comments on commit d5341e3

Please sign in to comment.