forked from matter-labs/dapp-portal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseEnsName.ts
43 lines (38 loc) · 1000 Bytes
/
useEnsName.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
import { useMemoize } from "@vueuse/core";
import { getEnsAddress } from "@wagmi/core";
import { wagmiConfig } from "@/data/wagmi";
export default (ensName: Ref<string>) => {
const fetchEnsAddress = useMemoize((name: string) => getEnsAddress(wagmiConfig, { name, chainId: 1 }));
const nameToAddress = ref<{ [name: string]: string }>({});
const {
inProgress,
error,
execute: parseEns,
} = usePromise(
async () => {
const name = ensName.value;
const result = await fetchEnsAddress(name);
if (result) {
nameToAddress.value[name] = result;
}
},
{ cache: false }
);
const isValidEnsFormat = computed(() => ensName.value.endsWith(".eth"));
watch(
ensName,
async () => {
if (isValidEnsFormat.value) {
await parseEns();
}
},
{ immediate: true }
);
return {
address: computed(() => nameToAddress.value[ensName.value]),
isValidEnsFormat,
inProgress,
error,
parseEns,
};
};