实体v ^ 0.5.0编译器错误[指定了无效的回调]

时间:2018-12-04 02:19:09

标签: ethereum solidity

我正在尝试编译合同,但出现此错误:

AssertionError [ERR_ASSERTION]: Invalid callback specified.

一个答案是更改编译器的版本,但我的版本是最新的(0.5.0)。 我实际上是在尝试采用旧代码(0.4.17)并对其进行升级。尝试了2天,但一直失败。

这是我的合同:

pragma solidity ^0.5.0;

contract Lottery{
 address public manager;
 address payable [] public players;

 modifier restricted {
     require(msg.sender == manager);
     _;
 }

 constructor() public {
     manager = msg.sender;
 }

 function participate() public payable {
     require(msg.value > .01 ether);
     players.push(msg.sender);
 }

 function pseudoRandom() private view returns(uint){
     return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
 }

 function pickWinner() public restricted {
     require(msg.sender == manager);
     uint index = pseudoRandom() % players.length;
     address(players[index]).transfer(address(this).balance);
     (players) = new address payable[](0);
 }

 function getPlayers() public view returns(address payable[] memory){
     return players;
 }  
}

这是我的package.json:

{
 "name": "lottery",
 "version": "1.0.0",
 "description": "lottery contract with Solidity",
 "main": "compile.js",
 "directories": {
   "test": "test"
 },
 "scripts": {
   "test": "mocha"
 },
 "author": "Torof",
 "license": "ISC",
 "dependencies": {
   "ganache-cli": "^6.2.1",
  "mocha": "^5.2.0",
  "save": "^2.3.2",
  "solc": "^0.5.0",
  "tar": "^4.4.8",
  "truffle": "^4.1.14",
  "truffle-hdwallet-provider": "0.0.6",
  "web3": "^1.0.0-beta.36"
}
}

这是编译器:

const path = require('path');
const fs = require('fs');
const solc = require('solc');            //Could the error be here ?

const lotteryPath = path.resolve(__dirname, 'contracts', 'Lottery.sol');
const source = fs.readFileSync( lotteryPath, 'utf8');

module.exports = solc.compile(source, 1).contracts[':Lottery'];


console.log(solc.compile(source, 1));

最后我发现了此错误消息,但没有得到:

[ts]
Could not find a declaration file for module 'solc'. 
'/home/torof/desk/coding/Udemy/ETH-stephenGrider/lottery/node_modules/solc/index.js'  
implicitly has an 'any' type.
Try `npm install @types/solc` if it exists or add a new declaration (.d.ts) file containing `declare module 'solc';`

3 个答案:

答案 0 :(得分:2)

solc的先前版本支持您使用的编译样式,但是看起来新版本仅支持标准的JSON输入和输出。您可能想要这样的东西:

console.log(JSON.parse(solc.compile(JSON.stringify({
  language: 'Solidity',
  sources: {
    'lottery.sol': {
      content: source,
    },
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['evm', 'bytecode'],
      },
    },
  },
}))).contracts['lottery.sol'].Lottery);

答案 1 :(得分:0)

您是否尝试安装此软件包:

npm install @types/solc

答案 2 :(得分:0)

这里是0.5.X及其部署代码的一种实现方式: 我就是这样编译的,存在深层嵌套破坏来获取字节码。

const path = require('path');
const fs = require('fs');
const solc = require('solc');

const templatePath = path.resolve(__dirname, 'contracts', 'templatename.sol');
const source = fs.readFileSync(templatePath, 'utf8');

const input = {
    language: 'Solidity',
    sources: {
        'yourtemplate.sol': {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
}

const { abi: interface, evm: { bytecode: { object } } } = JSON.parse(solc.compile(JSON.stringify(input))).contracts['yourtemplate.sol'].Templatename; // 

module.exports = { interface, object }; // object is the actual name of the bytecode

以及部署代码:

const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const { interface, object: bytecode } = require('../compile'); 
// i've renamed object with bytecode 

const accounts = await web3.eth.getAccounts();
  templatename = await new web3.eth.Contract(interface)
    .deploy({ data: bytecode, arguments: [INPUT_PARAMETER_GOES_HERE] })
    .send({ from: accounts[0], gas: '1000000' });