Skip to content

Repository files navigation

gleam-sbpf

CI License Gleam Solana

Write Solana programs in Gleam - the friendly functional language that compiles to Solana BPF bytecode.

Why Gleam for Solana?

  • Type-safe - Catch errors at compile time
  • Fast to learn - Simple, consistent syntax
  • Functional - Immutable data, pure functions
  • Reliable - No null, no exceptions
  • Well-loved - Consistently ranked as most loved language

Quick Start

# Install Gleam
curl -fsSL https://gleam.run/install.sh | sh

# Clone and build
git clone https://github.com/openSVM/gleam-sbpf.git
cd gleam-sbpf
gleam build

# Run your first program
gleam run

New to Solana development?Start with the Onboarding Guide

Key Features

🔐 GleamSVM - Secure Testing VM

Test your Solana programs with GleamSVM, a Gleam-native VM with 10x more security checks than LiteSVM!

import gleamsvm/vm
import gleamsvm/security

// Create VM with 20+ security validations
let vm_state = vm.new()
let vm_with_account = vm.airdrop(vm_state, 123, 1_000_000_000)

GleamSVM Documentation

⚓ Anchor-like Framework

Build Solana programs with an Anchor-like framework for Gleam! Get automatic account validation, instruction dispatch, and error handling.

import anchor.{type Context, type ProgramResult, Success, Signer, Writable}

pub fn process_initialize(ctx: Context) -> ProgramResult(Int) {
  case anchor.get_account(ctx, 0) {
    Success(account) -> {
      case anchor.validate_account(account, [Signer, Writable]) {
        Success(_) -> Success(0)  // Initialized!
        Error(code, msg) -> Error(code, msg)
      }
    }
    Error(code, msg) -> Error(code, msg)
  }
}

Anchor Quick Start | Full Anchor Docs

🧪 Fast Testing with LiteSVM

Test your programs 100x faster than the Solana test validator with in-memory testing:

scripts/test_litesvm.sh

LiteSVM Testing Guide

Documentation

📚 Complete Documentation Index

Getting Started

Framework & Testing

Advanced

What You Can Build

DeFi Primitives

  • Tokens: SPL-compatible tokens with mint, burn, transfer
  • AMMs: Uniswap-style swaps, liquidity pools
  • Lending: Collateralized loans, interest rates
  • Staking: Lock tokens, earn rewards
  • NFTs: Minting, marketplaces, royalties
  • Derivatives: Options, futures, perpetuals

Trading Bots

  • Arbitrage: DEX arbitrage, flash loans
  • Market Making: Bid/ask spreads, inventory management
  • Grid Trading: Range-bound strategies

Explore 50+ tutorials →

Examples

Simple Return

import compiler.{IntLiteral, Return}

Return(IntLiteral(42))

Compiles to Solana BPF:

b7 00 00 00 2a 00 00 00  // MOV r0, 42
95 00 00 00 00 00 00 00  // EXIT

Counter Program (Anchor)

pub fn process_increment(ctx: Context, current: Int) -> ProgramResult(Int) {
  case anchor.get_account(ctx, 0) {
    Success(counter) -> {
      case anchor.validate_account(counter, [Writable]) {
        Success(_) -> Success(current + 1)
        Error(code, msg) -> Error(code, msg)
      }
    }
    Error(code, msg) -> Error(code, msg)
  }
}

See Counter Program and 25 Anchor Examples

Project Structure

gleam-sbpf/
├── src/                    # Source code
│   ├── compiler.gleam      # Expression compilation
│   ├── opcode.gleam        # BPF opcode definitions (105+ opcodes)
│   ├── instruction.gleam   # Instruction encoding
│   ├── elf.gleam          # Solana BPF ELF generation
│   ├── anchor.gleam       # Anchor-like framework
│   ├── counter.gleam      # Counter program example
│   └── gleamsvm/          # GleamSVM testing VM
├── test/                  # Test suite (64+ tests)
├── docs/                  # Documentation
│   ├── guides/           # Getting started & tutorials
│   ├── testing/          # Testing documentation
│   ├── architecture/     # Technical deep dives
│   ├── reference/        # Best practices & tooling
│   └── releases/         # Release notes & summaries
├── scripts/              # Utility scripts
│   ├── test_litesvm.sh
│   ├── test_solana_tooling.sh
│   └── test_validator_deploy.sh
├── anchor_examples/      # 25+ Anchor framework examples
├── tutorials/            # 50+ DeFi tutorials
├── trading_bots/         # Trading bot examples
├── reference_implementations/  # Production-quality code
└── litesvm_tests/        # LiteSVM integration tests

Features

  • Complete BPF Support: All 105+ Solana BPF opcodes
  • Solana Compatible: Generates valid .so files for deployment
  • Type Safety: Leverages Gleam's type system for correctness
  • Fast Testing: LiteSVM integration (100x faster than validator)
  • Anchor Framework: Build programs like Rust's Anchor
  • Security First: GleamSVM with 20+ security validations
  • Comprehensive Tests: 64+ unit, integration, and end-to-end tests

Testing

Run All Tests

gleam test

Expected output: 64 passed, no failures

Fast Integration Tests (LiteSVM)

scripts/test_litesvm.sh

Validator Testing

scripts/test_validator_deploy.sh

See Testing Documentation for details.

Installation

Prerequisites

  • Gleam 1.13.0 or later
  • Erlang/OTP 25 or later
  • Rebar3

Building

gleam build

Running

gleam run

This generates example Solana BPF files: example1.so, example2.so

Verification

file example1.so
# Output: ELF 64-bit LSB shared object, eBPF, version 1 (SYSV)

readelf -h example1.so
# Shows: Type: DYN (Shared object), Flags: 0x2 (SBPF v2)

Deployment

Deploy compiled programs to Solana:

# Configure for devnet
solana config set --url https://api.devnet.solana.com

# Deploy program
solana program deploy example1.so

See Solana Tooling Guide for complete deployment documentation.

Learning Path

New to blockchain development? Follow this path:

  1. Onboarding Guide - Complete walkthrough (START HERE)
  2. Anchor Quick Start - Build with Anchor framework
  3. Getting Started - First program
  4. Simple Token Tutorial - First DeFi app

Ready for DeFi? Try these:

  1. Counter Program - Learn Anchor patterns
  2. Constant Product AMM - Build a DEX
  3. Simple Staking - Create yield farming

Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

Ways to contribute:

  • 🐛 Report bugs via GitHub Issues
  • 💡 Request features via GitHub Discussions
  • 📝 Improve documentation
  • 🔧 Submit pull requests
  • 📚 Create tutorials and examples

Please read our Code of Conduct before contributing.

Community & Support

Project Statistics

  • 64+ tests: Unit, integration, Anchor, LiteSVM
  • 105+ BPF opcodes: Complete instruction set
  • 50+ tutorials: Comprehensive learning materials
  • 25+ Anchor examples: Framework demonstrations
  • 100x faster testing: LiteSVM integration
  • Production-ready: Full Solana compatibility

Security

Found a security issue? Please email security@gleam-sbpf.dev (or open a private security advisory).

For best practices, see our Security Guide.

Resources

Official Documentation

BPF References

License

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

Acknowledgments

  • Gleam team - For creating an excellent functional programming language
  • Solana team - For the robust BPF infrastructure and developer tools
  • Community contributors - For tutorials, bug reports, and feature improvements

Questions? Visit GitHub Discussions

Ready to build? Start with the Onboarding Guide

This implementation bears strength in discipline.

About

Gleam Solana BPF compiler, write Solana programs in the most loved language after Rust (according to surveys)

Resources

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages