LI.FI Widget provides a useWidgetEvents hook that lets you subscribe to a series of widget events and helps you retrieve helpful information about executing routes, track bridge and swap progress, track selection of chains and tokens, interactions with specific UI elements, and more. We continue working on extending available events and if you are interested in a specific event, reach out via our support. To minimize unnecessary re-renders and prevent potential glitches in the main Widget component, please integrate the useWidgetEvents hook outside of the component where the main LiFiWidget is integrated. Example of how to subscribe to widget events:
import type { Route } from '@lifi/sdk';
import type { RouteExecutionUpdate } from '@lifi/widget';
import { useWidgetEvents, WidgetEvent } from '@lifi/widget';
import { useEffect } from 'react';

export const WidgetEventsExample = () => {
  const widgetEvents = useWidgetEvents();

  // ...

  useEffect(() => {
    const onRouteExecutionStarted = (route: Route) => {
      // console.log('onRouteExecutionStarted fired.');
    };
    const onRouteExecutionUpdated = (update: RouteExecutionUpdate) => {
      // console.log('onRouteExecutionUpdated fired.');
    };
    const onRouteExecutionCompleted = (route: Route) => {
      // console.log('onRouteExecutionCompleted fired.');
    };
    const onRouteExecutionFailed = (update: RouteExecutionUpdate) => {
      // console.log('onRouteExecutionFailed fired.');
    };
    const onRouteHighValueLoss = (update: RouteHighValueLossUpdate) => {
      // console.log('onRouteHighValueLoss continued.');
    };
    
    widgetEvents.on(WidgetEvent.RouteExecutionStarted, onRouteExecutionStarted);
    widgetEvents.on(WidgetEvent.RouteExecutionUpdated, onRouteExecutionUpdated);
    widgetEvents.on(WidgetEvent.RouteExecutionCompleted, onRouteExecutionCompleted);
    widgetEvents.on(WidgetEvent.RouteExecutionFailed, onRouteExecutionFailed);
    widgetEvents.on(WidgetEvent.RouteHighValueLoss, onRouteHighValueLoss);
    
    return () => widgetEvents.all.clear();
  }, [widgetEvents]);

  // ...
  
  // Return null because it's an example
  return null;
};

List of events

Here is the list of all available events: AvailableRoutes
  • Type: Route[]
The event fires when available routes are returned after the user has selected source and destination tokens, entered an amount, and requested the routes. RouteSelected
  • Type: RouteSelected
The event fires when the user selects a specific route from the list of available routes. RouteExecutionStarted
  • Type: Route
The event fires when the user clicks on the Start swapping or Start bridging button. RouteExecutionUpdated
  • Type: RouteExecutionUpdate
The event fires when there is an update to the Route object during execution. RouteExecutionCompleted
  • Type: Route
The event fires when the execution is completed successfully. RouteExecutionFailed
  • Type: RouteExecutionUpdate
The event fires when the execution has failed. RouteHighValueLoss
  • Type: RouteHighValueLossUpdate
The event fires when the High Value Loss bottom sheet appears on the screen. ContactSupport
  • Type: ContactSupport
The event fires when the user clicks on the Contact support button on the Transaction Details page. SourceChainTokenSelected
  • Type: ChainTokenSelected
The event fires when the user selects the source chain and token. DestinationChainTokenSelected
  • Type: ChainTokenSelected
The event fires when the user selects the destination chain and token. SendToWalletToggled
  • Type: boolean
The event fires when the user clicks on the wallet icon next to the action button on the main page to show/hide the destination wallet selection UI. WalletConnected
  • Type: WalletConnected
The event fires when the user connects the wallet via the internal wallet management UI. WidgetExpanded
  • Type: boolean
The event fires when the side panel with routes is shown to the user. Only available in the wide widget variant. PageEntered
  • Type: NavigationRouteType
The event fires when the user navigates to a page in the widget. FormFieldChanged
  • Type: FormFieldChanged
The event fires whenever a form value is changed in the widget. SettingUpdated
  • Type: SettingUpdated
The event fires whenever a setting is updated in the widget. TokenSearch
  • Type: TokenSearch
The event fires when the user searches for a token (includes the query value and the matched token results). LowAddressActivityConfirmed
  • Type: LowAddressActivityConfirmed
The event fires when the user confirms proceeding despite a low address activity warning for the specified address and chain. ChainPinned
  • Type: ChainPinned
The event fires when the user pins or unpins a chain in the UI. Routes: Some of the events here present information about routes. A route is the LI.FI way of presenting a quote on an exchange/transfer. A route is a collection of steps, transactions and costs associated with that transfer. In the Widget we present a set of routes that the user can select from. Once selected the execution of that route can begin and the user will be guided through the steps required to complete that route. The route events above can help track a route status.

Widget Events types

Properties and types of the useWidgetEvents hook.
enum WidgetEvent {
  AvailableRoutes = 'availableRoutes',
  ChainPinned = 'chainPinned',
  ContactSupport = 'contactSupport',
  DestinationChainTokenSelected = 'destinationChainTokenSelected',
  FormFieldChanged = 'formFieldChanged',
  LowAddressActivityConfirmed = 'lowAddressActivityConfirmed',
  PageEntered = 'pageEntered',
  RouteExecutionCompleted = 'routeExecutionCompleted',
  RouteExecutionFailed = 'routeExecutionFailed',
  RouteExecutionStarted = 'routeExecutionStarted',
  RouteExecutionUpdated = 'routeExecutionUpdated',
  RouteHighValueLoss = 'routeHighValueLoss',
  RouteSelected = 'routeSelected',
  SendToWalletToggled = 'sendToWalletToggled',
  SettingUpdated = 'settingUpdated',
  SourceChainTokenSelected = 'sourceChainTokenSelected',
  TokenSearch = 'tokenSearch',
  WidgetExpanded = 'widgetExpanded',
}

type WidgetEvents = {
  availableRoutes: Route[]
  chainPinned: ChainPinned
  contactSupport: ContactSupport
  destinationChainTokenSelected: ChainTokenSelected
  formFieldChanged: FormFieldChanged
  lowAddressActivityConfirmed: LowAddressActivityConfirmed
  pageEntered: NavigationRouteType
  routeExecutionCompleted: Route
  routeExecutionFailed: RouteExecutionUpdate
  routeExecutionStarted: Route
  routeExecutionUpdated: RouteExecutionUpdate
  routeHighValueLoss: RouteHighValueLossUpdate
  routeSelected: RouteSelected
  sendToWalletToggled: boolean
  settingUpdated: SettingUpdated
  sourceChainTokenSelected: ChainTokenSelected
  tokenSearch: TokenSearch
  walletConnected: WalletConnected
  widgetExpanded: boolean
}

type ContactSupport = {
  supportId?: string
}

type RouteHighValueLossUpdate = {
  fromAmountUSD: number
  toAmountUSD: number
  gasCostUSD?: number
  feeCostUSD?: number
  valueLoss: number
}

type RouteExecutionUpdate = {
  route: Route
  process: Process
}

type RouteSelected = {
  route: Route
  routes: Route[]
}

type TokenSearch = {
  value: string
  tokens: TokenAmount[]
}

type ChainTokenSelected = {
  chainId: ChainId
  tokenAddress: string
}

type WalletConnected = {
  address?: string
  chainId?: number
  chainType?: ChainType
}

type FormFieldChanged = {
  [K in keyof DefaultValues]: {
    fieldName: K
    newValue: DefaultValues[K]
    oldValue: DefaultValues[K]
  }
}[keyof DefaultValues]

type SettingUpdated<
  K extends keyof SettingsProps = keyof SettingsProps,
> = {
  setting: K
  newValue: SettingsProps[K]
  oldValue: SettingsProps[K]
  newSettings: SettingsProps
  oldSettings: SettingsProps
}

type ChainPinned = {
  chainId: number
  pinned: boolean
}

type LowAddressActivityConfirmed = {
  address: string
  chainId: number
}