UNPKG

@lifi/sdk

Version:

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

92 lines 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isDelegationDesignatorCode = exports.getDomainChainId = exports.retryCount = exports.retryDelay = exports.getMulticallAddress = exports.getMaxPriorityFeePerGas = exports.convertExtendedChain = void 0; exports.isExtendedChain = isExtendedChain; const actions_1 = require("viem/actions"); const config_js_1 = require("../../config.js"); const median_js_1 = require("../../utils/median.js"); const getActionWithFallback_js_1 = require("./getActionWithFallback.js"); const convertExtendedChain = (chain) => ({ ...chain, ...chain.metamask, blockExplorers: chain.metamask.blockExplorerUrls.reduce((blockExplorers, blockExplorer, index) => { blockExplorers[index === 0 ? 'default' : `${index}`] = { name: blockExplorer, url: blockExplorer, }; return blockExplorers; }, {}), name: chain.metamask.chainName, rpcUrls: { default: { http: chain.metamask.rpcUrls }, public: { http: chain.metamask.rpcUrls }, }, contracts: { ...(chain.multicallAddress ? { multicall3: { address: chain.multicallAddress } } : undefined), }, }); exports.convertExtendedChain = convertExtendedChain; function isExtendedChain(chain) { return (typeof chain === 'object' && chain !== null && 'key' in chain && 'chainType' in chain && 'coin' in chain && 'mainnet' in chain && 'logoURI' in chain && typeof chain.metamask === 'object' && chain.metamask !== null && typeof chain.nativeToken === 'object' && chain.nativeToken !== null); } const getMaxPriorityFeePerGas = async (client) => { const block = await (0, getActionWithFallback_js_1.getActionWithFallback)(client, actions_1.getBlock, 'getBlock', { includeTransactions: true, }); const maxPriorityFeePerGasList = block.transactions .filter((tx) => tx.maxPriorityFeePerGas) .map((tx) => tx.maxPriorityFeePerGas); if (!maxPriorityFeePerGasList.length) { return; } let maxPriorityFeePerGasSum = 0n; for (const value of maxPriorityFeePerGasList) { maxPriorityFeePerGasSum += value; } const maxPriorityFeePerGasMedian = (0, median_js_1.median)(maxPriorityFeePerGasList) ?? 0n; const maxPriorityFeePerGasAvg = maxPriorityFeePerGasSum / BigInt(maxPriorityFeePerGasList.length); return maxPriorityFeePerGasMedian > maxPriorityFeePerGasAvg ? maxPriorityFeePerGasAvg : maxPriorityFeePerGasMedian; }; exports.getMaxPriorityFeePerGas = getMaxPriorityFeePerGas; const getMulticallAddress = async (chainId) => { const chains = await config_js_1.config.getChains(); return chains.find((chain) => chain.id === chainId) ?.multicallAddress; }; exports.getMulticallAddress = getMulticallAddress; const retryDelay = ({ count }) => Math.min(~~(1 << count) * 200, 3000); exports.retryDelay = retryDelay; exports.retryCount = 30; const getDomainChainId = (domain) => { if (domain.chainId) { return Number(domain.chainId); } if (!domain.salt) { return null; } try { const saltChainId = Number(domain.salt); return !Number.isNaN(saltChainId) ? saltChainId : null; } catch { return null; } }; exports.getDomainChainId = getDomainChainId; const isDelegationDesignatorCode = (code) => code?.startsWith('0xef0100'); exports.isDelegationDesignatorCode = isDelegationDesignatorCode; //# sourceMappingURL=utils.js.map