> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Solana: Priority fees for a Jupiter Swap in Python

> Calculate and apply optimal priority fees for Jupiter swap transactions on Solana using Python with compute budget instructions through Chainstack.

> **Part of the [How to land transactions on Solana](https://docs.chainstack.com/docs/solana-how-to-land-transactions.md) guide.**

**TLDR:**

* Jupiter offers a quick way to swap tokens on Solana but lacks a Python SDK, so we call the `/build` endpoint of Jupiter's Swap API v2 for the raw swap instructions.
* We calculate our priority fee by fetching recent block data via [`getRecentPrioritizationFees`](https://docs.chainstack.com/reference/solana-getrecentprioritizationfees), then take the median of the non-zero slots and multiply it by a user-defined factor.
* We assemble the transaction ourselves with that compute unit price and a compute unit limit measured by simulation. The priority fee, paid on top of the 5000-lamport base fee, helps the transaction land under congestion.
* The flow makes one Jupiter call and uses your own Solana node (e.g., Chainstack) for fee data, simulation, and the final broadcast, giving you more control over performance and reliability.

> [!WARNING]
>
> Jupiter retired the old `quote-api.jup.ag/v6` endpoints on October 1, 2025, and recommends [Swap API v2](https://developers.jup.ag/docs/swap) over v1 (`/quote` and `/swap`). This guide uses the v2 Router endpoint, `https://api.jup.ag/swap/v2/build`, which returns raw swap instructions for a transaction you send through your own node. `api.jup.ag` works without a key at a low request rate and takes an `x-api-key` header for higher tiers (get a key at [portal.jup.ag](https://portal.jup.ag)); `lite-api.jup.ag` is being phased out.

## Main article

With its cumulative volume in [USD billions](https://defillama.com/protocol/jupiter-aggregator#information), Jupiter is one of the top DEX aggregators on Solana.

In this tutorial, we are going to have a look at how add our own prioritization fees to swap for a token pair through a Jupiter aggregator route.

As Jupiter does not have a Python SDK at the time of this tutorial, it's more fun doing this in Python.

For JavaScript, check out the [Jupiter developer docs](https://developers.jup.ag/docs/swap).

## Transaction fees on Solana

The topic of transactions fees on Solana is both a nascent development and its own can of worms at the same time, so we are not going to do a deep dive into this.

Here's however what you need to know in a succinct form for this tutorial:

* Base fee — fixed 5000 lamports (0.000005 SOL) per transaction signature, which 99% of the time means 5000 lamports per transaction
* Priority fee — a non-fixed amount that you can include in your transaction on top of the 5000 lamports base fee to get ahead of others in the queue for the block inclusion of the current leader.

So in theory — the higher the priority fee, the more chances you have to skip ahead and become a part of the state of the chain than the others you are racing against.

Let's implement adding a proper priority fee to our transaction based on an interaction with the Jupiter aggregator.

Get yourself a Chainstack Solana node.

## Overview

Here's how it's going to work:

1. Get the priority fee data for the recent 150 blocks with the `getRecentPrioritizationFees` call from a Chainstack Solana node, and calculate the compute unit price.

2. Get the swap instructions for the token pair (route, liquidity pools, and the accounts they use) from the Jupiter `/build` endpoint.

3. Assemble the transaction with your compute unit price, and simulate it through the Chainstack Solana node to set the compute unit limit.

4. Sign the transaction and send it through the Chainstack Solana node.

## Implementation

The flow is: estimate a priority fee from recent on-chain data, get the swap instructions from Jupiter, assemble the transaction with that fee, then simulate, sign, and send it through your own Solana node. Jupiter doesn't have an official Python SDK, so we call its REST API directly and build and sign the transaction with `solders`.

Install the dependencies:

```shell Shell theme={"system"}
pip install requests solders
```

The complete script:

```python jupiter_swap.py theme={"system"}
import base64
import math
import statistics

import requests
from solders.address_lookup_table_account import AddressLookupTableAccount
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
from solders.hash import Hash
from solders.instruction import AccountMeta, Instruction
from solders.keypair import Keypair
from solders.message import MessageV0
from solders.pubkey import Pubkey
from solders.transaction import VersionedTransaction

# Your Chainstack Solana node and wallet
RPC_ENDPOINT = "YOUR_CHAINSTACK_SOLANA_NODE"
PRIVATE_KEY = "YOUR_PRIVATE_KEY"  # base58-encoded

# Jupiter Swap API v2. Works without a key at a low request rate; for higher
# rates, send an x-api-key header with a key from https://portal.jup.ag
JUPITER = "https://api.jup.ag/swap/v2"

INPUT_MINT = "So11111111111111111111111111111111111111112"   # SOL
OUTPUT_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"  # USDC
FEE_ACCOUNT = "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2"  # Raydium SOL-USDC pool state
AMOUNT = 1_000_000          # 0.001 SOL, in lamports
SLIPPAGE_BPS = 50           # 0.5%
PRIORITY_MULTIPLIER = 1.1   # bump over the median fee
MAX_CU_LIMIT = 1_400_000    # the per-transaction maximum

wallet = Keypair.from_base58_string(PRIVATE_KEY)


def rpc(method, params):
    resp = requests.post(RPC_ENDPOINT, json={
        "jsonrpc": "2.0", "id": 1, "method": method, "params": params,
    }).json()
    if "error" in resp:
        raise RuntimeError(resp["error"])
    return resp["result"]


def to_instruction(ix):
    return Instruction(
        Pubkey.from_string(ix["programId"]),
        base64.b64decode(ix["data"]),
        [AccountMeta(Pubkey.from_string(a["pubkey"]), a["isSigner"], a["isWritable"])
         for a in ix["accounts"]],
    )


# 1) Estimate the compute unit price from recent blocks. Pass a writable account
#    that swaps on this pair lock in most slots, such as a busy pool's state account.
#    Slots where nothing wrote to it return 0, so take the median of the non-zero slots.
#    Use the median, not the max: occasional astronomical fees can drain your wallet.
recent = rpc("getRecentPrioritizationFees", [[FEE_ACCOUNT]])
fees = [x["prioritizationFee"] for x in recent if x["prioritizationFee"] > 0]
median_fee = int(statistics.median(fees)) if fees else 0
cu_price = int(median_fee * PRIORITY_MULTIPLIER) or 1_000
print(f"Median priority fee: {median_fee} micro-lamports/CU over {len(fees)} non-zero slots")
print(f"Compute unit price: {cu_price} micro-lamports/CU")

# 2) Get the swap instructions from Jupiter.
build = requests.get(f"{JUPITER}/build", params={
    "inputMint": INPUT_MINT, "outputMint": OUTPUT_MINT, "amount": AMOUNT,
    "slippageBps": SLIPPAGE_BPS, "taker": str(wallet.pubkey()),
}).json()
if "error" in build:
    raise RuntimeError(build["error"])
print(f"Quote: {AMOUNT} -> {build['outAmount']} (out)")

# Leave out build["computeBudgetInstructions"]: it holds Jupiter's own price estimate,
# and the transaction below carries your price from step 1 instead.
swap_ixs = [to_instruction(ix) for ix in build["setupInstructions"]]
swap_ixs.append(to_instruction(build["swapInstruction"]))
if build["cleanupInstruction"]:
    swap_ixs.append(to_instruction(build["cleanupInstruction"]))
swap_ixs += [to_instruction(ix) for ix in build["otherInstructions"]]

lookup_tables = [
    AddressLookupTableAccount(Pubkey.from_string(key), [Pubkey.from_string(a) for a in addresses])
    for key, addresses in (build["addressesByLookupTableAddress"] or {}).items()
]
blockhash = Hash.from_string(rpc("getLatestBlockhash", [{"commitment": "confirmed"}])["value"]["blockhash"])


def compile_tx(cu_limit):
    ixs = [set_compute_unit_limit(cu_limit), set_compute_unit_price(cu_price)] + swap_ixs
    message = MessageV0.try_compile(wallet.pubkey(), ixs, lookup_tables, blockhash)
    return VersionedTransaction(message, [wallet])


# 3) Simulate at the maximum limit to measure the compute units the swap uses,
#    then set the limit to 1.2x that, so you don't pay a priority fee on unused units.
sim = rpc("simulateTransaction", [
    base64.b64encode(bytes(compile_tx(MAX_CU_LIMIT))).decode(),
    {"encoding": "base64", "replaceRecentBlockhash": True},
])["value"]
if sim["err"]:
    raise RuntimeError(f"Simulation failed: {sim['err']}")
cu_limit = min(math.ceil(sim["unitsConsumed"] * 1.2), MAX_CU_LIMIT)
priority_fee = math.ceil(cu_price * cu_limit / 1_000_000)
print(f"Compute unit limit: {cu_limit}, priority fee: {priority_fee} lamports")

# 4) Sign the transaction and send it through your node.
signed = compile_tx(cu_limit)
sig = rpc("sendTransaction", [
    base64.b64encode(bytes(signed)).decode(),
    {"encoding": "base64", "skipPreflight": True},
])
print(f"Sent: https://solscan.io/tx/{sig}")
```

A few notes on the current Jupiter API:

* **Compute unit price.** `/build` returns a `SetComputeUnitPrice` instruction with Jupiter's own estimate in `computeBudgetInstructions`. The script leaves it out and sets the price from step 1. To use Jupiter's estimate instead, include those instructions and choose the level with the `computeUnitPricePercentile` parameter (`medium`, `high`, `veryHigh`, or a value in basis points).
* **Compute unit limit.** `/build` doesn't set one. The script simulates at the 1,400,000 maximum, then sets the limit to 1.2 times the units consumed. The priority fee in lamports is the compute unit price times the compute unit limit divided by 1,000,000, rounded up, so a tighter limit costs less.
* **Versioned transactions.** `/build` returns instructions for a v0 transaction, plus the addresses in each lookup table in `addressesByLookupTableAddress`, so `solders` compiles the message without fetching the tables. With `transactionVersion=1`, it returns no compute budget instructions; a [v1 transaction](https://docs.chainstack.com/docs/solana-transaction-v1.md) carries its compute unit limit and priority fee in the message instead.
* **Why the median.** Fees over the recent window can include random astronomical outliers — basing your fee on the max can drain your wallet. The median of the non-zero slots (optionally bumped by `PRIORITY_MULTIPLIER`) is a safer baseline. Tune it for your needs.

> [!NOTE]
>
> The companion repository [jupiter-swaps-priority-fees-python](https://github.com/chainstacklabs/jupiter-swaps-priority-fees-python) has the full project. The base fee on Solana is a fixed 5000 lamports per signature; the priority fee above is added on top.

## Conclusion

Solana is a very fun and extremely lively ecosystem, so have fun building, keep and eye on the all changes and how the fee market develops — this can give you an edge.

And use Chainstack, of course.

> [card] Ake
>
> ![Ake](https://mintcdn.com/chainstack/UN3rP7zhB69idvnC/images/docs/profile_images/1719912994363326464/8_Bi4fdM_400x400.jpg?fit=max\&auto=format\&n=UN3rP7zhB69idvnC\&q=85\&s=792a24ab1b4682406fa589c0ecd88e5d)
>
> &#x20;Director of Developer Experience @ Chainstack
> \
> &#x20;Talk to me all things Web3
> \
> 20 years in technology | 8+ years in Web3 full time years experience
>
> [https://github.com/akegaviar/](https://github.com/akegaviar/)
>
> [https://twitter.com/akegaviar](https://twitter.com/akegaviar)
>
> [https://www.linkedin.com/in/ake/](https://www.linkedin.com/in/ake/)
