-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuseChainAndScan.ts
49 lines (41 loc) · 1.24 KB
/
useChainAndScan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { useEffect, useState } from "react";
import {
UniqueChain,
UniqueChainInstance,
UniqueIndexer,
UniqueIndexerInstance
} from "@unique-nft/sdk";
type ChainAndScan = {
chain: UniqueChainInstance | null;
scan: UniqueIndexerInstance | null;
};
/**
* Custom hook to initialize and access `UniqueChain` and `UniqueScan` instances for a specified network.
**/
export const useChainAndScan = () => {
const [data, setData] = useState<ChainAndScan>({ chain: null, scan: null });
const [loading, setLoading] = useState(true);
const chainUrl =
process.env.REACT_APP_REST_URL || "https://rest.unique.network/v2/unique";
const scanUrl =
process.env.REACT_APP_SCAN_URL || "https://api-unique.uniquescan.io/v2";
useEffect(() => {
const getChainAndScan = async () => {
try {
const uniqueChain = UniqueChain({
baseUrl: chainUrl,
});
const uniqueScan = UniqueIndexer({
baseUrl: scanUrl,
});
setData({ chain: uniqueChain, scan: uniqueScan });
} catch (error) {
console.error("Error fetching chain and scan:", error);
} finally {
setLoading(false);
}
};
getChainAndScan();
}, [chainUrl, scanUrl]);
return { ...data, loading };
};