Skip to content

Commit fd4414b

Browse files
committed
[BLD-71] SDK, Dashboard: Fix Supplies shown in ERC721 contract (#7803)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR updates the `getTotalClaimedSupply` and `getTotalUnclaimedSupply` functions in the ERC721 extensions to account for `startTokenId`, improving the accuracy of token supply calculations. ### Detailed summary - Modified `getTotalClaimedSupply` to subtract `startTokenId` from `nextTokenIdToClaim`. - Updated `getTotalUnclaimedSupply` to subtract `startTokenId` and `totalClaimedSupply` from `nextTokenIdToMint`. - Changed imports in `supply-cards.tsx` to use the new supply functions. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Improved supply calculations in NFT dashboards by directly using contract data instead of manual computations. * Adjusted logic to accurately account for token ID offsets when displaying claimed and unclaimed NFT supply. * **Bug Fixes** * Resolved inconsistencies in claimed and unclaimed NFT supply values when contracts use a custom starting token ID. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent d10436d commit fd4414b

File tree

4 files changed

+35
-29
lines changed

4 files changed

+35
-29
lines changed

.changeset/lemon-mammals-slide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Update `getTotalClaimedSupply` and `getTotalUnclaimedSupply` erc721 extensions to consider `startTokenId` if the contract has `startTokenId` set.

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/nfts/components/supply-cards.tsx

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
"use client";
2-
import { useMemo } from "react";
32
import type { ThirdwebContract } from "thirdweb";
43
import {
5-
nextTokenIdToMint,
6-
startTokenId,
7-
totalSupply,
4+
getTotalClaimedSupply,
5+
totalSupply as getTotalSupply,
6+
getTotalUnclaimedSupply,
87
} from "thirdweb/extensions/erc721";
98
import { useReadContract } from "thirdweb/react";
109
import { StatCard } from "../../overview/components/stat-card";
@@ -14,42 +13,34 @@ interface SupplyCardsProps {
1413
}
1514

1615
export const SupplyCards: React.FC<SupplyCardsProps> = ({ contract }) => {
17-
const nextTokenIdQuery = useReadContract(nextTokenIdToMint, {
16+
const totalSupplyQuery = useReadContract(getTotalSupply, {
1817
contract,
1918
});
2019

21-
const totalSupplyQuery = useReadContract(totalSupply, {
20+
const totalClaimedSupplyQuery = useReadContract(getTotalClaimedSupply, {
2221
contract,
2322
});
2423

25-
const startTokenIdQuery = useReadContract(startTokenId, { contract });
26-
27-
const realTotalSupply = useMemo(
28-
() => (nextTokenIdQuery.data || 0n) - (startTokenIdQuery.data || 0n),
29-
[nextTokenIdQuery.data, startTokenIdQuery.data],
30-
);
31-
32-
const unclaimedSupply = useMemo(
33-
() => (realTotalSupply - (totalSupplyQuery?.data || 0n)).toString(),
34-
[realTotalSupply, totalSupplyQuery.data],
35-
);
24+
const totalUnClaimedSupplyQuery = useReadContract(getTotalUnclaimedSupply, {
25+
contract,
26+
});
3627

3728
return (
3829
<div className="grid grid-cols-1 gap-4 lg:grid-cols-3">
3930
<StatCard
40-
isPending={nextTokenIdQuery.isPending}
31+
isPending={totalSupplyQuery.isPending}
4132
label="Total Supply"
42-
value={realTotalSupply.toString()}
33+
value={totalSupplyQuery.data?.toString() || "N/A"}
4334
/>
4435
<StatCard
45-
isPending={totalSupplyQuery.isPending}
36+
isPending={totalClaimedSupplyQuery.isPending}
4637
label="Claimed Supply"
47-
value={totalSupplyQuery?.data?.toString() || "N/A"}
38+
value={totalClaimedSupplyQuery.data?.toString() || "N/A"}
4839
/>
4940
<StatCard
50-
isPending={totalSupplyQuery.isPending || nextTokenIdQuery.isPending}
41+
isPending={totalUnClaimedSupplyQuery.isPending}
5142
label="Unclaimed Supply"
52-
value={unclaimedSupply}
43+
value={totalUnClaimedSupplyQuery.data?.toString() || "N/A"}
5344
/>
5445
</div>
5546
);

packages/thirdweb/src/extensions/erc721/read/getTotalClaimedSupply.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BaseTransactionOptions } from "../../../transaction/types.js";
22
import { nextTokenIdToClaim } from "../__generated__/IDrop/read/nextTokenIdToClaim.js";
3+
import { startTokenId } from "../__generated__/IERC721A/read/startTokenId.js";
34

45
/**
56
* Retrieves the total claimed supply of ERC721 tokens.
@@ -20,5 +21,10 @@ import { nextTokenIdToClaim } from "../__generated__/IDrop/read/nextTokenIdToCla
2021
export async function getTotalClaimedSupply(
2122
options: BaseTransactionOptions,
2223
): Promise<bigint> {
23-
return nextTokenIdToClaim(options);
24+
const [startTokenId_, nextTokenIdToClaim_] = await Promise.all([
25+
startTokenId(options).catch(() => 0n),
26+
nextTokenIdToClaim(options),
27+
]);
28+
29+
return nextTokenIdToClaim_ - startTokenId_;
2430
}

packages/thirdweb/src/extensions/erc721/read/getTotalUnclaimedSupply.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { BaseTransactionOptions } from "../../../transaction/types.js";
2+
import { startTokenId } from "../__generated__/IERC721A/read/startTokenId.js";
23
import { nextTokenIdToMint } from "../__generated__/IERC721Enumerable/read/nextTokenIdToMint.js";
34
import { getTotalClaimedSupply } from "./getTotalClaimedSupply.js";
45

@@ -19,9 +20,12 @@ import { getTotalClaimedSupply } from "./getTotalClaimedSupply.js";
1920
export async function getTotalUnclaimedSupply(
2021
options: BaseTransactionOptions,
2122
): Promise<bigint> {
22-
const [nextTokenIdToMint_, totalClaimedSupply_] = await Promise.all([
23-
nextTokenIdToMint(options),
24-
getTotalClaimedSupply(options),
25-
]);
26-
return nextTokenIdToMint_ - totalClaimedSupply_;
23+
const [startTokenId_, nextTokenIdToMint_, totalClaimedSupply_] =
24+
await Promise.all([
25+
startTokenId(options).catch(() => 0n),
26+
nextTokenIdToMint(options),
27+
getTotalClaimedSupply(options),
28+
]);
29+
30+
return nextTokenIdToMint_ - startTokenId_ - totalClaimedSupply_;
2731
}

0 commit comments

Comments
 (0)