DEV Community

Cover image for token-nft-mint: an Anchor program that mints an SPL token and a 1-of-1 NFT with metadata
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

token-nft-mint: an Anchor program that mints an SPL token and a 1-of-1 NFT with metadata

On Solana, a fungible token and an NFT are made from the same parts. Both are SPL Token mints. Both get their name, symbol and image link from a separate Metaplex Token Metadata account. The difference between "a currency" and "a unique item" comes down to a few settings: the number of decimals, how many units are minted, and who is allowed to mint more.

That is easy to read and harder to believe until you build it. token-nft-mint is an Anchor 1.2 program that creates both, plus a TypeScript script that calls it on devnet or localnet.

The warning from the README comes first: not audited, devnet / localnet only. This is a learning project. Do not deploy it to mainnet or use it with real funds.

What it does

The program creates two things:

  1. A fungible SPL token with Metaplex Token Metadata (name, symbol, URI), and mints it to any wallet.
  2. A 1-of-1 NFT: a 0-decimal mint, exactly one token minted to the creator, a metadata account, and a master edition with max_supply = 0.

It also includes a TypeScript script that calls both instructions on devnet or localnet. The script reads the program ID and the discriminators from target/idl/token_nft_mint.json. It refuses mainnet, both by URL and by genesis hash, and never prints the wallet's secret key.

How to use it

Clone the repository, build the program, fetch the Token Metadata program for the tests, and run them:

git clone https://github.com/AbdeldjalilChougui/token-nft-mint.git
cd token-nft-mint
anchor build                      # or: anchor build --tools-version v1.58
./scripts/fetch-programs.sh       # dumps Token Metadata from devnet into tests/fixtures (not committed)
cargo test                        # LiteSVM with the real Token Metadata program loaded
Enter fullscreen mode Exit fullscreen mode

Then mint with the script:

pnpm install
pnpm mint token --name "Test Dinar" --symbol TDZD --uri https://example.org/tdzd.json --decimals 6 --amount 1000 --cluster devnet
pnpm mint nft   --name "Sahara #1" --symbol ART --uri https://example.org/nft/1.json --cluster devnet
Enter fullscreen mode Exit fullscreen mode

The first command creates a fungible token with 6 decimals and mints 1000 of it. The second creates a single NFT. The --uri points to a JSON file you host yourself; the example URLs are placeholders.

How the NFT works

The whole NFT is one instruction, create_nft, that does five things in order:

create_nft:
  init mint (decimals 0, authority = payer)        [Anchor `init` + `mint::*` constraints]
  init payer's ATA                                  [associated_token::*]
  mint_to(1)                                        [CPI SPL Token]
  create_metadata_accounts_v3                       [CPI Token Metadata]
  create_master_edition_v3(max_supply = Some(0))    [CPI Token Metadata]
Enter fullscreen mode Exit fullscreen mode

Step by step:

  • The mint. Zero decimals means the token cannot be divided. Anchor's init with the mint::* constraints creates it, with the payer as the first mint authority.
  • The token account. The payer's associated token account (ATA) for this mint is created to hold the token.
  • The single unit. A CPI to SPL Token mints exactly 1.
  • The metadata. A CPI to Token Metadata creates the metadata account with the name, symbol and URI.
  • The master edition. A second CPI creates the master edition with max_supply = Some(0), which means no prints can be made from it.

The last step is what makes it a 1-of-1. Creating the master edition moves the mint and freeze authority to the edition PDA. After that, nobody can mint a second token, including the creator. The tests check exactly this: mint_authority == edition PDA, and a later mint_tokens fails with ConstraintMintMintAuthority.

What I learned

"Unique" is an authority change, not a flag. There is no "is NFT" field on a mint. Supply 1 and decimals 0 only describe the present. What guarantees the future is that the mint authority now belongs to a PDA of the Token Metadata program, which will not sign another mint. Understanding that turned the NFT from a special kind of object into an ordinary mint with its mint authority given away.

Check the addresses of accounts you pass to another program. Metadata and edition addresses are Token Metadata PDAs: ["metadata", program, mint] and the same seeds followed by "edition". The program checks them with seeds::program = token_metadata_program, so a fake metadata account is rejected. Without that check, the program would trust whatever account the caller supplied.

A new mint must sign its own creation. init of a new mint needs the mint keypair's signature. It is easy to forget on the client side, and there is a test for it: nft_requires_mint_keypair_signature.

Metadata has length limits. A name longer than 32 bytes or a symbol longer than 10 bytes is rejected with custom errors before the CPI is made. Failing early in my own program gives a clearer error than letting the other program reject it.

Test against the real program, not a mock. The tests run in LiteSVM with the real Token Metadata program loaded. scripts/fetch-programs.sh dumps that program from devnet into tests/fixtures, which is not committed. The suite in programs/token-nft-mint/tests/mint.rs:

  • fungible_token_with_metadata: metadata fields and authority; minting twice adds up
  • only_mint_authority_can_mint: wrong signer (mint::authority)
  • metadata_length_limits: name over 32 or symbol over 10 bytes rejected with custom errors
  • nft_is_one_of_one_with_master_edition: supply 1, master edition max_supply = Some(0), mint authority moved to the edition, and further minting rejected
  • nft_requires_mint_keypair_signature: init of a new mint needs the mint's own signature

A client script should defend itself. The script refuses mainnet twice, by URL and by genesis hash. Checking the URL alone would miss a custom RPC endpoint that points at mainnet; the genesis hash does not lie. Reading the program ID and discriminators from the IDL also means the script cannot drift from the built program.

Things to know

  • Not audited. Devnet / localnet only. Do not deploy to mainnet or use with real funds.
  • No collections or verified creators. Royalties are set to 0.
  • Fungible metadata uses create_metadata_accounts_v3, not the newer CreateV1 or programmable NFTs.
  • Tests need the dumped Token Metadata binary. Without it they fail with a message telling you to run scripts/fetch-programs.sh.
  • What was run: the script was run end to end against solana-test-validator, with the program and the dumped Token Metadata program loaded through --bpf-program. It created a token and an NFT. That is a local validator run; the README does not claim more than that.

Why I built it

Creating a token on Solana is usually done with a ready-made CLI or SDK call, and creating an NFT with a higher-level library. Both hide the sequence of accounts and CPIs underneath. I wanted to write that sequence myself in one program: the mint, the token account, the metadata, the master edition, and the checks around each. Putting the fungible token and the NFT side by side also shows how little separates them, which is the main thing this project is meant to teach.

Get the code

The program, the mint script and the LiteSVM tests are on GitHub: https://github.com/AbdeldjalilChougui/token-nft-mint. It is MIT licensed. Issues and pull requests are welcome.


Originally published at abdeldjalilchougui.biz.

Top comments (0)