Skip to content

Commit 79189b4

Browse files
authored
Enable explorer link on custom RPC (MystenLabs#7617)
1 parent b4ea500 commit 79189b4

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

apps/wallet/src/ui/app/components/explorer-link/Explorer.ts

+26-9
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,47 @@ const API_ENV_TO_EXPLORER_ENV: Record<API_ENV, string | undefined> = {
1212
[API_ENV.customRPC]: '',
1313
};
1414

15-
function getExplorerUrl(path: string, apiEnv: API_ENV = DEFAULT_API_ENV) {
15+
//TODO - this is a temporary solution, we should have a better way to get the explorer url
16+
function getExplorerUrl(
17+
path: string,
18+
apiEnv: API_ENV = DEFAULT_API_ENV,
19+
customRPC: string
20+
) {
1621
const base =
1722
apiEnv === API_ENV.local
1823
? 'http://localhost:3000/'
1924
: 'https://explorer.sui.io/';
2025

21-
return new URL(`${path}/?network=${API_ENV_TO_EXPLORER_ENV[apiEnv]}`, base)
22-
.href;
26+
const explorerEnv =
27+
apiEnv === 'customRPC' ? customRPC : API_ENV_TO_EXPLORER_ENV[apiEnv];
28+
29+
return new URL(`${path}/?network=${explorerEnv}`, base).href;
2330
}
2431

25-
export function getObjectUrl(objectID: ObjectId, apiEnv: API_ENV) {
26-
return getExplorerUrl(`/object/${objectID}`, apiEnv);
32+
export function getObjectUrl(
33+
objectID: ObjectId,
34+
apiEnv: API_ENV,
35+
customRPC: string
36+
) {
37+
return getExplorerUrl(`/object/${objectID}`, apiEnv, customRPC);
2738
}
2839

2940
export function getTransactionUrl(
3041
txDigest: TransactionDigest,
31-
apiEnv: API_ENV
42+
apiEnv: API_ENV,
43+
customRPC: string
3244
) {
3345
return getExplorerUrl(
3446
`/transaction/${encodeURIComponent(txDigest)}`,
35-
apiEnv
47+
apiEnv,
48+
customRPC
3649
);
3750
}
3851

39-
export function getAddressUrl(address: SuiAddress, apiEnv: API_ENV) {
40-
return getExplorerUrl(`/address/${address}`, apiEnv);
52+
export function getAddressUrl(
53+
address: SuiAddress,
54+
apiEnv: API_ENV,
55+
customRPC: string
56+
) {
57+
return getExplorerUrl(`/address/${address}`, apiEnv, customRPC);
4158
}

apps/wallet/src/ui/app/components/explorer-link/index.tsx

+16-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { memo, useCallback, useMemo } from 'react';
55

66
import { getObjectUrl, getAddressUrl, getTransactionUrl } from './Explorer';
77
import { ExplorerLinkType } from './ExplorerLinkType';
8-
import { API_ENV } from '_app/ApiProvider';
98
import ExternalLink from '_components/external-link';
109
import Icon, { SuiIcons } from '_components/icon';
1110
import { useAppSelector } from '_hooks';
@@ -48,34 +47,40 @@ function useAddress(props: ExplorerLinkProps) {
4847
function ExplorerLink(props: ExplorerLinkProps) {
4948
const { type, children, className, title, showIcon = true } = props;
5049
const address = useAddress(props);
51-
const selectedApiEnv = useAppSelector(({ app }) => app.apiEnv);
52-
const notCustomRPC = selectedApiEnv !== API_ENV.customRPC;
50+
const [selectedApiEnv, customRPC] = useAppSelector(({ app }) => [
51+
app.apiEnv,
52+
app.customRPC,
53+
]);
5354

5455
const objectID = type === ExplorerLinkType.object ? props.objectID : null;
5556
const transactionID =
5657
type === ExplorerLinkType.transaction ? props.transactionID : null;
58+
59+
// fallback to localhost if customRPC is not set
60+
const customRPCUrl = customRPC || 'http://localhost:3000/';
5761
const explorerHref = useMemo(() => {
5862
switch (type) {
5963
case ExplorerLinkType.address:
6064
return (
6165
address &&
62-
notCustomRPC &&
63-
getAddressUrl(address, selectedApiEnv)
66+
getAddressUrl(address, selectedApiEnv, customRPCUrl)
6467
);
6568
case ExplorerLinkType.object:
6669
return (
6770
objectID &&
68-
notCustomRPC &&
69-
getObjectUrl(objectID, selectedApiEnv)
71+
getObjectUrl(objectID, selectedApiEnv, customRPCUrl)
7072
);
7173
case ExplorerLinkType.transaction:
7274
return (
7375
transactionID &&
74-
notCustomRPC &&
75-
getTransactionUrl(transactionID, selectedApiEnv)
76+
getTransactionUrl(
77+
transactionID,
78+
selectedApiEnv,
79+
customRPCUrl
80+
)
7681
);
7782
}
78-
}, [type, address, notCustomRPC, selectedApiEnv, objectID, transactionID]);
83+
}, [type, address, selectedApiEnv, objectID, transactionID, customRPCUrl]);
7984

8085
const handleclick = useCallback(() => {
8186
if (props.track) {
@@ -86,9 +91,7 @@ function ExplorerLink(props: ExplorerLinkProps) {
8691
if (!explorerHref) {
8792
return null;
8893
}
89-
if (selectedApiEnv === API_ENV.customRPC) {
90-
return null;
91-
}
94+
9295
return (
9396
<ExternalLink
9497
href={explorerHref}

0 commit comments

Comments
 (0)