Skip to content

Examples ​

Complete templates you can copy, grouped by what they do. Each one shows the template and the code that runs it, in TypeScript and Rust.

New to Ballista? Getting started takes one template from definition to a run. To see what a template can do that an ordinary transaction can't, read the guide pages on amounts read at run time, conditional calls, loops and guardrails.

The Plain transaction? column in the tables below says whether you can get the same result without Ballista:

  • Yes: ordinary instructions in one transaction do the same thing. What the template adds is a single instruction and a sequence of calls that is stored on chain and was checked when it was uploaded.
  • Yes, weaker: you can send the same instructions, but a check the template makes on chain is left to whoever builds the transaction.
  • No: no sequence of instructions can do it, because a decision depends on account data read while the transaction runs.

Protocol templates ​

Twelve templates work with Jupiter, Kamino, Orca, pump.fun and Pyth, or settle a trade at a signed quote. Each acts on a value that exists only while the transaction runs, such as what a swap produced. They were tested locally in LiteSVM, against the programs they call and accounts copied from mainnet, and have not yet run on devnet or mainnet.

Composition ​

Templates that call other programs in sequence, with checks between the calls. Your client still finds routes, quotes and accounts; the template fixes the order of the calls and the checks.

RecipeWhat it doesPlain transaction?
Swap then depositSwap, check that enough arrived, then depositYes, weaker
Claim then distributeClaim rewards once, then pay each recipient the same amountYes
Fallback routeExactly one of two routes runsYes, weaker
Time-gated governanceExecute only once a proposal is approved and its time has passedNo
Keeper crankSame maintenance call over many accountsYes

Payments ​

Templates that pay out SOL.

RecipeWhat it doesPlain transaction?
Revenue splitSplit an amount by basis points, losing nothing to roundingYes, weaker
Weighted rewardsPay the first recipient 1 × base, the second 2 × base, and so onYes, weaker
Deadline refundRefund only before a deadlineYes, weaker
Reserve-preserving sweepMove SOL up to a cap without dropping below a reserveYes, weaker

Token accounts ​

Templates that create, pay into, close and check SPL token accounts. An ATA (associated token account) is the standard token account for a given wallet and mint.

RecipeWhat it doesPlain transaction?
Create then transferCheck each recipient's ATA address, create it if missing, then payYes
Token payrollPay many existing token accountsYes
Conditional ATA setupCreate an ATA only if it doesn't exist yetYes
Close empty accountsClose each account whose balance is zeroYes, weaker
Exact token debitCheck the transfer took exactly the stated amountYes, weaker

BALLISTA / A SMALL MACHINE FOR COMPLEX TRANSACTIONS