Handling Events Emitted By Smart Contract On Matic Network

I am trying to listen to and handle events that are emitted by our smart contract. With our contract on Ropsten I can use Infura’s WebSocket gateway (wss://ropsten.infura.io/ws), but there is no such Infura gateway for the Matic Network.

How may I handle smart contract events emitted on the Matic sidechain?

Edit: I should note the reason we are using the Infura provider is because we need to handle the events on the server, where of course we don’t have access to MetaMask.

Matic also provides websocket gateway which you can use to listen to events, just switch the RPC url to https://testnet.matic.network. Alternatively you can use dagger to listen to events on both Ropsten and Matic.

Great, thanks for the info!

Steve

2 Likes

Should the WebSocket gateway be proceeded by “https”? I tried “https://testnet.matic.network” with no luck. I then tried “wss://testnet.matic.network”, but still no luck.

Steve

If you are using https://testnet2.matic.network, dagger endpoint for Testnet2 is wss://matic.dagger2.matic.network. You can also play with it here: https://matic.network/dagger/play

@steve You can use the following:

  1. For RPC connections : https://testnet2.matic.network
  2. For Websocket connections: wss://testnet2.matic.network
  3. For dagger: wss://matic.dagger2.matic.network
  4. For viewing transaction status : https://explorer.testnet2.matic.network

Thank you, wss://matic.dagger2.matic.network for Dagger is working great.

But I’m having trouble obtaining the logs data. When I use:

dagger.on('latest:log/' + <address />, function(result) {
    console.log(result);
});

I get an object containing various data, including the blockHash, transactionHash, blockNumber, etc. But I’m unable to obtain the logs data. However, when I check Remix I see the data is being returned. For example:

logs: [
    {
	    "from": "0xabff528a6b58fa554d46aec318f025212be89a96",
	    "topic": "0x11a679e84067f3949dee0d8352a5dd5ec9ebd4d70500c2bdd23f0d78abd3f61d",
	    "event": "SpinResult",
	    "args": {
		    "0": "224",
		    "1": "0",
		    "_number": "224",
		    "_amountWin": "0",
		    "length": 2
	    }
    }
]

Is being returned in Remix, but not from Dagger. How may I obtain this data?

@steve

As discussed in our TG chat, you can use the event config log/contractAddress/filter/topic0/topic1 for finer querying. You will need to add the indexed parameter to your events in the contract for this.

Thanks, adding the indexed parameter got it working.