As a dApp developer, one of the biggest challenges is managing cross-chain user journeys. LI.FI Official solves this by acting as a Bridge Aggregator and Cross-Chain Swap Aggregator, finding the best route for any swap. This guide shows you how to perform a LI.FI SDK Integration.
The Problem: A Maze of Bridges and DEXs
To swap from USDC on Polygon to ETH on Arbitrum, a user might need to use a bridge, then a DEX, paying multiple fees and dealing with a clunky UX.
The Solution: LI.FI Smart Routing
LI.FI abstracts this entire process away. Its Smart Routing DeFi engine queries dozens of bridges, DEXs, and solvers to find the most efficient path for any given swap, optimizing for speed, cost, and security.
How to use LI.FI: The SDK Integration
Install the SDK:
bash
npm install @lifi/sdk
Initialize and Request a Route: In your application, initialize the SDK and request a route.
Example: Get a Route and Execute a Swap (React/TS)
JavaScript
import LiFi from '@lifi/sdk';
const lifi = new LiFi({
integrator: 'My-Awesome-dApp'
});
async function getAndExecuteSwap() {
const routeRequest = {
fromChainId: 137, // Polygon
fromAmount: '100000000', // 100 USDC
fromTokenAddress: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC on Polygon
toChainId: 42161, // Arbitrum
toTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', // ETH on Arbitrum
options: {
slippage: 0.03, // 3%
}
};
// 1. Request the best route
const routeResponse = await lifi.getRoutes(routeRequest);
const bestRoute = routeResponse.routes[0];
console.log('Best route found:', bestRoute);
// 2. Execute the swap (requires a wallet signer)
// The SDK handles all approvals and contract calls
await lifi.executeRoute(signer, bestRoute);
console.log('Swap executed!');
}
You can also use the highly-customizable LI.FI Widget for a no-code integration. For full SDK options, please refer to https://sites.google.com/verified-web3-portal.com/li-fi/.
Top comments (0)