The 0x Solana Swap API is available via priority access. Apply now to start building.
How to calculate the byte cost of instructions you add around a 0x swap.
V0 transaction limit: 1232 bytes. Each 0x swap has a defined structure, which reserves a fixed fraction of bytes.
On top of this, adding your own instructions will require some bytes. Follow these steps to calculate byte costs from your own instructions
For each instruction you add, calculate:
Add up all your instructions. 1
List every unique program your instructions invoke at the top level (not via CPI).
For each new program: add bytes cost from footnote ²
List every account referenced across all your instructions. Remove duplicates, remove programs counted in Step 2, and remove free accounts. ³
For each remaining account add:
For each Address Lookup Table you bring: +34 bytes
Pack accounts into fewer ALTs to minimize this. ⁴
This is the most common example - where 0x provides the core setup and swap instructions, and you are required to add further instructions required for sending the transaction
Step 1 — instruction bytes:
Step 2 — invoked programs:
Step 3 — No accounts introduced
Step 4 — No ALTs introduced
1 Instruction encoding. Each instruction serializes as: 1 (program_id_index) + 1 (compact-u16 account count) + N (account indexes) + 1 (compact-u16 data length) + M (data). The compact-u16 fields use 2 bytes instead of 1 when the value is ≥ 128. In practice N < 128 and M < 128, so the cost simplifies to N + M + 3.
Common instructions for reference:
² Top Level Invoked Programs Cost (cost 0 in Step 2):
Note the costs of +31 bytes comes from these being accounts that are usually resolved via 0x ALTs, but are now promoted to a static 32 byte key through being a top level invoked program
³ Free accounts:
These accounts are already present in the 0x transaction. Invoking any of them as a top-level program incurs the one time promotion cost in Step 2, but using them as instruction accounts costs no additional bytes.
⁴ ALT overhead. Each ALT entry in the transaction costs 32 (address) + 1 (writable indexes length) + 1 (readonly indexes length) = 34 bytes, plus the per-account index bytes already counted in Step 3.