[Ethereum] How to Listen to Contract Events in Drizzle

drizzleeventsreacttruffleweb3js

Let's take the example of the SimpleStorage contract that comes with the truffle box drizzle-box

How can we listen to the event StorageSet and console.log it to the JS console?

drizzleOptions.js

import SimpleStorage from './../build/contracts/SimpleStorage.json'

const drizzleOptions = {
web3: {
    block: false,
    fallback: {
    type: 'ws',
    url: 'ws://127.0.0.1:8545'
    }
},
contracts: [
    SimpleStorage
],
events: {
    SimpleStorage: ['StorageSet'],
},
polls: {
    accounts: 1500
}
}

export default drizzleOptions

Best Answer

I guess you use Metamask, so the problem might be the following: "The current provider doesn't support subscriptions: MetamaskInpageProvider".

I don't either see this error message, since drizzle doesn't print it. You can have a look here. On this way, you should be able to test that event subscription thing on your own/manually. Something like that:

drizzle.contracts.SimpleStorage.events
    .StorageSet({/* eventOptions */}, (error, event) => {
        console.log(error, event);
    })
    .on('data', (event) => console.log(event))
    .on('changed', (event) => console.log(event))
    .on('error', (error) => console.log(error));

Here you can find the documentation for web3 events.

Optionally, you could check for events in the transaction receipt.

Related Topic