Apart from sending transactions, an exchange also needs a way to listen for transactions sent to their addresses (deposits). We won't cover how this can be done for ERC20-like smart contracts on Zilliqa in this tutorial, but the same strategy can be applied.
The code in this tutorial is derived from the example application.
To implement a simple and familiar polling mechanism in Node.js, we will use a few additional dependencies:
Implementing the Handler Function
We will use a simple
DepositCron to set up our cron job.
We'll start by implementing a handler method, aptly named
handler. We are taking several steps:
- Fetch the current
- We compare the value of the current
TxBlockagainst the one we have recorded using
- If there is a difference, we fetch all transactions that have been
lastFetchedTxBlock + 1and the current
- i.e., everyone transaction we have missed.
- We then call
svc.getDepositsfor every transaction processed in that span of blocks. It compares the
toAddrproperty of each transaction against the
addressesarray we passed to the
constructor, checking if it contains our
toAddr. If so, then a transaction to an address we are interested in has occurred.
Starting the Cron Job
So far we have no way of starting up or controlling our
CronJob. We'll do that by
Now that we have our methods, we can use the cron job like so: