Solidity:如何在 compile.js 文件中编译多个智能合约?
Posted
技术标签:
【中文标题】Solidity:如何在 compile.js 文件中编译多个智能合约?【英文标题】:Solidity: How to compile multiple smart contracts in compile.js file? 【发布时间】:2019-12-19 06:06:37 【问题描述】:我想在一个 compile.js 文件中编译多个合约,但我不知道该怎么做。
我的带有单个合约的 compile.js 文件如下所示:
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync(lotteryPath, 'utf8');
module.exports = solc.compile(source, 1);
如何向 compile.js 文件添加更多合约?我知道1必须改为合同数量,但不知道还需要什么?
【问题讨论】:
【参考方案1】:这是我做的一个例子。您可以在my public repo 中找到它。简而言之,我有一个“构建”文件夹,我将每个已编译合约的输出写入 Json 文件。
const path = require("path"); //nodejs ’path’ module
const solc = require("solc"); //solidity compiler module
const fs = require("fs-extra"); //file system module
// Feth path of build
const buildPath = path.resolve(__dirname, "build");
const contractspath = path.resolve(__dirname, "contracts");
// Removes folder build and every file in it
fs.removeSync(buildPath);
// Fetch all Contract files in Contracts folder
const fileNames = fs.readdirSync(contractspath);
// Gets ABI of all contracts into variable input
const input = fileNames.reduce(
(input, fileName) =>
const filePath = path.resolve(__dirname, "contracts", fileName);
const source = fs.readFileSync(filePath, "utf8");
return sources: ...input.sources, [fileName]: source ;
,
sources:
);
// Compile all contracts
const output = solc.compile(input, 1).contracts;
// Re-Create build folder for output files from each contract
fs.ensureDirSync(buildPath);
// Output contains all objects from all contracts
// Write the contents of each to different files
for (let contract in output)
fs.outputJsonSync(
path.resolve(buildPath, contract.split(":")[1] + ".json"),
output[contract]
);
基本上,如果您不将路径结构更改为我的,则必须更改上述代码的这一部分:
// Feth path of build
const buildPath = path.resolve(__dirname, "build");
const contractspath = path.resolve(__dirname, "contracts");
【讨论】:
【参考方案2】:批准的解决方案不适用于 solidity >0.6.0
和 <=0.8.1
。
对于提到的版本,我解决了如下:
const path = require("path");
const fs = require("fs-extra");
const solc = require("solc");
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
const contractPath = path.resolve(__dirname, "contracts");
const fileNames = fs.readdirSync(contractPath);
const compilerInput =
language: "Solidity",
sources: fileNames.reduce((input, fileName) =>
const filePath = path.resolve(contractPath, fileName);
const source = fs.readFileSync(filePath, "utf8");
return ...input, [fileName]: content: source ;
, ),
settings:
outputSelection:
"*":
"*": ["abi", "evm.bytecode.object"],
,
,
,
;
// Compile All contracts
const compiled = JSON.parse(solc.compile(JSON.stringify(compilerInput)));
fs.ensureDirSync(buildPath);
fileNames.map((fileName) =>
const contracts = Object.keys(compiled.contracts[fileName]);
contracts.map((contract) =>
fs.outputJsonSync(
path.resolve(buildPath, contract + ".json"),
compiled.contracts[fileName][contract]
);
);
);
请务必检查您的 pragma solidity x.x.x
是否与您的 package.json
中指定的版本匹配。例如,如果我使用solidity 0.6.12
我的solidity 编译将是:
"dependencies":
...
"solc": "^0.6.12",
...
【讨论】:
这似乎不是solidity版本,而是npm solc更改【参考方案3】:编译.js:
const path= require('path');
const solc = require('solc');
const fs = require('fs-extra');
const builtPath = path.resolve(__dirname, 'build');
//remove file in build module
fs.removeSync(builtPath);
const healthPath = path.resolve(__dirname, 'contract','health.sol');
//read content present in file
console.log(healthPath);
const source = fs.readFileSync(healthPath,'utf8');
//compile contract
const output = solc.compile(source,1).contracts;
//create build folder
fs.ensureDirSync(builtPath);
console.log(output);
for(let contract in output)
fs.outputJsonSync(
path.resolve(buildPath, contract.replace(':','')+ '.json'),
output[contract]
);
【讨论】:
以上是关于Solidity:如何在 compile.js 文件中编译多个智能合约?的主要内容,如果未能解决你的问题,请参考以下文章