Access Networks and Contracts

Previously, we connected to test-network, and now, it is possible to invoke the BalanceTransfer chaincode. First, let’s obtain a Network object, which represents the mychannel channel.

const network = await gateway.getNetwork('mychannel');

Next, we should specify which chaincode to trigger. To do that, we can get the Contract object representing the BalanceTransfer chaincode.

const contract = await network.getContract('balance_transfer');

Note that we should pass the same chaincode name as used in the chaincode deployment flow.

Finally, we can invoke the BalanceTransfer chaincode using the Contract.submitTransaction() function. Same as before, we will use CLI arguments instead of hardcoding the transaction parameters.

const functionName = args[1];
const chaincodeArgs = args.slice(2);
const response = await contract.submitTransaction(functionName, ...chaincodeArgs); 
if (`${response}` != '') {
    console.log('Response from ${functionName}: ${response}`);
}

The response returned from the submitTransaction function is, in essence, a payload returned by the contract.

Now, submitTransaction.js is fully implemented. You can find a complete code in the Resources tab in the Menu. Let’s now create an account on behalf of User1 from Org1.

  1. Install all the dependencies required to run submitTransaction.js.

 npm install
  1. Create an account on behalf of User1 using submitTransaction.js.

node submitTransaction.js 'User1@org1.example.com' initAccount U1 150
  1. Check the account existence by listing all of the accounts available to User1.

node submitTransaction.js 'User1@org1.example.com' listAccounts

You should see a response printed to the terminal.

Response from listAccounts:
[{"balance":150,"id":"U1","owner":"{\"mspid\":\"Org1MSP\",\"id\":\"x509::/C=US/ST=North Carolina/O=Hyperledger/OU=client/CN=user1::/C=US/ST=North Carolina/L=Durham/O=org1.example.com/CN=ca.org1.example.com\"}"}]

In summary, we have implemented a client application based on the fabric-network programming model. According to this model, the application has to follow six basic steps to submit a transaction:

  • Select an identity from a wallet

  • Connect to a gateway

  • Access a desired network

  • Construct a transaction request for a smart contract

  • Submit the transaction to the network

  • Process the response.

Last updated

Was this helpful?