Skip to content

Commit

Permalink
wip: additional APR
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed Feb 14, 2024
1 parent 8a6753d commit 239eb63
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 45 deletions.
24 changes: 4 additions & 20 deletions components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,11 @@ export default function Table({ selectedChain, allianceAssets, prices }: TablePr
<tr key={row.denom}>
<td className="flex justify-start lg:justify-center pt-4"><TableIcon row={row} chain={tableState.selectedChain} /></td>
<td className="text-center lg:text-center pt-4">{tableState.getAllianceAssetName(row.denom)}</td>
<td className="text-center lg:text-center pt-4">{tableState.getTotalTokens(row.denom) }</td>
<td className="text-center lg:text-center pt-4">${tableState.getTotalValueStaked(row.denom)}</td>
<td className="text-center lg:text-center pt-4">
{toLocaleString(getTakeRate(row, tableState.chainParams?.take_rate_claim_interval as string) * 100)}%
</td>
<td className="text-center lg:text-center pt-4">{toLocaleString(tableState.getTotalTokens(row.denom))}</td>
<td className="text-center lg:text-center pt-4">${toLocaleString(tableState.getTotalValueStaked(row.denom))}</td>
<td className="text-center lg:text-center pt-4">{toLocaleString(tableState.getTakeRate(row.denom))}%</td>
<td className="text-center lg:text-center pt-4">{toLocaleString(parseFloat(row.reward_weight) * 100)}%</td>
<td className="text-center lg:text-center pt-4">
{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
)
)}
%
</td>
<td className="text-center lg:text-center pt-4">{toLocaleString(tableState.getAdditionalYield(row.denom))}%</td>
</tr>
))}
</tbody>
Expand Down
3 changes: 3 additions & 0 deletions const/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
12 changes: 7 additions & 5 deletions models/AllianceFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -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
);
};

Expand Down
3 changes: 3 additions & 0 deletions models/Chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export class Chain implements ChainModel {
id: string;
bondDenom: string;
bondDenomPriceKey: string;
name: string;
decimals: number;
icon: string;
Expand All @@ -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]
Expand All @@ -40,6 +42,7 @@ export class Chain implements ChainModel {
export interface ChainModel {
id: string;
bondDenom: string;
bondDenomPriceKey: string;
name: string;
decimals: number;
icon: string;
Expand Down
76 changes: 56 additions & 20 deletions models/TableState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
});
}

0 comments on commit 239eb63

Please sign in to comment.