While it’s possible to use the getRoutes
family of methods to retrieve routes, it’s a better user experience to stream that data back to the user so that are not waiting for a batch of routes.
Our websocket connection will send over routes as they become calculated and available.
Features
- Cross-Chain Swaps: Execute swaps across different blockchain networks.
- Quotes: Get quotes for cross-chain swaps.
- Easy to Use: Simplified methods for fetching and executing swaps.
Usage Example
import { SuaveSDK } from 'suave-sdk';
const sdk = new SuaveSDK({
apiKey: 'your-api-key'
});
/**
* Options for the route
*/
const options = {
preference:'price',//"speed"
feePercent: 0.04,
feeChainId:137,
feeTokenAddress:'0xa23202318e7eAf7179A845068aC64CE332Ea66B'
}
const slippagePercent = 0.5;
//the .getStreamingRoutes method returns a wss url, and can be used with any websocket library
const socket = new WebSocket(sdk.getStreamingRoutes({
tokenA,chainA,amount,tokenB,chainB,slippagePercent,options
}));
socket.onopen = () => {
console.log('WebSocket connection established');
// Send a message to request routes
socket.send(JSON.stringify({
action: 'getRoutes',
tokenA,
chainA,
amount,
tokenB,
chainB
}));
};
socket.onmessage = (event) => {
const routes = JSON.parse(event.data);
console.log('Received routes:', routes);
};
socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
socket.onclose = () => {
console.log('WebSocket connection closed');
};