Candy Observer: Monitor Solana for newly minted NFTs in real-time 

Overview

NFTs generated a total of more than 40B transaction volume in 2021, and still going. There are new tokens generated every second, and this number increases exponentially day by day.

Solana, with close to 2.5M active monthly accounts, 0.4 second network confirmation time, and cheap transactions, is a natural fit for the NFT market, among other things.

This is confirmed by Metaplex, the biggest NFT protocol on Solana, that generated over 5M NFTs in less than a year since its launch.

Would it be useful to have a tool that sends a notification every time an NFT is minted on Solana and does so in real time?

With Solana being a recent addition to Chainstack, I am going to explain the inner workings of how Metaplex works, how Solana works, and how to listen to the Solana network over WebSocket through a Chainstack node.

And I’m going to do it through Candy Observer—a lightweight and installation-free tool that you can create yourself and that demonstrates how to listen live to NFT minting on Solana.

Prerequisites

A Solana mainnet node with a WebSocket endpoint:

To get one:

  1. Sign up with Chainstack.
  2. Deploy a Solana node.
  3. Get the deployed node’s WSS endpoint.

Using Candy Observer

The live code:

Candy Observer is a lightweight web app.

Whenever a new Solana NFT is created using Candy Machine—a Metaplex program, the minting event is captured by Candy Observer and displayed to the user:

A sample of results received
  1. Open Candy Observer.
  2. Enter the WebSocket endpoint address in the text field.
  3. Click connect.
  4. Wait until the status changes to Connected, then click start.

Simple as it is, now the code is up and running. The app monitors the Solana network for new NFTs. When a new token is created, the app gets notified about the transaction.

Clicking the public address that the Candy Observer displays will get you to the Solana explorer with the minted NFT information.

That is everything about the web app itself. If you are interested in learning more about the Candy Machine, NFT minting on Solana, subscribing to programs, or how to extend this app, the rest of this article elaborates on these. Read it, it’s fun and educational.

Inner workings

Metaplex and Candy Machine

Candy Observer is watching the Candy Machine v2 program—the most popular tool for NFT minting on Solana.

Thousands of creators upload their art pieces and mint NFTs using Candy Machine every day. All NFTs are searchable on the Solana explorer with the public address:

Metaplex is a decentralized protocol and a collection of tools designed to facilitate launching and selling of NFTs. Candy Machine is one of the major projects owned and maintained by Metaplex.

The process of creating a new NFT is called “minting“. Minting is always a transaction. For the full flow of a token mint with Metaplex, see Metaplex docs: Creating the Token Mint.

To study this process with a real-life example:

  1. Visit the Solana explorer.
  2. Search for “NFT candy machine program”.

A “program account” page for the Candy Machine is opened. The public address of the Candy Machine program can be found here too. This is the address hardcoded in the Candy Observer web app.

Scrolling down the page, all the transactions for this program are listed below. Hit the Refresh button to get the latest transactions.

Click on any of the signatures, the transaction detail page opens.

The address of the newly minted NFT can be found here.

As well as the details of the minting process.

On-chain programs

Notice all the programs involved in the transaction? They are on-chain programs—a unique feature on Solana.

Think of on-chain programs as pieces of customized code running on the network itself. They are stored in a special “account” called program account.

The Candy Machine program is addressed at cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ, which you have already seen in the previous step.

The Candy Machine program facilitates various other system programs to conduct the minting process and makes the process easy for users.

WebSocket endpoint

An endpoint is the door to the blockchain network. Solana’s free WebSocket endpoint is wss://api.mainnet-beta.solana.com, which times out every minute. For best experience, it’s good to create your own endpoint (as stated in the Prerequisites section) on Chainstack with a free developer account, there is no usage fee either.

WebSocket subscription

Solana provides a set of RPC APIs which are unique to the protocols.

Candy Observer leverages on the logsSubscribe API to constantly listen to the network and receive notification whenever an NFT is “minted”.

The initial JSON message looks like this:

var message = JSON.stringify({
  "jsonrpc": "2.0",
  "id": 1,
  "method": "logsSubscribe",
  "params": [
    {
    "mentions": [ "cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ" ]
    },
    {
    "commitment": "finalized"
    }
  ]
})

When running, the app subscribes to the global transaction logs and finds any transaction that is related to the Metaplex Candy Machine. The response object contains its public address, slot number, and detailed instructions of the transaction.

Candy Observer code

The implementation is straightforward—there are just three main parts (see below).

Initializing the WebSocket:

socket = new WebSocket(websocketurl);

Sending an initial message, which initializes listening on all transactions to catch the ones that contain the Candy Machine’s public address:

async function init() {
    if (socket) {
        var message = JSON.stringify({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "logsSubscribe",
            "params": [
                {
                "mentions": ["cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ" ]
                },
                {
                "commitment": "finalized"
                }
            ]
        })
        socket.send(message);
        document.getElementById("info").innerHTML += "<p>Start listening</p>";
        console.log("start listening")
    }
}

Whenever a token is newly generated, a message is received over the WebSocket, the information is shown on the page:

socket.onmessage = function(message) {
  try{
    data = JSON.parse(message.data);
    infodiv.innerHTML += "<p>New token: <a href='https://explorer.solana.com/tx/" +data.params.result.value.signature+ "' target='_blank'>"+ data.params.result.value.signature+"</a></p>";
    console.log(data)
    console.log(data.params.result.value.signature)
  }
  catch(error){console.log(error)}
};

Conclusion

Solana is unlike any of the other blockchain protocols—it has its own concepts and technology, and it is fun to play with. Try it out.

Read more about the Solana programming model, JSON RPC API, and try out a detailed tutorial on how to deploy your own program on Solana.

Happy coding!

  • Connect to the Ethereum, Polygon, BNB Smart Chain, Avalanche, Fantom, Solana, Harmony, Tezos and StarkNet mainnet or testnets through the interface designed to help you get the job done.
  • Get access to the Ethereum, Polygon, BNB Smart Chain, Avalanche, Fantom, and Tezos archive nodes to query the entire history of the mainnet—starting at just $49 per month.
  • Choose where you want to deploy, and we will provide you with the dedicated managed infrastructure that can handle high-volume, high-velocity read/write access to the network.
  • To learn more about Chainstack, visit our Knowledge Center or join our Discord server and Telegram group. 

Have you already explored what you can achieve with Chainstack? Get started for free today.

ERC4626: A new standard for DeFi applications

Standards like ERC20 and ERC721 are the building blocks that allowed hundreds of apps to appear in recent years. ERC4626 is the latest one for creating tokenized vaults.

Antonio Ufano
May 27
Chainstack uses cookies to provide you with a secure and
personalized experience on its website. Learn more.