Watching orderbook events

Next let's write a method which will track events as they happen on the orderbook:

  1. The watchOrderbook method tracks the orderbook smart contract for any event. When a new event is received, that is, a new offer is submitted to the orderbook, it refreshes the orderbook to add the new event:
watchOrderbook() {
let app = this;

var contractOB = new app.watchweb3.eth.Contract(this.OrderbookABI.abi,this.OrderbookABI.address);

We start by instantiating our orderbook contract through the watchweb3 web3 object.

  1. Next, we get the latest block number from the blockchain. watchOrderbook starts watching for events after the orderbook is initialized and set for the first time. Thus, it starts watching from the block, after which the app is initialized:
app.watchweb3.eth.getBlockNumber(function(error,response){
if(response)
{
let lastBlock = response;
  1. Next, we use the web3 object to track all the events on our orderbook contract instance:
contractOB.events.allEvents({
fromBlock: lastBlock+1 },
function(error, event){
console.log("Event",event);
app.setOrderbook();
}).on('error', console.error);

We track all events from the orderbook contract after the block where the listener is first initialized. Any time there is a new event, the listener logs a new event to the console and calls the setOrderbook method in order to set the orderbook again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.27.178