How to Invoke ChainCode From Client Application in HyperLedger Fabric ?

Mehmet Emin Duran
stm-blockchain
Published in
2 min readMar 2, 2021
HyperLedger Fabric

In this tutorial, we will see how to invoke smart contracts or chaincodes on Visual Studio Code. We will use Fabric samples to get started working with Hyperledger Fabric. You can clone the git repository from https://github.com/hyperledger/fabric-samples.

Firstly, you should install chaincode at fabric-samples/asset-transfer-events/chaincode-java/ folder to your blockchain network. For this step, if you don’t know how to do this, you can follow the steps described at https://medium.com/stm-blockchain/how-to-setup-hyperledger-fabric-smart-contract-java-environment-on-visual-studio-code-7d51f6a60788.

To export connection profile and wallet, we will use IBM Blockchain extension and Visual Studio Code. You can download this extension from Vs Code Extensions tab or you can follow steps described at IBM Blockchain Platform documentation.

Export Connection Profile and Wallet

In this example, we use asset-transfer-event project from fabric samples.So, connect gateway and export connection profile under folder fabric-samples/asset-transfer-events/application-javascript shown as image below.

IBM Blockchain extension export connection profile

Create a wallet folder in fabric-samples/asset-transfer-events/application-javascript folder and export the wallet to fabric-samples/asset-transfer-events/application-javascript folder shown as the image below.

IBM Blockchain extension export wallet

Then, Open fabric-samples/test-application/javascript/ folder as a workspace. This project consists of helpful tools to connect gateway. To connect our gateway, firstly we should change the wallet path given at the AppUtil.js file as below.

const ccpPath = path.resolve(__dirname, ‘MiniGwConnection.json’);

Running Client Application

Finally, open fabric-samples/asset-transfer-events/application-javascript workspace from Visual Studio Code. And, organize the configurations which are channelName, chainCodeName, org1, walletPath, org1UserId, certificate authority according to your network.

Sample configuration :

const channelName = ‘acochannel’;const chaincodeName = ‘asset-transfer-events’;const org1 = ‘cop-com’;const walletPath = path.join(__dirname, ‘wallet’);const Org1UserId = ‘admin’;const walletPathOrg1 = path.join(__dirname, ‘wallet’, ‘org1’);const walletOrg1 = await buildWallet(Wallets, walletPathOrg1);const caOrg1Client = buildCAClient(FabricCAServices, ccpOrg1, ‘ca1.cop.com’);// setup the wallet to cache the credentials of the application user, on the app server locallyconst walletOrg1 = await buildWallet(Wallets, walletPath);

After all, you can run the client application and see the result by the commands below.

npm installnode app

--

--