Skip to content

shashank-poola/solana-program

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Solana Programs Collection

A collection of Solana smart contracts demonstrating different programming patterns and use cases on the Solana blockchain.

Programs

1. Counter Program

A native Solana program implementing a simple counter with multiple arithmetic operations.

Features:

  • Initialize counter with value 1
  • Double the current count
  • Halve the current count
  • Add a custom amount to the counter
  • Subtract a custom amount from the counter
  • Uses Borsh serialization for efficient data handling
  • Implements overflow protection with saturating arithmetic

Tech Stack:

  • Native Solana Program (no framework)
  • Borsh for serialization
  • TypeScript client with Bun runtime

Checkout: counter/

2. Escrow Program

An SPL token escrow program built with Anchor framework for secure token transfers between parties.

Features:

  • Initialize escrow with specified token amount
  • Lock tokens in a vault controlled by a PDA (Program Derived Address)
  • Allow designated receiver to claim escrowed tokens
  • Automatic vault creation using Associated Token Accounts
  • Secure transfer validation with has_one constraint

Tech Stack:

  • Anchor Framework v0.32.1
  • SPL Token Program
  • Associated Token Program

Checkout: escrow/

Prerequisites

Installation

  1. Clone the repository:
git clone <repository-url>
cd solana-programs
  1. Configure Solana CLI for your desired network:
# For devnet
solana config set --url devnet

# For localnet
solana config set --url localhost

Building the Programs

Counter Program

cd counter
cargo build-sbf

The compiled program will be in target/deploy/contracts.so

Escrow Program

cd escrow
anchor build

The compiled program will be in target/deploy/escrow.so

Deploying the Programs

Counter Program

cd counter
solana program deploy target/deploy/contracts.so

Escrow Program

cd escrow
anchor deploy

Testing

Counter Program

The counter program includes a TypeScript client for testing:

cd counter/src/client
bun install
bun test

Escrow Program

cd escrow
anchor test

Folder/file Structure

solana-programs/
├── counter/
│   ├── src/
│   │   ├── lib.rs              # Program entry point
│   │   ├── programs/
│   │   │   └── main.rs         # Counter logic implementation
│   │   └── client/             # TypeScript client
│   │       ├── index.ts        # Client interface
│   │       └── tests/          # Client tests
│   └── Cargo.toml
├── escrow/
│   ├── programs/
│   │   └── escrow/
│   │       ├── src/
│   │       │   └── lib.rs      # Escrow program logic
│   │       └── Cargo.toml
│   └── Cargo.toml              # Workspace config
└── vault/                      # In development

Program Descriptions

Counter Program Architecture

The counter program demonstrates native Solana program development without frameworks. It showcases:

  • Custom instruction serialization using Borsh
  • Account data management
  • Signer validation
  • State mutations

Instructions:

  • Init: Initializes counter to 1
  • Double: Multiplies counter by 2
  • Half: Divides counter by 2
  • Add { amount }: Adds specified amount
  • Subtract { amount }: Subtracts specified amount

Escrow Program Architecture

The escrow program uses the Anchor framework to simplify development and implements:

  • PDA-based vault authority for secure token custody
  • SPL Token transfers using Cross-Program Invocations (CPI)
  • Account validation using Anchor constraints
  • Automatic space calculation for account creation

Instructions:

  • initialize_escrow: Creates escrow and deposits tokens into vault
  • claim_escrow: Allows receiver to claim escrowed tokens

Development

Counter Program Development

The counter program is written in pure Rust without frameworks, providing insight into low-level Solana program development.

Key concepts demonstrated:

  • Program entrypoint definition
  • Account iteration and validation
  • Borsh serialization/deserialization
  • Error handling with ProgramError

Escrow Program Development

Built with Anchor, showcasing modern Solana development practices:

  • Declarative account validation
  • Automatic space calculation
  • Type-safe instruction handlers
  • Built-in security checks

Security Considerations

  • Counter: Implements saturating arithmetic to prevent overflow/underflow
  • Escrow: Uses PDA for vault authority to prevent unauthorized access
  • Escrow: Validates receiver using has_one constraint
  • All programs require proper signer validation

Optimization

The workspace is configured with release optimizations:

  • Link-Time Optimization (LTO) enabled
  • Single codegen unit for maximum optimization
  • Overflow checks enabled in release builds

Resources

PLAY WITH RUST, SOLANA

About

Basic solana programs - counter, escrow, vault programs with anchor framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published