import { Etherscan } from '@dequanto/explorer/Etherscan'; import { IBlockchainExplorer } from '@dequanto/explorer/IBlockchainExplorer'; import { IContractDetails } from '@dequanto/models/IContractDetails'; import { TAddress } from '@dequanto/models/TAddress'; import di from 'a-di'; interface IContract { address: TAddress abi: string name?: string } export interface IContractProvider { getAbi (address: string): Promise getInfo (address: string): Promise } export class ContractProvider implements IContractProvider { constructor (public api: IBlockchainExplorer = di.resolve(Etherscan)) { } async getByName(name: string): Promise { let info = this.api.inMemoryDb.find(x => x.name === name); let { abi } = await this.api.getContractAbi(info.address); return { ...info, abi }; } async getByAddress (address: string): Promise { let info = await this.getInfo(address); if (info == null) { throw new Error(`Contract info not found for ${address}`) } let abi = await this.getAbi(info.address); return { ...info, abi }; } async getAbi (address: TAddress): Promise { let { abi } = await this.api.getContractAbi(address); return abi; } async getInfo (q: string): Promise { return this.api.getContractMeta(q); } }