Introduction
Typhoon is a high-performance Solana Sealevel Framework designed for developers who need maximum efficiency and control. Built on top of pinocchio, Typhoon provides a thin abstraction layer that simplifies program development while maintaining a no-std environment and zero-cost abstractions.
Why Typhoon?
Traditional frameworks can often introduce significant overhead in terms of binary size and compute units. Typhoon addresses this by:
- Minimal Abstractions: Only what you need to build robust programs.
- High Performance: Optimized for the Solana VM.
- No-Std by Default: Ensuring compatibility with the most constrained environments.
- Procedural Macros: Simplifying boilerplate with powerful macros like
#[context]andbasic_router!.
Getting Started
To get started with Typhoon, you’ll need a working Rust environment set up for Solana development.
Installation
Add Typhoon to your Cargo.toml:
cargo add typhoon
Your First Program
Here is a simple “Hello World” program using Typhoon:
#![allow(unused)]
#![no_std]
fn main() {
use typhoon::prelude::*;
program_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
nostd_panic_handler!();
no_allocator!();
entrypoint!();
pub const ROUTER: EntryFn = basic_router! {
0 => hello_world
};
pub fn hello_world(ProgramIdArg(program_id): ProgramIdArg) -> ProgramResult {
solana_msg::sol_log("Hello World");
assert_eq!(program_id, &crate::ID);
Ok(())
}
}
This program defines a single instruction hello_world that logs a message. The basic_router! macro handles the dispatching logic based on the instruction discriminator.
Crates Overview
Typhoon is organized into several crates, each providing specific functionality for building Solana programs.
Core Crates
typhoon: The main entry point, re-exporting modules from other crates for ease of use.typhoon-accounts: Handles account-related abstractions and data structures.typhoon-context: Provides the infrastructure for instruction contexts.typhoon-errors: Defines the error handling system used throughout the framework.typhoon-traits: Core traits used for cross-compatible implementations.
Macro Crates
typhoon-context-macro: Implements the#[context]attribute macro.typhoon-account-macro: Macros for working with account states and data.typhoon-program-id-macro: Implements theprogram_id!macro.
Utility Crates
typhoon-utility: General-purpose utilities, including byte manipulation.typhoon-utility-traits: Common utility traits for efficient data handling.typhoon-discriminator: Logic for generating instruction and account discriminators.
Constraints
Constraints are a powerful feature in Typhoon that allow you to declaratively define validation logic for your instruction accounts. They are used within the #[context] macro.
Basic Usage
Constraints are added using the #[constraint(...)] attribute on fields in your context struct.
#![allow(unused)]
fn main() {
#[context]
pub struct InitContext {
pub payer: Mut<Signer>,
#[constraint(
init,
payer = payer,
)]
pub counter: Mut<SignerNoCheck<Account<Counter>>>,
pub system: Program<System>,
}
}
Common Constraints
init: Marks an account to be initialized. Requires apayer.payer = <field>: Specifies which account pays for the creation of an initialized account.seeds = [...]: Defines the seeds for a Program Derived Address (PDA).bump = <value>: Sets or validates the bump for a PDA.owner = <address>: Ensures the account is owned by a specific program.
Constraints significantly reduce boilerplate and make your programs more secure by centralizing validation logic.
Examples Walkthrough
Typhoon includes several examples to help you understand how to build real-world programs. You can find them in the examples/ directory.
Core Examples
Hello World
A minimal example showing the basic structure of a Typhoon program. Covered in Getting Started.
Counter
Demonstrates how to maintain state in an account and increment a counter.
Transfer SOL
Shows how to handle SOL transfers between accounts.
Advanced Examples
Escrow
A more complex example involving multiple accounts, state transitions, and token transfers.
CPI (Cross-Program Invocation)
Demonstrates how to call other programs from within a Typhoon program.
Anchor CPI
Shows how Typhoon can interact with programs built using the Anchor framework.
Each example is designed to be self-contained and demonstrates a specific feature or pattern of the Typhoon framework.