Onchain program examples for ⚓ Anchor 🦀 Native Rust, and 🐍 Python
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.
Using this repo
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. Useanchor build && anchor deployto build & deploy the program. Runanchor run testto test it.native- Written using Solana's native Rust crates and vanilla Rust. Usecicd.shto build & deploy the program. Runyarn run testto test it.seahorse- Written using the Seahorse framework, which converts your Python code to Anchor Rust. Useseahorse build && anchor deployto build & deploy the program. Runanchor run testto test it.
If a given example is missing, please add it!
The example projects
Basics
account-data
Store and retrieve data using Solana accounts.
checking-accounts
Check that the accounts provided in incoming instructions meet particular criteria.
close-account
Close an account and get the Lamports back.
counter
Use a PDA to store global state, making a counter that increments when called.
create-account
Make new accounts on the blockchain.
cross-program-invocation
Invoke an instruction handler from one onchain program in another onchain program.
favorites
Save and update per-user state on the blockchain, ensuring users can only update their own information.
hello-solana
Hello World on Solana! A minimal program that logs a greeting.
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.
program-derived-addresses
Store and retrieve state in Solana.
realloc
How to store state that changes size in Solana.
rent
Determine the necessary minimum rent by calculating an account's size.
repository-layout
Layout larger Solana onchain programs.
transfer-sol
Tokens
create-token
Create a token on Solana with a token symbol and icon.
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!
nft-minter
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.
pda-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.
spl-token-minter
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.
token-swap
transfer-tokens
Token Extensions
basics
Create token mints, mint tokens, and transferr tokens using Token Extensions.
cpi-guard
Enable CPI guard to prevents certain token action from occurring within CPI (Cross-Program Invocation).
default-account-state
Create new token accounts that are frozen by default.
group
Create tokens that belong to larger groups of tokens using the Group Pointer extension.
immutable-owner
Create tokens whose owning program cannot be changed.
interest-bearing
Create tokens that show an 'interest' calculation.
memo-transfer
Create tokens where transfers must have a memo describing the transaction attached.
metadata
Create tokens that store their onchain metadata inside the token mint, without needing to use or pay for additional programs.
mint-close-authority
Allow a designated account to close a Mint.
multiple-extensions
Use multiple Token Extensions at once.
non-transferable
Create tokens that cannot be transferred.
permanent-delegate
Create tokens that remain under the control of an account, even when transferred elsewhere.
transfer-fee
Create tokens
Compression
cnft-burn
Burn compressed NFTs.
cnft-vault
Store Metaplex compressed NFTs inside a PDA.
cutils
Work with Metaplex compressed NFTs.
Examples We'd Love to See!
- Examples needed for Native:
- Token Extensions
- Examples needed for Anchor:
- Additional Accounts & Resolving Accounts
- Examples needed for Seahorse
- Any existing example missing a
seahorsefolder
- Any existing example missing a
- New examples needed for Anchor, Native, Solidity & Seahorse:
- Token lending
- Token swapping
- Escrow
- Staking
- Wrapped tokens
- Pyth
- Clockwork
- VRF
- Any oracle
- Merkle trees (compression)