Slab Program

Independent LP-run perpetual trading engines

Each Slab Program is an independent perpetual trading engine operated by liquidity providers, with strict resource constraints for predictable performance.

// Slab Program - Memory-Efficient Design
#[repr(C)]
pub struct Slab {
    pub authority: Pubkey,
    pub instruments: [Instrument; 32],
    pub accounts: FreeList<Account, 5000>,
    pub orders: FreeList<Order, 30000>,
    pub positions: FreeList<Position, 30000>,
}

pub struct FreeList<T, const N: usize> {
    data: [T; N],
    free: Vec<u16>,
    used: u16,
}

impl<T: Default, const N: usize> FreeList<T, N> {
    // O(1) allocation
    pub fn alloc(&mut self) -> Option<&mut T> {
        self.free.pop().map(|idx| &mut self.data[idx as usize])
    }
    
    // O(1) deallocation
    pub fn free(&mut self, idx: u16) {
        self.free.push(idx);
    }
}

Technical Specifications

Memory Budget
10 MB
Max Accounts
5,000
Max Orders
30,000
Trading Pairs
32

Features

O(1) Freelist Allocation
Constant-time memory operations with zero allocations after initialization
Self-Contained Matching
Independent order book and matching engine for each slab
Position Tracking
Real-time position management and PnL calculations
// Order Matching Engine
pub fn match_order(
    slab: &mut Slab,
    order: &Order,
) -> Result<Vec<Fill>> {
    let book = &mut slab.instruments[order.instrument_id].book;
    let mut fills = Vec::new();
    
    while let Some(maker) = book.best_match(order) {
        let fill_size = order.size.min(maker.size);
        fills.push(Fill {
            maker: maker.id,
            taker: order.id,
            price: maker.price,
            size: fill_size,
        });
        
        maker.size -= fill_size;
        if maker.size == 0 {
            slab.orders.free(maker.idx);
        }
    }
    
    Ok(fills)
}

Continue Learning

Explore security and trading features