2021-10-31 05:38:41 +00:00
|
|
|
import { BaseProvider } from "@ethersproject/providers";
|
|
|
|
|
import { Contract } from "@ethersproject/contracts";
|
|
|
|
|
import { IAddressResolver } from "./address-resolver";
|
|
|
|
|
import erc20 from "../../erc20.json";
|
2021-10-31 22:51:11 +00:00
|
|
|
import { TokenMeta } from "../../types";
|
2021-10-31 05:38:41 +00:00
|
|
|
|
2021-10-31 22:51:11 +00:00
|
|
|
export class ERCTokenResolver implements IAddressResolver<TokenMeta> {
|
2021-10-31 05:38:41 +00:00
|
|
|
async resolveAddress(
|
|
|
|
|
provider: BaseProvider,
|
|
|
|
|
address: string
|
2021-10-31 22:51:11 +00:00
|
|
|
): Promise<TokenMeta | undefined> {
|
2021-10-31 05:38:41 +00:00
|
|
|
const erc20Contract = new Contract(address, erc20, provider);
|
|
|
|
|
try {
|
|
|
|
|
const [name, symbol, decimals] = await Promise.all([
|
|
|
|
|
erc20Contract.name(),
|
|
|
|
|
erc20Contract.symbol(),
|
|
|
|
|
erc20Contract.decimals(),
|
|
|
|
|
]);
|
2021-10-31 22:51:11 +00:00
|
|
|
return {
|
|
|
|
|
name,
|
|
|
|
|
symbol,
|
|
|
|
|
decimals,
|
|
|
|
|
};
|
2021-10-31 05:38:41 +00:00
|
|
|
} catch (err) {
|
2021-10-31 22:51:11 +00:00
|
|
|
// Ignore on purpose; this indicates the probe failed and the address
|
|
|
|
|
// is not a token
|
2021-10-31 05:38:41 +00:00
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|