Reduce the amount of network calls by probing first only name() and on success probe other erc20 methods
This commit is contained in:
parent
53630f43a4
commit
832f5f435f
|
@ -1,24 +1,31 @@
|
||||||
import { BaseProvider } from "@ethersproject/providers";
|
import { BaseProvider } from "@ethersproject/providers";
|
||||||
import { Contract } from "@ethersproject/contracts";
|
import { Contract } from "@ethersproject/contracts";
|
||||||
|
import { Interface } from "@ethersproject/abi";
|
||||||
import { IAddressResolver } from "./address-resolver";
|
import { IAddressResolver } from "./address-resolver";
|
||||||
import erc20 from "../../erc20.json";
|
import erc20 from "../../erc20.json";
|
||||||
import { TokenMeta } from "../../types";
|
import { TokenMeta } from "../../types";
|
||||||
|
|
||||||
|
const erc20Interface = new Interface(erc20);
|
||||||
|
|
||||||
export class ERCTokenResolver implements IAddressResolver<TokenMeta> {
|
export class ERCTokenResolver implements IAddressResolver<TokenMeta> {
|
||||||
async resolveAddress(
|
async resolveAddress(
|
||||||
provider: BaseProvider,
|
provider: BaseProvider,
|
||||||
address: string
|
address: string
|
||||||
): Promise<TokenMeta | undefined> {
|
): Promise<TokenMeta | undefined> {
|
||||||
const erc20Contract = new Contract(address, erc20, provider);
|
const erc20Contract = new Contract(address, erc20Interface, provider);
|
||||||
try {
|
try {
|
||||||
const [name, symbol, decimals] = (await Promise.all([
|
const name = (await erc20Contract.name()) as string;
|
||||||
erc20Contract.name(),
|
if (!name.trim()) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [symbol, decimals] = (await Promise.all([
|
||||||
erc20Contract.symbol(),
|
erc20Contract.symbol(),
|
||||||
erc20Contract.decimals(),
|
erc20Contract.decimals(),
|
||||||
])) as [string, string, number];
|
])) as [string, number];
|
||||||
|
|
||||||
// Prevent faulty tokens with empty name/symbol
|
// Prevent faulty tokens with empty name/symbol
|
||||||
if (!name.trim() || !symbol.trim()) {
|
if (!symbol.trim()) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue