๐ src/web3/vue/composables/useChain.ts
D-OPEN SOVEREIGN
Documentation & Insights
Chain display info + read client + route helper (spec ยง13.3). Replaces
`BrandData[brand]` (display), `readOnlyAdapters.evm[chainId]` (contract
client), and `chainId2Tech()` embedded in route `:to` bindings.1import { computed, type ComputedRef, ref, unref, type MaybeRef, type Ref } from 'vue';
2import { getChain, type EvmChainConfig } from '../../network/chains';
3import { getReadClient } from '../../client/registry';
4import type { ReadOnlyDomainAPI } from '../../client/types';
5
6export interface UseChainReturn {
7 chain: EvmChainConfig;
8 client: ReadOnlyDomainAPI;
9
10 usdPrice: Ref<number | null>;
11 isPriceLoading: Ref<boolean>;
12 refreshPrice(): Promise<void>;
13
14 isMainnet: ComputedRef<boolean>;
15 isDeployed: ComputedRef<boolean>;
16
17 chainRouteQuery(extra?: Record<string, string | number>): Record<string, string | number>;
18}
19
20/**
21 * Chain display info + read client + route helper (spec ยง13.3). Replaces
22 * `BrandData[brand]` (display), `readOnlyAdapters.evm[chainId]` (contract
23 * client), and `chainId2Tech()` embedded in route `:to` bindings.
24 */
25export function useChain(chainId: MaybeRef<number>): UseChainReturn {
26 const id = unref(chainId);
27 const chain = getChain(id);
28 const client = getReadClient(id);
29
30 const usdPrice = ref<number | null>(null);
31 const isPriceLoading = ref(false);
32
33 async function refreshPrice(): Promise<void> {
34 isPriceLoading.value = true;
35 try {
36 usdPrice.value = await client.getUsdPrice();
37 } finally {
38 isPriceLoading.value = false;
39 }
40 }
41
42 const isMainnet = computed(() => chain.main);
43 const isDeployed = computed(() => !!chain.deployed);
44
45 function chainRouteQuery(extra?: Record<string, string | number>): Record<string, string | number> {
46 return { chainId: id, ...extra };
47 }
48
49 return { chain, client, usdPrice, isPriceLoading, refreshPrice, isMainnet, isDeployed, chainRouteQuery };
50}
51