Solidity 编译器 - HelloWorld 智能合约的问题

Solidity compiler - problem with HelloWorld smart contract

我正在尝试 运行 我在 Enthereum 网络上的第一个 HelloWorld 智能合约。这是我的 HelloWorld.sol 合同。

pragma solidity ^0.5.0;

contract HelloWorld {
  bytes32 message;
  constructor (bytes32 myMessage) public {
    message = myMessage;
  }
  function getMessage() public returns(bytes32) {
    return message;
  }
}

当我尝试使用 solcjs HelloWorld.sol --bin 构建它时,只有一个警告并且没有错误。我已经使用 npm 安装了 web3 和 solc。当我 运行 在一个节点上

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

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"}]}'

问题出在哪里?

问题是,你不能将原始的 solidity 智能合约放入 solc.compile() 函数中。必须有Compiler Standard Input JSON。根据我的 我找到了这个解决方案:

> var Web3 = require('web3');
> var solc = require('solc');
> var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
> var CONTRACT_FILE = 'HelloWorld.sol'
> var content =fs.readFileSync(CONTRACT_FILE).toString()
> var input = {
  language: 'Solidity',
  sources: {
    [CONTRACT_FILE]: {
      content: content
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
}

> var compiled = solc.compile(JSON.stringify(input))
> var output = JSON.parse(compiled)
> var abi = output.contracts[CONTRACT_FILE]['HelloWorld'].abi
> var bytecode = output.contracts[CONTRACT_FILE]['HelloWorld'].evm.bytecode.object
> var HelloWorld = new web3.eth.Contract(abi);
> var HelloWorldTx = HelloWorld.deploy({data: bytecode, arguments: [web3.utils.asciiToHex('Hello')]});
> web3.eth.estimateGas(HelloWorldTx).then(console.log); //this does not work, because it can not connect to the localhost:8545. I don't know why.
> HelloWorldTx.send({from: '0x99d54a45f2cd3b9c6443d623003416aaf944338e', gas: 1000000}).then(console.log);