Skip to main content

Utility Functions

The SDK provides utility functions for common operations:

chainToViemChain(chain)

Convert a Metalayer Chain object to Viem chain format. Use the result as your wagmi chains list and pass the same viem Chain values into @metalayer/widget helpers such as createWidgetTransport, createWidgetClient, and createWidgetTransportsRecord (see Widget Components).
import { chainToViemChain } from '@metalayer/sdk';

const viemChain = chainToViemChain(chain);

chainsToViemChains(chains)

Convert an array of Metalayer Chain objects to an array of Viem chains. Invalid or non-EVM chains are filtered out. The returned array is suitable for wagmi and for the widget transport helpers linked above.
import { chainsToViemChains } from '@metalayer/sdk';

const viemChains = chainsToViemChains(chains);

collectChainHttpRpcUrls(chain)

Collects unique HTTP RPC URLs from a viem Chain produced by chainToViemChain, in Metalayer order: URLs under rpcUrls.default, then alternate slots rpc-0, rpc-1, … (sorted by index), which map to alternativeRpcs from the Metalayer chain. Used internally by createWidgetTransport in @metalayer/widget; you can use it directly when building custom viem fallback() stacks or other transports.
import { collectChainHttpRpcUrls } from '@metalayer/sdk';

const urls = collectChainHttpRpcUrls(viemChain);

formatQuoteProvider(provider)

Format a QuoteProvider enum value to a readable string.
import { formatQuoteProvider, QuoteProvider } from '@metalayer/sdk';

const name = formatQuoteProvider(QuoteProvider.ACROSS); // "Across"