Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit b23749a

Browse files
committed
Add the NFT supply hook implementation
Testing is still outstanding.
1 parent 1690dc0 commit b23749a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

frontend/src/hooks/useDevNFTSupply.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { useEffect, useState } from 'react';
2+
import { ethers } from 'ethers';
3+
4+
import {
5+
DEVELOPER_DAO_CONTRACT,
6+
DEVELOPER_DAO_CONTRACT_ABI,
7+
} from '../utils/DeveloperDaoConstants';
8+
import {
9+
AlchemyProvider,
10+
FallbackProvider,
11+
InfuraProvider,
12+
} from '@ethersproject/providers';
13+
14+
// TokenIDs start at 1. Tokens 1-7777 (supply of 7777) are open to mint by anyone. 7778-8000 are locked to mint by contract owner
15+
const MAX_SUPPLY = 8000;
16+
const PUBLIC_MAX_SUPPLY = 7777;
17+
18+
const contract = new ethers.Contract(
19+
DEVELOPER_DAO_CONTRACT,
20+
DEVELOPER_DAO_CONTRACT_ABI,
21+
new FallbackProvider([
22+
{ provider: new InfuraProvider() },
23+
{ provider: new AlchemyProvider() },
24+
]),
25+
);
26+
27+
export default function useDevNFTSupply() {
28+
const [totalSupply, setTotalSupply] = useState<number>(-1);
29+
const [lockedSupply, setLockedSupply] = useState<number>(-1);
30+
31+
useEffect(() => {
32+
// Fetches the count of minted DEV NFTs
33+
const fetchTotalSupply = async () => {
34+
const totalSupply: number = (
35+
await contract.functions.totalSupply()
36+
)[0].toNumber();
37+
38+
setTotalSupply(totalSupply);
39+
};
40+
41+
// Counts how many tokens of the locked supply have been minted
42+
const fetchLockedSupply = async () => {
43+
const requests = [];
44+
45+
for (
46+
let lockedTokenId = PUBLIC_MAX_SUPPLY + 1;
47+
lockedTokenId <= MAX_SUPPLY;
48+
lockedTokenId++
49+
) {
50+
requests.push(
51+
contract.functions.ownerOf(lockedTokenId).then(
52+
// Return the owner's address
53+
(addressResponse) => addressResponse[0],
54+
// Return the zero-address if no owner was found
55+
() => ethers.constants.AddressZero,
56+
),
57+
);
58+
}
59+
60+
const addresses = await Promise.all(requests);
61+
62+
const countMinted = addresses.reduce(
63+
(count, address) =>
64+
address !== ethers.constants.AddressZero ? ++count : count,
65+
0,
66+
);
67+
68+
setLockedSupply(countMinted);
69+
};
70+
71+
fetchTotalSupply();
72+
fetchLockedSupply();
73+
}, []);
74+
75+
return {
76+
loading: totalSupply === -1 || lockedSupply === -1,
77+
// This is the amount of total minted DEV NFTs, same as calling `totalSupply()` on the contract
78+
totalSupply,
79+
// This is the amount of minted DEV NFTs that only the contract owner can mint (TokenIDs 7778-8000, incl.)
80+
lockedSupply,
81+
// This is the amount of publicly minted DEV NFTs, which is open to mint by anyone (TokenIDs 1-7777, incl.)
82+
publicSupply: totalSupply - lockedSupply,
83+
// Total number of possible DEV NFT. This is a constant that cannot be changed
84+
maxSupply: MAX_SUPPLY,
85+
// Some calculations to make these values easily accessible for display in UI
86+
remainingTotalSupply: MAX_SUPPLY - totalSupply,
87+
remainingPublicSupply: PUBLIC_MAX_SUPPLY - (totalSupply - lockedSupply),
88+
remainingLockedSupply: MAX_SUPPLY - PUBLIC_MAX_SUPPLY - lockedSupply,
89+
};
90+
}

0 commit comments

Comments
 (0)