The packages do not depend on any wallet library. The examples expect this interface:
export type WalletTransactionRequest = { readonly chainId: number; readonly from: Hex; readonly to: Hex; readonly data: Hex; readonly value: bigint; readonly gas?: bigint; // set only when the plan names a gas limit};export type WalletSigner = { readonly address: Hex; /** Broadcasts on `request.chainId` and resolves with the hash, before mining. */ readonly sendTransaction: (request: WalletTransactionRequest) => Promise<Hex>;};
Here is the signer. This guide shows the viem version, and the adapter files hold the WalletConnect, ethers and web3.js versions. On the docs site, the menu above the code picks one:
// Open the modal with `appKit.open()`. Once the user connects, wagmi knows the account.const { address } = getAccount(config);if (address === undefined) throw new Error('connect a wallet in the AppKit modal first');const signer: WalletSigner = { address, sendTransaction: async (request) => { // The wallet may sit on another chain. Ask it to switch before it signs. if (getAccount(config).chainId !== request.chainId) await switchChain(config, { chainId: request.chainId }); return sendTransaction(config, { chainId: request.chainId, to: request.to, data: request.data, value: request.value, ...(request.gas === undefined ? {} : { gas: request.gas }), }); },};
const signer: WalletSigner = { address, sendTransaction: async (request) => { const chainSigner = signerByChainId.get(request.chainId); if (chainSigner === undefined) throw new Error(`no ethers signer for chain ${request.chainId}`); // ethers resolves once the wallet broadcast the transaction, before it is mined. const response = await chainSigner.sendTransaction({ chainId: request.chainId, to: request.to, data: request.data, value: request.value, ...(request.gas === undefined ? {} : { gasLimit: request.gas }), }); return toTransactionHash(response.hash); },};
const signer: WalletSigner = { address, sendTransaction: (request) => new Promise((resolve, reject) => { const web3 = web3ByChainId.get(request.chainId); if (web3 === undefined) return reject(new Error(`no web3 for chain ${request.chainId}`)); // `sendTransaction` returns a PromiEvent. Awaiting it gives the mined // receipt. The `transactionHash` event fires first, and the signer resolves with it. web3.eth .sendTransaction({ from: request.from, to: request.to, data: request.data, value: request.value, ...(request.gas === undefined ? {} : { gas: request.gas }), }) .on('transactionHash', (hash) => resolve(toTransactionHash(String(hash)))) .on('error', reject); }),};
The examples wait for receipts themselves, and only where the plan says waitForReceipt.
#Step 1: one viem client per chain, from your own RPC endpoints
// The search and the planner read through viem clients. Build them as in the viem example.const clientForChain = createClientLookup(rpcUrlByChainId);const requireClient = requireClientWith(clientForChain);// Your own reads, receipts and status checks go through ethers.const ethersProviderFor = (chainId: number, urls: readonly string[]): Provider => { const network = Network.from(chainId); const providers = urls.map((url) => new JsonRpcProvider(url, network, { staticNetwork: network })); const [only] = providers; if (only !== undefined && providers.length === 1) return only; // A lower priority number is asked first. With a quorum of one, the first answer wins. const configs = providers.map((provider, index) => ({ provider, priority: index + 1, weight: 1 })); return new FallbackProvider(configs, network, { quorum: 1 });};const providerByChainId = new Map( [...rpcUrlByChainId].map(([chainId, urls]) => [chainId, ethersProviderFor(chainId, endpointList(urls))]),);const requireProvider = (chainId: number): Provider => { const provider = providerByChainId.get(chainId); if (provider === undefined) throw new Error(`the wallet has no RPC endpoint for chain ${chainId}`); return provider;};
// The search and the planner read through viem clients. Build them as in the viem example.const clientForChain = createClientLookup(rpcUrlByChainId);const requireClient = requireClientWith(clientForChain);// Your own reads, receipts and status checks go through web3.js. It has no// fallback provider, so keep one Web3 per endpoint and try them in order.const web3ListByChainId = new Map( [...rpcUrlByChainId].map(([chainId, urls]) => [chainId, endpointList(urls).map((url) => new Web3(url))]),);const readWithFallback: Web3Reader = async (chainId, read) => { let lastError: unknown = new Error(`the wallet has no RPC endpoint for chain ${chainId}`); for (const web3 of web3ListByChainId.get(chainId) ?? []) { try { return await read(web3); } catch (error) { lastError = error; } } throw lastError;};
Give each chain one endpoint or as many as you like, most reliable first. createClientLookup builds one createPublicClient per chain. A chain with one endpoint uses http(url). A chain with several uses fallback([http(first), http(second), …]), so the client moves to the next endpoint when one fails. The Gibs interface does the same with the endpoints a reader enters in its settings. The lookup answers null for a chain that you have no endpoint for.
viem's fallback moves on only after an HTTP error or an answer it cannot read. An endpoint that answers 200 with an empty body counts as a success, so put the endpoint you trust most first.
The search and the planner always read through these viem clients. With ethers, FallbackProvider shares several endpoints for your own reads. web3.js 4 has no fallback provider, so its adapter tries each endpoint in order.
search.routes holds the routes the search may serve, one per provider, best first. The best is search.routes[0]. When the list is empty, search.priceImpactRefusal says whether price impact is the reason, and search.refusals lists every refusal.
A served route has candidate (what you plan), plan (the engine's hops and signature groups), route (display data) and routeSignatureCount. For ETH to PLS, the usual best route is a crossing into PulseChain and then a PulseX swap: two sections.
#Step 4: build ONE section's inputs, from what it spends now
The example plans and sends one section at a time. The first hop of a section names its carrier. The example builds one SectionExecutionInputs for it:
Omnibridge ('pulsechain' or 'tokensex'): read the token mapping with tokenBridgeInfo, then state the delivery choice. When the crossing enters PulseChain, the bridge executes it itself. So { shouldDeliver: false } is the only accepted choice, and the plan refuses { shouldDeliver: true, ... } with delivery-not-offered.
Any aggregator: the live quote, asked now for this wallet, this recipient and this amount.
const live = await fetchLiveSectionQuote({ plan: served.plan, sectionIndex, resolveToken: search.resolveToken, amountIn, account: signer.address, recipient, clientForChain, // reads the token mapping of a section that fuses with an omnibridge entry fetch: (input, init) => globalThis.fetch(input, init), keys: { lifiApiKey, relayApiKey }, signal,});if (live.kind !== 'quoted') throw new Error(`no live quote (${live.kind})`);const inputs = { amountIn, recipient, aggregator: { quote: live.quote, request: live.request } };
The first section spends amountWei. Each later section spends what actually arrived from the section before it. The example reads the next section's token balance on its chain before the section runs, and again after the crossing is delivered. The difference is what arrived.
Show plan.deliveries and the fees before the user signs. For the usual route, the two sections plan to:
Section
Order
Chain
Kind
Waits for receipt
0
1
Ethereum
bridge (wrapAndRelayTokens, value = the ether)
no (last of its plan)
1
1
PulseChain
approve (wrapped ether from Ethereum, to the PulseX router, exact amount)
yes
1
2
PulseChain
swap
no
Native ether needs no approval.
#Step 6: send, then wait for arrival before the next section
sendPlannedTransactions (in shared.ts) sends each transaction of the section in order. It broadcasts through your signer, waits for the receipt where waitForReceipt is true, and throws on a revert. The example then waits for the last receipt of the section. The receipt wait comes from chainAccess:
const waitForReceipt = async (request: { chainId: number; hash: Hex }): Promise<ReceiptOutcome> => { const receipt = await requireProvider(request.chainId).waitForTransaction(request.hash); if (receipt === null) throw new Error(`no receipt for ${request.hash}`); // ethers reports 1 for success and 0 for a revert. return receipt.status === 1 ? 'success' : 'reverted';};
// web3.js 4 has no call that waits for a receipt, so poll for it.const waitForReceipt = async (request: { chainId: number; hash: Hex }): Promise<ReceiptOutcome> => { for (;;) { const receipt = await readWithFallback(request.chainId, (web3) => web3.eth.getTransactionReceipt(request.hash)); // web3.js answers null while the transaction is not mined, although its type does not say so. if (receipt !== null && receipt !== undefined) return BigInt(receipt.status) === 1n ? 'success' : 'reverted'; await sleep(4_000); }};
A receipt is not arrival. It only says that the crossing started. So before the example plans the next section, it polls fetchTransferStatus on the crossing transaction until the state is delivered. Then it measures what arrived.
originClient makes two reads on the origin chain: a receipt and the finalized block. A viem client has both. Other libraries need a small reader:
Library
// A viem public client already has the two reads that `fetchTransferStatus` makes.const originReaderFor = (chainId: number): OriginChainReader => requireClient(chainId);
AppKit reads the chain through wagmi and viem, so this is the viem code.
// A viem public client already has the two reads that `fetchTransferStatus` makes.const originReaderFor = (chainId: number): OriginChainReader => requireClient(chainId);
// `fetchTransferStatus` makes two reads on the origin chain. ethers answers block numbers as plain numbers.const originReaderFor = (chainId: number): OriginChainReader => { const provider = requireProvider(chainId); return { getTransactionReceipt: async ({ hash }) => { const receipt = await provider.getTransactionReceipt(hash); // A throw tells `fetchTransferStatus` that the transaction is not mined yet. if (receipt === null) throw new Error(`no receipt for ${hash} yet`); return { blockNumber: BigInt(receipt.blockNumber) }; }, getBlock: async ({ blockTag }) => { const block = await provider.getBlock(blockTag); return { number: block === null ? null : BigInt(block.number) }; }, };};
// `fetchTransferStatus` makes two reads on the origin chain.const originReaderFor = (chainId: number): OriginChainReader => ({ getTransactionReceipt: async ({ hash }) => { const receipt = await readWithFallback(chainId, (web3) => web3.eth.getTransactionReceipt(hash)); // A throw tells `fetchTransferStatus` that the transaction is not mined yet. if (receipt === null || receipt === undefined) throw new Error(`no receipt for ${hash} yet`); return { blockNumber: BigInt(receipt.blockNumber) }; }, getBlock: async ({ blockTag }) => { const block = await readWithFallback(chainId, (web3) => web3.eth.getBlock(blockTag)); return { number: block.number === undefined ? null : BigInt(block.number) }; },});
waitForTransferStatus throws TransferEndedError on failed or stranded. If the route started with an aggregator, the example adds aggregator to the status options: { provider: 'lifi' }, { provider: 'relay', requestId: quote.quoteId, apiKey } or { provider: 'near-intents', depositAddress: quote.quoteId }. A route that ends with a swap on PulseChain is done when the receipt of that swap succeeds.