Crate solana_exec

Source
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 URL
  • JITO_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§

constants

Structs§

ComputeBudgetInstructionIdl
Compute budget instruction IDL structure for serialization
ExecutionContext
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.
SwapConfig
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_account is 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_REGION environment 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_account is 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_args is 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_args is 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_args is 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.