Example: Quote With Fallback
This example shows an implementation for token swapping using the ParaSwap API, particularly focusing on getting a quote for a token swap and handling both Delta pricing and a fallback to market pricing when Delta fails.
We recommend using Paraswap SDK for better developer experience. You can find SDK example of the exact functionality of this code here.
Example:
Expected Behavior for Integrators:
When executed, this script:
Connects to an Ethereum wallet.
Retrieves the user's wallet address and prepares to swap 100 DAI for PSP tokens on Ethereum Mainnet (Chain ID: 1).
Requests a quote from ParaSwap with the
mode: "all"
, meaning it will prioritize Delta pricing but fall back to market pricing if Delta is unavailable.If Delta pricing is available, the script:
Builds an order for execution.
Signs the order using EIP-712 structured signing.
Submits the order for execution through the Delta auction system.
Polls the order status to confirm whether it was executed.
If Delta pricing is not available, the script:
Logs the reason for the fallback.
Retrieves a market price route instead.
Builds a transaction using the market price.
Submits the transaction directly to the Ethereum network using the user's wallet.
This example ensures the swap execution is optimized for efficiency while providing a fail-safe fallback in case Delta pricing is unavailable. As a developer, you should expect a seamless experience for token swaps with automatic routing adjustments.
Last updated