简体   繁体   English

Solidity (solc) 编译挑战

[英]Solidity (solc) compile challenge

I'm trying to leverage what I thought was pretty good code I found to another answer that allows me to compile a number of.sol files in a directory at once while producing the.json files in a /build directory.我正在尝试利用我认为非常好的代码,我发现另一个答案允许我一次编译目录中的多个.sol 文件,同时在 /build 目录中生成 .json 文件。

I've spent several solid days on this...but can't break through.我已经为此花了好几天的时间……但无法突破。 As you can see from the code below and from the console log statements, I know compile.js is:从下面的代码和控制台日志语句中可以看出,我知道 compile.js 是:

  1. Reading all the contract.sol files in the /contracts folder...of which there are 7. (They're all short. Only one of which is coded. The rest merely have the pragam statement as well as the contract name delaration).读取 /contracts 文件夹中的所有 contract.sol 文件......其中有 7 个。(它们都很短。只有其中一个被编码。rest 只有 pragam 声明以及合同名称声明) .

  2. It's deleting the /build folder and its' contents and recreating it before it does the compile.它正在删除 /build 文件夹及其内容并在编译之前重新创建它。

  3. It's getting the following error on the solc.compile line and console logs that output as per the error below '/////// Output is - '.它在 solc.compile 行和控制台日志中出现以下错误,即 output 根据以下错误“/////// Output is -”。

    errors: [ 'Error parsing input JSON: * Line 1, Column 1\n' + ' Syntax error: value, object or array expected.\n' + '* Line 1, Column 1\n' + ' A valid JSON document must be either an array or an object value.\n' ] errors: [ 'Error parsing input JSON: * Line 1, Column 1\n' + ' Syntax error: value, object or array expected.\n' + '* Line 1, Column 1\n' + ' A valid JSON document必须是数组或 object 值。\n' ]

  4. It enters the for loop...but doesn't seem to output anything other than the error in the /build directory in an errors.json file.它进入了 for 循环......但 output 除了errors.json 文件中的/build 目录中的错误之外似乎没有任何其他内容。

My programs are all pragma ^0.4.25 and so is the solc compiler version in my package.json file.我的程序都是 pragma ^0.4.25,我的 package.json 文件中的 solc 编译器版本也是如此。

I'm DESPERATE for experienced eyes to see the problem and help me get through this compile step.我渴望有经验的眼睛看到问题并帮助我完成这个编译步骤。

I know many will suggest using Truffle but this is a pre-existing React app and a) I don't want to start from scratch and b) I really do want to understand this critical solc.compile step and the following code in the for loop!我知道很多人会建议使用 Truffle,但这是一个预先存在的 React 应用程序,a)我不想从头开始,b)我真的想了解这个关键的 solc.compile 步骤和 for环形!

PS contract compiles, deploys and works cleanly in Remix. PS 合约可以在 Remix 中干净地编译、部署和工作。

But I need to get access to the interface and bytecode in React app so that I can initiate a number of different Ethereum transactions.但是我需要访问 React 应用程序中的接口和字节码,以便我可以启动许多不同的以太坊交易。

Thank you.谢谢你。

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**

Try:尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM