2019-05-15 01:48:48 +03:00
|
|
|
'use strict';
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
var fs_1 = __importDefault(require("fs"));
|
2020-01-30 05:43:56 +03:00
|
|
|
var module_1 = __importDefault(require("module"));
|
2019-05-15 01:48:48 +03:00
|
|
|
var path_1 = require("path");
|
|
|
|
var ethers_1 = require("ethers");
|
|
|
|
;
|
2020-01-30 05:43:56 +03:00
|
|
|
function populateOptions(options) {
|
2019-05-15 01:48:48 +03:00
|
|
|
options = ethers_1.ethers.utils.shallowCopy(options || {});
|
|
|
|
if (options.filename && !options.basedir) {
|
|
|
|
options.basedir = path_1.dirname(options.filename);
|
|
|
|
}
|
|
|
|
if (!options.filename) {
|
|
|
|
options.filename = "_contract.sol";
|
|
|
|
}
|
|
|
|
if (!options.basedir) {
|
|
|
|
options.basedir = ".";
|
|
|
|
}
|
2020-01-30 05:43:56 +03:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
function getInput(source, options) {
|
2019-05-15 01:48:48 +03:00
|
|
|
var sources = {};
|
|
|
|
sources[options.filename] = { content: source };
|
|
|
|
var input = {
|
|
|
|
language: "Solidity",
|
|
|
|
sources: sources,
|
|
|
|
settings: {
|
|
|
|
outputSelection: {
|
|
|
|
"*": {
|
|
|
|
"*": ["*"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (options.optimize) {
|
|
|
|
input.settings.optimizer = {
|
|
|
|
enabled: true,
|
|
|
|
runs: 200
|
|
|
|
};
|
|
|
|
}
|
2020-01-30 05:43:56 +03:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
function _compile(_solc, source, options) {
|
|
|
|
var compilerVersion = _solc.version();
|
|
|
|
var ver = compilerVersion.match(/(\d+)\.(\d+)\.(\d+)/);
|
|
|
|
if (!ver || ver[1] !== "0") {
|
|
|
|
throw new Error("unknown version");
|
|
|
|
}
|
|
|
|
var version = parseFloat(ver[2] + "." + ver[3]);
|
|
|
|
//if (version < 4.11 || version >= 7) {
|
|
|
|
if (version < 5.0 || version >= 7.0) {
|
|
|
|
throw new Error("unsupported version: " + ver[1] + "." + ver[2] + "." + ver[3]);
|
|
|
|
}
|
|
|
|
options = populateOptions(options);
|
|
|
|
var input = getInput(source, options);
|
2019-05-15 01:48:48 +03:00
|
|
|
var findImport = function (filename) {
|
|
|
|
try {
|
|
|
|
return {
|
2019-11-20 12:57:38 +03:00
|
|
|
contents: fs_1.default.readFileSync(path_1.resolve(options.basedir, filename)).toString()
|
2019-05-15 01:48:48 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
return { error: error.message };
|
|
|
|
}
|
|
|
|
};
|
2020-01-30 05:43:56 +03:00
|
|
|
if (version >= 6) {
|
|
|
|
findImport = { import: findImport };
|
|
|
|
}
|
|
|
|
var outputJson = _solc.compile(JSON.stringify(input), findImport);
|
|
|
|
var output = JSON.parse(outputJson);
|
2019-05-15 01:48:48 +03:00
|
|
|
var errors = (output.errors || []).filter(function (x) { return (x.severity === "error" || options.throwWarnings); }).map(function (x) { return x.formattedMessage; });
|
|
|
|
if (errors.length) {
|
|
|
|
var error = new Error("compilation error");
|
|
|
|
error.errors = errors;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
var result = [];
|
|
|
|
for (var filename in output.contracts) {
|
|
|
|
for (var name_1 in output.contracts[filename]) {
|
|
|
|
var contract = output.contracts[filename][name_1];
|
2020-01-30 05:43:56 +03:00
|
|
|
// Skip empty contracts
|
|
|
|
if (!contract.evm.bytecode.object) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-15 01:48:48 +03:00
|
|
|
result.push({
|
|
|
|
name: name_1,
|
|
|
|
interface: new ethers_1.ethers.utils.Interface(contract.abi),
|
|
|
|
bytecode: "0x" + contract.evm.bytecode.object,
|
2020-01-30 05:43:56 +03:00
|
|
|
runtime: "0x" + contract.evm.deployedBytecode.object,
|
|
|
|
compiler: compilerVersion
|
2019-05-15 01:48:48 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-01-30 05:43:56 +03:00
|
|
|
// Creates a require which will first search from the current location,
|
|
|
|
// and for solc will fallback onto the version included in @ethersproject/cli
|
|
|
|
function customRequire(path) {
|
2020-02-12 23:55:09 +03:00
|
|
|
// Node 8.x does not support createRequireFromPath
|
|
|
|
var createRequire = (module_1.default.createRequireFromPath || (function (path) {
|
|
|
|
return require;
|
|
|
|
}));
|
|
|
|
var pathRequire = createRequire(path_1.resolve(path, "./sandbox.js"));
|
|
|
|
var libRequire = createRequire(path_1.resolve(__filename));
|
2020-01-30 05:43:56 +03:00
|
|
|
return function (name) {
|
|
|
|
try {
|
|
|
|
return pathRequire(name);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
if (name === "solc") {
|
|
|
|
try {
|
|
|
|
return libRequire(name);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.customRequire = customRequire;
|
|
|
|
function wrapSolc(_solc) {
|
|
|
|
return function (source, options) {
|
|
|
|
return _compile(_solc, source, options || {});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.wrapSolc = wrapSolc;
|
|
|
|
exports.compile = wrapSolc(customRequire(".")("solc"));
|