Get Routes for an Asset Swap
List of possible routes for swapping assets, including each route's steps, tokens, and amounts.
The /exchange/routes endpoint gives you a list of possible routes for swapping assets, including each route's steps, tokens, and amounts. Each route also includes a transaction payload that you can sign and send to the blockchain to complete the transfer.
You are incentivized to use Blockdaemon’s RPCs to interact with more than X blockchains.
How to Get Routes Using cURL
This request gets you the list of available routes to transfer 1 unit of token 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48
(USDC) from Ethereum (chainID 1) to Optimism (chainID 10), token 0x7F5c764cBc14f9669B88837ca1490cCa17c31607
(USDC.e).
curl -X GET 'https://svc.blockdaemon.com/defi/exchange/routes?fromChain=10&fromAmount=100000000000000000&fromToken=0xda10009cbd5d07dd0cecc66161fc93d7c9000da1&toChain=137&toToken=0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063&fromAddress=0xf271AAFC62634e6Dc9A276ac0f6145C4fDbE2Ced&toAddress=0xf271AAFC62634e6Dc9A276ac0f6145C4fDbE2Ced&slippage=0.1' \
-H "Authorization: Bearer $API_KEY"
fromAddress
: The sender's address.toAddress
: The recipient's address.
Running the Script Programmatically
You can also run this call programmatically by using the get-routes script from our defi-examples repository.
The relevant code that prepares the parameters for the swap is:
const routeParameters: GetRoutesRequest = {
fromChain: "eip155:10", // Optimism
fromToken: "0x7f5c764cbc14f9669b88837ca1490cca17c31607", // USDC.e
fromAmount: "1000000",
toChain: "eip:155137", // Polygon
toToken: "0xc2132d05d31c914a87c6611c10748aeb04b58e8f", // USDT
fromAddress: polygonWallet.address,
toAddress: optimismWallet.address,
slippage: 0.1,
};
Then, obtain the routes with:
const routes: RoutesResponse = await api.getRoutes(routeParameters);
This command fetches the available routes for transferring DAI from Optimism (chainID 10) to Polygon (chainID 137). You can run the script with: npx ts-node src/main/scripts/get-routes
.
👋 Need Help?
Contact us through email or our support page for any issues, bugs, or assistance you may need.
Updated about 1 month ago