NEM Notify Plugin for WordPress

NEM Notify is a WordPress plugin that lets you monitor a NEM address and receive an email any time new payments or tips are received. You can also monitor your delegated harvesting node and get notified if it gets rebooted and stops harvesting for you.

I use it to automatically monitor my account for payments and tips, as well as keep an eye on my harvesting status, without having to keep checking in NanoWallet or in the blockchain explorer.

The plugin also includes a shortcode which lets you display the count of specified mosaic owned by your NEM address on a WordPress page or post (defaults to your XEM balance). For example, my NEM address holds 0 NEM Pioneer award mosaics.

With WordPress powering around 25% of websites on the internet, I hope this will be useful as a template for any WordPress developer looking to develop for NEM (or any NEM developer looking to develop for WordPress) as well as making NEM website integration immediately accessible to non-technical WordPress users.

How it works

The plugin uses the NEM API to check for incoming payments to your nominated NEM account, as well as the status of your delegated harvesting account. It runs a check hourly, using the WordPress inbuilt cron scheduling facility.

If it finds new payments, or isn’t able to verify that your node is still harvesting for you, then it will send an email to the WordPress admin.

NEM Notify lets you know when new tips or payments come in, and if delegated harvesting stops

The mosaic count shortcode works by checking the mosaic count for your NEM address via the API and then caching the result for an hour (or as long as you choose). It then displays the balance of the particular mosaic you specified, or the nem:xem mosaic (i.e. your balance) if no other mosaic is specified.

And because it’s written as a WordPress plugin, it’s simple and familiar enough to be used by non-technical NEM users, as well as working ‘out of the box’ for around 25% of websites on the interwebs.

Diving Deeper

The plugin attempts to tackle a number of common transaction handling tasks, so I’ve tried to make it as easy as possible to pull apart and use for your own needs.

Here’s a brief overview of the file structure:

/nem-notify.php – this is the bootstrap for the plugin. It simply defines a couple of path constants, registers an autoloader for the other class files, and then activates the core controller class. Nothing to see here.

/lib/class-nem-notify.php – this is the core controller. It sets up the WordPress admin settings page, and then registers two cron jobs: one to keep an eye on your NEM account and the other to keep an eye on your delegated harvesting account.

/lib/class-nem-api.php – this is where all the NEM magic happens. It contains the transaction lookup and processing methods, as well as a method to check your harvesting status.

The challenges faced in creating this plugin were:

Lookup order: The API returns a maximum of 25 transactions at a time, and “pagination” is achieved by specifying the hash of the latest transaction to return. In other words, the API looks backwards 25 transactions from any specified transaction hash.

In this plugin, however, I wanted to look forward from the last ‘seen’ hash. So I had to build a Nem_Api method called get_transactions_since() to iterate backwards, looking up transactions until I reached the one I’d seen before.

Transaction ID: The first iteration of this plugin used the transaction id (meta->id) to do lookup pagination. But I found this changes node by node, so was not reliable when using node redundancy! So although you can paginate with hash or id, in reality, transaction hash is the only reliable option if you query more than one node.

MultiSig Transactions: MultiSig transactions store the details of the underlying transfer in a different part of the transaction object to non-MultiSig transactions, so I created a Nem_Api method called get_transaction_data() to normalise the transfer data for other methods.

XEM as a Mosaic: NEM’s native currency, XEM, is just another regular Mosaic, so can be transferred to another account either as a ‘payment’ or as a mosaic transfer. This is why the Nem_Api method, get_transaction_amount(), loops though the mosaics and adds any XEM it finds to the total transfer amount.

API Ports When I first put the plugin onto this site, it just wouldn’t work! After much head scratching and debugging, I realised that the server wasn’t allowing traffic out on NEM’s API port. It was a simple matter to add port 7890 to the firewall’s allowed outgoing ports, but figuring out that was the issue took some time 🙂