Setup
- EVM
- Solana
This example swaps 100 USDC for WETH on Arbitrum. The SDK signs and sends transactions with
walletClient, a viem wallet client built from the private key in the PRIVATE_KEY environment variable. In a browser app, pass the connected wallet’s client instead. No API key is needed.Amounts are whole numbers in the token’s smallest unit, written as strings. USDC has 6 decimals, so 100 USDC is 100000000.Steps
- EVM
- Solana
1
Quote
getTradeQuotes asks the DEXs and aggregators DZap supports for a price. The response holds one entry per token pair, read into pair: its quoteRates lists every route that can fill the swap, and recommendedSource names the one DZap recommends. slippage: 1 accepts up to 1% less WETH than quoted; below that, the swap fails.2
Approve
The DZap router, the contract that performs the swap, needs your permission to move your USDC.
getAllowance reads how much it may spend now; if that’s less than the swap amount, approve() sends an approval for exactly that amount, which costs a little ETH in gas.Permit2 vs EIP-2612 vs plain approvals, and gasless permit signing, are in Check allowance and Approval mechanisms.
3
Build & send
trade() asks DZap to build the transaction for the recommended route, then signs it with your wallet and sends it. DZap simulates it first: if it would fail, for example because your USDC balance or allowance is too low, nothing is sent and result says why. recipient gets the WETH and refundee any refund; gasless: false means you pay the gas.4
Status
trade() returns as soon as the transaction is sent, so check getTradeTxnStatus every 15 seconds until the status leaves PENDING; COMPLETED means the swap went through. DZap answers 404 until it has picked up the transaction, and 429 once the keyless limit of 5 requests a minute is used up, so the catch skips only those two and rethrows anything else. The loop gives up after 5 minutes, and the check after it throws unless the status is COMPLETED.End-to-end
- EVM
- Solana
API usage
The same flow over REST. Non-EVM chains build a chain-specific transaction you sign locally, then submit via Broadcast.- EVM
- Solana
What can go wrong
- No route:
pair.quoteRatesis empty;pair.unavailableRoutessays why each provider declined. - The build fails:
trade()returns an error andbuildTradeTxnthrows. The error’sactionsays what to fix:ADD_FUNDS,INCREASE_ALLOWANCE, orTRY_ANOTHER_ROUTE(quote again). expiredfrombroadcastTradeTx: build and sign again right away.Not completed: no status: DZap never returned a status, usually because the transaction failed on-chain. Check the hash in a block explorer.- HTTP 429: without an API key, the trade API allows 5 requests a minute in total, shared by every endpoint, and
retryAftersays how many seconds to wait. The status loop skips 429s and polls again.