Skip to content

Commit 147f58a

Browse files
authored
feat: use v3 STX supply endpoint(#2861)
1 parent 6d761ea commit 147f58a

8 files changed

Lines changed: 86 additions & 60 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { system } from '@/ui/theme/theme';
2+
import { ChakraProvider } from '@chakra-ui/react';
3+
import '@testing-library/jest-dom';
4+
import { render, screen } from '@testing-library/react';
5+
import { ReactNode } from 'react';
6+
7+
import { useSuspenseStxSupply } from '../../../../../common/queries/useStxSupply';
8+
import { StxSupply } from '../index';
9+
10+
jest.mock('../../../../../common/queries/useStxSupply', () => ({
11+
useSuspenseStxSupply: jest.fn(),
12+
}));
13+
jest.mock('../../../../../common/context/useGlobalContext', () => ({
14+
useGlobalContext: () => ({ activeNetwork: { mode: 'mainnet' } }),
15+
}));
16+
jest.mock('../../../../../ui/Tooltip', () => ({
17+
Tooltip: ({ content, children }: { content: string; children: ReactNode }) => (
18+
<div title={content}>{children}</div>
19+
),
20+
}));
21+
jest.mock('../../StatSection', () => ({
22+
StatSection: (props: Record<string, ReactNode>) => (
23+
<div>
24+
{props.title}
25+
<span data-testid="main">{props.bodyMainText}</span>
26+
<span data-testid="secondary">{props.bodySecondaryText}</span>
27+
<span data-testid="caption">{props.caption}</span>
28+
</div>
29+
),
30+
}));
31+
jest.mock('../../../ErrorBoundary', () => ({
32+
ExplorerErrorBoundary: ({ children }: { children: ReactNode }) => <>{children}</>,
33+
}));
34+
35+
const mockSupply = useSuspenseStxSupply as jest.MockedFunction<typeof useSuspenseStxSupply>;
36+
37+
test('renders the v3 µSTX total as both circulating and total supply', () => {
38+
mockSupply.mockReturnValue({
39+
data: { total: '1874370687477029', projected_total_2050: '2318000000000000' },
40+
} as ReturnType<typeof useSuspenseStxSupply>);
41+
42+
render(
43+
<ChakraProvider value={system}>
44+
<StxSupply />
45+
</ChakraProvider>
46+
);
47+
48+
expect(screen.getByTestId('main')).toHaveTextContent('1.87B');
49+
expect(screen.getByTestId('secondary')).toHaveTextContent('1.87B');
50+
expect(screen.getByTestId('caption')).toHaveTextContent('100% of total supply is circulating');
51+
expect(screen.getByTitle(/Year 2050 supply is ~2.32B/)).toBeInTheDocument();
52+
});

‎src/app/_components/Stats/StxSupply/index.tsx‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import { Info } from '@phosphor-icons/react';
55

66
import { useGlobalContext } from '../../../../common/context/useGlobalContext';
77
import { useSuspenseStxSupply } from '../../../../common/queries/useStxSupply';
8-
import { abbreviateNumber } from '../../../../common/utils/utils';
8+
import { MICROSTACKS_IN_STACKS, abbreviateNumber } from '../../../../common/utils/utils';
99
import { Tooltip } from '../../../../ui/Tooltip';
1010
import { ExplorerErrorBoundary } from '../../ErrorBoundary';
1111
import { StatSection } from '../StatSection';
1212

1313
function StxSupplyBase(props: StackProps) {
1414
const {
15-
data: { total_stx, unlocked_stx, total_stx_year_2050 },
15+
data: { total, projected_total_2050 },
1616
} = useSuspenseStxSupply();
17-
const circulatingSupplyNumber = unlocked_stx ? Number(unlocked_stx) : 0;
18-
const circulatingSupplyFormatted = abbreviateNumber(circulatingSupplyNumber);
19-
const totalSupplyNumber = total_stx ? Number(total_stx) : 0;
17+
// All STX is unlocked, so the circulating supply is the total supply.
18+
const totalSupplyNumber = total ? Number(total) / MICROSTACKS_IN_STACKS : 0;
2019
const totalSupplyFormatted = abbreviateNumber(totalSupplyNumber);
21-
const maxSupplyBy2050Number = total_stx_year_2050 ? Number(total_stx_year_2050) : 0;
20+
const maxSupplyBy2050Number = projected_total_2050
21+
? Number(projected_total_2050) / MICROSTACKS_IN_STACKS
22+
: 0;
2223
const maxSupplyBy2050Formatted = abbreviateNumber(maxSupplyBy2050Number);
23-
const percentageUnlocked = ((circulatingSupplyNumber / totalSupplyNumber) * 100).toFixed(1);
2424
const isMainnet = useGlobalContext().activeNetwork.mode === 'mainnet';
2525

2626
return (
@@ -42,7 +42,7 @@ function StxSupplyBase(props: StackProps) {
4242
)}
4343
</Flex>
4444
}
45-
bodyMainText={circulatingSupplyFormatted}
45+
bodyMainText={totalSupplyFormatted}
4646
bodySecondaryText={
4747
<Flex alignItems={'center'} gap={1}>
4848
<span>/</span>
@@ -51,7 +51,7 @@ function StxSupplyBase(props: StackProps) {
5151
}
5252
caption={
5353
<>
54-
<b>{percentageUnlocked}%</b> of total supply is circulating
54+
<b>100%</b> of total supply is circulating
5555
</>
5656
}
5757
{...props}

‎src/app/stx/__tests__/data.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** @jest-environment node */
2+
import { stacksAPIFetchJson } from '@/api/stacksAPIFetch';
3+
4+
import { fetchStxSupply } from '../data';
5+
6+
jest.mock('@/api/stacksAPIFetch', () => ({ stacksAPIFetchJson: jest.fn() }));
7+
8+
const fetchJsonMock = stacksAPIFetchJson as jest.MockedFunction<typeof stacksAPIFetchJson>;
9+
10+
test('fetchStxSupply reads the v3 STX supply endpoint', async () => {
11+
const supply = { total: '1874370687477029', projected_total_2050: '2318000000000000' };
12+
fetchJsonMock.mockResolvedValue(supply);
13+
14+
await expect(fetchStxSupply('https://api.hiro.so')).resolves.toEqual(supply);
15+
expect(fetchJsonMock.mock.calls[0][0]).toBe('https://api.hiro.so/extended/v3/tokens/stx/supply');
16+
});

‎src/app/stx/__tests__/utils.test.ts‎

Lines changed: 0 additions & 22 deletions
This file was deleted.

‎src/app/stx/data.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import { stacksAPIFetchJson } from '@/api/stacksAPIFetch';
22

3+
import { OperationResponse } from '@stacks/blockchain-api-client';
34
import { FungibleTokenHolderList } from '@stacks/stacks-blockchain-api-types';
45

56
import { STX_ASSET_ID } from './consts';
67

78
const STX_SUPPLY_REVALIDATION_SECONDS = 60;
89
const STX_HOLDERS_REVALIDATION_SECONDS = 60;
910

10-
export interface StxSupplyResponse {
11-
unlocked_percent: string;
12-
total_stx: string;
13-
total_stx_year_2050: string;
14-
unlocked_stx: string;
15-
block_height: number;
16-
}
11+
export type StxSupplyResponse = OperationResponse['/extended/v3/tokens/stx/supply'];
1712

1813
export async function fetchStxSupply(apiUrl: string): Promise<StxSupplyResponse> {
1914
return stacksAPIFetchJson<StxSupplyResponse>(
20-
`${apiUrl}/extended/v1/stx_supply`,
15+
`${apiUrl}/extended/v3/tokens/stx/supply`,
2116
{ cache: 'default', next: { revalidate: STX_SUPPLY_REVALIDATION_SECONDS } },
2217
'Failed to fetch STX supply'
2318
);

‎src/app/stx/page.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
STX_SYMBOL,
2121
} from './consts';
2222
import { fetchStxHolders, fetchStxSupply } from './data';
23-
import { stxToMicro } from './utils';
2423

2524
export default async function (props: { searchParams: Promise<CommonSearchParams> }) {
2625
const searchParams = await props.searchParams;
@@ -67,7 +66,7 @@ export default async function (props: { searchParams: Promise<CommonSearchParams
6766
symbol: STX_SYMBOL,
6867
imageUri: STX_LOGO_URL,
6968
decimals: STX_DECIMALS,
70-
totalSupply: stxToMicro(supply?.total_stx),
69+
totalSupply: supply ? Number(supply.total) : undefined,
7170
};
7271
} catch (error) {
7372
logError(error as Error, 'STX token page server-side fetch', { chain, api }, 'error');

‎src/app/stx/utils.ts‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎src/common/queries/useStxSupply.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import { useSuspenseQuery } from '@tanstack/react-query';
22

3+
import { OperationResponse } from '@stacks/blockchain-api-client';
4+
35
import { useGlobalContext } from '../context/useGlobalContext';
46

5-
interface StxSupplyResponse {
6-
unlocked_percent: string;
7-
total_stx: string;
8-
total_stx_year_2050: string;
9-
unlocked_stx: string;
10-
block_height: number;
11-
}
7+
type StxSupplyResponse = OperationResponse['/extended/v3/tokens/stx/supply'];
128

139
export const useSuspenseStxSupply = () => {
1410
const { url: activeNetworkUrl } = useGlobalContext().activeNetwork;
1511
return useSuspenseQuery<StxSupplyResponse>({
1612
queryKey: ['stx-supply'],
17-
queryFn: () => fetch(`${activeNetworkUrl}/extended/v1/stx_supply`).then(res => res.json()),
13+
queryFn: () =>
14+
fetch(`${activeNetworkUrl}/extended/v3/tokens/stx/supply`).then(res => res.json()),
1815
staleTime: 30 * 60 * 1000,
1916
});
2017
};

0 commit comments

Comments
 (0)