Creating an NFT on Solana with Metaplex Candy Machine

Creating an NFT on Solana with Metaplex Candy Machine

After what felt like walking through a minefield trying to figure out how to create my own NFT on Solana with the current guides, I decided to put together a guide myself in a very beginner-friendly fashion.

I won’t be taking a deeper dive into the specifics of how everything works, but rather help people with zero knowledge of rust obtain a general understanding of how to create their very own NFT on Solana with the help of a Metaplex candy machine and a web starter kit.

What is a Metaplex Candy Machine?

Metaplex is a command-line tool that interacts with the candy-machine program. In this guide, we will use it to:

  1. Upload your images along with their metadata to arweave, then register them on the Solana blockchain.
  2. Verify that the state of your candy machine is valid and complete.
  3. Mint individual tokens.

Candy Machine is a system that manages fair mints.
• The minting process starts and finishes at the same time for everyone.
• It won’t accept your funds if there are no more NFTs to sell.

Prerequisites

  1. NodeJS (version 14.17.6)
  2. SolanaCLI
  3. Metaplex CLI
  4. Phantom Wallet

If you have a different version of Node installed on your system, you can use nvm which allows you to quickly install and use different versions of the node via the command line.

Getting Started

Installing The SolanaCLI

Solana already has really well-made guides on installing and using the Solana command line.
• Install Solana Command Line Tools

Using Devnet

Devnet is really useful for developers to test out their programs and applications.

You can set your default Solana URL to devnet using:

$ solana config set --url https://api.devnet.solana.com

Now, let’s create a devnet wallet:

$ solana-keygen new --outfile ~/.config/solana/devnet.json

Remember to store your seed phrase somewhere safe.

I highly recommend making devnet your default keypair

$ solana config set --keypair ~/.config/solana/devnet.json

Funding The Devnet Wallet

Firstly, let’s make sure that we’re on devnet by checking the configuration.

$ solana config get
//Output:
Config File: /Users/dev/.config/solana/cli/config.yml
RPC URL: https://api.devnet.solana.com
WebSocket URL: wss://api.devnet.solana.com/ (computed)
Keypair Path: /Users/dev/.config/solana/devnet.json
Commitment: confirmed

Now, let’s fund that wallet:
Firstly, We check the balance of our current wallet.

$ solana balance 
//Output:
0 SOL

Then we airdrop the amount of SOL to our wallet. Remember, the amount is capped to 5 SOL.

$ solana airdrop 4
//Output
Requesting airdrop of 4

Signature: Transaction Signature

4 SOL

Now, Let’s check our balance to confirm the airdrop was successful.

$ solana balance
//Output:
4 SOL //This can vary depending on the balance you initially had.

In case you’re confused by any of the above steps, you can check the manuals to get a better understanding by running:

$ solana help config
$ solana help balance
$ solana help airdrop

Configuring Phantom Wallet

After setting up your phantom wallet, we can link our newly created devnet wallet above to phantom.
To do so first, we need to open its settings, click on Change Network and select Devnet

Now, we need to obtain the devnet wallet private key. To obtain that, open your terminal and cat to view the contents of the keypair.json file

$ cat .config/solana/devnet.json
//Output:
[12,22,.....]

Now copy the output you received and open the phantom wallet. Click on the top left navigation menu and click on and then click on and give it a suitable name and paste the contents we copied before in the Private Key field.

You should be able to see 4 SOL in your wallet.

Running Candy Machine CLI

Please ensure that you have node and yarn installed before proceeding.

Also, install <code>ts-node</code>&nbsp;by running:<br><code>$ npm install -g ts-node</code>

Now, let’s clone the metaplex project into the location of your choice.

$ git clone https://github.com/metaplex-foundation/metaplex.git
$ cd metaplex/js
$ yarn install &amp;&amp; yarn bootstrap &amp;&amp; yarn build

To run the command line utility,

$  ts-node metaplex/js/packages/cli/src/candy-machine-cli.ts help
Usage: candy-machine-cli [options] [command]

Options:
  -V, --version                          output the version number
  -h, --help                             display help for command

Commands:
  upload [options] &lt;directory&gt;
  verify [options]
  verify_price [options]
  create_candy_machine [options]
  update_candy_machine [options]
  mint_one_token [options]
  sign [options]
  sign_candy_machine_metadata [options]
  help [command]                         display help for command

Organizing & Uploading Your Assets

For this guide, we will be using pre-made assets which you can download by clicking here. courtesy of solana-candy-factory
Place this assets folder in a suitable location.
Here is how you should organize your NFT files to upload:

ls assets | sort -n
0.json
0.png
1.json
1.png
2.json
2.png
3.json
3.png
....

You can notice that these files come in numerical pairs, that is 1.png and 1.json are the two halves of the NFT. The png file is the artwork and the json file contains the metadata.

The directory name does not matter. You can go with anything you like.

Validating Your Assets

This may feel tedious but it’s just as important. Check out the manual on carrying this out at
https://docs.metaplex.com/nft-standard

Uploading Your Project Assets

Now that we have the funds, assets all organized and validated, we can proceed with the fun stuff!

We will proceed with uploading our assets with the CLI. Remember, our assets are located at metaplex\js\packages\cli\example-assets

$ ts-node metaplex/js/packages/cli/src/candy-machine-cli.ts upload assets --env devnet --keypair ~/.config/solana/devnet.json

//Output
Processing file: 0
Processing file: 1
Processing file: 2
Processing file: 3
Done

By uploading, it sends the files to Arweave and also registers those files with your candy machine. Both Arweave and Solana are initialized after a successful run.

Validating Your Candy Machine

You can confirm the health and status of your on-chain assets using:

$ ts-node metaplex/js/packages/cli/src/candy-machine-cli.ts verify --env devnet --keypair ~/.config/solana/devnet.json
//Output:
Looking at key  0
Name {redacted-name} 0 with https://arweave.net/{redacted-tx-id} checked out
Looking at key  1
Name {redacted-name} 1 with https://arweave.net/{redacted-tx-id} checked out
Looking at key  2
Name {redacted-name} 2 with https://arweave.net/{redacted-tx-id} checked out
Looking at key  3
Name {redacted-name} 3 with https://arweave.net/{redacted-tx-id} checked out

Starting Your Candy Machine

After verifying that our assets are good to go, we can finally start the candy machine.<br><code>$ ts-node metaplex/js/packages/cli/src/candy-machine-cli.ts create_candy_machine --env devnet --keypair ~/.config/solana/devnet.json</code>

Updating Your Candy Machine

We can modify our candy machine details to include a start date and/or price etc.

$ ts-node metaplex/js/packages/cli/src/candy-machine-cli.ts update_candy_machine --env devnet --keypair ~/.config/solana/devnet.json --price 1 --date "29 Oct 2021 00:12:00 GMT"

Minting Our NFT

To mint our NFT, we can use mint_one_token like so:

$ ts-node metaplex/js/packages/cli/src/candy-machine-cli.ts mint_one_token --env devnet --keypair ~/.config/solana/devnet.json

If all goes well, you can now open your phantom wallet, open the collectibles page (It’s beside the $ symbol on the bottom), and voila! Your newly minted NFT will be there!

Setting Up The Web Starter Kit

Now that we’ve successfully minted an NFT into our wallet, let’s make a web application to carry out the mint instead!
Note: This project is very new and could cause some issues, if it does then please report it on Github.

The goal of the project is for you to be able to configure it and customize it to your liking.

Fork the project and then clone it to your desired location.
Link: https://github.com/exiled-apes/candy-machine-mint

Now we need to build the project,

cd candy-machine-mint
yarn install
yarn build

This is where things get a little bit complicated. When we uploaded our NFTs, a cache file was created in the same directory as our assets directory. However, this .cache folder is hidden! If you’re on Ubuntu, use ctrl+h to display hidden files.

Once you have discovered that folder, open it and you’ll find devnet-temp file. Open it in your IDE and you’ll see the following

{
    "program": {
        "uuid": "Ch3xxx",
        "config": "Ch3xxx"
    },
    "items": {
        "0": {
            "link": "https://arweave.net/xxxx",
            "name": "TEST",
            "onChain": true
        },
        "1": {
            "link": "https://arweave.net/xxxx",
            "name": "TEST",
            "onChain": true
        },
        "2": {
            "link": "https://arweave.net/xxxx",
            "name": "TEST",
            "onChain": true
        },
        "3": {
            "link": "https://arweave.net/xxxx",
            "name": "TEST",
            "onChain": true
        },
        "4": {
            "link": "https://arweave.net/xxxx",
            "name": "TEST",
            "onChain": true
        },
        "5": {
            "link": "https://arweave.net/xxxx",
            "name": "TEST",
            "onChain": true
        }
    },
    "env": "devnet",
    "cacheName": "temp",
    "authority": "9xJxxxx",
    "candyMachineAddress": "3Wmxxxx",
    "startDate": 1632615120
}

We’re going to need all this data when we run our web application to mint.

Configuring Candy-Machine-Mint

Open up the candy-machine-mint folder, where you will find a file called .env.example
(The file is usually hidden, use ctrl+h to display hidden files)
Rename it to .env and then open it to edit the following details:

REACT_APP_CANDY_MACHINE_CONFIG=__PLACEHOLDER__

This is the program.config key from our .cache/devnet-temp file.

REACT_APP_CANDY_MACHINE_ID=__PLACEHOLDER__

This is the candyMachineAddress from our .cache/devnet-temp file.

REACT_APP_TREASURY_ADDRESS=__PLACEHOLDER__

This is the Solana address that receives the funds gathered during the minting process. You can set this to your devnet wallet address.

REACT_APP_CANDY_START_DATE=__PLACEHOLDER__

This is the startDate key from our .cache/devnet-temp file.
Note: If you cannot find it, use update_candy_machine as mentioned above as you may have missed out on mentioning the date.

REACT_APP_SOLANA_NETWORK=devnet

This identifies the Solana network you want to connect to. Options are devnet, testnet, and mainnet.

REACT_APP_SOLANA_RPC_HOST=https://explorer-api.devnet.solana.com

This identifies the RPC server your web app will access the Solana network through.

Starting The Web Application

Open your terminal and navigate to the candy-machine-mint directory and start the react app by using yarn start

Once you see Compiled Successfully in your terminal, Open http://localhost:3000 to view it in the browser.

You can now proceed with connecting your wallet and clicking on the mint button.

After clicking on the mint button, you can check the collectibles page on your phantom wallet and you’ll see your newly minted NFT!

This article is available on my blog