首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >solc编译挑战

solc编译挑战
EN

Stack Overflow用户
提问于 2020-11-28 21:09:44
回答 1查看 764关注 0票数 0

我正在尝试利用我认为非常好的代码来解决另一个问题,它允许我同时在一个目录中编译多个.sol文件,同时在/build目录中生成.json文件。

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

  1. 读取/contracts folder...of中的所有contract.sol文件,其中有7个。(它们都很短。其中只有一个被编码了。剩下的只有pragam语句以及合同名称分层)。

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

  1. 在solc.compile行和控制台日志上得到了以下错误,输出与‘/输出是-’下面的错误相同。

错误:‘错误解析输入JSON:*第1行,第1列\n’+‘语法错误:值,对象或数组预期。\n’+ '*第1行,第1列\n‘+’有效的JSON文档必须是数组或对象值。\n‘

它输入的

  1. 似乎除了在errors.json文件中的/build目录中输出错误之外,没有输出任何其他内容。

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

我迫切需要有经验的眼睛来发现问题,并帮助我完成这个编译步骤。

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

合同在Remix中编译、部署和工作干净。

但是我需要访问React应用程序中的接口和字节码,这样我就可以启动许多不同的Ethereum事务。

谢谢。

代码语言:javascript
复制
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**
EN

回答 1

Stack Overflow用户

发布于 2021-01-28 08:13:56

尝试:

代码语言:javascript
复制
var output = JSON.parse(solc.compile(JSON.stringify(input).sources)));
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65054466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档