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

add btc usd price #440

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
}

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
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
? ` ($${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;
}