UNPKG

@lifi/sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

52 lines 1.8 kB
import { http, createClient, fallback, publicActions, rpcSchema, walletActions, } from '@bigmi/core'; import { blockchair } from '@bigmi/core'; import { blockcypher } from '@bigmi/core'; import { mempool } from '@bigmi/core'; import { config } from '../../config.js'; import { getRpcUrls } from '../rpc.js'; // cached providers const publicClients = {}; /** * Get an instance of a provider for a specific chain * @param chainId - Id of the chain the provider is for * @returns The public client for the given chain */ export const getUTXOPublicClient = async (chainId) => { if (!publicClients[chainId]) { const urls = await getRpcUrls(chainId); const fallbackTransports = urls.map((url) => http(url, { fetchOptions: { method: 'POST', }, })); const _chain = await config.getChainById(chainId); const chain = { ..._chain, ..._chain.metamask, name: _chain.metamask.chainName, rpcUrls: { default: { http: _chain.metamask.rpcUrls }, public: { http: _chain.metamask.rpcUrls }, }, }; const client = createClient({ chain, rpcSchema: rpcSchema(), transport: fallback([ blockchair(), blockcypher(), mempool(), ...fallbackTransports, ]), pollingInterval: 10_000, }) .extend(publicActions) .extend(walletActions); publicClients[chainId] = client; } if (!publicClients[chainId]) { throw new Error(`Unable to configure provider for chain ${chainId}`); } return publicClients[chainId]; }; //# sourceMappingURL=getUTXOPublicClient.js.map