-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move explorer link generation logic to core
- Loading branch information
Showing
20 changed files
with
154 additions
and
82 deletions.
There are no files selected for viewing
File renamed without changes.
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,4 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './ExplorerLinkType'; |
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
File renamed without changes.
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,58 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// Modifications Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getCustomNetwork } from '.'; | ||
import { getNetwork, Network, NetworkId } from '@iota/iota-sdk/client'; | ||
import { getAddressUrl, getObjectUrl, getTransactionUrl, getValidatorUrl } from '.'; | ||
import { ExplorerLinkType } from '../enums'; | ||
|
||
export type ExplorerLinkConfig = | ||
| { | ||
type: ExplorerLinkType.Address; | ||
address?: string; | ||
useActiveAddress?: false; | ||
} | ||
| { | ||
type: ExplorerLinkType.Address; | ||
useActiveAddress: true; | ||
} | ||
| { type: ExplorerLinkType.Object; objectID: string; moduleName?: string } | ||
| { type: ExplorerLinkType.Transaction; transactionID: string } | ||
| { type: ExplorerLinkType.Validator; validator: string }; | ||
|
||
function getAddress(linkConfig: ExplorerLinkConfig, activeAddress: string | null) { | ||
const { type } = linkConfig; | ||
const isAddress = type === ExplorerLinkType.Address; | ||
const isProvidedAddress = isAddress && !linkConfig.useActiveAddress; | ||
return isProvidedAddress ? linkConfig.address : activeAddress; | ||
} | ||
|
||
export function getExplorerLink( | ||
linkConfig: ExplorerLinkConfig, | ||
activeAddress: string | null, | ||
network: NetworkId, | ||
) { | ||
const { type } = linkConfig; | ||
const address = getAddress(linkConfig, activeAddress); | ||
const objectID = type === ExplorerLinkType.Object ? linkConfig.objectID : null; | ||
const transactionID = type === ExplorerLinkType.Transaction ? linkConfig.transactionID : null; | ||
const validator = type === ExplorerLinkType.Validator ? linkConfig.validator : null; | ||
const moduleName = type === ExplorerLinkType.Object ? linkConfig.moduleName : null; | ||
|
||
// fallback to localhost if customRPC is not set | ||
const customExplorer = | ||
network === Network.Custom ? getCustomNetwork().explorer : getNetwork(network).explorer; | ||
|
||
if (!address) return null; | ||
switch (type) { | ||
case ExplorerLinkType.Address: | ||
return address && getAddressUrl(address, network, customExplorer); | ||
case ExplorerLinkType.Object: | ||
return objectID && getObjectUrl(objectID, network, customExplorer, moduleName); | ||
case ExplorerLinkType.Transaction: | ||
return transactionID && getTransactionUrl(transactionID, network, customExplorer); | ||
case ExplorerLinkType.Validator: | ||
return validator && getValidatorUrl(validator, network, customExplorer); | ||
} | ||
} |
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,47 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// Modifications Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getNetwork, Network, NetworkId } from '@iota/iota-sdk/client'; | ||
|
||
function getExplorerUrl( | ||
path: string, | ||
network: NetworkId, | ||
customExplorer: string, | ||
getUrlWithDeviceId: (url: URL) => URL = (url) => url, | ||
) { | ||
const networkConfig = getNetwork(network); | ||
const explorer = network === Network.Custom ? customExplorer : networkConfig?.explorer; | ||
|
||
const url = getUrlWithDeviceId(new URL(path, explorer)); | ||
if (explorer) { | ||
url.searchParams.append('network', network); | ||
} | ||
|
||
return url.href; | ||
} | ||
|
||
export function getObjectUrl( | ||
objectID: string, | ||
network: NetworkId, | ||
customExplorer: string, | ||
moduleName?: string | null, | ||
) { | ||
return getExplorerUrl( | ||
`/object/${objectID}${moduleName ? `?module=${moduleName}` : ''}`, | ||
network, | ||
customExplorer, | ||
); | ||
} | ||
|
||
export function getTransactionUrl(txDigest: string, network: NetworkId, customExplorer: string) { | ||
return getExplorerUrl(`/txblock/${encodeURIComponent(txDigest)}`, network, customExplorer); | ||
} | ||
|
||
export function getAddressUrl(address: string, network: NetworkId, customExplorer: string) { | ||
return getExplorerUrl(`/address/${address}`, network, customExplorer); | ||
} | ||
|
||
export function getValidatorUrl(address: string, network: NetworkId, customExplorer: string) { | ||
return getExplorerUrl(`/validator/${address}`, network, customExplorer); | ||
} |
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 was deleted.
Oops, something went wrong.
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,14 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getExplorerLink } from '@iota/core'; | ||
import { useCurrentAccount, useIotaClientContext } from '@iota/dapp-kit'; | ||
|
||
export function useExplorerLinkGetter(): ( | ||
linkConfig: Parameters<typeof getExplorerLink>[0], | ||
) => ReturnType<typeof getExplorerLink> { | ||
const { network } = useIotaClientContext(); | ||
const address = useCurrentAccount()?.address || null; | ||
|
||
return (linkConfig) => getExplorerLink(linkConfig, address, network); | ||
} |
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
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