diff --git a/sdk/examples/utils/countUniqueSubjects.ts b/sdk/examples/utils/countUniqueSubjects.ts index 2715cac7..1ccfaba3 100644 --- a/sdk/examples/utils/countUniqueSubjects.ts +++ b/sdk/examples/utils/countUniqueSubjects.ts @@ -1,6 +1,6 @@ +import { VeraxSdk } from "../../src/VeraxSdk"; import fs from "fs"; import path from "path"; -import { VeraxSdk } from "../../src/VeraxSdk"; const BATCH_SIZE = 100000; @@ -11,13 +11,13 @@ const fetchSubjectsFromFile = async (fileSuffix: number): Promise => { async function main() { const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET); - const attestationsNumber = await veraxSdk.utils.getAttestationIdCounter(); - const filesNumber = Math.ceil(Number(attestationsNumber) / BATCH_SIZE); + const attestationNumber = await veraxSdk.utils.getAttestationIdCounter(); + const filesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE); const allSubjects: string[][] = []; const uniqueSubjects: Set = new Set(); - for (let i = 0; i <= filesNumber; i++) { + for (let i = 0; i < filesNumber; i++) { allSubjects.push(await fetchSubjectsFromFile(i)); } @@ -30,6 +30,23 @@ async function main() { } console.log(`Unique subjects = ${uniqueSubjects.size}`); + + const uniqueSubjectsArray = Array.from(uniqueSubjects); + const chunks = Math.ceil(uniqueSubjectsArray.length / BATCH_SIZE); + + for (let i = 0; i < chunks; i++) { + const start = i * BATCH_SIZE; + const end = start + BATCH_SIZE; + const chunk = uniqueSubjectsArray.slice(start, end); + + fs.writeFile(path.resolve(__dirname, `../../uniqueSubjects-${i}.txt`), JSON.stringify(chunk), function (err) { + if (err) { + return console.log(err); + } + + console.log(`File uniqueSubjects-${i}.txt was saved!`); + }); + } } // We recommend this pattern to be able to use async/await everywhere diff --git a/sdk/examples/utils/getAllAttestations.ts b/sdk/examples/utils/getAllAttestations.ts index e615c393..42871c1d 100644 --- a/sdk/examples/utils/getAllAttestations.ts +++ b/sdk/examples/utils/getAllAttestations.ts @@ -9,7 +9,7 @@ const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) => let hasMoreResults = true; const batch: string[] = []; - while (hasMoreResults && batch.length <= BATCH_SIZE) { + while (hasMoreResults && batch.length < BATCH_SIZE) { console.log(`Query batch #${skip}`); const matchingAttestations = await veraxSdk.attestation.findBy(1000, skip * 1000 + batchNumber * BATCH_SIZE); @@ -29,15 +29,13 @@ const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) => async function main() { const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET); - const attestationsNumber = await veraxSdk.utils.getAttestationIdCounter(); - const batchesNumber = Math.ceil(Number(attestationsNumber) / BATCH_SIZE); + const attestationNumber = await veraxSdk.utils.getAttestationIdCounter(); + const batchesNumber = Math.ceil(Number(attestationNumber) / BATCH_SIZE); - console.log( - `We expect ${batchesNumber} batches of ${BATCH_SIZE} attestations to get all ${attestationsNumber} attestations.`, - ); + console.log(`Creating ${batchesNumber} batches of ${BATCH_SIZE} items to get all ${attestationNumber} attestations.`); - for (let i = 0; i <= batchesNumber; i++) { - console.log(`Attestations batch #${i}`); + for (let i = 8; i < batchesNumber; i++) { + console.log(`Attestation batch #${i}`); const subjectsBatch = await fetchAllAttestations(i, veraxSdk); fs.writeFile(path.resolve(__dirname, `../../allSubjects-${i}.txt`), JSON.stringify(subjectsBatch), function (err) {