From f446964a1dd2f6880ea609f3c779bd9a3ef9bc91 Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Tue, 24 Dec 2024 17:52:30 -0600 Subject: [PATCH] fix: modifed updateRegistry to allow merge and sort contracts Signed-off-by: Logan Nguyen --- .../src/services/registryGenerator.ts | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tools/erc-repository-indexer/erc-contract-indexer/src/services/registryGenerator.ts b/tools/erc-repository-indexer/erc-contract-indexer/src/services/registryGenerator.ts index 3a6c66880..d4d371d65 100644 --- a/tools/erc-repository-indexer/erc-contract-indexer/src/services/registryGenerator.ts +++ b/tools/erc-repository-indexer/erc-contract-indexer/src/services/registryGenerator.ts @@ -118,23 +118,36 @@ export class RegistryGenerator { filePath: string, newContracts: ERCOutputInterface[] ): Promise { + let uniqueContracts: ERCOutputInterface[] = []; const fileContent = this.readContentsFromFile(filePath); const existingContracts = fileContent ? (JSON.parse(fileContent) as ERCOutputInterface[]) : []; - // Create a Map to deduplicate contracts by contractId - const contractMap = new Map( - [...existingContracts, ...newContracts].map((contract) => [ - contract.contractId, - contract, - ]) - ); + if (!existingContracts.length) { + uniqueContracts = newContracts; + } else if ( + // Since both arrays are sorted in ascending order, if the `contractId` of the last item in `existingContracts` + // is less than the `contractId` of the first item in `newContracts`, just merged the contracts and remove dups without sorting. + existingContracts[existingContracts.length - 1].contractId < + newContracts[0].contractId + ) { + // Create a Map to deduplicate contracts by contractId + const contractMap = new Map( + [...existingContracts, ...newContracts].map((contract) => [ + contract.contractId, + contract, + ]) + ); + uniqueContracts = Array.from(contractMap.values()); + } else { + uniqueContracts = Helper.mergeAndSort(existingContracts, newContracts); + } + + await this.writeContentsToFile(filePath, uniqueContracts); // Convert Map values back to array for file writing - const uniqueContracts = Array.from(contractMap.values()); - await this.writeContentsToFile(filePath, uniqueContracts); console.log( `Finished writing ${newContracts.length} new ERC token contracts to registry.` );