1

I would like to listen to an event emitted by a function of my smartcontract with javascript.

filter = {
address: THE_ADDRESS_OF_YOUR_CONTRACT,
topics: [
    // the name of the event, parnetheses containing the data type of each event, no spaces
    utils.id("WinnerPicked(address)")
]}
provider.on(filter, (address) => {
   console.log(address)
})

However, I'm also using Moralis, and I don't know how to get the "provider" to do "provider.on". I can't find a function "getMoralisProvider" or something like this. This listening of event is used in a component in react, and the father of this component is wrapped with <MoralisProvider initializeOnMount={false}>

Thanks for your help !

1 Answer 1

1

If you want to get a web3 provider you can do for example:

const { enableWeb3 } = useMoralis(); 

const provider = await enableWeb3(); // provider from injected wallet e.g. MetaMask extension

provider.on('block', (blockNumber) => {
   console.log('blockNumber', blockNumber);
});

Reference: https://v1docs.moralis.io/moralis-dapp/web3/web3#connectors

If you have a backend, you may also be interested in using the Streams API where event data is streamed to your backend in real-time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.