Skip to content

Commit

Permalink
add btc usd price (#440)
Browse files Browse the repository at this point in the history
* add btc usd price

* resolve comments

* resolve comments

* bump version
  • Loading branch information
jeremy-babylonlabs authored Dec 5, 2024
1 parent 6021c6d commit 01538d6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-staking",
"version": "0.3.15",
"version": "0.3.16",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/getStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface StatsAPI {
total_delegations: number;
total_stakers: number;
unconfirmed_tvl: number;
btc_price_usd: number;
}

export const getStats = async (): Promise<StakingStats> => {
Expand All @@ -27,5 +28,6 @@ export const getStats = async (): Promise<StakingStats> => {
totalDelegations: statsAPI.total_delegations,
totalStakers: statsAPI.total_stakers,
unconfirmedTVLSat: statsAPI.unconfirmed_tvl,
btcPriceUsd: statsAPI.btc_price_usd,
};
};
11 changes: 10 additions & 1 deletion src/app/components/Stats/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useBtcHeight } from "@/app/context/mempool/BtcHeightProvider";
import { GlobalParamsVersion } from "@/app/types/globalParams";
import { getNetworkConfig } from "@/config/network.config";
import { satoshiToBtc } from "@/utils/btcConversions";
import { formatAmount } from "@/utils/formatAmount";
import {
ParamsWithContext,
getCurrentGlobalParamsVersion,
Expand Down Expand Up @@ -82,6 +83,7 @@ export const Stats: React.FC = () => {
totalDelegations: 0,
totalStakers: 0,
unconfirmedTVLSat: 0,
btcPriceUsd: 0,
});
const [stakingCapText, setStakingCapText] = useState<{
title: string;
Expand Down Expand Up @@ -135,7 +137,14 @@ export const Stats: React.FC = () => {
{
title: "Confirmed TVL",
value: stakingStats?.activeTVLSat
? `${maxDecimals(satoshiToBtc(stakingStats.activeTVLSat), 2)} ${coinName}`
? `${maxDecimals(satoshiToBtc(stakingStats.activeTVLSat), 2)} ${coinName}${
stakingStats.btcPriceUsd
? ` ($${formatAmount(
satoshiToBtc(stakingStats.activeTVLSat) *
stakingStats.btcPriceUsd,
)})`
: ""
}`
: 0,
icon: confirmedTvl,
},
Expand Down
1 change: 1 addition & 0 deletions src/app/context/api/StakingStatsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface StakingStats {
totalDelegations: number;
totalStakers: number;
unconfirmedTVLSat: number;
btcPriceUsd?: number;
}

interface StakingStatsProviderProps {
Expand Down
1 change: 1 addition & 0 deletions src/app/types/stakingStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface StakingStats {
totalDelegations: number;
totalStakers: number;
unconfirmedTVLSat: number;
btcPriceUsd?: number;
}
35 changes: 35 additions & 0 deletions src/utils/formatAmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function formatAmount(
amount: number,
locale: string = "en-US",
currency?: string,
): string {
let abbreviated: string;
const formatter = new Intl.NumberFormat(locale, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
...(currency && { style: "currency", currency }),
});

const trillion = 1_000_000_000_000;
const billion = 1_000_000_000;
const million = 1_000_000;
const thousand = 1_000;

if (amount >= trillion) {
// Trillions
abbreviated = formatter.format(amount / trillion) + "T";
} else if (amount >= billion) {
// Billions
abbreviated = formatter.format(amount / billion) + "B";
} else if (amount >= million) {
// Millions
abbreviated = formatter.format(amount / million) + "M";
} else if (amount >= thousand) {
// Thousands
abbreviated = formatter.format(amount / thousand) + "k";
} else {
abbreviated = formatter.format(amount);
}

return abbreviated;
}

0 comments on commit 01538d6

Please sign in to comment.