Skip to content

Place a swap

Swaps run on the DEX. You must be logged in with a minted DID, and you should always derive a real min_amount_out from a quote — never zero.

Sides

from primedelta import SwapSide

SwapSide.STABLECOIN_TO_STOCK   # buy:  dUSD -> token
SwapSide.STOCK_TO_STABLECOIN   # sell: token -> dUSD

There is no "buy"/"sell" string — use the enum.

Exact-input (spend a fixed amount)

from decimal import Decimal

quote = pd.quote_swap("AMMT1", SwapSide.STABLECOIN_TO_STOCK, Decimal("10"))
tx = pd.swap_exact_input(
    "AMMT1",
    SwapSide.STABLECOIN_TO_STOCK,
    amount_in=Decimal("10"),
    min_amount_out=pd.min_out_from_quote(quote, slippage_bps=100),
    deadline_seconds=600,   # optional
)

swap_exact_input returns a transaction hash.

Exact-output (receive a fixed amount)

tx = pd.swap_exact_output(
    "AMMT1",
    SwapSide.STABLECOIN_TO_STOCK,
    amount_out=Decimal("1"),
    max_amount_in=Decimal("12"),
)

Token-to-token

To swap between two tokens without spelling out a dUSD side (routed through dUSD under the hood):

tx = pd.swap_token_to_token_exact_input(
    input_symbol="AMMT1",
    output_symbol="AMMT2",
    amount_in=Decimal("5"),
    min_amount_out=Decimal("…"),
)

There's a matching swap_token_to_token_exact_output.

Oracle stocks

An oracle stock (e.g. AAPL) has no AMM quote. Check the market is open, read the oracle price, compute a minimum-out with extra slippage for the pool fee, and swap with the same STABLECOIN_TO_STOCK / STOCK_TO_STABLECOIN side. The signed price expires, so act promptly.

if not pd.is_market_open():
    raise SystemExit("market closed")

Safe pattern, every time

  1. instrument_kind(symbol) — AMM or oracle?
  2. AMM: quote_swapmin_out_from_quote. Oracle: oracle_price → compute min-out with extra slippage.
  3. is_market_open() for oracle stocks.
  4. swap_exact_input(...) with a positive min_amount_out.

Never min_amount_out=0

A zero floor accepts any price and invites a sandwich. Always derive it from a quote. The server rejects a non-positive floor outright.

Common reverts

  • slippage revert — price moved past your floor; re-quote and retry.
  • no-liquidity revert — the pool is too thin for that size; reduce it.
  • MarketClosed / stale price — an oracle stock outside US market hours.

See Troubleshooting.


Also: Quote & simulate · On-chain balances