简体   繁体   English

持续监听智能合约事件

[英]Continuously listening to smart contract events

My NFT project architecture in such way that only when a minting event has occurred, a minted event is emitted and then I want to listen to that event so I trigger an upload of the JSON content that would be then visible to token owner (since anyone can potentially access the content of all the token by basically going to baseURI/tokenID and I want to keep the rarity of the tokens a secret until minted).我的 NFT 项目架构是这样的,只有当发生铸造事件时,才会发出minted事件,然后我想听那个事件,所以我触发了 JSON 内容的上传,然后令牌所有者可以看到该内容(因为任何人可以通过基本访问baseURI/tokenID来潜在地访问所有令牌的内容,并且我想在铸造之前将令牌的稀有性保密)。

I want to create a function that continuously listens to new events from the minted event.我想创建一个函数来持续监听来自minted事件的新事件。 I'm trying to access new events by using the following implementation, because I couldn't find an implementation that continuously listens to events from the contract.我正在尝试使用以下实现来访问新事件,因为我找不到持续侦听合同事件的实现。

async function listener() {
    events = nftContract.getPastEvents("minted",{fromBlock: 1}, function(err,res) {
        console.log(res);
    })

What is the best way to create a function that continues to poll events from the contract?创建继续从合约轮询事件的函数的最佳方法是什么?

The full code of the solution is so:解决方案的完整代码是这样的:

const ethers = require('ethers');
const CONTRACT_ADDRESS = "0x10820dB......";
const ABIJSON = JSON.parse('{"_format": "hh-sol-artifact-1", "contractName": "testcontract", "....}')
provider = new ethers.providers.AlchemyProvider("ropsten");
const contract = new ethers.Contract(CONTRACT_ADDRESS, ABIJSON.abi, provider);

contract.on("eventName", ( caller,tokenID) => {
      //this section is called every time an event is emitted 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM