This example listens for OrderFill events emitted from the OrderProcessor contract using a websocket connection.
This example can be expanded to include listeners for other types of events, including OrderCancelled and OrderFulfilled which represent final states that close out the order request.
After an ethers provider has been created using the websocket RPC URL, an event filter is created to listen for OrderFill events pertaining to a specific account. Then, the process waits for matching events.
// ------------------ Listen ------------------// fill event filter for a specific accountconstfilter:ethers.EventFilter=orderProcessor.filters.OrderFill();// Listen for new OrderFill eventsorderProcessor.on(filter, (orderId: ethers.BigNumber, paymentToken: string, assetToken: string, requesterAccount: string, assetAmount: ethers.BigNumber, paymentAmount: ethers.BigNumber, feesTaken: ethers.BigNumber, sell: boolean) => {
console.log('New OrderFill event detected');if (requesterAccount.toLowerCase() ===requester.toLowerCase()) {console.log(`Account ${requesterAccount} Order ${orderId.toString()} filled. Paid ${feesTaken.toString()} fees.`);if (sell) {console.log(`${assetToken}:${assetAmount.toString()} => ${paymentToken}:${paymentAmount.toString()}`); } else {console.log(`${paymentToken}:${paymentAmount.toString()} => ${assetToken}:${assetAmount.toString()}`); } }});