Canonical: https://solanashreds.xyz/docs/programs/jupiter

# Jupiter recipe: decoding routed swaps

Read every Jupiter v6 route instruction into one record: who swapped, from which account to which, the amount in, the quoted amount out, the slippage they accepted and the venues on the route. Tested on real routes, and explicit about the one number the instruction cannot give you.

To decode a Jupiter swap, match the route instruction's discriminator in Jupiter's on-chain IDL, then read the input amount, the quoted output, the slippage in basis points and the route plan from its arguments, and the user and token accounts by position. The amount actually received is not in the instruction; it exists only after execution.

## Program ID and IDL

| What | Value |
| --- | --- |
| Program ID | `JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4` |
| IDL to use | the on-chain Anchor IDL, account `C88XWfp26heEmDkmfSzeXP7Fd7GQJ2j9dDTUsyiZbUTa` |
| IDL to avoid | `idl.json` in the jupiter-cpi repository: no `route_v2`, and an older list of swap variants |
| Route instructions | `route`, `shared_accounts_route`, `exact_out_route`, `route_with_token_ledger`, their `v2` forms and the `with_attestation` forms |

Fetch the on-chain IDL as [reading an IDL](https://solanashreds.xyz/learn/reading-an-idl) describes, and pin the copy you used.

## Steps

1. Keep top-level instructions whose program is Jupiter v6.
2. Decode with the on-chain IDL through the [Anchor decoder](https://solanashreds.xyz/docs/decode/anchor-instructions); keep only route-family instructions.
3. Classify the mode: exact in (you fix the input), exact out (you fix the output) or token ledger (the input is read from a ledger account at run time).
4. Normalise the accounts. Plain routes call them `user_source_token_account` and `user_destination_token_account`; shared-accounts routes call them `source_token_account` and `destination_token_account`. Plain `route` has no `source_mint` at all.
5. Read the route plan: each step names a venue through its swap variant, a share of the input, and input and output indexes.

## jupiter.ts

```typescript file="src/jupiter.ts"
// Jupiter v6 recipe: one shape for every route instruction (route, shared_accounts_route, the exact-out and v2
// variants), decoded with Jupiter's on-chain Anchor IDL (account C88XWfp26heEmDkmfSzeXP7Fd7GQJ2j9dDTUsyiZbUTa).
// The copy in the jupiter-cpi repository is older: it has no route_v2 and fewer swap variants.
import { AnchorDecoder } from "./anchor.ts";

export const JUPITER_V6 = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";

export type Leg = { dex: string; share: number; shareUnit: "percent" | "bps"; inputIndex: number; outputIndex: number };
export type JupiterRoute = {
  instruction: string;
  mode: "exact_in" | "exact_out" | "token_ledger"; // token_ledger: the input amount is read from a ledger at run time
  user: string;                // signs and owns the source account
  sourceAccount: string;       // user's token account the input comes from
  destinationAccount: string;  // token account the output goes to
  sourceMint: string | null;   // null for `route`, which does not list it: look it up from the source account
  destinationMint: string;
  inAmount?: bigint; quotedOutAmount?: bigint;   // exact in: what goes in, and the quote for what comes out
  outAmount?: bigint; quotedInAmount?: bigint;   // exact out: what must come out, and the quote for what goes in
  slippageBps: number; platformFeeBps: number;
  legs: Leg[];
};

export class JupiterDecoder {
  private dec: AnchorDecoder;
  constructor(onChainIdl: object) { this.dec = new AnchorDecoder(onChainIdl as any); }

  decode(data: Uint8Array, accounts: string[]): JupiterRoute | null {
    const d = this.dec.decodeWithAccounts(data, accounts, JUPITER_V6);
    if (!d || !/route/.test(d.name)) return null;
    const a = d.accounts, x = d.args as Record<string, any>;
    const legs: Leg[] = (x.route_plan as any[]).map((step) => ({
      dex: Object.keys(step.swap)[0],                                  // the Swap enum variant names the venue
      share: step.percent ?? step.bps, shareUnit: step.percent !== undefined ? "percent" : "bps",
      inputIndex: step.input_index, outputIndex: step.output_index,
    }));
    return {
      instruction: d.name,
      mode: /ledger/.test(d.name) ? "token_ledger" : /exact_out/.test(d.name) ? "exact_out" : "exact_in",
      user: a.user_transfer_authority!,
      sourceAccount: (a.user_source_token_account ?? a.source_token_account)!,
      destinationAccount: (a.destination_token_account ?? a.user_destination_token_account)!,
      sourceMint: a.source_mint ?? null,
      destinationMint: a.destination_mint!,
      inAmount: x.in_amount, quotedOutAmount: x.quoted_out_amount, outAmount: x.out_amount, quotedInAmount: x.quoted_in_amount,
      slippageBps: x.slippage_bps, platformFeeBps: x.platform_fee_bps, legs,
    };
  }
}
```

## One route, decoded and compared with the result

This `route` from mainnet slot 470,901,816 sends a token through one Orca Whirlpool pool:

| Field | From the instruction | From execution |
| --- | --- | --- |
| Mode | exact in | |
| In amount | 16,071,789 base units | |
| Quoted out amount | 13,483,579 base units | |
| Slippage | 200 basis points | |
| Least acceptable output | 13,483,579 less 2%: 13,213,907 | |
| Route plan | one leg: `WhirlpoolSwapV2`, 100% | |
| Received | not in the instruction | 13,483,579 |

Here the fill matched the quote exactly. In another route from the same sample the quote was 5,709,799 and 5,706,480 arrived, inside a 100 basis point tolerance. You can only learn which of those happened from execution data.

## Route plan legs

Each step's swap variant is the venue, named in the IDL's `Swap` enum, which has 211 variants in the current on-chain IDL. In 16 real routes we saw 18 different ones, from `WhirlpoolSwapV2` and `MeteoraDlmmSwapV2` to `PumpSwapBuyV4` and `RaydiumClmmV2`. Older routes give each step a `percent`; v2 routes give `bps`, so a split of 10,000 means all of the input.

The venue programs themselves appear only as accounts passed to Jupiter. Jupiter calls them during execution, so the swaps on each venue are inner instructions and are not in the raw transaction; [inner instructions and CPIs](https://solanashreds.xyz/learn/inner-instructions-and-cpi) shows what that looks like for a route like this one. For the venues' own instructions, see the [Whirlpool recipe](https://solanashreds.xyz/docs/programs/orca-whirlpool) and the [Raydium AMM v4 recipe](https://solanashreds.xyz/docs/programs/raydium-amm).

## How this code was tested

Sixteen successful top-level Jupiter routes from finalized mainnet transactions (6 `route`, 10 `route_v2`), decoded with the on-chain IDL and checked against the transactions' own balances:

| Check | Result |
| --- | --- |
| The decoded user signed the transaction | 16 of 16 |
| Exact in: the source account went down by exactly `in_amount` | 6 of 6 where no other instruction touched that account |
| Exact in: what arrived is at least the quote less the stated slippage | 7 of 7 where no other instruction touched the destination |

Transactions that moved the same accounts with other instructions, such as a route followed by a sell, were left out of the balance checks because their balances do not isolate the route.

## What you cannot know before execution

The instruction records intent: an exact amount in, a quote, and how far below the quote the user will accept. The actual output, the price each venue gave, any positive slippage kept or returned and whether the route succeeded at all are execution results. A shred decoder can report "this wallet asked to swap 16,071,789 units for at least 13,213,907"; it cannot report the fill until you [reconcile with RPC](https://solanashreds.xyz/docs/pipeline/reconcile). [Who made this swap](https://solanashreds.xyz/blog/who-made-this-swap) looks at attributing routes to users.

## Routes this recipe will not see

Routes that another program sends through Jupiter by CPI are inner instructions and never appear in the raw transaction. Token ledger routes do not state their input amount at all. And if Jupiter adds route instructions or swap variants, a pinned IDL stops matching: an unknown discriminator from this program, or a variant index past the end of the enum, means the IDL needs refreshing, as covered in [what your decoder cannot see](https://solanashreds.xyz/blog/what-your-decoder-cannot-see).

## Following the route further

[Reading a DEX swap](https://solanashreds.xyz/learn/how-swaps-look-as-instructions) explains the shape all swap instructions share, and the [Borsh layout rules](https://solanashreds.xyz/learn/borsh-encoding) decode this exact route byte by byte. To run the recipe on your own feed, [see pricing](https://solanashreds.xyz/pricing).
