Configure set of resolvers per chainId

This commit is contained in:
Willian Mitsuda 2022-02-13 16:15:51 -03:00
parent 8669003ac6
commit fb8d5d8921
3 changed files with 41 additions and 20 deletions

View File

@ -17,9 +17,14 @@ export class CompositeAddressResolver<T = any>
address: string address: string
): Promise<SelectedResolvedName<T> | undefined> { ): Promise<SelectedResolvedName<T> | undefined> {
for (const r of this.resolvers) { for (const r of this.resolvers) {
const resolvedAddress = await r.resolveAddress(provider, address); try {
if (resolvedAddress !== undefined) { const resolvedAddress = await r.resolveAddress(provider, address);
return [r, resolvedAddress]; if (resolvedAddress !== undefined) {
return [r, resolvedAddress];
}
} catch (err) {
console.warn(`Error while trying to resolve addr ${address}`);
console.warn(err);
} }
} }

View File

@ -19,23 +19,38 @@ import { HardcodedAddressResolver } from "./HardcodedAddressResolver";
export type ResolvedAddresses = Record<string, SelectedResolvedName<any>>; export type ResolvedAddresses = Record<string, SelectedResolvedName<any>>;
// Create and configure the main resolver // Create and configure the main resolver
export const ensResolver = new ENSAddressResolver(); const ensResolver = new ENSAddressResolver();
export const uniswapV1Resolver = new UniswapV1Resolver(); const uniswapV1Resolver = new UniswapV1Resolver();
export const uniswapV2Resolver = new UniswapV2Resolver(); const uniswapV2Resolver = new UniswapV2Resolver();
export const uniswapV3Resolver = new UniswapV3Resolver(); const uniswapV3Resolver = new UniswapV3Resolver();
export const ercTokenResolver = new ERCTokenResolver(); const ercTokenResolver = new ERCTokenResolver();
export const hardcodedResolver = new HardcodedAddressResolver(); const hardcodedResolver = new HardcodedAddressResolver();
const _mainResolver = new CompositeAddressResolver(); const _mainnetResolver = new CompositeAddressResolver();
_mainResolver.addResolver(ensResolver); _mainnetResolver.addResolver(ensResolver);
_mainResolver.addResolver(uniswapV3Resolver); _mainnetResolver.addResolver(uniswapV3Resolver);
_mainResolver.addResolver(uniswapV2Resolver); _mainnetResolver.addResolver(uniswapV2Resolver);
_mainResolver.addResolver(uniswapV1Resolver); _mainnetResolver.addResolver(uniswapV1Resolver);
_mainResolver.addResolver(ercTokenResolver); _mainnetResolver.addResolver(ercTokenResolver);
_mainResolver.addResolver(hardcodedResolver); _mainnetResolver.addResolver(hardcodedResolver);
export const mainResolver: IAddressResolver<SelectedResolvedName<any>> = const _defaultResolver = new CompositeAddressResolver();
_mainResolver; _defaultResolver.addResolver(hardcodedResolver);
const resolvers: Record<number, IAddressResolver<SelectedResolvedName<any>>> = {
1: _mainnetResolver,
0: _defaultResolver,
};
export const getResolver = (
chainId: number
): IAddressResolver<SelectedResolvedName<any>> => {
const resolver = resolvers[chainId];
if (resolver === undefined) {
return resolver[0]; // default MAGIC NUMBER
}
return resolver;
};
export const resolverRendererRegistry = new Map< export const resolverRendererRegistry = new Map<
IAddressResolver<any>, IAddressResolver<any>,

View File

@ -2,7 +2,7 @@ import { useState, useEffect, useContext } from "react";
import { BaseProvider } from "@ethersproject/providers"; import { BaseProvider } from "@ethersproject/providers";
import { getAddress, isAddress } from "@ethersproject/address"; import { getAddress, isAddress } from "@ethersproject/address";
import useSWRImmutable from "swr/immutable"; import useSWRImmutable from "swr/immutable";
import { mainResolver } from "./api/address-resolver"; import { getResolver } from "./api/address-resolver";
import { SelectedResolvedName } from "./api/address-resolver/CompositeAddressResolver"; import { SelectedResolvedName } from "./api/address-resolver/CompositeAddressResolver";
import { RuntimeContext } from "./useRuntime"; import { RuntimeContext } from "./useRuntime";
import { ChecksummedAddress } from "./types"; import { ChecksummedAddress } from "./types";
@ -76,7 +76,8 @@ export const useResolvedAddress = (
if (!provider) { if (!provider) {
return undefined; return undefined;
} }
return mainResolver.resolveAddress(provider, address); const resolver = getResolver(provider.network.chainId);
return resolver.resolveAddress(provider, address);
}; };
const { data, error } = useSWRImmutable(address, fetcher); const { data, error } = useSWRImmutable(address, fetcher);