forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseContracts.ts
More file actions
45 lines (37 loc) · 1.21 KB
/
Copy pathuseContracts.ts
File metadata and controls
45 lines (37 loc) · 1.21 KB
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
import { useQuery } from "@tanstack/react-query";
import { Client } from "@stellar/stellar-sdk/contract";
import {
loadContractMetadata,
ContractMetadata,
} from "../util/loadContractMetada";
const contractModules = import.meta.glob("../../contracts/*.ts");
type ContractModule = {
default: Client;
metadata?: ContractMetadata;
};
type ContractMap = Record<string, ContractModule>;
const loadContracts = async () => {
const loadedContracts: ContractMap = {};
const failed: Record<string, string> = {};
for (const [path, importFn] of Object.entries(contractModules)) {
const filename = path.split("/").pop()?.replace(".ts", "") || "";
if (filename && filename === "util") continue;
try {
const module = (await importFn()) as ContractModule;
const metadata = await loadContractMetadata(
module.default.options.contractId,
);
loadedContracts[filename] = { ...module, metadata };
} catch (error) {
failed[filename] = error instanceof Error ? error.message : String(error);
}
}
return { loadedContracts, failed };
};
export function useContracts() {
return useQuery({
queryKey: ["contracts"],
queryFn: loadContracts,
staleTime: Infinity,
});
}