【问题标题】:Solidity v^0.5.0 compiler error [invalid callback specified]Solidity v^0.5.0 编译器错误 [指定的回调无效]
【发布时间】:2019-05-05 09:34:43
【问题描述】:

我正在尝试编译我的合同,但收到此错误:

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';`

【问题讨论】:

    标签: ethereum solidity


    【解决方案1】:

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

    npm install @types/solc
    

    【讨论】:

    • npm ERR! code E404 npm ERR! 404 Not Found: @types/solc@latest npm ERR! A complete log of this run can be found in: npm ERR! /home/torof/.npm/_logs/2018-12-04T05_40_55_564Z-debug.log
    • 也许在这里试试答案:[stackoverflow.com/questions/41292559/…
    【解决方案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);
    

    【讨论】:

    • 哇,谢谢,终于有一个有用的提示了。但是你在哪里找到的?我找不到任何关于如何在 0.5.0 中编译的信息
    • 我自己尝试了代码,发现错误来自对solc.compile的调用,然后阅读自述文件,发现语法不再存在:github.com/ethereum/solc-js#readme
    【解决方案3】:

    这是 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' });
    

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2020-08-28
      • 2021-09-16
      • 2020-06-30
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多