How sell NFT using SPL Tokens + Candy Machine

To complete this tutorial you will need some knowledge using git and some other command line tools.


Create Tokens

First we create a Token with 1 decimal so each token’s value would be 0.1 (we will need 10 tokens to buy one NFT)

spl-token create-token --decimals 1

Create an account to hold the Tokens, using the TOKEN generated on previous step:

spl-token create-account <TOKEN>

Mint 40 tokens using the TOKEN generated on first step and the TOKEN_ACCOUNT generated on previous step:

spl-token mint <TOKEN> 40 <TOKEN_ACCOUNT>

Add Metadata to token

We can upload metadata like an image that will be displayed on wallets to represent our token. Go to the metaplex CLI folder:

cd metaplex/js/packages/cli

Create the folder metaplex/js/packages/cli/token-metadata and make sure it an image called 0.png and a metadata json file called 0.json. Upload the metadata to Arweave:

build/candy-machine-cli.js upload -e devnet -k ~/.config/solana/devnet.json ./token-metadata

This will generate a cache file at metaplex/js/packages/cli/.cache/devnet-temp with the results.

Now go to the metaplex/rust folder:

cd /metaplex/rust
compile the function for adding metadata:
cargo build --bin metaplex-token-metadata-test-client
cd ./target/debug

Compile the function for adding metadata:

cargo build --bin metaplex-token-metadata-test-client
cd ./target/debug

Attach the uploaded metadata into the token (You can get the IMAGE_LINK from the file generated when uploading metadata, located at metaplex/js/packages/cli/.cache/devnet-temp)

./metaplex-token-metadata-test-client create_metadata_accounts --keypair ~/.config/solana/devnet.json --name <SOME_NAME> --symbol <SOME_SYMBOL> --mint <TOKEN> --uri <IMAGE_LINK>

Disable future mints

This is optional, if you only want to create a fixed number of tokens you can disable mint forever. The Mint authority will be removed so you won’t be able to mint more tokens or create/modify metadata after doing this:

spl-token authorize mint --disable

Transfer tokens to other users

You can send tokens to other users if they already have an account for your specific token:

spl-token transfer <TOKEN> 1 <NEW_USER_ADDRESS>

If you get the error Recipient's associated token account does not exist. you can fund their account by adding the flag --fund-recipient:

spl-token transfer --fund-recipient <TOKEN> 1 <NEW_USER_ADDRESS>

Upload NFT images

Same as we did earlier for the token metadata, now we are going to upload the NFT images and metadata. In mi case it’s stored on folder metaplex/js/packages/cli/example-assets. Go to the right folder:

cd metaplex/js/packages/cli

Delete the previous cache:

rm .cache/devnet-temp 

And upload the assets:

build/candy-machine-cli.js upload -e devnet -k ~/.config/solana/devnet.json ./example-assets

Create candy machine

Now we will create a candy machine using the TOKEN and TOKEN_ACCOUNT generated at the beginning.

node build/candy-machine-cli.js create_candy_machine \
  -e devnet \
  -k ~/.config/solana/devnet.json \
  -p 1 \
  --spl-token <TOKEN> \
  --spl-token-account <TOKEN_ACCOUNT>

Update Candy machine setting the start date (in my case some day in the past):

node build/candy-machine-cli.js update_candy_machine \
    -e devnet \
    -k ~/.config/solana/devnet.json \
    --date "2021 Nov 23 00:00:00 GMT"

Now you can use show to display the summary of the existing Candy machine:

node build/candy-machine-cli.js show \                
    -e devnet \
    -k ~/.config/solana/devnet.json

Note that the price is 10 instead of 1, because you need 10 tokens to buy a single NFT (remember that value of one token was 0.1)

Use a web frontend to sell NFT

Now that we have the candy machine up and running we can download a fronted like exiled-apes and update the ENV variables:


Leave a Reply

Your email address will not be published. Required fields are marked *