Embedding Swap in a dApp
This guide shows how to add token swap functionality to a web application using the Argyros API and a Solana wallet adapter.Prerequisites
- A web app with a Solana wallet adapter (e.g.,
@solana/wallet-adapter-react) @solana/web3.jsinstalled
Implementation
1. Quote function
TypeScript
2. Swap function
TypeScript
3. Putting it together (React)
TypeScript
Error handling
Always handle these cases in your UI:| Error | User-facing message |
|---|---|
no route found | ”No swap route available for this pair” |
insufficient liquidity | ”Not enough liquidity. Try a smaller amount.” |
Simulation insufficientFunds | ”Insufficient balance” |
Simulation slippageExceeded | ”Price changed. Try again.” |
| Transaction expired | ”Transaction expired. Retry.” |
| Wallet rejected | ”Transaction cancelled” |
Best practices
- Show price impact. Display
priceImpactSeverityand warn users onmoderateor higher. - Show route info. Display
hopCountand pool types so users understand the execution path. - Refresh quotes. Quotes go stale. Re-fetch if the user hasn’t submitted within 15-30 seconds.
- Handle rate limits. Implement retry with backoff for
429responses. See Rate Limits.