Generalize resolver framework to allow tracking of the resolver source

This commit is contained in:
Willian Mitsuda 2021-10-31 02:38:41 -03:00
parent 3a0401d25e
commit 25cc823c91
6 changed files with 57 additions and 23 deletions

View File

@ -1,21 +1,25 @@
import { BaseProvider } from "@ethersproject/providers";
import { IAddressResolver } from "./address-resolver";
export class CompositeAddressResolver implements IAddressResolver {
private resolvers: IAddressResolver[] = [];
export type SelectedResolvedName<T> = [IAddressResolver<T>, T];
addResolver(resolver: IAddressResolver) {
export class CompositeAddressResolver<T>
implements IAddressResolver<SelectedResolvedName<T>>
{
private resolvers: IAddressResolver<T>[] = [];
addResolver(resolver: IAddressResolver<T>) {
this.resolvers.push(resolver);
}
async resolveAddress(
provider: BaseProvider,
address: string
): Promise<string | undefined> {
): Promise<SelectedResolvedName<T> | undefined> {
for (const r of this.resolvers) {
const name = r.resolveAddress(provider, address);
const name = await r.resolveAddress(provider, address);
if (name !== undefined) {
return name;
return [r, name];
}
}

View File

@ -1,7 +1,7 @@
import { BaseProvider } from "@ethersproject/providers";
import { IAddressResolver } from "./address-resolver";
export class ENSAddressResolver implements IAddressResolver {
export class ENSAddressResolver implements IAddressResolver<string> {
async resolveAddress(
provider: BaseProvider,
address: string

View File

@ -0,0 +1,24 @@
import { BaseProvider } from "@ethersproject/providers";
import { Contract } from "@ethersproject/contracts";
import { IAddressResolver } from "./address-resolver";
import erc20 from "../../erc20.json";
export class ERCTokenResolver implements IAddressResolver<string> {
async resolveAddress(
provider: BaseProvider,
address: string
): Promise<string | undefined> {
const erc20Contract = new Contract(address, erc20, provider);
try {
const [name, symbol, decimals] = await Promise.all([
erc20Contract.name(),
erc20Contract.symbol(),
erc20Contract.decimals(),
]);
return name;
} catch (err) {
console.warn(`Couldn't get token ${address} metadata; ignoring`, err);
}
return undefined;
}
}

View File

@ -1,8 +1,8 @@
import { BaseProvider } from "@ethersproject/providers";
export interface IAddressResolver {
export interface IAddressResolver<T> {
resolveAddress(
provider: BaseProvider,
address: string
): Promise<string | undefined>;
): Promise<T | undefined>;
}

View File

@ -1,34 +1,40 @@
import { BaseProvider } from "@ethersproject/providers";
import { IAddressResolver } from "./address-resolver";
import { CompositeAddressResolver } from "./CompositeAddressResolver";
import {
CompositeAddressResolver,
SelectedResolvedName,
} from "./CompositeAddressResolver";
import { ENSAddressResolver } from "./ENSAddressResolver";
export type ResolvedAddresses = Record<string, string>;
export type ResolvedAddresses = Record<string, SelectedResolvedName<string>>;
// Create and configure the main resolver
const _mainResolver = new CompositeAddressResolver();
_mainResolver.addResolver(new ENSAddressResolver());
export const ensResolver = new ENSAddressResolver();
export const mainResolver: IAddressResolver = _mainResolver;
const _mainResolver = new CompositeAddressResolver<string>();
_mainResolver.addResolver(ensResolver);
export const mainResolver: IAddressResolver<SelectedResolvedName<string>> =
_mainResolver;
export const batchPopulate = async (
provider: BaseProvider,
addresses: string[]
): Promise<ResolvedAddresses> => {
const solvers: Promise<string | undefined>[] = [];
const solvers: Promise<SelectedResolvedName<string> | undefined>[] = [];
for (const a of addresses) {
solvers.push(mainResolver.resolveAddress(provider, a));
}
const results = await Promise.all(solvers);
const cache: ResolvedAddresses = {};
const resultMap: ResolvedAddresses = {};
for (let i = 0; i < results.length; i++) {
const r = results[i];
if (r === undefined) {
continue;
}
cache[addresses[i]] = r;
resultMap[addresses[i]] = r;
}
return cache;
return resultMap;
};

View File

@ -3,7 +3,7 @@ import Address from "./Address";
import AddressLink from "./AddressLink";
import ENSName from "./ENSName";
import ENSNameLink from "./ENSNameLink";
import { ResolvedAddresses } from "../api/address-resolver";
import { ensResolver, ResolvedAddresses } from "../api/address-resolver";
type AddressOrENSNameProps = {
address: string;
@ -25,17 +25,17 @@ const AddressOrENSName: React.FC<AddressOrENSNameProps> = ({
<>
{address === selectedAddress ? (
<>
{name ? (
<ENSName name={name} address={address} />
{name?.[0] === ensResolver ? (
<ENSName name={name[1]} address={address} />
) : (
<Address address={address} />
)}
</>
) : (
<>
{name ? (
{name?.[0] ? (
<ENSNameLink
name={name}
name={name[1]}
address={address}
dontOverrideColors={dontOverrideColors}
/>