Skip to content

Commit

Permalink
fix: added test coverage for contracts that do not support optional m…
Browse files Browse the repository at this point in the history
…ethods

Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Dec 17, 2024
1 parent a5b98a1 commit 7267256
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand All @@ -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(
Expand All @@ -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];
});
Expand All @@ -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;
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -68,7 +70,8 @@ export default class Helper {
static async deploySmartContractsViaSdk(
sdkClient: Client,
bytecode: string,
params: ContractFunctionParameters | null
params: ContractFunctionParameters | null,
contractType: string
): Promise<string> {
const contractCreateFlow = new ContractCreateFlow()
.setGas(1_000_000)
Expand All @@ -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()}`;
Expand Down Expand Up @@ -114,36 +117,44 @@ 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)
.addString(testConstants.ERC_CONSTRUCTOR_PARAMS.erc20.tokenSymbol),
},
{
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,
},
Expand All @@ -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
)
);
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 7267256

Please sign in to comment.