@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
56 lines (55 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPublicClient = void 0;
const types_1 = require("@lifi/types");
const viem_1 = require("viem");
const chains_1 = require("viem/chains");
const config_1 = require("../../config");
const rpc_1 = require("../rpc");
// 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
*/
const getPublicClient = async (chainId) => {
if (publicClients[chainId]) {
return publicClients[chainId];
}
const urls = await (0, rpc_1.getRpcUrls)(chainId);
const fallbackTransports = urls.map((url) => url.startsWith('wss')
? (0, viem_1.webSocket)(url)
: (0, viem_1.http)(url, {
batch: {
batchSize: 64,
},
}));
const _chain = await config_1.config.getChainById(chainId);
const chain = {
..._chain,
..._chain.metamask,
name: _chain.metamask.chainName,
rpcUrls: {
default: { http: _chain.metamask.rpcUrls },
public: { http: _chain.metamask.rpcUrls },
},
};
// Add ENS contracts
if (chain.id === types_1.ChainId.ETH) {
chain.contracts = {
...chains_1.mainnet.contracts,
...chain.contracts,
};
}
const provider = config_1.config.getProvider(types_1.ChainType.EVM);
publicClients[chainId] = (0, viem_1.createClient)({
chain: chain,
transport: (0, viem_1.fallback)(fallbackTransports, provider?.options?.fallbackTransportConfig),
batch: {
multicall: true,
},
});
return publicClients[chainId];
};
exports.getPublicClient = getPublicClient;