Tutorial: a guarded swap with the SDK¶
This walks through a complete, runnable script that buys a token on testnet — with a spend cap you enforce yourself, a real slippage floor, and a balance check to confirm it worked. By the end you'll have the safe pattern you can build on.
You'll need: the SDK installed, a dedicated agent wallet funded on testnet, and a minted DID on that wallet.
The whole script¶
from decimal import Decimal
from primedelta import PrimeDelta, SwapSide, AccountStatus
RPC = "https://chain.testnet.primedelta.io"
SYMBOL = "AMMT1"
SPEND = Decimal("10") # dUSD to spend
MAX_TRADE_USD = Decimal("25") # your own per-trade cap
SLIPPAGE_BPS = 100 # 1%
pd = PrimeDelta(
private_key="0x…", # a dedicated agent wallet, funded on testnet
web3_provider_url=RPC,
network="testnet",
)
# 1. Authenticate.
pd.login()
# 2. Make sure this wallet can actually trade.
if pd.get_account_status() != AccountStatus.DID_MINTED:
raise SystemExit("wallet has no minted DID — verify it first")
# 3. Enforce your own cap BEFORE quoting.
if SPEND > MAX_TRADE_USD:
raise SystemExit(f"{SPEND} dUSD exceeds your cap of {MAX_TRADE_USD}")
# 4. Quote, and derive a real minimum-out (never zero).
quote = pd.quote_swap(SYMBOL, SwapSide.STABLECOIN_TO_STOCK, SPEND)
min_out = pd.min_out_from_quote(quote, slippage_bps=SLIPPAGE_BPS)
print(f"quote: {SPEND} dUSD -> ~{quote} {SYMBOL}, min accepted {min_out}")
# 5. Swap.
tx = pd.swap_exact_input(
SYMBOL,
SwapSide.STABLECOIN_TO_STOCK,
amount_in=SPEND,
min_amount_out=min_out,
)
print("swap tx:", tx)
# 6. Confirm it landed.
balances = pd.get_onchain_balances()
print(f"{SYMBOL} balance now: {balances.get(SYMBOL, Decimal(0))}")
What each step is doing¶
- Login signs a SIWE message with your signer and opens a session.
- The DID check is the gate: swaps require
AccountStatus.DID_MINTED. Checking it yourself gives a clear error instead of a revert deep in the swap. - The cap check comes before the quote. For a buy, the dUSD you spend is the trade's value, so you can reject an over-cap trade without pricing it — the same order the server uses.
min_out_from_quoteturns the quote into a floor:quote × (1 − slippage). Passing zero would accept any price; the swap rejects a non-positive floor anyway.swap_exact_inputreturns a transaction hash.get_onchain_balancesreturns adict[str, Decimal]; the token appears once its balance is above zero.
Make it a sell¶
Flip the side and swap a token amount for dUSD:
quote = pd.quote_swap(SYMBOL, SwapSide.STOCK_TO_STABLECOIN, Decimal("2"))
min_out = pd.min_out_from_quote(quote, slippage_bps=SLIPPAGE_BPS)
tx = pd.swap_exact_input(SYMBOL, SwapSide.STOCK_TO_STABLECOIN,
amount_in=Decimal("2"), min_amount_out=min_out)
For a sell the value isn't known until the quote, so check your cap against the quoted dUSD out right after quoting.
Oracle stocks are different¶
A tokenized stock like AAPL has no AMM quote — check the market is open, read the oracle price, and compute the minimum-out yourself with extra slippage. See Place a swap.
Next → Watch a price and swap