Skip to content
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
53 changes: 26 additions & 27 deletions app/licenses/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ErrorComponent from '@/app/server-components/shared/ErrorComponent';
import { getLicensesTotalSupply } from '@/lib/api/blockchain';
import { LicenseItem } from '@/typedefs/general';
import { unstable_cache } from 'next/cache';
import List from '../server-components/Licenses/List';
import { BorderedCard } from '../server-components/shared/cards/BorderedCard';
import { CardHorizontal } from '../server-components/shared/cards/CardHorizontal';
import ErrorComponent from '@/app/server-components/shared/ErrorComponent';
import { getAllLicenseTokenIds } from '@/lib/api/blockchain';
import { LicenseItem } from '@/typedefs/general';
import { unstable_cache } from 'next/cache';
import List from '../server-components/Licenses/List';
import { BorderedCard } from '../server-components/shared/cards/BorderedCard';
import { CardHorizontal } from '../server-components/shared/cards/CardHorizontal';

export async function generateMetadata({ searchParams }: { searchParams?: Promise<{ page?: string }> }) {
const resolvedSearchParams = await searchParams;
Expand All @@ -22,7 +22,7 @@ export async function generateMetadata({ searchParams }: { searchParams?: Promis
};
}

const getCachedSupply = unstable_cache(getLicensesTotalSupply, ['licenses-total-supply'], { revalidate: 300 });
const getCachedLicenseTokenIds = unstable_cache(getAllLicenseTokenIds, ['licenses-token-ids'], { revalidate: 300 });

export default async function LicensesPage(props: {
searchParams?: Promise<{
Expand All @@ -32,25 +32,24 @@ export default async function LicensesPage(props: {
const searchParams = await props.searchParams;
const currentPage = Number(searchParams?.page) || 1;

let ndTotalSupply: number, mndTotalSupply: number;
let licenses: LicenseItem[];

try {
const { ndTotalSupply: ndTotalSupplyStr, mndTotalSupply: mndTotalSupplyStr } = await getCachedSupply();

ndTotalSupply = Number(ndTotalSupplyStr);
mndTotalSupply = Number(mndTotalSupplyStr);

licenses = [
...Array.from({ length: Number(mndTotalSupply) }, (_, i) => ({
licenseId: i + 1,
licenseType: i === 0 ? ('GND' as const) : ('MND' as const),
})),
...Array.from({ length: Number(ndTotalSupply) }, (_, i) => ({
licenseId: i + 1,
licenseType: 'ND' as const,
})),
];
let ndTotalSupply: number, mndTotalSupply: number;
let licenses: LicenseItem[];

try {
const { ndLicenseIds, mndLicenseIds } = await getCachedLicenseTokenIds();
ndTotalSupply = ndLicenseIds.length;
mndTotalSupply = mndLicenseIds.length;

licenses = [
...mndLicenseIds.map((licenseId) => ({
licenseId,
licenseType: licenseId === 1 ? ('GND' as const) : ('MND' as const),
})),
...ndLicenseIds.map((licenseId) => ({
licenseId,
licenseType: 'ND' as const,
})),
];
} catch (error) {
console.error(error);
console.log('[Licenses Page] Failed to fetch license data');
Expand Down
63 changes: 63 additions & 0 deletions lib/api/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,69 @@ export async function getLicensesTotalSupply(): Promise<{
};
}

const LICENSE_ENUMERATION_CHUNK_SIZE = 200;

const getEnumerableLicenseIds = async (
publicClient: Awaited<ReturnType<typeof getPublicClient>>,
address: types.EthAddress,
abi: typeof NDContractAbi | typeof MNDContractAbi,
totalSupply: bigint,
): Promise<number[]> => {
if (totalSupply === 0n) {
return [];
}

const total = Number(totalSupply);
const licenseIds: number[] = [];

for (let start = 0; start < total; start += LICENSE_ENUMERATION_CHUNK_SIZE) {
const end = Math.min(start + LICENSE_ENUMERATION_CHUNK_SIZE, total);
Comment on lines +293 to +297
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalSupply (bigint) is converted to a JS number via Number(totalSupply). If totalSupply ever exceeds Number.MAX_SAFE_INTEGER, this will silently lose precision and the loop will enumerate the wrong indices. Consider iterating using bigint counters (and only converting to number at the UI boundary), or at least guard/throw when totalSupply > BigInt(Number.MAX_SAFE_INTEGER) to avoid incorrect results.

Copilot uses AI. Check for mistakes.
const tokenIds = await publicClient.multicall({
contracts: Array.from({ length: end - start }, (_, index) => ({
address,
abi,
functionName: 'tokenByIndex' as const,
args: [BigInt(start + index)],
})),
allowFailure: false,
});

licenseIds.push(...tokenIds.map((tokenId) => Number(tokenId)));
}
Comment on lines +308 to +309
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokenId values are converted to JS number (Number(tokenId)), which can truncate/round for ids > 2^53-1 and produce wrong license ids (and wrong sorting). Prefer returning bigint[]/string[] from this API (matching the on-chain uint256), or validate that each tokenId fits in Number.MAX_SAFE_INTEGER before converting.

Copilot uses AI. Check for mistakes.

return licenseIds.sort((a, b) => a - b);
};

export async function getAllLicenseTokenIds(): Promise<{
mndLicenseIds: number[];
ndLicenseIds: number[];
}> {
const publicClient = await getPublicClient();

const [mndTotalSupply, ndTotalSupply] = await Promise.all([
publicClient.readContract({
address: config.mndContractAddress,
abi: MNDContractAbi,
functionName: 'totalSupply',
}),
publicClient.readContract({
address: config.ndContractAddress,
abi: NDContractAbi,
functionName: 'totalSupply',
}),
]);

const [mndLicenseIds, ndLicenseIds] = await Promise.all([
getEnumerableLicenseIds(publicClient, config.mndContractAddress, MNDContractAbi, mndTotalSupply),
getEnumerableLicenseIds(publicClient, config.ndContractAddress, NDContractAbi, ndTotalSupply),
]);

return {
mndLicenseIds,
ndLicenseIds,
};
}

export async function getLicenseHolders(licenseType: 'ND' | 'MND' | 'GND'): Promise<
{
ownerOf: EvmAddress | undefined;
Expand Down
Loading