Skip to content

feat(token-2022/metadata): add pinocchio example - #706

Open
MarkFeder wants to merge 1 commit into
solana-foundation:mainfrom
MarkFeder:tokens-token-2022-metadata-pinocchio
Open

feat(token-2022/metadata): add pinocchio example#706
MarkFeder wants to merge 1 commit into
solana-foundation:mainfrom
MarkFeder:tokens-token-2022-metadata-pinocchio

Conversation

@MarkFeder

Copy link
Copy Markdown
Contributor

What

Adds a Pinocchio implementation of the Token-2022 metadata example (the anchor example has no native sibling), following the same kit + litesvm template as the other Token-2022 pinocchio examples. This is the last of the token-2022 mint extensions.

How it works

The single instruction creates a mint that stores its own on-chain metadata:

  1. CreateAccount — sized for the base mint + the fixed-length MetadataPointer extension (234 bytes).
  2. InitializeMetadataPointer (wrapper 39) — points the mint at itself as the metadata account.
  3. InitializeMint2 (20) — decimals 2, payer as mint authority.
  4. System Transfer — tops up the mint's lamports so it stays rent-exempt after the metadata realloc.
  5. TokenMetadataInitialize — the SPL Token Metadata interface instruction (Token-2022 implements it), which reallocates the mint to fit and writes name/symbol/uri.

Two things worth noting:

  • The metadata interface dispatches on an 8-byte SPL discriminator (sha256("spl_token_metadata_interface:initialize_account")[..8]), not the 1-byte Token-2022 variants.
  • The metadata is variable-length, so the account can't be fully sized up front — hence the realloc (done by the CPI) and the rent top-up (done here). The program forwards the Borsh-encoded name/symbol/uri from its instruction data verbatim after the discriminator.

Test

litesvm + @solana/kit. The test encodes name/symbol/uri, invokes the program, then decodes the mint with the official @solana-program/token-2022 codec and asserts the MetadataPointer points at the mint and the TokenMetadata holds the expected fields.

Token-2022 Metadata (Pinocchio)
  ✔ Creates a Token-2022 mint with on-chain metadata
1 passing

Verified locally: cargo build-sbf, the litesvm test, tsc --noEmit, Prettier, cargo fmt --check, Clippy, and pnpm install --frozen-lockfile all clean. (The deploy script uses the *.so glob per #702.)


AI use: I directed the design (the CPI sequence, the realloc + rent top-up, the instruction-data passthrough) and verified every encoding against the spl-token-2022-interface / spl-token-metadata-interface sources; implementation and tests were written with Claude Code and reviewed by me.

Ports the metadata Token-2022 example to Pinocchio (the anchor example has no
native sibling). The single instruction creates a mint with the MetadataPointer
extension pointing at itself, then writes on-chain TokenMetadata into the same
account via the SPL Token Metadata interface (which Token-2022 implements).

The metadata is variable-length, so the account is created sized for the base
mint and metadata pointer only, and the metadata Initialize CPI reallocates it
to fit; the program tops up the mint's rent for the larger size beforehand.
name/symbol/uri arrive Borsh-encoded in the instruction data and are forwarded
verbatim after the interface's 8-byte discriminator. The litesvm test decodes
the mint with the official Token-2022 codec and asserts the pointer and the
stored name/symbol/uri.
@MarkFeder
MarkFeder requested a review from dev-jodee as a code owner August 29, 2026 23:42
@greptile-apps

greptile-apps Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds a Pinocchio implementation of the Token-2022 metadata example.

  • Creates and funds an extended Token-2022 mint through System Program and Token-2022 CPIs.
  • Initializes a self-referencing metadata pointer and variable-length on-chain metadata.
  • Adds a LiteSVM test that validates the mint and extensions with the official Token-2022 codec.
  • Registers the program in the Cargo workspace and adds package, lockfile, build, and deployment configuration.

Confidence Score: 5/5

The PR appears safe to merge; no concrete changed-code defect or security issue was identified.

The implementation keeps extension initialization order, serialized layouts, CPI account roles, account sizing, and rent funding aligned, while the end-to-end test validates the resulting mint with the official Token-2022 decoder.

Important Files Changed

Filename Overview
tokens/token-2022/metadata/pinocchio/program/src/instructions/create_mint.rs Implements the mint lifecycle, fixed program CPIs, rent top-up, and metadata initialization with internally consistent account and serialization layouts.
tokens/token-2022/metadata/pinocchio/program/src/instructions/mod.rs Defines the Token-2022 program ID, extended mint size, and decimal configuration used by the implementation.
tokens/token-2022/metadata/pinocchio/program/src/processor.rs Forwards the single instruction's Borsh metadata payload to the mint creation handler.
tokens/token-2022/metadata/pinocchio/tests/test.ts Exercises the complete LiteSVM flow and validates ownership, account size, mint fields, metadata pointer, and token metadata.
tokens/token-2022/metadata/pinocchio/package.json Adds build and test commands plus dependencies pinned by the committed pnpm lockfile.
Cargo.toml Registers the new Pinocchio metadata program as a workspace member.

Reviews (1): Last reviewed commit: "feat(token-2022/metadata): add pinocchio..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant