How to watch every Nano transaction— Developing with Nano Currency

SomeNano
SomeNano
Feb 17 · 8 min read

Hello, I am glad you are here! Welcome to one of my articles on “Developing with Nano Currency”. These articles aim to teach developers, coders, and tinkers alike how to write code that integrates Nano into any application.

If you are new to writing code with Nano (cryptocurrency), I highly recommend you read my “Getting Started” series. It is a three-part series where I aim to take you from zero knowledge of Nano to fully integrating and interacting with the Nano Network in your applications on both client and server-side.

You will start by building your foundation in Part 1 to understand the terminology and design of Nano as in pertains to you as a developer. You will then write code in Part 2 that interacts directly with a Nano Public Node to create Nano Accounts and send/receive Nano transactions.

And finally, in Part 3, you will write code that interacts with existing Programmable Software Wallets that make the integration of Nano into your applications simple and seamless. In all articles you can use free Nano from a Nano Faucet, which after all is said and done, as Nano has zero fees, is yours to keep.

If you find any errors in this article, contact me here on Medium (SomeNano), send me an email (jason@somenano.com), or find me on Twitter (@SomeNanoTweets).

Disclaimer: I link to many apps/frameworks/projects with which I have no association. While I have personally used every app/framework/project that I reference, I can not guarantee its security or availability. When in doubt, never use a “seed” or private key given from software you don’t know to be secure. Also, I have no association with the Nano Foundation and nothing in these articles should be taken as “Official” guidance. I am simply an advocate for Nano and want to support the Nano Community, and specifically those looking to code with Nano, as best I can.

Introduction

So you want to watch every single transaction that traverses the Nano Network? I can totally sympathize and understand this desire. Whether you are curious about the contents of transactions moving across the Nano Network or you are interested in meta aggregation and counting how many transactions move across the Nano Network, this article is a great place to start.

I myself have a few applications that do just this. I really enjoy Nano Network Visualizers. These are applications that visually depict activity that is occurring on the Nano Network. In my case, I monitor Nano Blocks for confirmations and display properties of that Nano Block (Nano Account, transaction amount, etc…) visually. The speed and throughput of Nano is stunning and can make for an impressive visual.

Now before we get started, to simply level set our knowledge base, I am going to assume you have worked through my “Getting Started” series of articles and have a fairly good understanding of how Nano works. To work through this article, specifically, you should already understand that a Nano transaction is actually comprised of Nano Blocks which contain the state of a Nano Account and has a subtype of “send”, “receive”, or “change”. You should also already understand the units of Nano and know that a Nano is actually comprised of 10³⁰ raw. And finally you should already understand that a Nano Blocks is “confirmed” across the Nano Network via a decentralized consensus mechanism called Open Representative Voting (although you do not need to know the details of how ORV works).

And on the topic of Nano Block confirmations, I will repeat the same caution that I offered in Part 2 of my “Getting Started” series. If you are taking action on the notification of certain confirmed Nano Blocks, whether through WebSockets or HTTP Callback, you need to check that you haven’t observed that Nano Block previously. You need to maintain a log of hashes from the hash field. If you’ve already seen that hash and taken action, skip, simple. This caution also comes from the Nano Docs.

Nano Node WebSocket

There are two options to watch every Nano Block that is confirmed across the Nano Network. The first option, as we’ll work through in this section, is to connect directly to a Nano Foundation designed Nano Node (whether it be your own Nano Node or a Nano Public Node) and communicate with the Nano Node’s WebSocket. The second option, as we’ll work through in the next section is to use my SomeNano Repeater.

The Nano Node provides quite a few capabilities via WebSocket, confirmations of Nano Blocks being just one. The problem, however, is that making this WebSocket available to a client-side application that does not require any server processing (like a website hosted on GitHub), is likely not in your best interest.

While the hardware recommendations for running a Nano Node are not all that intense, given a set of circumstances when network throughput is high and traffic to your website/subscriptions to your WebSocket is high, the performance of your server might suffer causing a delay to local confirmations. Additionally, some WebSocket communications are quite chatty, and if you aren’t proxying which requests are and are not allowed through your WebSocket, your outbound bandwidth may skyrocket.

But those are just considerations and decisions for YOU to make, not me. Maybe you want to subscribe to your own WebSocket on your own server and not make it available to an unlimited number of clients. Whatever your design is, I’m here to help you get there.

So, here is a JSFiddle of a WebSocket communicating with a NanoNode WebSocket.

In this example we are using the Public Nano Node that is provided by My Nano Ninja (wss://ws.mynano.ninja/). You can see on my listing of Public Nano Nodes that not many Public Nano Nodes allow subscriptions to all Nano Addresses. This is likely due to the reasons I have mentioned above.

Digging into the code, we create a new WebSocket('wss://ws.mynano.ninja') and then onopen send the subscription request:

{
action: 'subscribe',
topic: 'confirmation',
ack: true
}

As documented in the Nano Docs, we can pass an optional ack: true to receive an acknowledgement via the WebSocket that our request was processed successfully. It is returned with a timestamp.

{
ack: "subscribe",
time: "1611955940314"
}

When the WebSocket receives a message it calls the onmessage function. We parse the JSON string to an object JSON.parse(response.data) and check the topic property of the object. If it is not “confirmation”, we simply ignore. So, even though we request the acknowledgement, as it doesn’t have a topic property, we don’t take any action on it.

Here is an example of response.data:

{
message: {
account: "nano_1pnano8...",
amount: "2853000000000000000000000000",
block: { ... },
confirmation_type: "active_quorum",
hash: "A0BC0FF6..."
},
time: "1611956673890",
topic: "confirmation"
}

In most cases you will likely care most about the content of the message property. So we pull out the message and send it to a message handler function for processing, which in our case, is just printing to the console.

Here is an example of message:

{
account: "nano_3ysa8x...",
amount: "100000000000000000000000000000",
block: {
account: "nano_3ysa8x...",
balance: "100000000000000000000000000000",
link: "5B232F1752241...",
link_as_account: "nano_1ps57wdo6...",
previous: "000000000000...",
representative: "nano_3pczxu...",
signature: "97ED0D078A9B...",
subtype: "receive",
type: "state",
work: "470c5a880956a437"
},
confirmation_type: "active_quorum",
hash: "ED883278..."
}

One last item I’ll leave you with on Nano Node WebSockets is some additional example clients. The JSFiddle I provide is the simplest no-nonsense JavaScript that I could come up with. If you are a Python or Node.js developer, check out these example clients from the Nano Docs.

SomeNano Repeater WebSocket

Another option for watching every Nano Block confirmed across the Nano Network is to use my SomeNano Repeater (open source).

The Repeater operates similar to the Nano Node WebSocket, except that it doesn’t have any subscription options and it doesn’t send a WebSocket message for every single Nano Block. The repeater is a middleman that subscribes to my own Nano Node, “buckets” Nano Blocks for a certain amount of time (5 seconds as of today…) and then sends out a message that includes an array of all the Nano Blocks that were confirmed in that window. This cuts down significantly on network actions during times of high confirmations per second.

This is the WebSocket that I use for both SomeNano Snow and SomeNano Plinko.

Here is the JSFiddle:

You will see that the code is largely the same as our first Nano Node WebSocket example. The difference is in the data that is returned:

Example response.data:

{
blocks: [ ... ],
cps: 0.7666666666666667,
dtg: "2021-01-29T21:57:18.564Z",
duration: 5000,
topic: "confirmation"
}

The blocks property is an array of what in the Nano Node WebSocket example was contained in the message variable. cps contains the calculated confirmations per second (over the last 30 seconds) according to my Nano Node. dtg contains a Date string for the time of WebSocket message transmission. duration contains the window of time (previous the dtg) covered by all the Nano Blocks in blocks. And we still have the topic property.

You will see in the output that I do not dump every Nano Block object but instead provide a short summary statement:

"Fri Jan 29 2021 4:05:48 PM: Received 5 new blocks from the SomeNano Repeater from the last 5 seconds with 0.50 confirmations per second over the last 30 seconds."

While iterating over all the Nano Blocks in the blocks array, I do still call the message handler, however, I take no further actions.

Conclusion

Watching every single confirmed Nano Block on the Nano Network is really quite an interesting exercise. There are countless applications of this information and I hope you found this article valuable in bringing your ideas to life. I always love to see what people create with Nano and would love to give it a shout out from my podium on Twitter (SomeNanoTweets).

And that’s it! In this article we…

  1. Subscribed to a Nano Node WebSocket and parsed confirmed Nano Blocks in real-time
  2. Subscribed to the SomeNano Repeater and parsed confirmed Nano Blocks in real-time

As a final capstone and comprehension test of what we covered I’ll give you a challenge.

Write a script that watches all Nano Blocks with subtype send for a corresponding Nano Block with subtype receive. And then keep track of the fastest “send” to “receive” duration.

I have linked many references throughout this article and I hope you keep learning and digging deeper into Nano.

While you are here on Medium, I recommend you also read articles from Nano Education, Nano, and the Nano Publication. If you are on Twitter, feel free to follow me @SomeNanoTweets. If you are on reddit, Nano has a phenomenal subreddit with a huge amount of helpful and highly intelligent people. And if you are more of a real-time chat sort of person, check out the Nano discord where there is a specific dev channel to help aspiring Nano “BUIDLers” like you and me.

~ SomeNano

Ps, these articles are a lot of fun to write but they take A LOT of time. I wrote it to support Nano Community developers. And I would be highly appreciative if you also consider supporting me in return. If you found this article useful, please consider sending a donation.

nano_1somenanerzdzdbyr4y6x996qbo764ifxk8beuhphydmwn1agpzkwquqqick (NanoCrawler link)

Nano

The best place for all of the latest Nano updates…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store