NFTs on Solana: Minting with Wonka.js and Candy Machine

Zorayr Khalapyan
4 min readFeb 3, 2022

Context: In the first write up NFTs on Solana: Minting 101, we talked about minting from solana’s command line tool. If we are building a real NFT project, we probably want to mint on demand from a dApp. So let’s see how far we can go using some JS or TS to mint Solana NFTs. If you want to jump straight to the solution, see Next.js Wonka Example App.

Ecosystem & Tools

Before we jump into a solution, let’s look at some of the available tools, standards and libraries within the ecosystem.

  1. Solana Web3.js Interface: The web3.js equivalent for Solana. This would be similar to minting on the solana CLI, but through a JS library. How to Mint an NFT on Solana gives a basic coverage. One issue here is that you’d need to come up with your own logic to store the NFT metadata, somewhere to store logic for paying of royalties, etc. This would be the bare more implementation, and we should probably not use it directly.
  2. Metaplex Ecosystem: Metaplex was one of Solana’s strategic initiatives building an ecosystem around Solana NFTs. You can think of it as two large parts: (a) a set of four programs which are like contracts for Solana that govern minting and auctioning, and (b) a handful of projects built around these programs like a storefront builder and an airdrop tool. You can if you want mint directly through metaplex’s JS APIs or CLI tools. It would require some understanding of the architecture 👷‍♀️ but it should work.
  3. Metaplex Candy Machine: Could be considered as a minting workflow manager that’s built on top of Metaple that takes care of some of the missing parts from the core metaplex architecture. Has a command line CLI for uploading assets to Metaplex and Arweave, and some other tools for minting and kicking off an auction. It’s a bit complicated to understand, but provides enough value to be used when building simple NFT projects.
  4. Wonka JS: This is a simple JS layer built on top Candy Machine to minimize the amount of wiring required for minting and processing metadata. We built this in house to make minting through APIs as simple as possible.

1. Create a Candy Machine

For a simple minting flow, we’ll use Candy Machine + Wonka JS. To create a candy machine, you can think about the high level steps below:

  1. Prepare the PNG assets with their associated JSON metadata.
  2. Create a candy machine with the PNGs (under the hood, the CLI will upload the PNGs to arweave and create metadata accounts pointing to the correct URI).
  3. Store the candy machine ID in an .env file inside your web app so you can use it with Wonka.

The best way to get the above done is to follow the steps in Candy Machine: Getting Started documentation.

When you have the candy machine ID ready, continue to the next section 🙂

2. Set up a Web App

Now that we have our Candy Machine setup & ready to mint, here is what we need to do to get our web app ready:

  1. Kick off a Next.js web app on your terminal. You can also use React, but Next has the benefit of API Routes which are helpful for building simple backend requests.
  2. Store your Candy Machine ID and Solana setup under your .env file.
  3. Authenticate the user. The very basic idea here is that if the user has one of Solana’s wallets installed, then you will have access to window.solana. If you have access to this, then you can Provider, which is a wallet wrapper from Project Serum widely used in Metaplex. Think of it as just a “wallet provider”. You generally don’t want to use window.solana directly, so going through something like wallet-adapter-react which has an interface for various solana wallets is probably better. Good discussion about this topic here: Is there a way to fetch Project-Serum Provider from Wallet Adapter?
# .env
NEXT_PUBLIC_CANDY_MACHINE_ID=...

If you want to see a working example, take a look at Wonka’s Next.js Example App. It’s a working version that takes care of all three steps above.

3. Mint with Wonka

The function to use for minting is mintCandyMachineToken(..). If we have already authenticated the user, we should have access to a Provider and can create an instance of Wonka with our candy machine ID.

And that’s pretty much it — enjoy minting! If you need to use Wonka through terminal, also checkout Wonka CLI:

--

--