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¶
Every DEX pool pairs a token with dUSD, so dUSD is the hub: you can swap any token for any other — an oracle-priced token for an oracle-free one, or one tokenized stock for another — and it routes through dUSD under the hood (two legs, one call), without your spelling out the dUSD side:
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-priced tokens¶
An oracle-priced token (e.g. AAPL) has no pool quote — its oracle-priced pool settles from a signed oracle, not pool reserves. 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.
Safe pattern, every time¶
instrument_kind(symbol)— oracle-free (amm) or oracle-priced (oracle)?- For an oracle-priced token, check
is_market_open()first — if it's closed, stop: the signed price is withheld (oracle_pricereturns null) and the pool reverts. Oracle-free tokens skip this. - Price it. Oracle-free:
quote_swap→min_out_from_quote. Oracle-priced:oracle_price→ compute min-out with extra slippage (oracle-priced tokens have noquote_swap/spot_price). swap_exact_input(...)with a positivemin_amount_out.
Never min_amount_out=0
A zero floor accepts any price and invites a sandwich. The raw SDK does not reject a non-positive floor — it submits the swap with no protection — so deriving min_amount_out from a quote is on you. (The MCP server does reject min_amount_out <= 0 on your behalf; the SDK does not.)
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-priced token outside US market hours.
See Troubleshooting.
Also: Quote & simulate · On-chain balances