diff --git a/plugins/import/import.ts b/plugins/import/import.ts index 9452c86d6..40d6e7c47 100644 --- a/plugins/import/import.ts +++ b/plugins/import/import.ts @@ -84,27 +84,27 @@ async function getEtherscanApiData(network: string, address: string, apiKey: str } async function scrapeContractCreationCodeFromEtherscan(network: string, address: string) { - const url = `${getEtherscanUrl(network)}/address/${address}#code`; - debug(`Attempting to scrape Contract Creation code at ${url}`); - const result = await get(url, {}); - const regex = /
[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g; - const regexDoubleQuotes = /
[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g; - const matches: Array = await new Promise((res, rej)=> { + return new Promise(async (resolve, reject) => { + const url = `${getEtherscanUrl(network)}/address/${address}#code`; try { - res([...result.matchAll(regex), ...result.matchAll(regexDoubleQuotes)]) - } catch (e) { - rej(new Error(`Failed to match contract: ${url}`)) - } - }) - if (matches.length === 0) { - if (result.match(/request throttled/i) || result.match(/try again later/i)) { - throw new Error(`Request throttled: ${url}`); - } else { - throw new Error(`Failed to pull deployed contract code from Etherscan: ${url}`); + debug(`Attempting to scrape Contract Creation code at ${url}`); + const result = await get(url, {}); + const regex = /
[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g; + const regexDoubleQuotes = /
[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g; + const matches = [...result.matchAll(regex), ...result.matchAll(regexDoubleQuotes)]; + if (matches.length === 0) { + if (result.match(/request throttled/i) || result.match(/try again later/i)) { + throw new Error(`Request throttled: ${url}`); + } else { + throw new Error(`Failed to pull deployed contract code from Etherscan: ${url}`); + } + } + debug(`Scraping successful for ${url}`); + resolve(matches[0][1]) + } catch (error) { + reject(error); } - } - debug(`Scraping successful for ${url}`); - return matches[0][1]; + }); } function paramString(params: { [k: string]: string | number }) {