Extract resolver api

This commit is contained in:
Willian Mitsuda 2021-10-30 21:06:11 -03:00
parent 6fefb2e7f9
commit 19f7af84a5
6 changed files with 85 additions and 17 deletions

View File

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

View File

@ -0,0 +1,15 @@
import { BaseProvider } from "@ethersproject/providers";
import { IAddressResolver } from "./address-resolver";
export class ENSAddressResolver implements IAddressResolver {
async resolveAddress(
provider: BaseProvider,
address: string
): Promise<string | undefined> {
const name = await provider.lookupAddress(address);
if (name === null) {
return undefined;
}
return name;
}
}

View File

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

View File

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

View File

@ -32,9 +32,7 @@ export type TransactionChunk = {
lastPage: boolean; lastPage: boolean;
}; };
export type ENSReverseCache = { export type ENSReverseCache = Record<string, string>;
[address: string]: string;
};
export type TransactionData = { export type TransactionData = {
transactionHash: string; transactionHash: string;

View File

@ -1,6 +1,7 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { JsonRpcProvider } from "@ethersproject/providers"; import { JsonRpcProvider } from "@ethersproject/providers";
import { ENSReverseCache, ProcessedTransaction } from "./types"; import { ENSReverseCache, ProcessedTransaction } from "./types";
import { batchPopulate } from "./api/address-resolver";
export const useENSCache = ( export const useENSCache = (
provider?: JsonRpcProvider, provider?: JsonRpcProvider,
@ -25,20 +26,7 @@ export const useENSCache = (
const addresses = Array.from(addrSet); const addresses = Array.from(addrSet);
const reverseResolve = async () => { const reverseResolve = async () => {
const solvers: Promise<string | null>[] = []; const cache = await batchPopulate(provider, addresses);
for (const a of addresses) {
solvers.push(provider.lookupAddress(a));
}
const results = await Promise.all(solvers);
const cache: ENSReverseCache = {};
for (let i = 0; i < results.length; i++) {
const r = results[i];
if (r === null) {
continue;
}
cache[addresses[i]] = r;
}
setReverseCache(cache); setReverseCache(cache);
}; };
reverseResolve(); reverseResolve();