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

automatic chain addition #22

Open
Lomet opened this issue Oct 23, 2023 · 4 comments
Open

automatic chain addition #22

Lomet opened this issue Oct 23, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Lomet
Copy link
Member

Lomet commented Oct 23, 2023

Create the Dynamic Index Generator Script

Description:

Create a script that will automatically generate an index.ts file in the configs directory, which imports all .config.ts files.

Steps:

Create a new script file named generateIndex.js.
In this script, use Node.js's fs module to read all files from the configs directory.
Filter out files that aren't .config.ts.
For each .config.ts file, add an import statement to a string.
Write this string of import statements to a new index.ts file in the configs directory.

Modify package.json Scripts

Description:

Add a new script to run the generateIndex.js file and modify the prebuild script to run this new script before the build process.

Steps:

Add a new script named generate-index to the scripts section of package.json that runs generateIndex.js.
Modify the prebuild script to run the generate-index script before any other commands.

Update Chain Configuration Files

Description: Modify each .config.ts file to use the new class-based approach, so they automatically register themselves upon being imported.

Steps:

Ensure each .config.ts file exports a class instance of TChainConfig.
The class constructor should handle the registration to the central AVAILABLE_CHAINS map.

Simplify the Constant Index File

Description:

Since configurations will register themselves upon import, the constant index file (where you previously manually added each chain to the map) can be simplified.

Steps:

Remove manual AVAILABLE_CHAINS.set(...) calls from the constant index file.
Import the new index.ts from the configs directory to ensure all configurations are loaded and registered.

Test

Description:

Ensure that all configurations are correctly registered and accessible.

Steps:

Run the build script to ensure everything compiles without errors.
Write tests or manually verify that all configurations are correctly loaded and registered in the AVAILABLE_CHAINS map.

Update Documentation

Description:

Update any relevant documentation or README files to describe the new dynamic import process and how to add new configurations.

Steps:

Describe the new class-based approach for chain configurations.
Explain how the generateIndex.js script works and its role in the build process.
Provide instructions for adding new chain configurations, emphasizing that there's no need to manually register them in the constant index file anymore.
By following these steps, you'll have a streamlined process for adding and registering new chain configurations, making the system more maintainable and reducing potential sources of error.

@Lomet
Copy link
Member Author

Lomet commented Oct 23, 2023

generateIndex.js:

const fs = require('fs');
const path = require('path');

const configsDirectory = path.join(__dirname, './src/contracts/configs');
const configFiles = fs.readdirSync(configsDirectory);

let imports = '';
configFiles.forEach(file => {
    if (file.endsWith('.config.ts')) {
        const fileNameWithoutExt = file.replace('.config.ts', '');
        imports += `import './${fileNameWithoutExt}.config.ts';\n`;
    }
});

const outputPath = path.join(configsDirectory, 'index.ts');
fs.writeFileSync(outputPath, imports);

@Lomet
Copy link
Member Author

Lomet commented Oct 23, 2023

package.json:

{
  "scripts": {
    "generate-index": "node path_to_script/generateIndex.js",
    "prebuild": "pnpm generate-index && ...other prebuild commands...",
    ...
  }
}

@Lomet
Copy link
Member Author

Lomet commented Oct 23, 2023

import './contracts/configs/index.ts'; // This will automatically import and register all configs

export const DEFAULT_CHAIN_ID = 1;
export const UINT_MINUS_ONE = "115792089237316195423570985008687907853269984665640564039457584007913129639935";
export const CUSTOMER_ACCOUNT_VARIABLE = "__CUSTOMER_ACCOUNT__";

@Lomet
Copy link
Member Author

Lomet commented Oct 23, 2023

## Adding New Chain Configurations

1. Create a `.config.ts` file in the `configs` directory for your chain.
2. Define your configuration in the file using the class-based approach.
3. The system will automatically import and register your configuration when building the project. There's no need for manual registration in the constant index file.

@Lomet Lomet added the enhancement New feature or request label Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant