// ... prepare a transfer ...
// define the switchChainHook
const switchChainHook = (requiredChainId: number) => {
// this is where MetaMask lives
const ethereum = (window as any).ethereum
// check if MetaMask is available
if (typeof ethereum === 'undefined') return
// use the MetaMask RPC API to automatically switch chains
method: 'wallet_switchEthereumChain',
params: [{ chainId: requiredChainId }],
// build a new provider for the new chain
const newProvider = new ethers.providers.Web3Provider(window.ethereum)
// return the associated Signer
return newProvider.getSigner()
const route = await LiFi.executeRoute(signer, chosenRoute, {...switchChainHook})