Solidity-输入JSON描述的实体代码 [英] Solidity - Solidity code to Input JSON Description

查看:666
本文介绍了Solidity-输入JSON描述的实体代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编译我的ehtereum HelloWorld.sol智能合约。在所有教程中,您都是这样做的:

I want to compile my ehtereum HelloWorld.sol smart contract. In all the tutorials is that you do it like this:

var solc = require('solc');
var compiledContract = solc.compile(fs.readFileSync('HelloWorld.sol').toString();

其中HelloWorld.sol为:

where HelloWorld.sol is:

pragma solidity ^0.5.1;

contract HelloWorld {
    bytes32 message;
    constructor(bytes32 myMessage) public {
        message = myMessage;
    }

    function getMessage() public view returns(bytes32){
        return message;
    }
}

换句话说,我将原始的Solidity合同代码放入了solc.compile()方法中,但是此过程给了我CompiledContract中的错误:

In other words I put my raw Solidity contract code into the solc.compile() method. But this process gives me this error in 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"}]}'

我一直在寻找解决方案,但是我发现的唯一发现是

I was looking for a solution quite long, but only thing I found is that


级别的API由一个单一的方法compile组成,其中
需要编译器标准输入和输出JSON。

"The high-level API consists of a single method, compile, which expects the Compiler Standard Input and Output JSON."

链接)。标准输入JSON看起来像是JSON和此坚固性代码的某种组合。所以我的问题是-如何将实体合同代码转移到编译器标准输入JSON中?我是否正确,这是如何编制合同的唯一方法?谢谢。

(link). The standard input JSON looks like some combination of JSON and this solidity code. So my question is - How to transfer the solidity contract code into a compiler standard input JSON? Am I correct that this is the only way how to compile the contract? Thank you.

推荐答案

此代码对我有用,index.js

This code works for me, 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

HelloWorld.sol

contract HelloWorld {
    bytes32 message;
    constructor(bytes32 myMessage) public {
        message = myMessage;
    }

    function getMessage() public view returns(bytes32){
        return message;
    }
}

这篇关于Solidity-输入JSON描述的实体代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆