DEV Community

Callis Ezenwaka
Callis Ezenwaka

Posted on • Updated on

Automating Blockchain Build and Integration with Node.js.

When implementing a blockchain solution using Node.js and Solidity, developers often need to copy the ABI code during deployment. This process often happens before commit to a remote repository.

In this article, we shall see a quick trick that achieves the same result without breaking a sweat. Before delving into the details, let's run a quick project setup with hardhat. For more info, see.

For a start, run the basic installation of hardhat npm install --save-dev hardhat if not previously done. Setup a new project with this command npx hardhat and follow the prompt.

The use-case for your blockchain application will differ from the one elaborate here. Be sure to run npm install <package name> to install packages that are peculiar to your use-case.

The project folder structure or tree should look like below image after setup:

Project Tree

Here, we can see all the directories especially the scripts and src/abi. Both directories will be handy in our implementations. Ensure that the file(s) for your solidity code are inside the contract folder.

Now, this is where the magic happens. Create a new script file files.ts inside the scripts directory and add the below code:

import fs from "fs";
import path from "path";

const folderList: string[] = [];
function output(folderPaths: string[]) {
  folderPaths.forEach(folderPath => {
    const results = fs.readdirSync(folderPath);
    const folders = results.filter((res: any) => fs.lstatSync(path.resolve(folderPath, res)).isDirectory());
    const innerFolderPaths = folders.map((folder: any) => path.resolve(folderPath, folder));
    if(innerFolderPaths.length === 0) {
      return;
    };
    innerFolderPaths.forEach((innerFolderPath: string) => {
      fs.readdirSync(innerFolderPath).forEach((file: string) => {
        if (file.split('.')[1] === 'json') {
          fs.rename(`${innerFolderPath}/${file}`, `./src/abi/${file}`, function (err: any) {
            if (err) {
              throw err;
            }
          })
        }
      });
      folderList.push(innerFolderPath)
    });
    output(innerFolderPaths);
  });
}

output([path.resolve(__dirname, "../artifacts/contracts")]);
Enter fullscreen mode Exit fullscreen mode

The above code uses recursion and fileSystem native API to loop through all the directories in a given directory, in this case ../artifacts/contracts. Then, open the package.json file and add the following piece of code inside the scripts object:

"files": "ts-node ./scripts/files.ts",
"compile": "npx hardhat compile && npm run files",
Enter fullscreen mode Exit fullscreen mode

What happens here is that once you run npm run compile command to compile your solidity smart contract, it will run the script ./scripts/files.ts. Afterward, it will copy the ABI codes for all the solidity code found inside the contract directory to the src/abi directory as shown below.

Abi folder tree

With this simple automation, there is no need to manually copy the ABIs compiled smart contracts to your project. The repository for this tutorial is on GitHub.

If you like the article, do like and share with friends.

Top comments (0)