@matterlabs/hardhat-zksync-solc
Version:
Hardhat plugin to compile smart contracts for the ZKsync network
92 lines • 3.9 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZkVmSolcCompilerDownloader = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const child_process_1 = require("child_process");
const utils_1 = require("../utils");
const constants_1 = require("../constants");
const errors_1 = require("../errors");
/**
* This class is responsible for downloading the zkvm solc binary.
*/
class ZkVmSolcCompilerDownloader {
static async getDownloaderWithVersionValidated(zkVmSolcVersion, solcVersion, compilersDir) {
if (!ZkVmSolcCompilerDownloader._instance ||
ZkVmSolcCompilerDownloader._instance.getZkVmSolcVersion() !== zkVmSolcVersion ||
ZkVmSolcCompilerDownloader._instance.getSolcVersion() !== solcVersion) {
ZkVmSolcCompilerDownloader._instance = new ZkVmSolcCompilerDownloader(solcVersion, zkVmSolcVersion, compilersDir);
}
return ZkVmSolcCompilerDownloader._instance;
}
/**
* Use `getDownloaderWithVersionValidated` to create an instance of this class.
*/
constructor(_solcVersion, _zkVmSolcVersion, _compilersDirectory) {
this._solcVersion = _solcVersion;
this._zkVmSolcVersion = _zkVmSolcVersion;
this._compilersDirectory = _compilersDirectory;
this.version = `${_solcVersion}-${_zkVmSolcVersion}`;
}
getSolcVersion() {
return this._solcVersion;
}
getZkVmSolcVersion() {
return this._zkVmSolcVersion;
}
getVersion() {
return this.version;
}
getCompilerPath() {
return path_1.default.join(this._compilersDirectory, 'zkvm-solc', `zkvm-solc-v${this.version}`);
}
async isCompilerDownloaded() {
const compilerPath = this.getCompilerPath();
return fs_extra_1.default.pathExists(compilerPath);
}
async downloadCompiler() {
try {
console.info(chalk_1.default.yellow(`Downloading zkvm-solc ${this.version}`));
await this._downloadCompiler();
console.info(chalk_1.default.green(`zkvm-solc version ${this.version} successfully downloaded`));
}
catch (e) {
throw new errors_1.ZkSyncSolcPluginError(e.message.split('\n')[0]);
}
await this._postProcessCompilerDownload();
await this._verifyCompiler();
}
async _downloadCompiler() {
const downloadPath = this.getCompilerPath();
const url = this._getCompilerUrl();
await this._attemptDownload(url, downloadPath);
return downloadPath;
}
_getCompilerUrl() {
return (0, utils_1.getZkVmSolcUrl)(constants_1.ZKVM_SOLC_BIN_REPOSITORY, this.version);
}
async _attemptDownload(url, downloadPath) {
return (0, utils_1.download)(url, downloadPath, constants_1.USER_AGENT, constants_1.DEFAULT_TIMEOUT_MILISECONDS);
}
async _postProcessCompilerDownload() {
const compilerPath = this.getCompilerPath();
fs_extra_1.default.chmodSync(compilerPath, 0o755);
}
async _verifyCompiler() {
const compilerPath = this.getCompilerPath();
const versionOutput = (0, child_process_1.spawnSync)(compilerPath, ['--version']);
const version = versionOutput.stdout
?.toString()
.match(/\d+\.\d+\.\d+/)
?.toString();
if (versionOutput.status !== 0 || version === null) {
throw new errors_1.ZkSyncSolcPluginError((0, constants_1.COMPILER_BINARY_CORRUPTION_ERROR_ZKVM_SOLC)(compilerPath));
}
}
}
exports.ZkVmSolcCompilerDownloader = ZkVmSolcCompilerDownloader;
//# sourceMappingURL=zkvm-solc-downloader.js.map