Solidity(solc)编译挑战



我正试图利用我发现的非常好的代码来获得另一个答案,该答案允许我在一个目录中同时编译多个.sol文件,同时在/build目录中生成.json文件。

我已经花了好几天的时间。。。但无法突破。正如您从下面的代码和控制台日志语句中看到的,我知道compile.js是:

  1. 正在读取/contents文件夹中的所有contract.sol文件。。。其中有7个。(它们都很短。只有一个是编码的。其余的只有pragam声明和合同名称delaration(。

  2. 它正在删除/build文件夹及其内容,并在编译之前重新创建它。

  3. 它在solc.compile行和控制台日志上得到以下错误,这些日志按照下面的错误"/////输出为-"输出。

    错误:['分析输入JSON:*第1行第1列时出错\n'+'语法错误:应为值、对象或数组。\n’+'*第1行,第1列\n'+"有效的JSON文档必须是数组或对象值。\n’]

  4. 它进入for循环。。。但是除了errors.json文件中的/build目录中的错误之外,似乎没有输出任何其他内容。

我的程序都是pragma^0.4.25,我的package.json文件中的solc编译器版本也是如此。

我很希望有经验的人能看到这个问题,并帮助我完成这个编译步骤。

我知道很多人会建议使用Truffle,但这是一个预先存在的React应用程序,a(我不想从头开始,b(我真的想了解这个关键的solc.compile步骤和for循环中的以下代码!

第页。美国合同在Remix中干净地编译、部署和工作。

但我需要访问React应用程序中的接口和字节码,这样我就可以发起许多不同的以太坊交易。

谢谢。

const path = require("path");
const solc = require("solc");
const fs = require("fs-extra");
// Pointing path to build folder so that we can delete everything in it.
// Fetch path of build
const buildPath = path.resolve(__dirname, "build");
// Removes folder build and every file in it
fs.removeSync(buildPath);
// Fetch all Contract files in Contracts folder
const contractsPath = path.resolve(__dirname, "contracts");
const fileNames = fs.readdirSync(contractsPath);
// console.log("buildPath - ", buildPath);
// console.log("contractsPath - ", contractsPath);
// console.log("fileNames is - ", fileNames);
// 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: {} }
);
console.log("input contains these SCs - ", input);
// Re-Create build folder for output files from each contract
fs.ensureDirSync(buildPath);
console.log("Recreated the directory...");
// Compile all contracts
// const output = solc.compile(input, 1).contract;
var output = solc.compile(JSON.stringify(input).sources);
console.log("//////// OUTPUT is - ", output);
// Output contains all objects from all contracts
// Write the contents of each to different files
for (let contract in output) {
console.log("In the for loop...");
fs.outputJsonSync(
path.resolve(buildPath, contract.replace(":", "") + ".json"),
// path.resolve(buildPath, contract.split(":")[1] + ".json"),
output[contract]
);
console.log("/// Interface - ", output[contract].interface);
console.log("/// Bytecode - ", output[contract].bytecode);
}

// ----------------------------------------------
// const bytecode = output.contracts[0].bytecode;
// const abi = JSON.parse(output.contracts[0].interface);
// console.log('nBytecode: ', bytecode, 'nABI: ', abi);
**strong text**

尝试:

var output = JSON.parse(solc.compile(JSON.stringify(input).sources)));

最新更新