-
Notifications
You must be signed in to change notification settings - Fork 30
/
remoteCompiler.js
92 lines (81 loc) · 3.01 KB
/
remoteCompiler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
/**
* @author github.com/tintinweb
* @license MIT
* */
/** IMPORT */
const { solcVersions } = require('./autogenerated/solcVersions.js')
const { generateSolidity } = require('abi-to-sol')
const request = require('request');
function normalizeSolcVersion(version) {
return version.replace('soljson-', '').replace('.js', '');
}
function getSolcJsCompilerList(options) {
options = options || {};
return new Promise((resolve, reject) => {
request.get('https://solc-bin.ethereum.org/bin/list.json', (err, res, body) => {
if (err) {
return reject(err)
} else {
let data = JSON.parse(body);
let releases = Object.values(data.releases)
if (options.nightly) {
releases = Array.from(new Set([...releases, ...data.builds.map(b => b.path)]));
}
return resolve(releases.map(normalizeSolcVersion))
}
})
});
}
function getRemoteCompiler(solidityVersion) {
return new Promise((resolve, reject) => {
//check if version is in static list (avoid http requests)
let remoteSolidityVersion = solcVersions.find(
(e) => !e.includes('nightly') && e.includes(`v${solidityVersion}`)
)
if (remoteSolidityVersion) {
return resolve(remoteSolidityVersion);
}
//download remote compiler list and check again.
getSolcJsCompilerList().then(solcJsCompilerList => {
let found = solcJsCompilerList.find(
(e) => !e.includes('nightly') && e.includes(`v${solidityVersion}`)
)
if (found) {
return resolve(found);
}
return reject(new Error(`No compiler found for version ${solidityVersion}`));
})
});
}
//.import interface 0x40cfee8d71d67108db46f772b7e2cd55813bf2fb test2
function getRemoteInterfaceFromEtherscan(address, name, chain, solidityVersion) {
return new Promise((resolve, reject) => {
let provider = `https://api${(!chain || chain == "mainnet") ? "" : `-${chain}`}.etherscan.io`
let url = `${provider}/api?module=contract&action=getabi&address=${address}`;
request.get(url, (err, res, body) => {
if (err) {
return reject(err)
} else {
let data = JSON.parse(body);
if (!data.status || data.status != "1" || !data.result) {
return reject(data)
}
let abi = JSON.parse(data.result);
let src = generateSolidity({
name: name,
solidityVersion: solidityVersion,
abi,
outputSource: false,
});
src = src.substring(src.indexOf("\n\n") + 2); // strip license/pragma
return resolve(src)
}
})
});
}
module.exports = {
getRemoteCompiler,
getSolcJsCompilerList,
getRemoteInterfaceFromEtherscan
}