@lifi/widget
Version:
LI.FI Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.
43 lines • 1.8 kB
JavaScript
import { lifiExplorerUrl } from '../config/constants.js';
import { useAvailableChains } from '../hooks/useAvailableChains.js';
import { useWidgetConfig } from '../providers/WidgetProvider/WidgetProvider.js';
const sanitiseBaseUrl = (baseUrl) => baseUrl.trim().replace(/\/+$/, '');
export const useExplorer = () => {
const { explorerUrls } = useWidgetConfig();
const { getChainById } = useAvailableChains();
const getBaseUrl = (chain) => {
const baseUrl = explorerUrls?.[chain.id]
? explorerUrls[chain.id][0]
: chain.metamask.blockExplorerUrls[0];
return sanitiseBaseUrl(baseUrl);
};
const resolveChain = (chain) => Number.isFinite(chain) ? getChainById(chain) : chain;
const getTransactionLink = ({ txHash, txLink, chain, }) => {
if (!txHash && txLink) {
return txLink;
}
if (!chain) {
const baseUrl = explorerUrls?.internal?.[0]
? sanitiseBaseUrl(explorerUrls?.internal[0])
: lifiExplorerUrl;
return `${baseUrl}/tx/${txHash}`;
}
const resolvedChain = resolveChain(chain);
return `${resolvedChain ? getBaseUrl(resolvedChain) : lifiExplorerUrl}/tx/${txHash}`;
};
const getAddressLink = (address, chain) => {
if (!chain) {
const baseUrl = explorerUrls?.internal?.[0]
? sanitiseBaseUrl(explorerUrls?.internal[0])
: lifiExplorerUrl;
return `${baseUrl}/address/${address}`;
}
const resolvedChain = resolveChain(chain);
return `${resolvedChain ? getBaseUrl(resolvedChain) : lifiExplorerUrl}/address/${address}`;
};
return {
getTransactionLink,
getAddressLink,
};
};
//# sourceMappingURL=useExplorer.js.map