How to parse a Solana swap transaction for any DEX

Every exchange on Solana encodes a swap differently, and routers nest swaps inside swaps. Writing a decoder per program is a job that never ends. There is a simpler way that works for all of them, including ones that do not exist yet.

The idea: read the result, not the instructions

getTransaction returns every account's balance before and after:

Whatever route the swap took, the trade is the difference.

The steps for one wallet

  1. SOL change = post − pre for the wallet's account. If the wallet paid the network fee, add meta.fee back: the fee is a cost of sending, not part of the trade. Add any change in wrapped SOL (So11111111111111111111111111111111111111112) too.
  2. Token changes = post − pre for every mint the wallet owns an account of. A missing entry means zero.
  3. Classify: SOL down and a token up is a buy. A token down and SOL up is a sell. One token down and another up is a swap.
  4. Price = SOL moved ÷ tokens moved.

Fetch the transaction properly

{ "method": "getTransaction",
  "params": [ "<signature>",
    { "encoding": "jsonParsed", "maxSupportedTransactionVersion": 0 } ] }

Without maxSupportedTransactionVersion: 0 the RPC refuses versioned transactions, which is most swaps today. Addresses loaded from lookup tables are in meta.loadedAddresses and come after the message's own keys in the balance arrays.

The cases that break naive parsers

Naming the exchange

Look for known program ids among the transaction's accounts, routers first, so a Jupiter swap through a Raydium pool reads "Jupiter". Unknown programs still parse; they just have no name.

Working code

solana-swap-parser implements all of the above with no dependencies:

npx github:blixvip/solana-swap-parser <signature>
npx github:blixvip/solana-swap-parser wallet <address> --limit 20
import { fetchSwap, walletSwaps, parseSwap } from "solana-swap-parser";

const swap = await fetchSwap("<signature>");
// { type: "sell", wallet, sold: { mint, amount }, bought: { mint: "SOL", amount }, priceSol, dex: "pump.fun", fee }

const trades = await walletSwaps("<wallet>", { limit: 20 });

// Already have the transaction from your own RPC call or a webhook? No network needed.
const same = parseSwap(transaction, { wallet: "<an account in it>" });

Checked against a real pump.fun sell: 10,119,863.26 tokens for 0.132831235 SOL, the same figures the program's own trade event reported.

Want it done for you?

This is the small, open version. Blix is the AI-powered Solana memecoin terminal it comes from: every pump.fun launch live, AI coin ratings, narratives, wallet and X tracking, and one-click trading with 0% fees. There is a public API and an MCP server too.

Start trading on BlixJoin the Discord

Help build it

solana-swap-parser is open source and takes pull requests. There are good first issues with step-by-step instructions, and setup is git clone and npm test.

More guides

How to get a pump.fun token's market cap and bonding curve progress without an APIpump-curveHow to detect new pump.fun tokens in real time (and stream every trade)pump-streamHow to calculate a Solana token's top 10 holders percentage (and why explorers overstate it)sol-holdersHow to stop Solana RPC 429 errors (Too Many Requests)solana-rpc-poolWhy your Solana WebSocket stops receiving messages, and how to keep it alivesolana-ws-keepalive