Swap API

Execute token swaps on prediction markets

Swap API

The Swap API provides imperative swap operations with route preview. Use this for a two-step process: first get a quote, then create the swap transaction.

Access

from dflow import DFlowClient
 
client = DFlowClient()
swap_api = client.swap

Methods

get_quote

Get a quote for a swap without creating a transaction.

def get_quote(
    input_mint: str,
    output_mint: str,
    amount: int | str,
    slippage_bps: int | None = None,
) -> SwapQuote

Parameters

ParameterTypeDescription
input_mintstrMint address of the token to sell
output_mintstrMint address of the token to buy
amountint | strAmount to trade in base units
slippage_bpsint | NoneSlippage tolerance in basis points

Example

from dflow import USDC_MINT
 
quote = client.swap.get_quote(
    input_mint=USDC_MINT,
    output_mint=market.accounts["usdc"].yes_mint,
    amount=1000000,  # 1 USDC
    slippage_bps=50,
)
 
print(f"Input: {quote.in_amount}")
print(f"Output: {quote.out_amount}")
print(f"Price Impact: {quote.price_impact_pct}%")

create_swap

Create a swap transaction ready for signing.

def create_swap(
    input_mint: str,
    output_mint: str,
    amount: int | str,
    slippage_bps: int,
    user_public_key: str,
    wrap_unwrap_sol: bool | None = None,
    priority_fee: PriorityFeeConfig | None = None,
) -> SwapResponse

Parameters

ParameterTypeDescription
input_mintstrMint address of the token to sell
output_mintstrMint address of the token to buy
amountint | strAmount to trade in base units
slippage_bpsintSlippage tolerance in basis points
user_public_keystrUser's Solana public key
wrap_unwrap_solbool | NoneWhether to wrap/unwrap SOL automatically
priority_feePriorityFeeConfig | NonePriority fee configuration

Example

from solders.keypair import Keypair
from dflow import USDC_MINT, sign_send_and_confirm
from solana.rpc.api import Client
 
keypair = Keypair.from_base58_string("your-private-key")
connection = Client("https://api.mainnet-beta.solana.com")
 
# Create swap transaction
swap = client.swap.create_swap(
    input_mint=USDC_MINT,
    output_mint=market.accounts["usdc"].yes_mint,
    amount=1000000,
    slippage_bps=50,
    user_public_key=str(keypair.pubkey()),
)
 
# Sign and send
result = sign_send_and_confirm(connection, swap.swap_transaction, keypair)
print(f"Transaction: {result.signature}")

get_swap_instructions

Get swap instructions for custom transaction composition.

def get_swap_instructions(
    input_mint: str,
    output_mint: str,
    amount: int | str,
    slippage_bps: int,
    user_public_key: str,
    wrap_unwrap_sol: bool | None = None,
    priority_fee: PriorityFeeConfig | None = None,
) -> SwapInstructionsResponse

Types

SwapQuote

class SwapQuote(BaseModel):
    input_mint: str
    output_mint: str
    in_amount: str
    out_amount: str
    price_impact_pct: float
    route_plan: list[RoutePlanStep] | None

SwapResponse

class SwapResponse(BaseModel):
    swap_transaction: str  # Base64 encoded transaction
    last_valid_block_height: int | None
    prioritization_fee_lamports: int | None
    compute_unit_limit: int | None
    prioritization_type: str | None
    quote: SwapQuote | None

PriorityFeeConfig

class PriorityFeeConfig(BaseModel):
    type: Literal["exact", "max"]
    amount: int

On this page