Skip to content

Commit

Permalink
fix: first try to enumerate then fallback to checking tokens in custody
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor committed Oct 28, 2022
1 parent 2e98169 commit 5094f78
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions packages/react-token-streams/src/hooks/useStreamTokensInCustody.ts
Expand Up @@ -111,8 +111,24 @@ export const useStreamTokensInCustody = (config: Config) => {
setIsLoading(true);
const finalData: BigNumberish[] = [];

// Enumerate allTokensInCustody first
if (allTokensInCustody && allTokensInCustody.length < 1000) {
// Enumerate to 20,000
const custodyStatusesPromises = [];

for (
let i = 0, l = Number(totalSupply?.toString() || 20_000);
i <= l;
i = i + 500
) {
custodyStatusesPromises.push(fetchTokensInCustodyInRange(i, i + 500));
}

for (const tokens of await Promise.all(custodyStatusesPromises)) {
if (tokens && tokens.length) {
finalData.push(...tokens);
}
}

if (!finalData.length && allTokensInCustody) {
const custodyStatuses = await Promise.all(
allTokensInCustody.map((token) =>
contract.tokensInCustody(
Expand All @@ -130,25 +146,6 @@ export const useStreamTokensInCustody = (config: Config) => {
finalData.push(allTokensInCustody[i].tokenId);
}
}
} else {
// Otherwise enumerate to 20,000
const custodyStatusesPromises = [];

for (
let i = 0, l = Number(totalSupply?.toString() || 20000);
i <= l;
i = i + 500
) {
custodyStatusesPromises.push(
fetchTokensInCustodyInRange(i, i + 500),
);
}

for (const tokens of await Promise.all(custodyStatusesPromises)) {
if (tokens && tokens.length) {
finalData.push(...tokens);
}
}
}

setData(finalData);
Expand Down

0 comments on commit 5094f78

Please sign in to comment.