Skip to content

HongThaiPham/solana-pinocchio-examples

Repository files navigation

Solana Pinocchio Examples

This repository contains examples demonstrating how to use Pinocchio, a zero-dependency library for creating Solana programs in Rust.

What is Pinocchio?

Pinocchio is a lightweight library that allows you to write Solana programs without depending on the solana-program crate. It leverages zero-copy types to read program input parameters directly from the byte array passed to the program's entrypoint by the SVM (Solana Virtual Machine) loader.

Key Benefits:

  • Zero dependencies: Eliminates the need for solana-program crate
  • Resolves dependency conflicts: Avoids versioning issues with the Solana runtime
  • Lightweight: Minimizes program size and complexity
  • Efficient: Uses zero-copy deserialization for better performance

The example programs

Basics

Hello world

Hello World on Solana! A minimal program that logs a greeting.

Account-data

Store and retrieve data using Solana accounts.

Storing global state - Counter

Use a PDA to store global state, making a counter that increments when called.

Saving per-user state - Favorites

Save and update per-user state on the blockchain, ensuring users can only update their own information.

Checking Instruction Accounts

Check that the accounts provided in incoming instructions meet particular criteria.

Closing Accounts

Close an account and get the Lamports back.

Creating Accounts

Make new accounts on the blockchain.

Cross program invocations

Invoke an instruction handler from one onchain program in another onchain program.

PDA rent-payer

Use a PDA to pay the rent for the creation of a new account.

Processing instructions

Add parameters to an instruction handler and use them.

Storing date in program derived addresses

Store and retrieve state in Solana.

Handling accounts that expand in size

How to store state that changes size in Solana.

Calculating account size to determine rent

Determine the necessary minimum rent by calculating an account's size.

Laying out larger programs

Layout larger Solana onchain programs.

Transferring SOL

Send SOL between two accounts.

Tokens

Creating tokens

Create a token on Solana with a token symbol and icon.

Minting NFTS

Mint an NFT from inside your own onchain program using the Token and Metaplex Token Metadata programs. Reminder: you don't need your own program just to mint an NFT, see the note at the top of this README.

Minting a token from inside a program

Mint a Token from inside your own onchain program using the Token program. Reminder: you don't need your own program just to mint an NFT, see the note at the top of this README.

Transferring Tokens

Transfer tokens between accounts

Allowing users to swap digital assets - Escrow

Allow two users to swap digital assets with each other, each getting 100% of what the other has offered due to the power of decentralization!

Minting a token from inside a program with a PDA as the mint authority

Mint a Token from inside your own onchain program using the Token program. Reminder: you don't need your own program just to mint an NFT, see the note at the top of this README.

Creating an Automated Market Maker

Create liquidity pools to allow trading of new digital assets and allows users that provide liquidity to be rewarded by creating an Automated Market Maker.

Token Extensions

Basics - create token mints, mint tokens, and transfer tokens with Token Extensions

Create token mints, mint tokens, and transferr tokens using Token Extensions.

Preventing CPIs with CPI guard

Enable CPI guard to prevents certain token action from occurring within CPI (Cross-Program Invocation).

Using default account state

Create new token accounts that are frozen by default.

Grouping tokens

Create tokens that belong to larger groups of tokens using the Group Pointer extension.

Creating token accounts whose owner cannot be changed

Create tokens whose owning program cannot be changed.

Interest bearing tokens

Create tokens that show an 'interest' calculation.

Requiring transactions to include descriptive memos

Create tokens where transfers must have a memo describing the transaction attached.

Adding on-chain metadata to the token mint

Create tokens that store their onchain metadata inside the token mint, without needing to use or pay for additional programs.

Allow a designedated account to close a mint

Allow a designated account to close a Mint.

Usng multiple token extensions

Use multiple Token Extensions at once.

Non-transferrable - create tokens that can't be transferred.

Create tokens that cannot be transferred.

Permanent Delegate - Create tokens permanently under the control of a particular account

Create tokens that remain under the control of an account, even when transferred elsewhere.

Create tokens with a transfer-fee.

Create tokens with an inbuilt transfer fee.

Compression

Cnft-burn

Burn compressed NFTs.

Cnft-vault

Store Metaplex compressed NFTs inside a PDA.

Cutils

Work with Metaplex compressed NFTs.

Oracles

pyth

Use a data source for offchain data (called an Oracle) to perform activities onchain.

Getting Started

Prerequisites

  • Rust and Cargo
  • Solana CLI tools
  • Basic knowledge of Solana program development

Building and Testing

# Clone the repository
git clone https://github.com/yourusername/solana-pinocchio-examples.git
cd solana-pinocchio-examples
bun i


# Build the program
cargo build-sbf
# or
cargo-build-sbf

# Run tests
cargo test
# or
cargo test -p example_program


# Test on devnet

# Get program ID
solana address -k target/deploy/example_program-keypair.json

# Deploy
solana program deploy target/deploy/example_program.so --program-id ./target/deploy/example_program-keypair.json

# Make sure you have update program ID in the codama node struct for generate client code
bun gen:client:example_program
bun test:client:example_program

How to create a new Pinocchio program

To create a new Pinocchio program you need install Rust and the Solana CLI tools. Then you can use the following steps:

  1. Create a new Rust library project:

    cargo new --lib my_pinocchio_program --edition 2021

    cd my_pinocchio_program

    
    
  2. Install the Pinocchio crate:

    cargo add pinocchio

That is all you need to do to set up a new Pinocchio program. Let start coding!

Pinocchio Program directory structure

The typical directory structure for a Pinocchio program inspired by the Anchor framework looks like this:

my_pinocchio_program/
├── Cargo.toml
├── src/
│   ├── lib.rs
│   ├── instructions/
|   |   └── mod.rs
│   ├── state/
|   |   └── mod.rs
│   ├── error.rs
|   ├── constants.rs
|   └── util.rs
├── tests/
│   └── integration_tests.rs
└── target/

Resources

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A repository of Solana program examples using Pinocchio

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published