2021-12-17 02:22:56 +00:00
|
|
|
import { useState, useEffect, useContext } from "react";
|
|
|
|
import { BaseProvider } from "@ethersproject/providers";
|
2021-11-25 19:20:44 +00:00
|
|
|
import { getAddress, isAddress } from "@ethersproject/address";
|
2021-12-18 10:09:10 +00:00
|
|
|
import useSWRImmutable from "swr/immutable";
|
2022-02-13 19:15:51 +00:00
|
|
|
import { getResolver } from "./api/address-resolver";
|
2021-12-17 02:17:13 +00:00
|
|
|
import { SelectedResolvedName } from "./api/address-resolver/CompositeAddressResolver";
|
2021-11-25 19:20:44 +00:00
|
|
|
import { RuntimeContext } from "./useRuntime";
|
2021-12-17 02:22:56 +00:00
|
|
|
import { ChecksummedAddress } from "./types";
|
2021-11-25 19:20:44 +00:00
|
|
|
|
2022-01-29 22:54:21 +00:00
|
|
|
export const useAddressOrENS = (
|
2021-11-25 19:20:44 +00:00
|
|
|
addressOrName: string,
|
|
|
|
urlFixer: (address: ChecksummedAddress) => void
|
|
|
|
): [
|
|
|
|
ChecksummedAddress | undefined,
|
|
|
|
boolean | undefined,
|
|
|
|
boolean | undefined
|
|
|
|
] => {
|
|
|
|
const { provider } = useContext(RuntimeContext);
|
|
|
|
const [checksummedAddress, setChecksummedAddress] = useState<
|
|
|
|
ChecksummedAddress | undefined
|
|
|
|
>();
|
|
|
|
const [isENS, setENS] = useState<boolean>();
|
|
|
|
const [error, setError] = useState<boolean>();
|
|
|
|
|
|
|
|
// If it looks like it is an ENS name, try to resolve it
|
|
|
|
useEffect(() => {
|
2021-12-04 19:54:27 +00:00
|
|
|
// Reset
|
|
|
|
setENS(false);
|
|
|
|
setError(false);
|
|
|
|
setChecksummedAddress(undefined);
|
|
|
|
|
2021-11-25 19:20:44 +00:00
|
|
|
// TODO: handle and offer fallback to bad checksummed addresses
|
|
|
|
if (isAddress(addressOrName)) {
|
|
|
|
// Normalize to checksummed address
|
|
|
|
const _checksummedAddress = getAddress(addressOrName);
|
|
|
|
if (_checksummedAddress !== addressOrName) {
|
|
|
|
// Request came with a non-checksummed address; fix the URL
|
|
|
|
urlFixer(_checksummedAddress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setENS(false);
|
|
|
|
setError(false);
|
|
|
|
setChecksummedAddress(_checksummedAddress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!provider) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-18 06:46:37 +00:00
|
|
|
if (provider.network.ensAddress) {
|
|
|
|
const resolveName = async () => {
|
|
|
|
const resolvedAddress = await provider.resolveName(addressOrName);
|
|
|
|
if (resolvedAddress !== null) {
|
|
|
|
setENS(true);
|
|
|
|
setError(false);
|
|
|
|
setChecksummedAddress(resolvedAddress);
|
|
|
|
} else {
|
|
|
|
setENS(false);
|
|
|
|
setError(true);
|
|
|
|
setChecksummedAddress(undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
resolveName();
|
|
|
|
} else {
|
|
|
|
setENS(false);
|
|
|
|
setError(true);
|
|
|
|
setChecksummedAddress(undefined);
|
|
|
|
}
|
2021-11-25 19:20:44 +00:00
|
|
|
}, [provider, addressOrName, urlFixer]);
|
|
|
|
|
|
|
|
return [checksummedAddress, isENS, error];
|
|
|
|
};
|
2021-10-31 00:49:23 +00:00
|
|
|
|
2021-12-17 02:17:13 +00:00
|
|
|
export const useResolvedAddress = (
|
|
|
|
provider: BaseProvider | undefined,
|
|
|
|
address: ChecksummedAddress
|
|
|
|
): SelectedResolvedName<any> | undefined => {
|
|
|
|
const fetcher = async (
|
|
|
|
key: string
|
|
|
|
): Promise<SelectedResolvedName<any> | undefined> => {
|
|
|
|
if (!provider) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2022-02-13 19:15:51 +00:00
|
|
|
const resolver = getResolver(provider.network.chainId);
|
|
|
|
return resolver.resolveAddress(provider, address);
|
2021-12-17 02:17:13 +00:00
|
|
|
};
|
|
|
|
|
2021-12-18 10:09:10 +00:00
|
|
|
const { data, error } = useSWRImmutable(address, fetcher);
|
2021-12-17 02:17:13 +00:00
|
|
|
if (error) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|