4⃣
Execute a Route (Transfer)
We allow you to execute any on-chain or cross-chain swap and bridging process and a combination of both.
After fetching routes, you can execute one using executeRoute as described here.

executeRoute

1
async function executeRoute (
2
signer: Signer,
3
route: Route,
4
settings?: ExecutionSettings
5
): Promise<Route>
6
7
interface ExecutionSettings {
8
updateCallback?: CallbackFunction
9
switchChainHook?: SwitchChainHook
10
}
Copied!
Executing a route requires a signer to send transactions to involved contracts. You can read more about signers in the official ethers documentation. Once you have the route and the signer, you call executeRoute.
1
const route = await LiFi.executeRoute(signer, route)
Copied!
Note that the function will return the executed route once the execution has been completed.
In addition to the first two parameters, executeRoutetakes an optional settings object as a third parameter that can contain an updateCallback function. This function will be called every time an action has been performed on the route by the SDK. You can use this callback to keep track of the execution status. The switchChainHook is explained in more detail here.

Controlling the execution of a route

moveExecutionToBackground

1
function moveExecutionToBackground (route: Route): void
Copied!
Once a route execution has started, it can be pushed "to the background" by calling this method. Once called, the execution will continue until it reaches a point where user interaction is required (i.e. signing a transaction). If such a point is reached, the execution will halt until resumeRoute is called with the same route object. The execution will then pick up where it halted.

stopExecution

1
function stopExecution (route: Route): Route
Copied!
This method immediately stops the execution of a given route. If a transaction has already been signed and sent by the user, it will be executed on-chain.

updateExecutionSettings

1
function updateExecutionSettings (
2
settings: ExecutionSettings,
3
route: Route
4
): void
Copied!
This function updates the execution settings of a route. Please see executeRoute for the ExecutionSettingsinterface.

resumeRoute

1
async function resumeRoute (
2
signer: Signer,
3
route: Route,
4
settings?: ExecutionSettings
5
): Promise<Route>
Copied!
resumeRoute takes in the same parameters as executeRoute and will resume either a route that has been stopped or a route that has been moved to the background.
​

Example code snippet

Building on the code from Broken linkwe can now choose a route and execute it.
1
// getting routes
2
const routeOptions =Β { ... }
3
const routesRequest = { ... }
4
​
5
const result = await Lifi.getRoutes(routesRequest)
6
const routes = result.routes
7
const chosenRoute = routes[0]
8
​
9
const updateCallback = (updatedRoute: Route) => {
10
console.log('Ping! Everytime a status update is made!')
11
}
12
​
13
// executing a route
14
const route = await LiFi.executeRoute(signer, chosenRoute, {...updateCallback})
15
​
Copied!