【问题标题】:node:assert:400 throw err; ^ AssertionError [ERR_ASSERTION]: Invalid callback object specified节点:断言:400 抛出错误; ^ AssertionError [ERR_ASSERTION]:指定的回调对象无效
【发布时间】:2021-12-23 03:41:53
【问题描述】:

试图运行 node compile.js 但它抛出了我上面提到的错误和想法我做错了什么

我的收件箱.sol

pragma solidity ^0.8.9;

contract Inbox{
    string public message;

    function Inbox(string intialMessage) public {
        message = intialMessage;
    }

    function setMessage(string newMessage) public {
        message = newMessage;
    }
}

我的 package.json

{
  "dependencies": {
    "ganache-cli": "^6.12.2",
    "mocha": "^9.1.3",
    "solc": "^0.8.9",
    "web3": "^1.6.0"
  }
}

我是这项技术的初学者,所以感谢您抽出宝贵时间

【问题讨论】:

    标签: node.js npm blockchain web3


    【解决方案1】:

    只需在 'compile.js' 中重写你的代码。即使在 0.8.0 版本的solidity 中也可以正常工作

    const path = require('path');
    const fs = require('fs');
    const solc = require('solc');
    
    const inboxpath = path.resolve(__dirname, 'Contracts', 'Inbox.sol');
    const source = fs.readFileSync(inboxpath, 'UTF-8');
    
    var input = {
        language: 'Solidity',
        sources: {
            'Inbox.sol' : {
                content: source
            }
        },
        settings: {
            outputSelection: {
                '*': {
                    '*': [ '*' ]
                }
            }
        }
    };
    
    var output = JSON.parse(solc.compile(JSON.stringify(input)));
    
    // console.log(output.contracts['Inbox.sol']['Inbox']);
    
    // exports.abi = output.contracts['Inbox.sol']['Inbox'].abi;
    // exports.bytecode = output.contracts['Inbox.sol']['Inbox'].evm.bytecode.object;
    

    【讨论】:

      【解决方案2】:

      它将适用于 solc 版本 0.4.17。

      【讨论】:

        【解决方案3】:

        该课程已过时,solidity 版本 0.6.6 已发布,您最好将代码更新到该版本。如果你不是一个优秀的程序员,你最好退还那门课,因为你以后会遇到很多问题,你会看到使用元掩码和 Web3 的一些错误。这门课程教给你很多东西,所以我真的建议你继续学习这门课程,并在整个课程中不断更新自己。这是第一个问题,更新版本的解决方法是这样的。

        这将是您的“inbox.sol”代码:

        pragma solidity ^0.6.6;
        
        contract Inbox{
        string public message;
        
        constructor (string memory initialMessage) public{
            message = initialMessage;
        }
        
        function setMessage(string memory newMessage) public{
            message = newMessage;
        }
        }
        

        这将是您的“compile.js”代码:

        const path = require('path');
        const fs = require('fs');
        const solc = require('solc');
        
        const inboxpath = path.resolve(__dirname, 'Contracts', 'Inbox.sol');
        const source = fs.readFileSync(inboxpath, 'UTF-8');
        
        var input = {
          language: 'Solidity',
          sources: {
            'Inbox.sol' : {
                content: source
            }
        },
        settings: {
            outputSelection: {
                '*': {
                    '*': [ '*' ]
                }
            }
        }
        };
        
        var output = JSON.parse(solc.compile(JSON.stringify(input)));
        
        exports.abi = output.contracts['Inbox.sol']['Inbox'].abi;
        exports.bytecode = output.contracts['Inbox.sol'] ['Inbox'].evm.bytecode.object;
        

        在新的solidity中,与旧编译器相比,编译器将为您提供另一个版本的编译代码,因此您需要将json文件传递给您的编译器,以便访问您需要的abi(接口)和字节码像我在这里做的那样。

        【讨论】:

        • 我的solidity版本有什么问题
        • 它也没有用
        【解决方案4】:

        只需在函数参数中存在的两个字符串之后添加内存即可。

        函数收件箱(字符串内存初始消息)... 和 函数 setMessage(string memory newMessage)...

        【讨论】:

        • 根本不工作
        猜你喜欢
        • 2020-09-17
        • 2019-04-20
        • 1970-01-01
        • 2021-11-19
        • 1970-01-01
        • 1970-01-01
        • 2021-07-29
        • 2020-07-08
        • 2019-03-07
        相关资源
        最近更新 更多