Skip to content

Commit

Permalink
make scrapeContractCreationCodeFromEtherscan async
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriy-woof-software committed Apr 6, 2024
1 parent 219a3ba commit 589dfe7
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions plugins/import/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <string>await get(url, {});
const regex = /<div id='verifiedbytecode2'>[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g;
const regexDoubleQuotes = /<div id="verifiedbytecode2">[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g;
const matches: Array<unknown> = 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 = <string>await get(url, {});
const regex = /<div id='verifiedbytecode2'>[\s\r\n]*([0-9a-fA-F]*)[\s\r\n]*<\/div>/g;
const regexDoubleQuotes = /<div id="verifiedbytecode2">[\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 }) {
Expand Down

0 comments on commit 589dfe7

Please sign in to comment.