AddContractListener如何在HyperLeDger Fabric 1.4版本中起作用

发布于 2025-02-08 18:46:11 字数 1802 浏览 1 评论 0原文

我正在使用Fabric版本1.4。我已经在第一个网络上部署了合同fabcar。我已经对现有合同进行了一些修改,例如添加setEvent(“启动”,[] byte {“ test”})。我想要的是使用以下代码来聆听这些事件,但无法提取事件。这是我的代码: -

代码

//connect to Fabric Network, but starting a new gateway
   const gateway = new Gateway();

   //use our config file, our peerIdentity, and our discovery options to connect to Fabric network.
   await gateway.connect(ccpPath, { wallet, identity: peerIdentity, discovery: { enabled: true, asLocalhost: true } });

   console.log('gateway connect');

   //connect to our channel that has been created on IBM Blockchain Platform
   const network = await gateway.getNetwork('mychannel');

   //connect to our insurance contract that has been installed / instantiated on IBM Blockchain Platform
   const contract = await network.getContract('fabcar');

await contract.addContractListener('my-contract-listener', 'InitLedger', (err, event, blockNumber, transactionId, status) => {
      if (err) {
        console.error(err);
        return;
      }

      //convert event to something we can parse 
      event = event.payload.toString();
      event = JSON.parse(event)

      //where we output the TradeEvent
      console.log('************************ Start Trade Event *******************************************************');
      console.log(`make: ${event.type}`);
      console.log(`model: ${event.ownerId}`);
      console.log(`colour: ${event.id}`);
      console.log(`owner: ${event.description}`);
      console.log(`Block Number: ${blockNumber} Transaction ID: ${transactionId} Status: ${status}`);
      console.log('************************ End Trade Event ************************************');
    });

在我在控制台中运行此代码时,它不会打印。如果我缺少任何建议或任何建议,请欢迎或建议我。

I'm using fabric version 1.4. I have deploy my contract fabcar on first network. I have do some modification in that existing contract like add SetEvent("initLedger",[]byte{"test"}). And what I want is to listen these events using my below code but not able to fetch events. Here is my code:-

CODE

//connect to Fabric Network, but starting a new gateway
   const gateway = new Gateway();

   //use our config file, our peerIdentity, and our discovery options to connect to Fabric network.
   await gateway.connect(ccpPath, { wallet, identity: peerIdentity, discovery: { enabled: true, asLocalhost: true } });

   console.log('gateway connect');

   //connect to our channel that has been created on IBM Blockchain Platform
   const network = await gateway.getNetwork('mychannel');

   //connect to our insurance contract that has been installed / instantiated on IBM Blockchain Platform
   const contract = await network.getContract('fabcar');

await contract.addContractListener('my-contract-listener', 'InitLedger', (err, event, blockNumber, transactionId, status) => {
      if (err) {
        console.error(err);
        return;
      }

      //convert event to something we can parse 
      event = event.payload.toString();
      event = JSON.parse(event)

      //where we output the TradeEvent
      console.log('************************ Start Trade Event *******************************************************');
      console.log(`make: ${event.type}`);
      console.log(`model: ${event.ownerId}`);
      console.log(`colour: ${event.id}`);
      console.log(`owner: ${event.description}`);
      console.log(`Block Number: ${blockNumber} Transaction ID: ${transactionId} Status: ${status}`);
      console.log('************************ End Trade Event ************************************');
    });

While I'm running this code in console it will print nothing. If I'm missing something or any suggestions please welcome or suggest me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文