gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:42:28 25 4
gpt4 key购买 nike

我想在一个 compile.js 文件中编译多个契约(Contract),但我不知道该怎么做。

我的 compile.js 文件与单个契约(Contract)如下所示:

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更改为契约(Contract)数量,但不确定还需要什么?

最佳答案

这是我做的一个例子。您可以在 my public repo 中找到它.简而言之,我有一个“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]
);
}

基本上,如果您不将路径结构更改为我的,则必须更改上述代码的这一部分:
// Feth path of build
const buildPath = path.resolve(__dirname, "build");
const contractspath = path.resolve(__dirname, "contracts");

关于ethereum - Solidity:如何在 compile.js 文件中编译多个智能合约?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57464939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com