Skip to content

About

Atomic multi-leg CPI arbitrage program for Solana — no_std Pinocchio, zero-heap, ~18 KB SBF, ships with its own security audit

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Universal Proxy — atomic CPI arbitrage program (Solana / Pinocchio)

A minimal, stack-only Solana program that executes a multi-leg arbitrage as a single atomic transaction and reverts on-chain if it isn't profitable. Written in Pinocchio (no_std, zero heap) for minimal compute.

Built as a generic execution layer for cross-DEX arbitrage: the off-chain bot passes a sequence of swap instructions; the program runs them, injects the realized intermediate amount into the sell leg, and enforces a min_profit invariant atomically.

What it does

  • Generic multi-leg CPI: runs up to MAX_INSTRUCTIONS arbitrary swaps (no per-DEX code).
  • Dynamic amount injection: rewrites the sell leg's input with the actual balance produced by the buy leg (sell_amount_offset), so the route adapts to real fills.
  • Atomic profit guard: measures token (and optionally SOL) balance delta of the owner and returns Custom(1) if net_profit < min_profit — the whole tx reverts, no partial fills.
  • Owner-locked: only a hardcoded owner key can invoke it.

Architecture

instruction_data = [ header(16B) | ix_0 | ix_1 | ... ]
header: wallet_idx, profit_token_idx, intermediate_token_idx, min_profit(i64),
        sell_ix_index, sell_amount_offset(u16), profit_mode, num_ix
each ix: program_id(32B) | num_accs | [acc_idx, is_signer, is_writable]* | data_len(u16) | data

Flow: validate header → run pre-sell legs → read intermediate balance → patch + run sell leg → run post-sell legs → assert profit. See src/lib.rs.

Security hardening (self-audit)

This version is the hardened build after a security review of an earlier iteration. Issues found and fixed:

# Severity Issue Fix
C-001 Critical MaybeUninit::uninit().assume_init() on an array → UB (runtime panic ~944 CU) Per-element init; slice built only from initialized elements
C-002 Critical token balance read without verifying the account is a token account owner + size + discriminant checks
H-001 High arbitrary CPI — any program id accepted ALLOWED_PROGRAMS whitelist
H-002 High account indices not bounds-checked validate idx < accounts.len()
H-003 High sell_amount_offset could overflow the ix buffer bounds check before write

The UB fix is the centerpiece: the original used uninitialized stack arrays unsafely; the fix uses [const { MaybeUninit::uninit() }; N] and constructs the slice from only the initialized prefix — no UB, still zero-heap.

Configure before building

OWNER in src/lib.rs is published as the all-zero pubkey (11111111111111111111111111111111) — nobody can sign for it, so the program is inert as-is. Replace it with the 32 raw bytes of your own wallet before you build:

const OWNER: [u8; 32] = [ /* your 32 bytes */ ];

ALLOWED_PROGRAMS is the CPI whitelist (Orca Whirlpool, Raydium AMM v4, Raydium CLMM, Meteora DLMM). Add the program IDs you actually route through — anything not listed is rejected.

Build

cargo-build-sbf
# -> target/deploy/universal_proxy.so  (17.9 KB)

Requires the Solana toolchain (agave-install / cargo-build-sbf). Built as a no_std, stack-only program: program_entrypoint! + no_allocator! + nostd_panic_handler!.

Status

Verified on a local mainnet fork (Surfpool). Compiles to an ~18 KB SBF binary; deploys; passes all its validations; builds the CPI account array without the UB (1966 CU, no panic); and invokes a real DEX (Orca) via CPI. Fork testing also surfaced and fixed two more bugs that would have blocked every real swap:

  • the token-account check read data[0] (the mint's first byte) instead of the account state at offset 108
  • ALLOWED_PROGRAMS held wrong program IDs — no real DEX was actually whitelisted

Remaining before mainnet use: a full 2-leg arb with real swap instructions + the on-chain profit guard, exercised on the fork.

Stack

Rust · Pinocchio 0.10 · no_std · SBF · opt-level=z + fat LTO + panic=abort.

About

Atomic multi-leg CPI arbitrage program for Solana — no_std Pinocchio, zero-heap, ~18 KB SBF, ships with its own security audit

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages