-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchBytecodeToRunWithProver.js
62 lines (53 loc) · 2.11 KB
/
fetchBytecodeToRunWithProver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let fs = require('fs')
let fetch = require("node-fetch")
let ethers = require("ethers")
let whatsabi = require("@shazow/whatsabi")
let EtherScanAPI = process.env.ETHERSCAN_KEY
let InfuraAPI = process.env.INFURA_ENDPOINT
async function fetchJSON(url) {
return await (await fetch(url)).json(); // improve error handling here
}
async function fetchData(address) {
console.log("Getting Etherscan Info", address)
let data = await fetchJSON("https://api.etherscan.io/api?module=contract&action=getsourcecode&address=" + address + "&apikey=" + EtherScanAPI)
if (data && data.result && data.result.length) {
return data.result[0]
} else {
console.log("Fail", address)
return {}
}
}
async function main() {
const address = process.argv.slice(2)[0]
if (address == null) {
console.log("Must provide an address")
return
}
let provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/" + InfuraAPI)
let deployedCode = await provider.getCode(address)
if (deployedCode == "0x") {
console.log("Empty code for " + address)
fs.writeFileSync("example_" + address + ".json", "") // saving a little bit of space
} else {
let info = await fetchData(address)
const abi = whatsabi.abiFromBytecode(deployedCode);
abi.forEach((it, index, array) => {
//If there is no name defined for the function, fallback to the sighash selector as name.
if(it.name == undefined){it.name = it.selector}
array[index] = it
});
let data = {
"address": address,
"ABI": JSON.stringify(abi),
"SourceCode": info.SourceCode,
"ContractName": info.ContractName,
"CompilerVersion": info.CompilerVersion,
"OptimizationUsed": info.OptimizationUsed,
"ConstructorArguments": info.ConstructorArguments,
"ContractCreationCode": info.ContractCreationCode,
"DeployedCode": deployedCode
}
fs.writeFileSync("example_" + address + ".json", JSON.stringify(data, null, 4))
}
}
main()