-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow configuration of explorer links (#293)
- Loading branch information
Showing
14 changed files
with
105 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Chain } from '@lifi/sdk'; | ||
import { lifiExplorerUrl } from '../config/constants.js'; | ||
import { useAvailableChains } from '../hooks/useAvailableChains.js'; | ||
import { useWidgetConfig } from '../providers/WidgetProvider/WidgetProvider.js'; | ||
|
||
const sanitiseBaseUrl = (baseUrl: string) => baseUrl.trim().replace(/\/+$/, ''); | ||
|
||
export const useExplorer = () => { | ||
const { explorerUrls } = useWidgetConfig(); | ||
const { getChainById } = useAvailableChains(); | ||
|
||
const getBaseUrl = (chain: Chain) => { | ||
const baseUrl = explorerUrls?.[chain.id] | ||
? explorerUrls[chain.id][0] | ||
: chain.metamask.blockExplorerUrls[0]; | ||
|
||
return sanitiseBaseUrl(baseUrl); | ||
}; | ||
|
||
const resolveChain = (chain: Chain | number) => | ||
Number.isFinite(chain) ? getChainById(chain as number) : (chain as Chain); | ||
|
||
const getTransactionLink = (txHash: string, chain?: Chain | number) => { | ||
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: string, chain?: Chain | number) => { | ||
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters