From fb8d5d8921a007d2ad41fa63c6f06fb1bc79742a Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Sun, 13 Feb 2022 16:15:51 -0300 Subject: [PATCH] Configure set of resolvers per chainId --- .../CompositeAddressResolver.ts | 11 +++-- src/api/address-resolver/index.ts | 45 ++++++++++++------- src/useResolvedAddresses.ts | 5 ++- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/api/address-resolver/CompositeAddressResolver.ts b/src/api/address-resolver/CompositeAddressResolver.ts index 55a7318..d6abe9a 100644 --- a/src/api/address-resolver/CompositeAddressResolver.ts +++ b/src/api/address-resolver/CompositeAddressResolver.ts @@ -17,9 +17,14 @@ export class CompositeAddressResolver address: string ): Promise | undefined> { for (const r of this.resolvers) { - const resolvedAddress = await r.resolveAddress(provider, address); - if (resolvedAddress !== undefined) { - return [r, resolvedAddress]; + try { + const resolvedAddress = await r.resolveAddress(provider, address); + if (resolvedAddress !== undefined) { + return [r, resolvedAddress]; + } + } catch (err) { + console.warn(`Error while trying to resolve addr ${address}`); + console.warn(err); } } diff --git a/src/api/address-resolver/index.ts b/src/api/address-resolver/index.ts index 57f1b37..5997e01 100644 --- a/src/api/address-resolver/index.ts +++ b/src/api/address-resolver/index.ts @@ -19,23 +19,38 @@ import { HardcodedAddressResolver } from "./HardcodedAddressResolver"; export type ResolvedAddresses = Record>; // Create and configure the main resolver -export const ensResolver = new ENSAddressResolver(); -export const uniswapV1Resolver = new UniswapV1Resolver(); -export const uniswapV2Resolver = new UniswapV2Resolver(); -export const uniswapV3Resolver = new UniswapV3Resolver(); -export const ercTokenResolver = new ERCTokenResolver(); -export const hardcodedResolver = new HardcodedAddressResolver(); +const ensResolver = new ENSAddressResolver(); +const uniswapV1Resolver = new UniswapV1Resolver(); +const uniswapV2Resolver = new UniswapV2Resolver(); +const uniswapV3Resolver = new UniswapV3Resolver(); +const ercTokenResolver = new ERCTokenResolver(); +const hardcodedResolver = new HardcodedAddressResolver(); -const _mainResolver = new CompositeAddressResolver(); -_mainResolver.addResolver(ensResolver); -_mainResolver.addResolver(uniswapV3Resolver); -_mainResolver.addResolver(uniswapV2Resolver); -_mainResolver.addResolver(uniswapV1Resolver); -_mainResolver.addResolver(ercTokenResolver); -_mainResolver.addResolver(hardcodedResolver); +const _mainnetResolver = new CompositeAddressResolver(); +_mainnetResolver.addResolver(ensResolver); +_mainnetResolver.addResolver(uniswapV3Resolver); +_mainnetResolver.addResolver(uniswapV2Resolver); +_mainnetResolver.addResolver(uniswapV1Resolver); +_mainnetResolver.addResolver(ercTokenResolver); +_mainnetResolver.addResolver(hardcodedResolver); -export const mainResolver: IAddressResolver> = - _mainResolver; +const _defaultResolver = new CompositeAddressResolver(); +_defaultResolver.addResolver(hardcodedResolver); + +const resolvers: Record>> = { + 1: _mainnetResolver, + 0: _defaultResolver, +}; + +export const getResolver = ( + chainId: number +): IAddressResolver> => { + const resolver = resolvers[chainId]; + if (resolver === undefined) { + return resolver[0]; // default MAGIC NUMBER + } + return resolver; +}; export const resolverRendererRegistry = new Map< IAddressResolver, diff --git a/src/useResolvedAddresses.ts b/src/useResolvedAddresses.ts index 056fa98..4870072 100644 --- a/src/useResolvedAddresses.ts +++ b/src/useResolvedAddresses.ts @@ -2,7 +2,7 @@ import { useState, useEffect, useContext } from "react"; import { BaseProvider } from "@ethersproject/providers"; import { getAddress, isAddress } from "@ethersproject/address"; 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 { RuntimeContext } from "./useRuntime"; import { ChecksummedAddress } from "./types"; @@ -76,7 +76,8 @@ export const useResolvedAddress = ( if (!provider) { return undefined; } - return mainResolver.resolveAddress(provider, address); + const resolver = getResolver(provider.network.chainId); + return resolver.resolveAddress(provider, address); }; const { data, error } = useSWRImmutable(address, fetcher);