To use NeonEventListener you can simply call its constructor passing the network you want to use.
import { NeonEventListener } from '@cityofzion/neon-dappkit'
import { default as Neon } from '@cityofzion/neon-js'
const eventListener = new NeonEventListener(NeonEventListener.MAINNET)
You can also pass a custom RPC endpoint:
const eventListener = new NeonEventListener('http://127.0.0.1:5001')
Everytime the specified event is triggered, the callback will be called
const contract = '0x123456'
const eventname = 'Transfer'
const callback = (event) => {
console.log(event)
}
eventListener.addEventListener(contract, eventname, callback)
Use this method to unsubscribe from the event, avoiding memory leaks
eventListener.removeEventListener(contract, eventname, callback)
You can also use the method removeAllEventListenersOfContract
or removeAllEventListenersOfEvent
.
On the following example we are waiting for a transaction to be completed, and then we are only confirming it has the
HALT
state.
const appLog = await eventListener.waitForApplicationLog(transactionId)
eventListener.confirmTransaction(appLog)
You can also confirm the transaction has a specific event, and if the return value of the transaction is true with the following optional parameters:
const appLog = await eventListener.waitForApplicationLog(transactionId)
const eventToCheck = {
contract: '0x123456',
eventname: 'Transfer'
}
const confirmStackTrue = true
eventListener.confirmTransaction(
appLog,
eventToCheck,
confirmStackTrue)
For more details on the methods signature, check the auto-generated Docs and the Source Code.