Configure set of resolvers per chainId
This commit is contained in:
parent
8669003ac6
commit
fb8d5d8921
|
@ -17,9 +17,14 @@ export class CompositeAddressResolver<T = any>
|
|||
address: string
|
||||
): Promise<SelectedResolvedName<T> | 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,23 +19,38 @@ import { HardcodedAddressResolver } from "./HardcodedAddressResolver";
|
|||
export type ResolvedAddresses = Record<string, SelectedResolvedName<any>>;
|
||||
|
||||
// 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<SelectedResolvedName<any>> =
|
||||
_mainResolver;
|
||||
const _defaultResolver = new CompositeAddressResolver();
|
||||
_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<
|
||||
IAddressResolver<any>,
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue