UNPKG

@openocean.finance/widget-sdk

Version:

OpenOcean Any-to-Any Cross-Chain-Swap SDK

56 lines 2.05 kB
import { UTXOAPIActions, UTXOActions, utxo } from '@bigmi/core'; import { http, createClient, fallback, rpcSchema, } from 'viem'; 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)); 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([ utxo('https://api.blockchair.com', { key: 'blockchair', includeChainToURL: true, }), utxo('https://api.blockcypher.com/v1/btc/main', { key: 'blockcypher', }), utxo('https://mempool.space/api', { key: 'mempool', }), utxo('https://rpc.ankr.com/http/btc_blockbook/api/v2', { key: 'ankr', }), ...fallbackTransports, ]), pollingInterval: 10_000, }) .extend(UTXOActions) .extend(UTXOAPIActions); publicClients[chainId] = client; } if (!publicClients[chainId]) { throw new Error(`Unable to configure provider for chain ${chainId}`); } return publicClients[chainId]; }; //# sourceMappingURL=getUTXOPublicClient.js.map