Add Token-2022 cpi-guard Pinocchio example - #701
Conversation
Ports the cpi-guard Token-2022 example to Pinocchio (the anchor example has no native sibling). Unlike the other extension examples, CpiGuard cannot be enabled or disabled through a CPI, so the program cannot initialize it; instead the program exposes a cpi_transfer instruction (a Token-2022 TransferChecked CPI, reading the mint's decimals from account data) and the example demonstrates the guard's effect. The litesvm test creates a source account with CpiGuard enabled (client-side) and funded, then shows the program's CPI transfer is rejected while the guard is on and succeeds once it is disabled, asserting the destination balance. Matches the kit + litesvm template of the other token-2022 pinocchio examples.
Greptile SummaryThis PR adds a Pinocchio implementation and LiteSVM test for demonstrating Token-2022 CPI Guard behavior.
Confidence Score: 4/5The program and test behavior appear sound, but the broken artifact paths in both deployment commands should be corrected before merging. The build emits Files Needing Attention: tokens/token-2022/cpi-guard/pinocchio/cicd.sh; tokens/token-2022/cpi-guard/pinocchio/package.json Important Files Changed
Reviews (1): Last reviewed commit: "token-2022 cpi-guard: add pinocchio exam..." | Re-trigger Greptile |
|
@amilz could you take a look at this one when you get a chance? No open review threads left on it, so it is ready for maintainer review. It is one of 23 open Pinocchio ports I have up — they are independent and self-contained, so they can be reviewed and merged in any order: https://github.com/solana-developers/program-examples/pulls/MarkFeder |
What
Adds a Pinocchio implementation of the Token-2022
cpi-guardexample. The example previously had only ananchorversion (nonative), so this is a fresh port, following the same kit + litesvm template as the other Token-2022 pinocchio examples.How it works — and why it's different
The
CpiGuardextension prevents privileged token operations (transfer, burn, approve, close) from being performed through a CPI when they're authorized by the account owner — a protection against malicious programs redirecting funds.Crucially, CpiGuard cannot be enabled or disabled through a CPI (Token-2022 requires those to be transaction-level). So — unlike every other extension example — the program cannot initialize this extension. Instead:
cpi_transferinstruction that performs a Token-2022TransferCheckedCPI (variant12) from a source account to a destination, signed by the source's owner. It reads the mint'sdecimalsdirectly from the mint account data (offset 44) so the checked transfer matches the mint.Test
litesvm+@solana/kit. The test:getEnableCpiGuardInstruction), funded with tokens, plus a plain destination account.cpi_transferwhile the guard is enabled → asserts it is rejected (FailedTransactionMetadata).cpi_transferagain → asserts it succeeds, and that the destination balance is1.The success-after-disable (with the balance moving) proves the guard is the cause of the rejection, not a mechanical transfer error.
Verified locally:
cargo build-sbf, the litesvm test,tsc --noEmit, Prettier,cargo fmt --check, Clippy, andpnpm install --frozen-lockfileall clean.