Skip to content

Instantly share code, notes, and snippets.

@vrimkus
Created April 22, 2022 19:18
Show Gist options
  • Save vrimkus/205f787641e81c115c3b97601c7e4dda to your computer and use it in GitHub Desktop.
Save vrimkus/205f787641e81c115c3b97601c7e4dda to your computer and use it in GitHub Desktop.
fetch FTM contracts from ftmscan.com
// INSTALL DEPENDENCIES:
// npm install node-fetch@2.0
// npm install node-html-parser
const fetch = require('node-fetch');
const HTMLParser = require('node-html-parser');
const fs = require('fs');
const path = require('path')
async function getContracts(address, destination) {
try {
let contractWorkspace = path.join(destination, address)
if (fs.existsSync(contractWorkspace)) {
fs.rmSync(contractWorkspace, { recursive: true, force: true })
}
fs.mkdirSync(contractWorkspace, { recursive: true })
let url = `https://ftmscan.com/address/${address}`;
let response = await fetch(url);
let body = await response.text();
let root = HTMLParser.parse(body)
let editors = root.querySelectorAll('.editor')
for (var i = 0; i < editors.length; i++) {
let current = editors[i]
let label = current.previousElementSibling.textContent
if (label.startsWith("File")) {
let fileName = label.split(' : ')[1]
let filePath = path.join(contractWorkspace, fileName)
fs.writeFileSync(filePath, current.textContent);
console.log(filePath)
}
}
} catch(exception) {
console.log(exception);
}
}
// pass in contract address and destination path
getContracts('0x32C7bb562e7ECC15beD153EA731BC371DC7Ff379', "C:/_dev/Misc/ContractTools/contracts");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment