Extract resolver api
This commit is contained in:
parent
6fefb2e7f9
commit
19f7af84a5
26
src/api/address-resolver/CompositeAddressResolver.ts
Normal file
26
src/api/address-resolver/CompositeAddressResolver.ts
Normal 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;
|
||||
}
|
||||
}
|
15
src/api/address-resolver/ENSAddressResolver.ts
Normal file
15
src/api/address-resolver/ENSAddressResolver.ts
Normal 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;
|
||||
}
|
||||
}
|
8
src/api/address-resolver/address-resolver.ts
Normal file
8
src/api/address-resolver/address-resolver.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { BaseProvider } from "@ethersproject/providers";
|
||||
|
||||
export interface IAddressResolver {
|
||||
resolveAddress(
|
||||
provider: BaseProvider,
|
||||
address: string
|
||||
): Promise<string | undefined>;
|
||||
}
|
33
src/api/address-resolver/index.ts
Normal file
33
src/api/address-resolver/index.ts
Normal 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;
|
||||
};
|
@ -32,9 +32,7 @@ export type TransactionChunk = {
|
||||
lastPage: boolean;
|
||||
};
|
||||
|
||||
export type ENSReverseCache = {
|
||||
[address: string]: string;
|
||||
};
|
||||
export type ENSReverseCache = Record<string, string>;
|
||||
|
||||
export type TransactionData = {
|
||||
transactionHash: string;
|
||||
|
@ -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<string | null>[] = [];
|
||||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user