Comment on page
🪄
Migrating to LI.FI SDK v2
Migration guide of upgrading LI.FI's SDK v1 to v2
This migration guide is a quick reference to the upgrades/fixes in LI.FI's SDK v2. All the breaking changes, deprecations, improvements, etc, are listed below. We advise going through all the changes and updating the SDK implementation in your project accordingly.
The method
getByChainID
is no longer available in the SDK, use ChainsService
instead.1
- const chainResponse = getChainById(Number(chainId));
2
3
+ import ChainsService from './ChainsService'
4
+
5
+ const chainService = ChainsService.getInshttps://meet.google.com/mda-wkwc-jmj?authuser=abhishek%40li.financetance()
6
+ const chainResponse = await chainService.getChainById(fromChainId)
The method
updateCallback
is renamed to updateRouteHook
import { LiFi } from "@lifi/sdk"
const lifi = new LiFi(lifiConfig);
const routeUpdateCallback = () => { ... }
const executionSettings: ExecutionSettings = {
- updateCallback: routeUpdateCallback,
+ updateRouteHook: routeUpdateCallback
}
const response = await lifi.execute(signer, route, executionSettings);
The method
acceptSlippageUpdatehook
is renamed to acceptExchangeRateUpdateHook
.1
const exchangeRateUpdateCallback = () => { ... }
2
3
const executionSettings: InternalExecutionSettings = {
4
- acceptSlippageUpdateHook: exchangeRateUpdateCallback,
5
+ acceptExchangeRateUpdateHook: exchangeRateUpdateCallback,
6
}
During the initialisation of the SDK, you should pass the
integrator
as part of the configuration object.The configuration to instantiate LiFi is now mandatory.
import { Lifi } from '@lifi/sdk'
// Mandatory to pass the configuration object
const lifiConfig = {
integrator: "your-integrator-name"
}
const lifi = new LiFi(lifiConfig)
By providing the
integrator
during the initialisation of the SDK, it'll be included as request headers for all requests made by the SDK which would later be used for tracking and identification purposes on the server side.Any route interaction to SDK would now return the initial Promise instance of Route created during the first execution of Route. In order to get more details about the Route, developer would need to resolve the Promise to access the information.
For single-step executions a new helper function,
convertQuoteToRoute
is added to the SDK. This function takes a Step
object as input and returns a Route
object. It is useful for converting quotes to routes, which is necessary for executing transactions.import { convertQuoteToRoute } from '@lifi/sdk';
import { LiFi } from '@lifi/sdk';
const lifi = new LiFi(lifiConfig);
const quoteWithSingleStep = await lifi.getQuote(quoteRequest);
// Use the convertQuoteToRoute function to convert the Step to a Route
const route: Route = convertQuoteToRoute(quoteWithSingleStep);
// Now, you can use the route object for executing transactions
const response = await lifi.executeRoute(route);
A new method
getConnections
is added to the SDK, which retrieves available connections for swapping or bridging tokens. This function takes a ConnectionsRequest
object as a parameter and returns a ConnectionsResponse
object.import { LiFi } from '@lifi/sdk';
const lifi = new LiFi(lifiConfig);
const request: ConnectionsRequest = {
fromChain: 1,
fromToken: '0x123456789abcdef',
toChain: 137,
toToken: '0x987654321fedcba',
allowBridges: ['connext', 'uniswap']
}
const connections = await lifi.getConnections(request);
A new callback is added that allows developers to customise the gas configuration for the transaction request before sending the transaction to the blockchain.
The callback can be set in
ExecutionSettings
while interacting with the SDK.import { LiFi } from '@lifi/sdk';
const lifi = new LiFi();
// define your logic for modifying the gas config of the transaction request
const updateGasConfig = async (txRequest: TxRequest): Promise<TxRequest> => {
const customGasConfig = await customGas();
const updatedTxRequest = {
...txRequest,
...customGasConfig
}
return updatedTxRequest;
}
// add callback function to modify the gas configurations, in settings
const settings: ExecutionSettings = {
signer,
route,
settings: {
updateTransactionRequestHook: updateGasConfig
}
}
const response = await lifi.executeRoute(signer, route, settings)
Only the modified gas config variable such as
gasLimit
, gasPrice
, maxPriorityFeePerGas
and maxFeePerGas
are picked up from it.The update refines the
executeStep
function's chain-switching logic, allowing users to leave the source chain once the transaction is signed. Users now only need to switch to the destination chain if a swap is required, streamlining the bridging process and improving the user experience.