From 239eb637129e37369f6e9350ddfdc00a7fc48943 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Wed, 14 Feb 2024 19:19:08 +0200 Subject: [PATCH] wip: additional APR --- components/Table.tsx | 24 ++---------- const/chains.ts | 3 ++ models/AllianceFunctions.ts | 12 +++--- models/Chain.ts | 3 ++ models/TableState.ts | 76 +++++++++++++++++++++++++++---------- 5 files changed, 73 insertions(+), 45 deletions(-) diff --git a/components/Table.tsx b/components/Table.tsx index 25f5595..f2b9c45 100644 --- a/components/Table.tsx +++ b/components/Table.tsx @@ -70,27 +70,11 @@ export default function Table({ selectedChain, allianceAssets, prices }: TablePr {tableState.getAllianceAssetName(row.denom)} - {tableState.getTotalTokens(row.denom) } - ${tableState.getTotalValueStaked(row.denom)} - - {toLocaleString(getTakeRate(row, tableState.chainParams?.take_rate_claim_interval as string) * 100)}% - + {toLocaleString(tableState.getTotalTokens(row.denom))} + ${toLocaleString(tableState.getTotalValueStaked(row.denom))} + {toLocaleString(tableState.getTakeRate(row.denom))}% {toLocaleString(parseFloat(row.reward_weight) * 100)}% - - {toLocaleString( - getAdditionalYield( - row, - tableState.totalSupply?.amount.toNumber() as number, - tableState.selectedChain?.id, - tableState.inflation.toNumber(), - tableState.getTotalRewardWeight(), - tableState.chainParams?.take_rate_claim_interval as string, - prices, - tableState.selectedChain?.decimals - ) - )} - % - + {toLocaleString(tableState.getAdditionalYield(row.denom))}% ))} diff --git a/const/chains.ts b/const/chains.ts index 21804f6..3f14358 100644 --- a/const/chains.ts +++ b/const/chains.ts @@ -6,6 +6,7 @@ export const SUPPORTED_CHAINS: { [key: string]: Chain } = { "carbon-1": Chain.fromAny({ id: "carbon-1", bondDenom: "swth", + bondDenomPriceKey: "SWTH", name: "Carbon", decimals: 8, icon: "https://raw.githubusercontent.com/terra-money/station-assets/main/img/chains/Carbon.svg", @@ -27,6 +28,7 @@ export const SUPPORTED_CHAINS: { [key: string]: Chain } = { "migaloo-1": Chain.fromAny({ id: "migaloo-1", bondDenom: "uwhale", + bondDenomPriceKey: "WHALE", name: "Migaloo", decimals: 6, allianceHub: { @@ -73,6 +75,7 @@ export const SUPPORTED_CHAINS: { [key: string]: Chain } = { "phoenix-1": Chain.fromAny({ id: "phoenix-1", bondDenom: "uluna", + bondDenomPriceKey: "LUNA", name: "Terra", decimals: 6, allianceHub: { diff --git a/models/AllianceFunctions.ts b/models/AllianceFunctions.ts index 7e0a534..edeb971 100644 --- a/models/AllianceFunctions.ts +++ b/models/AllianceFunctions.ts @@ -27,7 +27,7 @@ const getNativeUsdValue = (totalSupplyAmount: number, chain: string, usdValues: return ((value ? value.usd : 0) * totalSupplyAmount) / 10 ** decimals; }; -const lsdLosePerYear = (row: AllianceAsset, chain: string, takeRate: string, usdValues: any) => { +const calcLsdLosePerYear = (row: AllianceAsset, chain: string, takeRate: string, usdValues: any) => { const usdStaked = getLsdUsdValue(row, chain, usdValues); return usdStaked * getTakeRate(row, takeRate); }; @@ -60,11 +60,13 @@ export const getAdditionalYield = ( decimals: number ) => { const usdStaked = getLsdUsdValue(row, chain, usdValues); + console.log("usdStaked", usdStaked) + const annualRewardsToNativeStakers = annualRewardsToLunaStakers(row, totalSupplyAmount, chain, inflation, totalRewardWeight, usdValues, decimals); + console.log("annualRewardsToNativeStakers", annualRewardsToNativeStakers) + const lsdLosePerYear = calcLsdLosePerYear(row, chain, takeRate, usdValues); + console.log("lsdLosePerYear", lsdLosePerYear) return ( - (100 * - (annualRewardsToLunaStakers(row, totalSupplyAmount, chain, inflation, totalRewardWeight, usdValues, decimals) - - lsdLosePerYear(row, chain, takeRate, usdValues))) / - usdStaked + (100 * (annualRewardsToNativeStakers - lsdLosePerYear)) / usdStaked ); }; diff --git a/models/Chain.ts b/models/Chain.ts index 11dfead..67e5c4e 100644 --- a/models/Chain.ts +++ b/models/Chain.ts @@ -3,6 +3,7 @@ export class Chain implements ChainModel { id: string; bondDenom: string; + bondDenomPriceKey: string; name: string; decimals: number; icon: string; @@ -21,6 +22,7 @@ export class Chain implements ChainModel { this.icon = model.icon; this.allianceCoins = {}; this.allianceHub = model.allianceHub; + this.bondDenomPriceKey = model.bondDenomPriceKey for (const key in model.allianceCoins) { let coin = model.allianceCoins[key] @@ -40,6 +42,7 @@ export class Chain implements ChainModel { export interface ChainModel { id: string; bondDenom: string; + bondDenomPriceKey: string; name: string; decimals: number; icon: string; diff --git a/models/TableState.ts b/models/TableState.ts index 784867c..61ef4cd 100644 --- a/models/TableState.ts +++ b/models/TableState.ts @@ -4,7 +4,7 @@ import { Chain } from "./Chain"; import { DEFAULT_CHAIN, SUPPORTED_CHAINS } from "../const/chains"; import { Prices } from "./Prices"; - +const SECONDS_IN_YEAR = 31_536_000; export default class TableState { private _totalRewardWeight: number = 0; @@ -51,43 +51,79 @@ export default class TableState { return this.selectedChain.allianceCoins[denom]?.name } - getTotalTokens = (denom: string): string => { + getTotalTokens = (denom: string): number => { const totalTokens = this.allianceAssets?.find((asset) => asset.denom === denom)?.total_tokens; if (totalTokens === undefined) { - return "-" + return 0 } - return this._toLocalString(parseInt(totalTokens) / 1_000_000); + return parseInt(totalTokens) / 1_000_000; } - getTotalValueStaked = (denom: string): string => { + getTotalValueStaked = (denom: string): number => { const priceKey = this.selectedChain.allianceCoins[denom]?.priceKey; - console.log("priceKey",priceKey) if (priceKey === undefined) { - return "-" + return 0 } - console.log(this.prices) const usdPrice = this.prices[priceKey]?.usd; - console.log("usdPrice",usdPrice) if (usdPrice === undefined) { - return "-" + return 0 } const totalTokens = this.allianceAssets?.find((asset) => asset.denom === denom)?.total_tokens; - console.log("totalTokens",totalTokens) if (totalTokens === undefined) { - return "-" + return 0 } - - // toLocaleString(getLsdUsdValue(row, tableState.selectedChain?.id, prices)) - return this._toLocalString(usdPrice * (parseInt(totalTokens) / 1_000_000)) + return usdPrice * (parseInt(totalTokens) / 1_000_000) } - private _toLocalString(n: number): string { - return n.toLocaleString("en-US", { - maximumFractionDigits: 2, - minimumFractionDigits: 2, - }); + getTakeRate = (denom: string): number => { + if (this.chainParams?.take_rate_claim_interval === undefined) { + return 0 + } + const takeRateClaimInterval = parseInt(this.chainParams.take_rate_claim_interval); + const denomTakeRate = this.allianceAssets?.find((asset) => asset.denom === denom)?.take_rate; + if (denomTakeRate === undefined) { + return 0 + } + const takeRate = 1 - (1 - parseFloat(denomTakeRate)) ** (SECONDS_IN_YEAR / takeRateClaimInterval); + return takeRate * 100; + }; + + getAdditionalYield = (denom: string): number => { + const asset = this.allianceAssets?.find((asset) => asset.denom === denom); + if (asset === undefined) { + return 0 + } + const nativeTokenPrice = this.prices[this.selectedChain.bondDenomPriceKey]?.usd; + const nativeTokenTotalSupply = new Dec(this.totalSupply?.amount); + const nativeTokenMarketCap = new Dec(nativeTokenPrice).mul(nativeTokenTotalSupply).div(10 ** this.selectedChain.decimals); + + let totalAssetStakedInUSD = new Dec(this.getTotalValueStaked(denom)); + console.log("totalAssetStakedInUSD", totalAssetStakedInUSD.toString()) + if (totalAssetStakedInUSD.isNaN()) { + return 0 + } + + const assetRewardWeight = new Dec(asset.reward_weight); + const annualRewardsToNativeStakers = nativeTokenMarketCap.mul(this.inflation) + .mul(assetRewardWeight.div(1 + this._totalRewardWeight)); + console.log("annualRewardsToNativeStakers", annualRewardsToNativeStakers.toString()) + + const lsdLosePerYear = totalAssetStakedInUSD.mul(this.getTakeRate(denom)); + console.log("lsdLosePerYear", lsdLosePerYear.toString()) + const a = lsdLosePerYear.minus(assetRewardWeight).mul(100); + console.log("a", a.toString()) + console.log("final", a.div(totalAssetStakedInUSD).toString()) + console.log("final", a.div(totalAssetStakedInUSD).toNumber()) + return a.div(totalAssetStakedInUSD).toNumber(); } +} + +export function toLocalString(n: number): string { + return n.toLocaleString("en-US", { + maximumFractionDigits: 2, + minimumFractionDigits: 2, + }); } \ No newline at end of file