Skip to content

mikemaccana/program-examples

 
 

Repository files navigation

Solana Program Examples

Solana onchain program examples for ⚓ Anchor, 💫 Quasar, 🤥 Pinocchio, and 🦀 Native Rust.

Anchor Quasar Pinocchio Native

This repo contains Solana onchain programs (referred to as 'Smart Contracts' in other blockchains).

Note

If you're new to Solana, you don't need to create your own programs to perform basic things like making accounts, creating tokens, sending tokens, or minting NFTs. These common tasks are handled with existing programs, for example the System Program (for making account or transferring SOL) or the token program (for creating tokens and NFTs). See the Solana Developer site to learn more.

Each folder includes examples for one or more of the following:

  • anchor - Written using Anchor, the most popular framework for Solana development, which uses Rust. Use anchor build and anchor deploy to build and deploy the program. Tests should be executed using pnpm test as defined in the Anchor.toml scripts section.

  • quasar - Written using Quasar, a zero-copy, zero-allocation no_std framework for Solana programs with Anchor-compatible ergonomics. Build and test commands are the same as native examples. Run pnpm test to execute tests.

  • pinocchio - Written using Pinocchio, a zero-copy, zero-allocation library for Solana programs. Build and test commands are the same as native examples. Run pnpm test to execute tests.

  • native - Written using Solana's native Rust crates and vanilla Rust. Build and test commands are defined via pnpm scripts and use litesvm for testing. Run pnpm test to execute tests.

If a given example is missing, please send us a PR to add it! Our aim is to have every example available in every option. We'd also love to see more programs involving staking, wrapped tokens, oracles, compression and VRF. Follow the contributing guidelines to keep things consistent.

The example programs

Basics

Hello world

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

Anchor Quasar Pinocchio Native

Account-data

Store and retrieve data using Solana accounts.

Anchor Quasar Pinocchio Native

Storing global state - Counter

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

Anchor Quasar Pinocchio Native

Saving per-user state - Favorites

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

Anchor Quasar Pinocchio Native

Checking Instruction Accounts

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

Anchor Quasar Pinocchio Native

Closing Accounts

Close an account and get the Lamports back.

Anchor Quasar Pinocchio Native

Creating Accounts

Make new accounts on the blockchain.

Anchor Quasar Pinocchio Native

Cross program invocations

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

Anchor Quasar Native

PDA rent-payer

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

Anchor Quasar Pinocchio Native

Processing instructions

Add parameters to an instruction handler and use them.

Anchor Quasar Pinocchio Native

Storing date in program derived addresses

Store and retrieve state in Solana.

Anchor Quasar Pinocchio Native

Handling accounts that expand in size

How to store state that changes size in Solana.

Anchor Quasar Pinocchio Native

Calculating account size to determine rent

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

Anchor Quasar Pinocchio Native

Laying out larger programs

Layout larger Solana onchain programs.

Anchor Quasar Native

Transferring SOL

Send SOL between two accounts.

Anchor Quasar Pinocchio Native

Tokens

Creating tokens

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

Anchor Quasar Native

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.

Anchor Quasar Native

NFT operations

Create an NFT collection, mint NFTs, and verify NFTs as part of a collection using Metaplex Token Metadata.

Anchor Quasar

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.

Anchor Quasar Native

Transferring Tokens

Transfer tokens between accounts

Anchor Quasar Native

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!

Anchor Quasar Native

Fundraising with SPL Tokens

Create a fundraiser account specifying a target mint and amount, allowing contributors to deposit tokens until the goal is reached.

Anchor Quasar

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.

Anchor Quasar Native

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.

Anchor Quasar

External delegate token master

Control token transfers using an external secp256k1 delegate signature.

Anchor Quasar

Token Extensions

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

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

Anchor Quasar

Preventing CPIs with CPI guard

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

Anchor Quasar

Using default account state

Create new token accounts that are frozen by default.

Anchor Quasar Native

Grouping tokens

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

Anchor Quasar

Creating token accounts whose owner cannot be changed

Create tokens whose owning program cannot be changed.

Anchor Quasar

Interest bearing tokens

Create tokens that show an 'interest' calculation.

Anchor Quasar

Requiring transactions to include descriptive memos

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

Anchor Quasar

Adding onchain 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.

Anchor Quasar

Storing NFT metadata using the metadata pointer extension

Create an NFT using the Token Extensions metadata pointer, storing onchain metadata (including custom fields) inside the mint account itself.

Anchor

Allow a designated account to close a mint

Allow a designated account to close a Mint.

Anchor Quasar Native

Using multiple token extensions

Use multiple Token Extensions at once.

Native

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

Create tokens that cannot be transferred.

Anchor Quasar Native

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.

Anchor Quasar

Create tokens with a transfer-fee.

Create tokens with an inbuilt transfer fee.

Anchor Quasar Native

Transfer hook - hello world

A minimal transfer hook program that executes custom logic on every token transfer.

Anchor Quasar

Transfer hook - counter

Count how many times tokens have been transferred using a transfer hook.

Anchor Quasar

Transfer hook - using account data as seed

Use token account owner data as seeds to derive extra accounts in a transfer hook.

Anchor Quasar

Transfer hook - allow/block list

Restrict or allow token transfers using an onchain allow/block list managed by a list authority.

Anchor Quasar

Transfer hook - transfer cost

Charge an additional cost or fee on every token transfer using a transfer hook.

Anchor Quasar

Transfer hook - transfer switch

Enable or disable token transfers with an onchain switch using a transfer hook.

Anchor Quasar

Transfer hook - whitelist

Restrict token transfers so only whitelisted accounts can receive tokens.

Anchor Quasar

Compression

Cnft-burn

Burn compressed NFTs.

Anchor Quasar

Cnft-vault

Store Metaplex compressed NFTs inside a PDA.

Anchor Quasar

Cutils

Work with Metaplex compressed NFTs.

Anchor Quasar

Oracles

pyth

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

Anchor Quasar

Tools

Shank and Solita

Use Shank and Solita to generate IDLs and TypeScript clients for native Solana programs, the same way Anchor does for Anchor programs.

Native


About

A repository of Solana program examples

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Rust 63.6%
  • TypeScript 21.2%
  • C# 9.2%
  • ShaderLab 3.6%
  • HLSL 0.6%
  • Shell 0.6%
  • Other 1.2%