Priority Fees

How to set Priority Fees in the Drift Client.

Here is the setup code we discussed on the last page for initializing a Drift Client, with one notable addition: the introduction of TxParams in the constructor.

import os
import asyncio

from dotenv import load_dotenv

from solders.keypair import Keypair

from solana.rpc.async_api import AsyncClient

from anchorpy import Wallet

from driftpy.drift_client import DriftClient
from driftpy.keypair import load_keypair
from driftpy.types import TxParams

async def main():
    load_dotenv()
    secret = os.getenv("PRIVATE_KEY") # as a [u64]
    url = os.getenv("RPC_URL") # as a string
    
    keypair = load_keypair(secret)
    wallet = Wallet(kp)
    
    connection = AsyncClient(url)
    
    compute_units_limit = 700_000
    microlamports_per_cu_requested = 100_000
    
    drift_client = DriftClient(
        connection,
        wallet,
        "mainnet", # or "devnet"
        tx_params=TxParams(compute_units_limit, microlamports_per_cu_requested)
    )
    
    await drift_client.add_user(0)
    
    await drift_client.subscribe()

The TxParams object represents the priority fees you want to add on to any transaction sent with the Drift Client. It is set by default to request 600,000 compute units with zero priority fee. Here's what the TxParams object looks like and what each field means.

@dataclass
class TxParams:
    compute_units: Optional[int]
    compute_units_price: Optional[int]

compute_units - the number of compute units you intend to request for all transactions sent from the Drift Client. This value will typically range from 300,000 to a maximum of 1,400,000. Any values larger than 1.4m here will be overridden by the Solana runtime and set to 1.4m. If compute_units_price is also set with this value, you will pay more SOL in transaction fees the higher the compute_units value increases.

compute_units_price - the number of microlamports you intend to pay as a priority fee per compute unit requested. This value will typically range from 100,000 to 100,000,000. The higher this number is, the more SOL you will pay in transaction fees.

Let's look at an example of how priority fee calculations work. Say you set compute_units to 1,000,000 and compute_units_price to 100,000 like this:

    drift_client = DriftClient(
        connection,
        wallet,
        "mainnet",
        tx_params=TxParams(1_000_000, 100_000)
    )

How much will your transaction cost? Let's do the math:

Plus the 5000 lamports from the default fee yields a total cost of .000105 SOL for that transaction. it's important to note that raising your priority fee does not guarantee inclusion in the block! The only data that we have regarding this topic is that transactions with any priority fee land more consistently than transactions without priority fees. Therefore, if you already have a priority fee set and are seeing dropped transactions, increasing your priority fee may help, but there is no guarantee that it will result in better transaction confirmation rates. However, if you do not have priority fees set at all, setting a priority fee of even 10,000 microlamports may significantly increase your chances of landing a transaction.

Last updated