Skip to content

Commit

Permalink
fix: modifed updateRegistry to allow merge and sort contracts
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Dec 24, 2024
1 parent cbaf701 commit f446964
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,36 @@ export class RegistryGenerator {
filePath: string,
newContracts: ERCOutputInterface[]
): Promise<void> {
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.`
);
Expand Down

0 comments on commit f446964

Please sign in to comment.