Embedding Swap in a dApp
This guide shows how to add token swap functionality to a web application using the Vulcx 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: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.