我正在尝试利用我认为非常好的代码来解决另一个问题,它允许我同时在一个目录中编译多个.sol文件,同时在/build目录中生成.json文件。
我花了几天时间在this...but上无法突破。正如您从下面的代码和控制台日志语句中看到的,我知道compile.js是:
错误:‘错误解析输入JSON:*第1行,第1列\n’+‘语法错误:值,对象或数组预期。\n’+ '*第1行,第1列\n‘+’有效的JSON文档必须是数组或对象值。\n‘
它输入的
我的程序都是务实^0.4.25,我的package.json文件中的solc编译器版本也是如此。
我迫切需要有经验的眼睛来发现问题,并帮助我完成这个编译步骤。
我知道很多人会建议使用特弗莱,但这是一个预先存在的反应应用程序和( a)我不想从头开始和b)我真的想了解这个关键的solc.compile步骤和下面的代码在for循环!
合同在Remix中编译、部署和工作干净。
但是我需要访问React应用程序中的接口和字节码,这样我就可以启动许多不同的Ethereum事务。
谢谢。
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**发布于 2021-01-28 08:13:56
尝试:
var output = JSON.parse(solc.compile(JSON.stringify(input).sources)));https://stackoverflow.com/questions/65054466
复制相似问题