我想编译我的ethereum HelloWorld.sol智能合同。在所有的教程中,您都是这样做的:
var solc = require('solc');
var compiledContract = solc.compile(fs.readFileSync('HelloWorld.sol').toString();其中HelloWorld.sol是:
pragma solidity ^0.5.1;
contract HelloWorld {
bytes32 message;
constructor(bytes32 myMessage) public {
message = myMessage;
}
function getMessage() public view returns(bytes32){
return message;
}
}换句话说,我将原始的实体契约代码放入solc.compile()方法中。但是这个过程给了我compiledContract中的错误
'{"errors":[{"component":"general","formattedMessage":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 1, Column 2\\n Extra non-whitespace after JSON value.\\n","message":"* Line 1, Column 1\\n Syntax error: value, object or array expected.\\n* Line 1, Column 2\\n Extra non-whitespace after JSON value.\\n","severity":"error","type":"JSONError"}]}'我很长一段时间都在寻找解决方案,但我唯一发现的就是
高级API由一个方法compile组成,它需要编译器标准输入和输出JSON。
(链接)。标准输入JSON看起来像是JSON和这个可靠代码的某种组合。所以我的问题是-
如何将solidity契约代码转换为编译器标准输入JSON?
我是正确的,这是唯一的方式,如何编制合同?
发布于 2018-12-15 21:31:27
这段代码适用于我,index.js
const solc = require('solc')
const fs = require('fs')
const CONTRACT_FILE = 'HelloWorld.sol'
const content = fs.readFileSync(CONTRACT_FILE).toString()
const input = {
language: 'Solidity',
sources: {
[CONTRACT_FILE]: {
content: content
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input)))
for (const contractName in output.contracts[CONTRACT_FILE]) {
console.log(output.contracts[CONTRACT_FILE][contractName].evm.bytecode.object)
}HelloWorld.sol
contract HelloWorld {
bytes32 message;
constructor(bytes32 myMessage) public {
message = myMessage;
}
function getMessage() public view returns(bytes32){
return message;
}
}发布于 2018-12-16 01:40:29
或者,您可以使用以下命令和输入数据运行solc (命令行工具)
solc --standard-json -o outputDirectory --bin --ast --asm HelloWorld.sol在上面的命令中,当--standard需要一个您可以提供的输入json文件时。
您可以在下面的链接中找到输入文件应该如何的示例。
来源:https://solidity.readthedocs.io/en/v0.4.24/using-the-compiler.html
发布于 2022-04-23 04:30:17
const solc = require("solc");
// file system - read and write files to your computer
const fs = require("fs");
// reading the file contents of the smart contract
const fileContent = fs.readFileSync("HelloWorld.sol").toString();
// create an input structure for my solidity compiler
var input = {
language: "Solidity",
sources: {
"HelloWorld.sol": {
content: fileContent,
},
},
settings: {
outputSelection: {
"*": {
"*": ["*"],
},
},
},
};
var output = JSON.parse(solc.compile(JSON.stringify(input)));
// console.log("Output: ", output);
const ABI = output.contracts["HelloWorld.sol"]["Demo"].abi;
const byteCode = output.contracts["HelloWorld.sol"]["Demo"].evm.bytecode.object;
// console.log("abi: ",ABI)
// console.log("byte code: ",byteCode)
npm run yorfilename.jshttps://stackoverflow.com/questions/53795971
复制相似问题