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 lists possible routes for swapping assets, including each route's steps, tokens, and amounts. Each route also includes a transaction payload 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/v1/exchange/routes?fromChain=eip155:10&fromAmount=100000000000000000&fromToken=0xda10009cbd5d07dd0cecc66161fc93d7c9000da1&toChain=eip155: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",
fromToken: sourceToken.address, //OP on OP
fromAmount: amountToTransferUnits,
toChain: "eip155:137",
toToken: targetToken.address, // MATIC on Polygon
fromAddress: optimismWallet.address,
toAddress: polygonWallet.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: npm exec ts-node src/main/scripts/get-routes
.
Mainnet vs Testnet
While we support a few testnets, reliability is minimal compared to operations in mainnet. Cross-chain bridges on testnets have limited liquidity and few incentives to operate. Thus, we do not guarantee high-reliability on getting routes for testnets.
👋 Need Help?
Contact us through email or our support page for any issues, bugs, or assistance you may need.
Updated 3 months ago