Extract resolver api
This commit is contained in:
parent
6fefb2e7f9
commit
19f7af84a5
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { BaseProvider } from "@ethersproject/providers";
|
||||||
|
|
||||||
|
export interface IAddressResolver {
|
||||||
|
resolveAddress(
|
||||||
|
provider: BaseProvider,
|
||||||
|
address: string
|
||||||
|
): Promise<string | undefined>;
|
||||||
|
}
|
|
@ -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;
|
lastPage: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ENSReverseCache = {
|
export type ENSReverseCache = Record<string, string>;
|
||||||
[address: string]: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export type TransactionData = {
|
export type TransactionData = {
|
||||||
transactionHash: string;
|
transactionHash: string;
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in New Issue