Build your fabric smart contract with VScode plug-in

Overview

What is IBM blockchain platform visual studio code extension?

The IBM Blockchain Platform Visual Studio (VS) Code extension gives an environment within the VS Code for creating, packaging, and testing smart contracts. It was released on 5 May 2018. The latest version 1.0.14 was released on 24 Oct 2019. You can practice the extension to create your smart contract project and start generating your business logic. You can then practice the VS Code to test your smart contract on your local machine by using a preconfigured instance of Hyperledger Fabric before you deploy the smart contract to the IBM Blockchain Platform. Quite interesting!

Let’s discuss how fabric can be responsive through VS Code.

ibm_image1
[Smart contract development]

Step 1: Install IBM Blockchain Platform VS Code Extension

Before you start to install the IBM Blockchain Platform VS Code extension, check prerequisites.

Prerequisites:

Install Extension:

  1. Go to the VS Code extension marketplace page or search for the IBM Blockchain Platform in the extensions panel within VS Code.

ibm_image2

  1. Click to Install.
  2. Restart VS Code to complete the installation.

After the installation, you can see the IBM Blockchain icon on the left side of the VS Code to initiate the IBM Blockchain Platform panel.

ibm_image3

Step 2: Set Up a Fabric Environment

Once you click on the icon(IBM Blockchain extension) you will get four following small box options:

  1. Smart contract
  2. Fabric environments
  3. Fabric gateway
  4. Fabric wallet

ibm_image4

Now click on the local fabric option inside the fabric environment which will extract all necessary images and run it as a container. Once the connection is successful, you will observe a full fabric environment.

ibm_image5

you can check your fabric environment using a docker ps command. If you have followed every installation dependency perfectly you will get the following output.

ibm_image6

Step 3: Create a Smart Contract

You can use the extension to generate a new smart contract project in VS Code. The extension creates a basic smart contract that maintains an example asset in the language of your choice. You can use the structure of the model as a starting point for developing your business logic. The extension gives all the dependencies that are required to deploy your smart contract to an instance of Hyperledger Fabric.

  • Click the IBM Blockchain icon to initiate the IBM Blockchain tab. Click the overflow menu in the smart contracts and click the Create New Project OR run ctrl+shift+p and click the Create New Project.
  • Select the language that you want to build a smart contract. The current selections are JavaScript, TypeScript, Go, and Java.

Key: You can use the VS Code extension to create and test Java smart contracts locally. However, you cannot deploy Java smart contracts to IBM Blockchain Platform networks. If you are deploying the smart contracts to a production network, JavaScript and TypeScript smart contracts require more support than contracts written in Go.

  • Create a folder with the name of your project and initiate it. (The structure of the project depends on the language that you chose. Here we chose a javascript language.)
  • After the successful creation of a smart contract, go through the lib folder and open the my-asset-contract.js file which is auto-generated. You can edit as per your business logic.

ibm_image7

Step 4: Package a Smart Contract

You need to package a smart contract in .cds format before you can install it on the IBM Blockchain Platform network.

  • Click the IBM Blockchain icon to initiate the IBM Blockchain tab.
  • In the Smart Contracts, click the overflow menu and choose the Package Open Project.

ibm_image8

Step 5: Deploy a Smart Contract to a Preconfigured Hyperledger Fabric Network

You can accept the VS Code to deploy your smart contract to a preconfigured Hyperledger Fabric network that the extension builds on your local machine. You can install, instantiate, and test your smart contract before you deploy it to a live network.

  • Ensure docker is running
  • In the Fabric Environments panel, click Install Smart Contract. Select the peer that you require to install the smart contract on. Select the smart contract package that you need to install, and click Install.

ibm_image9

  • In the Fabric Environments, click Instantiate Smart Contract. Select the installed smart contract to instantiate.
  • Enter the name of the instantiate function in a smart contract. If you practiced the default smart contract template, no instantiate function is used. (optional)

ibm_image10

If you adjusted your smart contract code and then repackaged it, you can upgrade the instantiated smart contract to deploy a newer version to the network.

ibm_image11

After a smart contract is installed and instantiated, you can submit transactions to the functions inside a smart contract by using the Fabric Gateways panel and also using a node environment.

Conclusion

So in this blog, we had just done the key steps in the workflow of local smart contract development! You’ve generated a dummy contract, deployed it locally, and used it to submit transactions. Stay tuned!! Happy coding!!

mm

Isha Padalia Blockchain Developer

I am a Blockchain Developer at Yudiz Solutions Pvt.Ltd. I am always eager to explore trending technologies and find efficient, optimized & user-friendly solutions. “Happy Coding”.

Leave a Reply

Top