如何在另一个链代码中调用一个链代码函数? [nodejs具体]
我正在使用 NodeJs 编写 Hyperledger Fabric 链代码 v2.x 并使用 const { Contract } = require('fabric-contract-api')
我有 2 套链码,用于维护用户
及其钱包金额,第二份合约包含有关资产
的信息,例如数量、价格、名称等。
我想转移资产一些数量来自user1 发送给 user2,想要从 user1 帐户中扣除资金并将其转入 user2 帐户。
如何在asset
合约中调用user
合约的转账功能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以在一个链码函数中调用另一个链码函数。
您可以在以下链接中找到更多信息:
https://hyperledger.github.io/fabric-chaincode-node/release-2.2/api/fabric-shim.ChaincodeStub.html#invokeChaincode__anchor
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
由于没有什么比一段代码更能解释 SDK 的工作原理的了,所以我想提供以下内容可能会有所帮助。 ExchangeAsset 是当前合约的方法签名。它接收 AssetId 和 BuyerId 作为参数。
再往下,在这个合约方法中,需要对客户端合约进行跨合约调用,其中包含买家和卖家的信息。首先创建一个包含要调用的函数名称和所需参数数量的数组,例如
然后使用以下内容进行调用
“client”字符串是要调用的其他合约的名称。
Values 数组被展开,将读取为 readKeyValue、buyerId,这意味着使用 readKeyValue 函数并使用buyerId 作为参数。
上面显示的“channelname”实际上是可以找到合约的通道的名称。
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.
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.
Then make the call using the following
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.