0

I'm using NodeJs to write the Hyperledger Fabric chaincode v2.x and using const { Contract } = require('fabric-contract-api')

I have 2 sets of chaincode, one to maintain the user and its wallet amount, and 2nd contract has the information about the asset e.g. quantity, price, name, etc.

I wanted to transfer the asset some quantity from user1 to user2 and wanted to deduct money from user1's account and transfer it to user2's account.

How can I call the function of transfer from the user contract inside the asset contract?

Shalabh Negi
  • 621
  • 7
  • 18

2 Answers2

0

Yes, you can call one chain code function in another chain code . You can find a few more information in the below link: https://hyperledger.github.io/fabric-chaincode-node/release-2.2/api/fabric-shim.ChaincodeStub.html#invokeChaincode__anchor

0

As there is nothing like a piece of code to explain how the SDK works, I would like to offer the following which may help. The exchangeAsset is a method signature on the current contract. It receives as parameters, the AssetId and the buyerId.

async exchangeAsset(ctx, assetId, buyerId) {

Further down in this contract method, there is a need to make a cross contract call to the client contract, which contains information on buyers and sellers. First create an array containing the name of the function to call and the number of parameters required e.g.

let values = new Array("readKeyValue", buyerId);

Then make the call using the following

    let buyer = await this.crossChannelRead(
  ctx,
  "client",
  [...values],
  "channelname"
);

The "client" string is the name of the other contract to call. The values array is spread and will read as readKeyValue, buyerId, which means use the readKeyValue function and use buyerId as the parameter. The "channelname" as shown above will actually be the name of the channel where the contract can be found.

Jerome O'Mahony
  • 167
  • 1
  • 5