From 7267256221c95f185ca6a19b6989633f592e131e Mon Sep 17 00:00:00 2001 From: Logan Nguyen Date: Tue, 17 Dec 2024 14:11:30 -0600 Subject: [PATCH] fix: added test coverage for contracts that do not support optional methods Signed-off-by: Logan Nguyen --- .../tests/acceptance/acceptance.test.ts | 85 +++++++++++++------ .../tests/acceptance/utils/helper.ts | 56 ++++++++---- 2 files changed, 98 insertions(+), 43 deletions(-) diff --git a/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/acceptance.test.ts b/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/acceptance.test.ts index c9e027710..49b63dd21 100644 --- a/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/acceptance.test.ts +++ b/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/acceptance.test.ts @@ -30,9 +30,8 @@ describe('ERC Registry Acceptance Test', () => { // production networks take more time to finish deployments jest.setTimeout(60000); - const totalExpectedNonERCContracts = 3; - const totalExpectedERC20Contracts = 6; - const totalExpectedERC721Contracts = 9; + const totalExpectedDeploymentsForEachContractType = 1; + const erc20JsonFilePath = Helper.buildFilePath( constants.ERC_20_JSON_FILE_NAME ); @@ -44,15 +43,15 @@ describe('ERC Registry Acceptance Test', () => { erc20: [] as string[], erc721: [] as string[], nonErc: [] as string[], + minimalErc20: [] as string[], + minimalErc721: [] as string[], }; let sdkClient: NodeClient | null = null; beforeAll(async () => { const contractDeploymentRequirements = testHelper.prepareContractDeployRequirements( - totalExpectedERC20Contracts, - totalExpectedERC721Contracts, - totalExpectedNonERCContracts + totalExpectedDeploymentsForEachContractType ); sdkClient = testHelper.buildSdkClient(); deployedAddresses = await testHelper.deployRequiredContracts( @@ -62,14 +61,18 @@ describe('ERC Registry Acceptance Test', () => { // Sort all contract addresses to identify the earliest deployed contract const allDeployedAddresses = testHelper.sortAddresses([ ...deployedAddresses.erc20, + ...deployedAddresses.minimalErc20, ...deployedAddresses.erc721, + ...deployedAddresses.minimalErc721, ...deployedAddresses.nonErc, ]); + const totalExpectedDeployments = - totalExpectedNonERCContracts + - totalExpectedERC20Contracts + - totalExpectedERC721Contracts; + totalExpectedDeploymentsForEachContractType * + contractDeploymentRequirements.length; + expect(allDeployedAddresses.length).toEqual(totalExpectedDeployments); + // Start the indexing process from the earliest contract in the batch, avoiding indexing from genesis. process.env.STARTING_POINT = allDeployedAddresses[0]; }); @@ -84,33 +87,61 @@ describe('ERC Registry Acceptance Test', () => { it('should execute the main ERC registry runner method and correctly record the number of detected ERC contracts in registry', async () => { // run the actual tool to start indexing the network and write to registry await ercRegistryRunner().then(); + // wait for 500ms for all the asynchronous tasks to finish await new Promise((resolve) => setTimeout(resolve, 500)); + // retrieve the newest erc contracts added to the registry const latestErc20sWrittenToRegistry = JSON.parse( testHelper.readContentsFromFile(erc20JsonFilePath) - ).slice(totalExpectedERC20Contracts * -1); + ).slice(totalExpectedDeploymentsForEachContractType * 2 * -1); const latestErc721sWrittenToRegistry = JSON.parse( testHelper.readContentsFromFile(erc721JsonFilePath) - ).slice(totalExpectedERC721Contracts * -1); + ).slice(totalExpectedDeploymentsForEachContractType * 2 * -1); + // assertion - latestErc20sWrittenToRegistry.forEach((object: any, index: number) => { - expect(object.address).toEqual(deployedAddresses.erc20[index]); - expect(object.name).toEqual( - testConstants.ERC_CONSTRUCTOR_PARAMS.erc20.tokenName - ); - expect(object.symbol).toEqual( - testConstants.ERC_CONSTRUCTOR_PARAMS.erc20.tokenSymbol - ); + latestErc20sWrittenToRegistry.forEach((object: any) => { + expect( + deployedAddresses.erc20.includes(object.address) || + deployedAddresses.minimalErc20.includes(object.address) + ).toBe(true); + + if (deployedAddresses.erc20.includes(object.address)) { + expect(object.name).toEqual( + testConstants.ERC_CONSTRUCTOR_PARAMS.erc20.tokenName + ); + expect(object.symbol).toEqual( + testConstants.ERC_CONSTRUCTOR_PARAMS.erc20.tokenSymbol + ); + } + + if (deployedAddresses.minimalErc20.includes(object.address)) { + expect(object.name).toBeNull; + expect(object.symbol).toBeNull; + expect(object.decimals).toBeNull; + expect(object.totalSupply).toBeNull; + } }); - latestErc721sWrittenToRegistry.forEach((object: any, index: number) => { - expect(object.address).toEqual(deployedAddresses.erc721[index]); - expect(object.name).toEqual( - testConstants.ERC_CONSTRUCTOR_PARAMS.erc721.tokenName - ); - expect(object.symbol).toEqual( - testConstants.ERC_CONSTRUCTOR_PARAMS.erc721.tokenSymbol - ); + + latestErc721sWrittenToRegistry.forEach((object: any) => { + expect( + deployedAddresses.erc721.includes(object.address) || + deployedAddresses.minimalErc721.includes(object.address) + ).toBe(true); + + if (deployedAddresses.erc721.includes(object.address)) { + expect(object.name).toEqual( + testConstants.ERC_CONSTRUCTOR_PARAMS.erc721.tokenName + ); + expect(object.symbol).toEqual( + testConstants.ERC_CONSTRUCTOR_PARAMS.erc721.tokenSymbol + ); + } + + if (deployedAddresses.minimalErc721.includes(object.address)) { + expect(object.name).toBeNull; + expect(object.symbol).toBeNull; + } }); }); }); diff --git a/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/utils/helper.ts b/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/utils/helper.ts index 30e8756c5..cbd1e96f3 100644 --- a/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/utils/helper.ts +++ b/tools/erc-repository-indexer/erc-contract-indexer/tests/acceptance/utils/helper.ts @@ -26,7 +26,9 @@ import { import fs from 'fs'; import testConstants from '../utils/constants'; import OZERC20Artifacts from '../contracts/erc-20/OZERC20Mock.json'; +import MinimalOZERC20Artifacts from '../contracts/erc-20/MinimalERC20.json'; import OZERC721Artifacts from '../contracts/erc-721/OZERC721Mock.json'; +import MinimalOZERC721Artifacts from '../contracts/erc-721/MinimalERC721.json'; import BasicArtifacts from '../contracts/non-ercs/Basic.json'; import NodeClient from '@hashgraph/sdk/lib/client/NodeClient'; @@ -68,7 +70,8 @@ export default class Helper { static async deploySmartContractsViaSdk( sdkClient: Client, bytecode: string, - params: ContractFunctionParameters | null + params: ContractFunctionParameters | null, + contractType: string ): Promise { const contractCreateFlow = new ContractCreateFlow() .setGas(1_000_000) @@ -82,7 +85,7 @@ export default class Helper { const receipt = await txResponse.getReceipt(sdkClient); console.log( - `New contract successfully deployed: contractId=${receipt.contractId}, contractEvmAddress=0x${receipt.contractId?.toSolidityAddress()}, contractEvmAddress=0x${receipt.contractId?.toSolidityAddress()}` + `New contract successfully deployed: contractId=${receipt.contractId}, contractType=${contractType}, contractEvmAddress=0x${receipt.contractId?.toSolidityAddress()}, contractEvmAddress=0x${receipt.contractId?.toSolidityAddress()}` ); return `0x${receipt.contractId!.toSolidityAddress()}`; @@ -114,20 +117,16 @@ export default class Helper { /** * Prepares contract deployment requirements for ERC20, ERC721, and non-ERC contracts. - * @param {number} totalErc20s - The total number of ERC20 contracts to deploy. - * @param {number} totalErc721s - The total number of ERC721 contracts to deploy. - * @param {number} totalNonErcs - The total number of non-ERC contracts to deploy. + * @param {number} totalExpectedDeploymentsForEachContractType - The total expected deployments for each contract type * @returns {ContractDeploymentRequirements[]} An array of contract deployment requirements. */ static prepareContractDeployRequirements( - totalErc20s: number, - totalErc721s: number, - totalNonErcs: number + totalExpectedDeploymentsForEachContractType: number ): ContractDeploymentRequirements[] { return [ { contractType: 'erc20', - totalDeployments: totalErc20s, + totalDeployments: totalExpectedDeploymentsForEachContractType, bytecode: OZERC20Artifacts.bytecode, ercConstructorParams: new ContractFunctionParameters() .addString(testConstants.ERC_CONSTRUCTOR_PARAMS.erc20.tokenName) @@ -135,15 +134,27 @@ export default class Helper { }, { contractType: 'erc721', - totalDeployments: totalErc721s, + totalDeployments: totalExpectedDeploymentsForEachContractType, bytecode: OZERC721Artifacts.bytecode, ercConstructorParams: new ContractFunctionParameters() .addString(testConstants.ERC_CONSTRUCTOR_PARAMS.erc721.tokenName) .addString(testConstants.ERC_CONSTRUCTOR_PARAMS.erc721.tokenSymbol), }, + { + contractType: 'minimalErc20', + totalDeployments: totalExpectedDeploymentsForEachContractType, + bytecode: MinimalOZERC20Artifacts.bytecode, + ercConstructorParams: null, + }, + { + contractType: 'minimalErc721', + totalDeployments: totalExpectedDeploymentsForEachContractType, + bytecode: MinimalOZERC721Artifacts.bytecode, + ercConstructorParams: null, + }, { contractType: 'nonErc', - totalDeployments: totalNonErcs, + totalDeployments: totalExpectedDeploymentsForEachContractType, bytecode: BasicArtifacts.bytecode, ercConstructorParams: null, }, @@ -163,22 +174,32 @@ export default class Helper { erc20: string[]; erc721: string[]; nonErc: string[]; + minimalErc20: string[]; + minimalErc721: string[]; }> { const deployedAddresses = { erc20: [] as any, + minimalErc20: [] as any, erc721: [] as any, + minimalErc721: [] as any, nonErc: [] as any, }; for (const contractObject of contractDeploymentRequirements) { for (let i = 0; i < contractObject.totalDeployments; i++) { deployedAddresses[ - contractObject.contractType as 'erc20' | 'erc721' | 'nonErc' + contractObject.contractType as + | 'erc20' + | 'minimalErc20' + | 'erc721' + | 'minimalErc721' + | 'nonErc' ].push( Helper.deploySmartContractsViaSdk( sdkClient, contractObject.bytecode, - contractObject.ercConstructorParams + contractObject.ercConstructorParams, + contractObject.contractType ) ); } @@ -187,9 +208,12 @@ export default class Helper { deployedAddresses.erc20 = await Promise.all(deployedAddresses.erc20); deployedAddresses.erc721 = await Promise.all(deployedAddresses.erc721); deployedAddresses.nonErc = await Promise.all(deployedAddresses.nonErc); - - deployedAddresses.erc20 = Helper.sortAddresses(deployedAddresses.erc20); - deployedAddresses.erc721 = Helper.sortAddresses(deployedAddresses.erc721); + deployedAddresses.minimalErc20 = await Promise.all( + deployedAddresses.minimalErc20 + ); + deployedAddresses.minimalErc721 = await Promise.all( + deployedAddresses.minimalErc721 + ); return deployedAddresses; }