Expand description
§Solana DEX Swap Execution Library
Library for building and executing DEX swap transactions on Solana.
This library provides:
- Entire solana transaction building from swap configurations (
create_tx_from_swap_configs) - Protocol-specific swap instruction builders
- Raydium Ammv4
- Raydium Cpmm/Ammv5
- Raydium Launchpad
- Pumpswap
- Pumpfun Bonding Curve
- Meteora Dammv2
- Meteora Dbc
- Token account management (opening/closing necessary token accounts when doing swaps)
- Low latency Jito block engine integration with their gRPC API for bundle submission, private routing, MEV protect, faster fills, etc.
§Usage
The easiest entry point is create_tx_from_swap_configs, which assembles a complete
transaction from a list of swap configurations. The transaction can then be sent via
Jito bundles (customizable tip) or standard JSON RPC.
§Environment Variables
RPC_NODE_URL: Solana JSON RPC node URLJITO_AUTH_SECRET_KEY: Jito authentication keypair (base58) for bundle submission. If you do not have a specific whitelisted Jito block engine account you can use any private key and your rate limit will be 1 tx per second.
Modules§
Structs§
- Compute
Budget Instruction Idl - Compute budget instruction IDL structure for serialization
- Execution
Context - Execution context for Jito bundle submission. Provides a Jito searcher client for submitting transaction bundles to Jito block engines. The client handles authentication and connection to Jito’s backend services.
- Swap
Config - Configuration for a single swap operation. Contains all information needed to execute a swap instruction. Multiple swap configs can be combined to build multi-hop swap transactions.
Constants§
- COMPUTE_
BUDGET_ LIMIT_ DISCRIMINATOR - Discriminator byte for compute budget limit instruction (2)
Functions§
- close_
all_ token_ accounts - Close all token accounts for a wallet. Fetches all token accounts owned by the wallet and creates close-account instructions for up to 15 accounts. Sends and confirms the transaction via JSON RPC.
- close_
token_ account_ check - Add instructions to close token accounts after swaps if needed. Iterates through swap configs
and adds close-account instructions for any swaps where
close_token_accountis true. Updates the compute unit budget accordingly. - create_
tx_ from_ swap_ configs - Assemble a Solana transaction from an ordered vector of swap configs. Builds a complete transaction including:
- get_
be_ url_ from_ region - Get Jito block engine URL from region environment variable.Reads
JITO_BE_REGIONenvironment variable (ny) and constructs the block engine URL (https://ny.mainnet.block-engine.jito.wtf) - get_
min_ amount_ out - Calculate minimum amount out for a swap based on slippage tolerance. Uses the formula: min_amount_out = amount_in * price_lp * (10^9 - slippage_lp) / (10^9 * 10^9)`. This calculation does not account for price impact or swap fees, which prevents execution when pools have extreme fees (100%) or zero liquidity. For exact calculations, use protocol-specific functions that support manual amount specification. Note: 100% slippage means accepting nothing for the trade (industry standard).
- get_
pf_ a_ amount_ out_ no_ price_ impact - Calculate token A amount out for Pumpfun bonding curve (without price impact) Calculates the amount of base token (token A) received for a given amount of quote token (SOL) input. Does not account for price impact. Note: Pumpfun bonding curve supports exact amount in/out instructions. This all can go in the dumpster because pf bonding curve supports buy exact in and sell is exact in.
- get_
pf_ max_ b_ amount_ in_ no_ price_ impact - See doc for get_pf_a_amount_out_no_price_impact
- make_
swap_ instructions - Create swap instructions from a list of swap configs. Processes swap configs in order and creates protocol-specific swap instructions. Also handles opening token accounts before swaps and closing them after. Updates the compute unit budget based on operations performed.
- open_
token_ account_ check - Add instructions to open token accounts for swap outputs if needed. Iterates through swap
configs and adds create-associated-token-account idempotent (not failing if already open)
instructions for any swaps where
open_token_accountis true. Updates the compute unit budget accordingly. - push_
pumpfun_ swap_ instruction - Create and add a Pumpfun bonding curve swap instruction. For Pumpfun, token A is the bonding curve token and token B is SOL. BtoA is a buy, AtoB is a sell (consistent with Pumpswap protocol when tokens migrate).
- push_
pumpswap_ swap_ instruction - Create and add a Pumpswap AMM swap instruction. If
use_manual_instruction_argsis true, uses exact amounts instead of slippage-based calculations. - push_
raydium_ ammv4_ swap_ instruction - Builds a Raydium AMMv4 swap instruction based on the swap config and adds it to the
instructions vector. If
use_manual_instruction_argsis true, uses exact amounts instead of slippage-based calculations. - push_
raydium_ cpmm_ swap_ instruction - Create and add a Raydium CPMM swap instruction. If
use_manual_instruction_argsis true, uses exact amounts instead of slippage-based calculations. - push_
raydium_ launchpad_ swap_ instruction - Create and add a Raydium launchpad swap instruction
- wrap_
sol - Wrap SOL into wrapped SOL (WSOL) token account. Creates a wrapped SOL token account and transfers the specified amount of SOL into it. Sends and confirms the transaction via JSON RPC.