Skip to content
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

feat: Enhance ERC registry update with efficient merge and deduplication #1136

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
"typescript": "^5.7.2"
},
"dependencies": {
"@types/lodash": "^4.17.14",
"ahocorasick": "^1.0.2",
"dotenv": "^16.4.5",
"ethers": "^6.13.4"
"ethers": "^6.13.4",
"lodash": "^4.17.21"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import path from 'path';
import constants from '../utils/constants';
import { ERCOutputInterface } from '../schemas/ERCRegistrySchemas';
import { Helper } from '../utils/helper';
import _ from 'lodash';

export class RegistryGenerator {
/**
Expand Down Expand Up @@ -108,33 +109,47 @@ export class RegistryGenerator {
}

/**
* Updates a registry file with new contracts, removing duplicates if any.
* @param {string} filePath - Path to the registry file
* @param {ERCOutputInterface[]} newContracts - New contracts to add to registry
* @returns {Promise<void>} Promise that resolves when registry is updated
* Updates a registry file with new contracts by merging them with existing contracts,
* ensuring the registry remains sorted and free of duplicates.
*
* @param {string} filePath - The file path to the registry file.
* @param {ERCOutputInterface[]} newContracts - The new contracts to add to the registry.
* @returns {Promise<void>} - A promise that resolves once the registry is successfully updated.
*
* @private
*/
private async updateRegistry(
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
) {
uniqueContracts = _.chain([...existingContracts, ...newContracts]) // merge contracts
.uniqBy('contractId') // Remove duplicates based on contractId
.value(); // Extract the final array
} else {
uniqueContracts = _.chain([...existingContracts, ...newContracts]) // merge contracts
.uniqBy('contractId') // Remove duplicates based on contractId
.sortBy((contract) => Number(contract.contractId.split('.')[2])) // Sort by the numeric value of contractId
.value(); // Extract the final array
}

await this.writeContentsToFile(filePath, uniqueContracts);

// Convert Map values back to array for file writing
quiet-node marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/

import axios, { AxiosInstance } from 'axios';
import path from 'path';
import constants from './constants';
import path from 'path';

export class Helper {
/**
Expand Down
Loading