-
Notifications
You must be signed in to change notification settings - Fork 0
fix: (HOT) temp solution to correctly handle burned licenses #24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| 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
|
||
|
|
||
| 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; | ||
|
|
||
There was a problem hiding this comment.
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 JSnumberviaNumber(totalSupply). If totalSupply ever exceedsNumber.MAX_SAFE_INTEGER, this will silently lose precision and the loop will enumerate the wrong indices. Consider iterating usingbigintcounters (and only converting to number at the UI boundary), or at least guard/throw whentotalSupply > BigInt(Number.MAX_SAFE_INTEGER)to avoid incorrect results.