From 19f7af84a5c8f3df70f0d0e0c67ddb07103f3423 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Sat, 30 Oct 2021 21:06:11 -0300 Subject: [PATCH] Extract resolver api --- .../CompositeAddressResolver.ts | 26 +++++++++++++++ .../address-resolver/ENSAddressResolver.ts | 15 +++++++++ src/api/address-resolver/address-resolver.ts | 8 +++++ src/api/address-resolver/index.ts | 33 +++++++++++++++++++ src/types.ts | 4 +-- src/useReverseCache.ts | 16 ++------- 6 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 src/api/address-resolver/CompositeAddressResolver.ts create mode 100644 src/api/address-resolver/ENSAddressResolver.ts create mode 100644 src/api/address-resolver/address-resolver.ts create mode 100644 src/api/address-resolver/index.ts diff --git a/src/api/address-resolver/CompositeAddressResolver.ts b/src/api/address-resolver/CompositeAddressResolver.ts new file mode 100644 index 0000000..bee73da --- /dev/null +++ b/src/api/address-resolver/CompositeAddressResolver.ts @@ -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 { + 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; + } +} diff --git a/src/api/address-resolver/ENSAddressResolver.ts b/src/api/address-resolver/ENSAddressResolver.ts new file mode 100644 index 0000000..ef45a64 --- /dev/null +++ b/src/api/address-resolver/ENSAddressResolver.ts @@ -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 { + const name = await provider.lookupAddress(address); + if (name === null) { + return undefined; + } + return name; + } +} diff --git a/src/api/address-resolver/address-resolver.ts b/src/api/address-resolver/address-resolver.ts new file mode 100644 index 0000000..246db34 --- /dev/null +++ b/src/api/address-resolver/address-resolver.ts @@ -0,0 +1,8 @@ +import { BaseProvider } from "@ethersproject/providers"; + +export interface IAddressResolver { + resolveAddress( + provider: BaseProvider, + address: string + ): Promise; +} diff --git a/src/api/address-resolver/index.ts b/src/api/address-resolver/index.ts new file mode 100644 index 0000000..a9b4ff6 --- /dev/null +++ b/src/api/address-resolver/index.ts @@ -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 => { + const solvers: Promise[] = []; + 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; +}; diff --git a/src/types.ts b/src/types.ts index 5904b31..76a1944 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,9 +32,7 @@ export type TransactionChunk = { lastPage: boolean; }; -export type ENSReverseCache = { - [address: string]: string; -}; +export type ENSReverseCache = Record; export type TransactionData = { transactionHash: string; diff --git a/src/useReverseCache.ts b/src/useReverseCache.ts index 365888a..fa0ca1a 100644 --- a/src/useReverseCache.ts +++ b/src/useReverseCache.ts @@ -1,6 +1,7 @@ import { useState, useEffect } from "react"; import { JsonRpcProvider } from "@ethersproject/providers"; import { ENSReverseCache, ProcessedTransaction } from "./types"; +import { batchPopulate } from "./api/address-resolver"; export const useENSCache = ( provider?: JsonRpcProvider, @@ -25,20 +26,7 @@ export const useENSCache = ( const addresses = Array.from(addrSet); const reverseResolve = async () => { - const solvers: Promise[] = []; - 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; - } + const cache = await batchPopulate(provider, addresses); setReverseCache(cache); }; reverseResolve();