Developing Games or Applications with the NEM Blockchain and Node.js

NEM logo

Introduction

Dear readers,

today I am going to share a piece of my development experience on the PacNEM — https://www.pacnem.com — game developed using Node.js and the nem blockchain (Feb. 2017 to Jul. 2017). Details of the implementation will be posted along with this article in a more in-depth article.

This beginner’s guide aims to be simple and should only introduce you to a few basic features of blockchain related application and game development.

The nem blockchain provides a very intuitive HTTP/JSON API, also pretty easy to use. This post will describe an example of an easy first implementations you can do with javascript and a blockchain in a game or application development project.

This article uses the NEM-sdk package, which can be installed through npm as follows: npm install nem-sdk . You can also download the minified SDK javascript from Github and link it in your HTML like this: <script type="text/javascript" src="/js/nem-sdk.min.js"></script>.

Link to the SDK: https://github.com/QuantumMechanics/NEM-sdk.
Link to PacNEM: https://www.pacnem.com


Brainstorming and Documentation

Linking a blockchain to a game or application development project is a little more complicated than what you might think as it requires a good understanding of the Peer to Peer networks brought back to life by blockchains.

Yes, blockchains use Peer to Peer and so — each cryptocurrency’s blockchain is distributed across a network of nodes. These nodes also validate transactions for a protocol defined by the given cryptocurrency — this validation process is usually referred to as Consensus Rules.

This combination of Peer to Peer networks and Consensus Rules is a game changer in the software development industry. It gives us a totally new — trustless — software environment. We can now build apps whose outputs will be validated not by one server but by thousands of blockchain nodes — replicating our data — and making sure it is censorship resistant through distribution.

I have looked around for quite some time before I found an easy to use blockchain with genuine features and growth — Then I discovered nem.

In this article I will describe my first idea of saving PacNEM high scores to the NEM blockchain. I won’t cover all the details about Multi Signature Accounts, Namespaces & Mosaics, etc. in this article - I will only give you an idea of how easy it is to interact with the NEM blockchain and integrate NEM into your business layer implementations.

The source code for the complete game is the result of months of documentation about blockchains, NEM, trustware, Multi Signature Accounts, Mosaics etc. and a good chunk of hobby programming time and pure developing envy. The full source code for PacNEM can be found at Github.

The nem blockchain has a way of making things easy for developers. Using the NEM-sdk is quite trivial and requires only a little knowledge of javascript because the SDK can be integrated into Browser javascript or into your backend Node.js application source code.

Now, let’s do a quick Brainstorming session with following keywords on the Board :

Those are the main areas we will be trespassing for this article. Feel free to document and come back later if anything is too complicated and I’ll try to link this article with good sources for your reading too. The blockchain features, implemented with nem, are more complicated to explain than they are to implement!.

Work the [ne]magic

PacNEM Logo: https://www.pacnem.com

So, the needs for our example are pretty simple: we defined that we want to save high scores on the nem blockchain.

First key aspect of blockchain application development is that saving data to a blockchain will always happen in a process of sending a transaction which gets included in a block of the given blockchain. nem provides with different transactions types but we will need only one: Mosaic Transfer Transactions.

A transfer transaction on the nem blockchain can contain an amount, a message, a fee and attached mosaics. This already gives us multiple implementation choices for the high scores implementation:

  • We could save high scores in transactions with messages (the message is the score).
  • We could save high scores in transactions with mosaics (amount of mosaic is the score).
  • We could save high scores in transactions with both messages (for authenticity) and mosaics (for the score).

In this article, I will illustrate an example of the second implementation choice. This keeps it quite simple.

We now know that our high scores will be represented as an amount of a given mosaic published on the nem blockchain. We don’t want people to be able to forward or transfer these high scores to other accounts so we will create a non-transferable mosaic. If you don’t know about NEM Namespaces & Mosaics yet, I recommend reading this article.

Another constraint of blockchain application development is that we will need a XEM account that we will create for our game or application. Using the NanoWallet is quite feasible for everyone, right? Once you have created the account, copy your account address and private key somewhere safe. In this article, we will refer to the created Application Wallet as pacnem-business. This wallet should contain your business funds (mosaics).

You are now all set to start developing games or applications with the NEM blockchain. As a little test experiment, following snippet will retrieve the last block of the NEM Testnet blockchain:

Easy, wasn’t it? OK, this was only a test experiment that will let you test your NEM-sdk package installation.

As you can see, the SDK lets you query the blockchain for data out-of-the-box. Now lets get back to our horses and create an easy function to query a given account’s High Scores entries.

Blockchain is distributed, but NEM is an account-based Ledger. This means that you can query blockchain nodes through HTTP APIs to get current Account State Data. With this knowledge, it is now possible to determine which account has received our Mosaic pacnem:cheese.

This process will be quite resource expensive as you will need to query each account who received the Mosaic to retrieve their current Balances. What would be more optimized is to determine a specific paying account for this Mosaic pacnem:cheese and query only this account’s outgoing transactions to retrieve Player High Scores. Again, we will call it the pacnem-business account — which, in fact, is just your NEM wallet address.

Also worth noting, the NEM blockchain nodes will only allow you to retrieve data about 25 transactions per request. Some nodes accept a hash parameter and all nodes accept the id parameter. We will need to query the blockchain multiple times in case there is more than 25 transactions, this is why the following example uses recursion to wrap around the SDK Promises calls and make sure we retrieve all available transactions.

Ok, now let’s build the code snippet to read High Scores from the Blockchain using our wallet pacnem-business and the SDK. For the sake of simplicity I will store the data in a global object that we will re-use in later examples. To the source code:

Executing this snippet will give you a list of outgoing transactions. This was pretty straight forward, as I explained earlier the only little hick-up is about the 25 transactions limit for which we end up querying a HTTP API several times. Nevertheless I think this example keeps it simple enough.

This code snippet will be used later in other gists. The outgoing transactions list is what we will be using to retrieve our Hall of Fame data in the form of NEM blockchain Mosaics.

NEM Mosaics are Tokens published on the NEM blockchain. Using our outgoing transactions list, we will be able to determine the amounts of Mosaics. This was a first step-in to reading blockchain transactions with the NEM-sdk.


Use NEM blockchain transactions

Now we have all outgoing transactions for the given pacnem-business account and all we need to do is check in these transactions to find all transactions containing pacnem:cheese mosaics.

NEM makes it really easy to read Mosaic data in blockchain transactions. Mosaic Transfers Transaction can be differentiated from others through their mosaics field containing an array of attached Mosaics. This is very simple too, so let's tackle the snippet to retrieve exact latest scores on the NEM Blockchain:

With this executed, you have successfully read your first High Score list ever from a blockchain.

Those code snippets are relatively long but they are also kept very simple and I think with this you should be able to interpret and use your first NEM blockchain transactions without problems.

The NEM-sdk for Node.js offers a lot more features. I recommend you have a read at: https://github.com/QuantumMechanics/NEM-sdk. This SDK implements pretty much anything needed to interact with the NEM blockchain and even allows connecting to nodes with Websockets!

Lots of projects have been started using the NEM blockchain and we can only be happier to see even more coming!

The NEM blockchain offers advanced blockchain features out-of-the-box and provides a great ground layer for any blockchain related Game or Application development project.

Remember, if you are new to the blockchain industry — this requires some thinking ahead — plan those extra hours of studying blockchain data if you don’t know about it yet!


Conclusion

I hope everyone could follow along with this article. I will be posting more articles about NEM and blockchain integrations in the Software Industry in the coming weeks and months. I hope you enjoyed it!

I would love some inputs/feedback on my latest projects:

This article and the PacNEM project are published Open Source. Feel free to send donations to one of the following addresses:

  • NEM/XEM: NCK34K5LIXL4OMPDLVGPTWPZMGFTDRZQEBRS5Q2S
  • Bitcoin: 1Js4Tz3KSC6PAbvzy4xHga5caQLtKLysye

Credits to Telegram user @spizzerb (Patrick) for the PacNEM logos !!! Thank you a lot Patrick!

See you soon*ish*!

Greg
Github: https://github.com/evias
Twitter: https://twitter.com/eVias


One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.