Ethereum Solidity:如何在compile.js文件中编译多个智能合约?

Ethereum Solidity:如何在compile.js文件中编译多个智能合约?,ethereum,solidity,web3,Ethereum,Solidity,Web3,我想在一个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.readFile

我想在一个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必须更改为合同数量,但不确定还需要什么?

以下是我做的一个示例。你可以在一本书中找到它。简单地说,我有一个“build”文件夹,在那里我将每个编译的契约的输出写入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]
      );
    }
基本上,如果不将path struct更改为My,则必须更改上述代码的这一部分:

// Feth path of build
        const buildPath = path.resolve(__dirname, "build");
        const contractspath = path.resolve(__dirname, "contracts");

经批准的解决方案不适用于坚固性
>0.6.0
{
const filePath=path.resolve(contractPath,fileName);
const source=fs.readFileSync(文件路径,“utf8”);
返回{…输入,[fileName]:{content:source};
}, {}),
设置:{
输出选择:{
"*": {
“*”:[“abi”,“evm.bytecode.object”],
},
},
},
};
//汇编所有合同
const compiled=JSON.parse(solc.compile(JSON.stringify(compileriput));
fs.ensureDirSync(buildPath);
fileNames.map((文件名)=>{
const contracts=Object.keys(compiled.contracts[fileName]);
contracts.map((contract)=>{
fs.outputJsonSync(
resolve(buildPath,contract+“.json”),
已编译。合同[文件名][合同]
);
});
});
确保检查您的
pragma solidity x.x.x
是否与
package.json
中指定的版本匹配。例如,如果我使用的是
solidity 0.6.12
我的solidity编译将是:

“依赖项”:{
...
“solc”:“^0.6.12”,
...
}
compile.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]
  );
}