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:
meta.preBalancesandmeta.postBalances: SOL, by account indexmeta.preTokenBalancesandmeta.postTokenBalances: tokens, each entry with itsownerandmint
Whatever route the swap took, the trade is the difference.
The steps for one wallet
- SOL change = post − pre for the wallet's account. If the wallet paid the network fee, add
meta.feeback: the fee is a cost of sending, not part of the trade. Add any change in wrapped SOL (So11111111111111111111111111111111111111112) too. - Token changes = post − pre for every mint the wallet owns an account of. A missing entry means zero.
- 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.
- 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
- The fee payer is not always the trader. Bots and relayers pay the fee from one account and trade from another. Then the fee payer's balances did not move. Look at the other accounts whose tokens changed: one is the trader, the other is the pool or bonding curve, and they mirror each other.
- First buys pay rent. Creating a token account costs about 0.002 SOL, which inflates the price of very small first buys.
- Failed transactions. Check
meta.err. A failed swap still charges the fee.
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.